|
23 | 23 | */ |
24 | 24 | 'use strict'; |
25 | 25 |
|
| 26 | +var slice = Array.prototype.slice; |
| 27 | + |
26 | 28 | var baseUrl = baseUrl || '.'; |
27 | 29 |
|
28 | 30 | /** |
@@ -74,6 +76,11 @@ Code.getLang = function () { |
74 | 76 | return lang; |
75 | 77 | }; |
76 | 78 |
|
| 79 | +Code.getPage = function () { |
| 80 | + var page = Code.getStringParamFromUrl('page', 'index'); |
| 81 | + return page; |
| 82 | +}; |
| 83 | + |
77 | 84 | /** |
78 | 85 | * Is the current language (Code.LANG) an RTL language? |
79 | 86 | * @return {boolean} True if RTL, false if LTR. |
@@ -166,6 +173,9 @@ Code.importPrettify = function () { |
166 | 173 | var link = document.createElement('link'); |
167 | 174 | link.setAttribute('rel', 'stylesheet'); |
168 | 175 | link.setAttribute('href', baseUrl + '/prettify.css'); |
| 176 | + link.onload = function () { |
| 177 | + Blockly.fireUiEvent(window, 'resize'); |
| 178 | + }; |
169 | 179 | document.head.appendChild(link); |
170 | 180 | var script = document.createElement('script'); |
171 | 181 | script.setAttribute('src', baseUrl + '/prettify.js'); |
@@ -202,6 +212,8 @@ Code.getBBox_ = function (element) { |
202 | 212 | */ |
203 | 213 | Code.LANG = Code.getLang(); |
204 | 214 |
|
| 215 | +Code.PAGE = Code.getPage(); |
| 216 | + |
205 | 217 | /** |
206 | 218 | * List of tab names. |
207 | 219 | * @private |
@@ -286,7 +298,6 @@ Code.renderContent = function () { |
286 | 298 | * Initialize Blockly. Called on page load. |
287 | 299 | */ |
288 | 300 | Code.init = function () { |
289 | | - Code.renderPage(); |
290 | 301 | Code.initLanguage(); |
291 | 302 |
|
292 | 303 | var rtl = Code.isRtl(); |
@@ -376,9 +387,33 @@ Code.init = function () { |
376 | 387 | window.setTimeout(Code.importPrettify, 1); |
377 | 388 | }; |
378 | 389 |
|
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(); |
382 | 417 | }; |
383 | 418 |
|
384 | 419 | /** |
@@ -419,7 +454,7 @@ Code.initLanguage = function () { |
419 | 454 | // Inject language strings. |
420 | 455 | document.title += ' ' + MSG['title']; |
421 | 456 | 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) { |
423 | 458 | return e.id |
424 | 459 | }); |
425 | 460 | for (var i = 0, cat; cat = categories[i]; i++) { |
@@ -466,4 +501,14 @@ document.write('<script src="' + baseUrl + '/msg/' + Code.LANG + '.js"></script> |
466 | 501 | document.write('<script src="' + baseUrl + '/components/blockly/msg/js/' + Code.LANG + '.js"></script>\n'); |
467 | 502 | document.write('<script src="' + baseUrl + '/blocks/msg/' + Code.LANG + '.js"></script>\n'); |
468 | 503 |
|
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