Skip to content

Commit 9b31802

Browse files
author
Ke, Mingze
committed
Generate page dynamically
1 parent 66e2087 commit 9b31802

3 files changed

Lines changed: 406 additions & 361 deletions

File tree

code.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
'use strict';
2525

26+
var slice = Array.prototype.slice;
27+
2628
var baseUrl = baseUrl || '.';
2729

2830
/**
@@ -74,6 +76,11 @@ Code.getLang = function () {
7476
return lang;
7577
};
7678

79+
Code.getPage = function () {
80+
var page = Code.getStringParamFromUrl('page', 'index');
81+
return page;
82+
};
83+
7784
/**
7885
* Is the current language (Code.LANG) an RTL language?
7986
* @return {boolean} True if RTL, false if LTR.
@@ -166,6 +173,9 @@ Code.importPrettify = function () {
166173
var link = document.createElement('link');
167174
link.setAttribute('rel', 'stylesheet');
168175
link.setAttribute('href', baseUrl + '/prettify.css');
176+
link.onload = function () {
177+
Blockly.fireUiEvent(window, 'resize');
178+
};
169179
document.head.appendChild(link);
170180
var script = document.createElement('script');
171181
script.setAttribute('src', baseUrl + '/prettify.js');
@@ -202,6 +212,8 @@ Code.getBBox_ = function (element) {
202212
*/
203213
Code.LANG = Code.getLang();
204214

215+
Code.PAGE = Code.getPage();
216+
205217
/**
206218
* List of tab names.
207219
* @private
@@ -286,7 +298,6 @@ Code.renderContent = function () {
286298
* Initialize Blockly. Called on page load.
287299
*/
288300
Code.init = function () {
289-
Code.renderPage();
290301
Code.initLanguage();
291302

292303
var rtl = Code.isRtl();
@@ -376,9 +387,33 @@ Code.init = function () {
376387
window.setTimeout(Code.importPrettify, 1);
377388
};
378389

379-
Code.renderPage = function () {
380-
var template = Handlebars.compile(document.getElementById('page-template').textContent);
381-
document.body.innerHTML = template(MSG);
390+
Code.renderPage = function (callback) {
391+
var req = new XMLHttpRequest(),
392+
head = document.head,
393+
body = document.body;
394+
395+
req.addEventListener('load', function () {
396+
var template = Handlebars.compile(this.responseText);
397+
body.innerHTML = template(MSG);
398+
399+
slice.call(body.querySelectorAll('script')).forEach(function (sc) {
400+
var script = document.createElement('script');
401+
if (sc.getAttribute('src')) {
402+
script.setAttribute('src', sc.getAttribute('src'));
403+
head.appendChild(script);
404+
body.removeChild(sc);
405+
} else if (sc.text) {
406+
script.text = sc.text;
407+
head.appendChild(script);
408+
body.removeChild(sc);
409+
}
410+
});
411+
412+
callback();
413+
});
414+
415+
req.open("get", baseUrl + '/views/' + Code.PAGE + '.handlebars', true);
416+
req.send();
382417
};
383418

384419
/**
@@ -419,7 +454,7 @@ Code.initLanguage = function () {
419454
// Inject language strings.
420455
document.title += ' ' + MSG['title'];
421456
var toolbox = document.getElementById('toolbox');
422-
var categories = Array.prototype.slice.call(toolbox.querySelectorAll('category')).map(function (e) {
457+
var categories = slice.call(toolbox.querySelectorAll('category')).map(function (e) {
423458
return e.id
424459
});
425460
for (var i = 0, cat; cat = categories[i]; i++) {
@@ -466,4 +501,14 @@ document.write('<script src="' + baseUrl + '/msg/' + Code.LANG + '.js"></script>
466501
document.write('<script src="' + baseUrl + '/components/blockly/msg/js/' + Code.LANG + '.js"></script>\n');
467502
document.write('<script src="' + baseUrl + '/blocks/msg/' + Code.LANG + '.js"></script>\n');
468503

469-
window.addEventListener('load', Code.init);
504+
if (Code.PAGE !== 'index') {
505+
document.write('<script src="' + baseUrl + '/msg/' + Code.PAGE + '/' + Code.LANG + '.js"></script>\n');
506+
document.write('<script src="' + baseUrl + '/blocks/' + Code.PAGE.split('/')[0] + '.js"></script>\n');
507+
document.write('<script src="' + baseUrl + '/generators/' + Code.PAGE.split('/')[0] + '.js"></script>\n');
508+
}
509+
510+
window.addEventListener('load', function () {
511+
Code.renderPage(function () {
512+
Code.init();
513+
});
514+
});

0 commit comments

Comments
 (0)