Skip to content

Commit 517fc01

Browse files
author
Ke, Mingze
committed
Refactor code.js
- added baseUrl variable - i18n with handlebars
1 parent cce6562 commit 517fc01

4 files changed

Lines changed: 372 additions & 343 deletions

File tree

code.js

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

26+
var baseUrl = baseUrl || '.';
27+
2628
/**
2729
* Create a namespace for the application.
2830
*/
@@ -163,10 +165,10 @@ Code.importPrettify = function () {
163165
//<script src="../prettify.js"></script>
164166
var link = document.createElement('link');
165167
link.setAttribute('rel', 'stylesheet');
166-
link.setAttribute('href', 'prettify.css');
168+
link.setAttribute('href', baseUrl + '/prettify.css');
167169
document.head.appendChild(link);
168170
var script = document.createElement('script');
169-
script.setAttribute('src', 'prettify.js');
171+
script.setAttribute('src', baseUrl + '/prettify.js');
170172
document.head.appendChild(script);
171173
};
172174

@@ -284,6 +286,7 @@ Code.renderContent = function () {
284286
* Initialize Blockly. Called on page load.
285287
*/
286288
Code.init = function () {
289+
Code.renderPage();
287290
Code.initLanguage();
288291

289292
var rtl = Code.isRtl();
@@ -318,7 +321,7 @@ Code.init = function () {
318321
colour: '#ccc',
319322
snap: true
320323
},
321-
media: 'components/blockly/media/',
324+
media: baseUrl + '/components/blockly/media/',
322325
rtl: rtl,
323326
toolbox: toolbox
324327
});
@@ -373,6 +376,11 @@ Code.init = function () {
373376
window.setTimeout(Code.importPrettify, 1);
374377
};
375378

379+
Code.renderPage = function () {
380+
var template = Handlebars.compile(document.getElementById('page-template').textContent);
381+
document.body.innerHTML = template(MSG);
382+
};
383+
376384
/**
377385
* Initialize the page language.
378386
*/
@@ -410,12 +418,6 @@ Code.initLanguage = function () {
410418

411419
// Inject language strings.
412420
document.title += ' ' + MSG['title'];
413-
document.getElementById('title').textContent = MSG['title'];
414-
document.getElementById('tab_blocks').textContent = MSG['blocks'];
415-
416-
document.getElementById('linkButton').title = MSG['linkTooltip'];
417-
document.getElementById('runButton').title = MSG['runTooltip'];
418-
document.getElementById('trashButton').title = MSG['trashTooltip'];
419421

420422
var toolbox = document.getElementById('toolbox');
421423
var categories = Array.prototype.slice.call(toolbox.querySelectorAll('category')).map(function (e) {
@@ -424,14 +426,6 @@ Code.initLanguage = function () {
424426
for (var i = 0, cat; cat = categories[i]; i++) {
425427
document.getElementById(cat).setAttribute('name', MSG[cat]);
426428
}
427-
var textVars = document.getElementsByClassName('textVar');
428-
for (var i = 0, textVar; textVar = textVars[i]; i++) {
429-
textVar.textContent = MSG['textVariable'];
430-
}
431-
var listVars = document.getElementsByClassName('listVar');
432-
for (var i = 0, listVar; listVar = listVars[i]; i++) {
433-
listVar.textContent = MSG['listVariable'];
434-
}
435429
};
436430

437431
/**
@@ -468,9 +462,9 @@ Code.discard = function () {
468462
};
469463

470464
// Load the Code demo's language strings.
471-
document.write('<script src="msg/' + Code.LANG + '.js"></script>\n');
465+
document.write('<script src="' + baseUrl + '/msg/' + Code.LANG + '.js"></script>\n');
472466
// Load Blockly's language strings.
473-
document.write('<script src="components/blockly/msg/js/' + Code.LANG + '.js"></script>\n');
474-
document.write('<script src="blocks/msg/' + Code.LANG + '.js"></script>\n');
467+
document.write('<script src="' + baseUrl + '/components/blockly/msg/js/' + Code.LANG + '.js"></script>\n');
468+
document.write('<script src="' + baseUrl + '/blocks/msg/' + Code.LANG + '.js"></script>\n');
475469

476470
window.addEventListener('load', Code.init);

0 commit comments

Comments
 (0)