code.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /**
  2. * Blockly Demos: Code
  3. *
  4. * Copyright 2012 Google Inc.
  5. * https://developers.google.com/blockly/
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * @fileoverview JavaScript for Blockly's Code demo.
  21. * @author fraser@google.com (Neil Fraser)
  22. */
  23. 'use strict';
  24. /**
  25. * Create a namespace for the application.
  26. */
  27. var Code = {};
  28. /**
  29. * Lookup for names of supported languages. Keys should be in ISO 639 format.
  30. */
  31. Code.LANGUAGE_NAME = {
  32. 'ar': 'العربية',
  33. 'be-tarask': 'Taraškievica',
  34. 'br': 'Brezhoneg',
  35. 'ca': 'Català',
  36. 'cs': 'Česky',
  37. 'da': 'Dansk',
  38. 'de': 'Deutsch',
  39. 'el': 'Ελληνικά',
  40. 'en': 'English',
  41. 'es': 'Español',
  42. 'et': 'Eesti',
  43. 'fa': 'فارسی',
  44. 'fr': 'Français',
  45. 'he': 'עברית',
  46. 'hrx': 'Hunsrik',
  47. 'hu': 'Magyar',
  48. 'ia': 'Interlingua',
  49. 'is': 'Íslenska',
  50. 'it': 'Italiano',
  51. 'ja': '日本語',
  52. 'ko': '한국어',
  53. 'mk': 'Македонски',
  54. 'ms': 'Bahasa Melayu',
  55. 'nb': 'Norsk Bokmål',
  56. 'nl': 'Nederlands, Vlaams',
  57. 'oc': 'Lenga d\'òc',
  58. 'pl': 'Polski',
  59. 'pms': 'Piemontèis',
  60. 'pt-br': 'Português Brasileiro',
  61. 'ro': 'Română',
  62. 'ru': 'Русский',
  63. 'sc': 'Sardu',
  64. 'sk': 'Slovenčina',
  65. 'sr': 'Српски',
  66. 'sv': 'Svenska',
  67. 'ta': 'தமிழ்',
  68. 'th': 'ภาษาไทย',
  69. 'tlh': 'tlhIngan Hol',
  70. 'tr': 'Türkçe',
  71. 'uk': 'Українська',
  72. 'vi': 'Tiếng Việt',
  73. 'zh-hans': '简体中文',
  74. 'zh-hant': '正體中文'
  75. };
  76. /**
  77. * List of RTL languages.
  78. */
  79. Code.LANGUAGE_RTL = ['ar', 'fa', 'he', 'lki'];
  80. /**
  81. * Blockly's main workspace.
  82. * @type {Blockly.WorkspaceSvg}
  83. */
  84. Code.workspace = null;
  85. /**
  86. * Extracts a parameter from the URL.
  87. * If the parameter is absent default_value is returned.
  88. * @param {string} name The name of the parameter.
  89. * @param {string} defaultValue Value to return if paramater not found.
  90. * @return {string} The parameter value or the default value if not found.
  91. */
  92. Code.getStringParamFromUrl = function(name, defaultValue) {
  93. var val = location.search.match(new RegExp('[?&]' + name + '=([^&]+)'));
  94. return val ? decodeURIComponent(val[1].replace(/\+/g, '%20')) : defaultValue;
  95. };
  96. /**
  97. * Get the language of this user from the URL.
  98. * @return {string} User's language.
  99. */
  100. Code.getLang = function() {
  101. var lang = Code.getStringParamFromUrl('lang', '');
  102. if (Code.LANGUAGE_NAME[lang] === undefined) {
  103. // Default to English.
  104. lang = 'en';
  105. }
  106. return lang;
  107. };
  108. /**
  109. * Is the current language (Code.LANG) an RTL language?
  110. * @return {boolean} True if RTL, false if LTR.
  111. */
  112. Code.isRtl = function() {
  113. return Code.LANGUAGE_RTL.indexOf(Code.LANG) != -1;
  114. };
  115. /**
  116. * Load blocks saved on App Engine Storage or in session/local storage.
  117. * @param {string} defaultXml Text representation of default blocks.
  118. */
  119. Code.loadBlocks = function(defaultXml) {
  120. try {
  121. var loadOnce = window.sessionStorage.loadOnceBlocks;
  122. } catch(e) {
  123. // Firefox sometimes throws a SecurityError when accessing sessionStorage.
  124. // Restarting Firefox fixes this, so it looks like a bug.
  125. var loadOnce = null;
  126. }
  127. if ('BlocklyStorage' in window && window.location.hash.length > 1) {
  128. // An href with #key trigers an AJAX call to retrieve saved blocks.
  129. BlocklyStorage.retrieveXml(window.location.hash.substring(1));
  130. } else if (loadOnce) {
  131. // Language switching stores the blocks during the reload.
  132. delete window.sessionStorage.loadOnceBlocks;
  133. var xml = Blockly.Xml.textToDom(loadOnce);
  134. Blockly.Xml.domToWorkspace(xml, Code.workspace);
  135. } else if (defaultXml) {
  136. // Load the editor with default starting blocks.
  137. var xml = Blockly.Xml.textToDom(defaultXml);
  138. Blockly.Xml.domToWorkspace(xml, Code.workspace);
  139. } else if ('BlocklyStorage' in window) {
  140. // Restore saved blocks in a separate thread so that subsequent
  141. // initialization is not affected from a failed load.
  142. window.setTimeout(BlocklyStorage.restoreBlocks, 0);
  143. }
  144. };
  145. /**
  146. * Save the blocks and reload with a different language.
  147. */
  148. Code.changeLanguage = function() {
  149. // Store the blocks for the duration of the reload.
  150. // This should be skipped for the index page, which has no blocks and does
  151. // not load Blockly.
  152. // MSIE 11 does not support sessionStorage on file:// URLs.
  153. if (typeof Blockly != 'undefined' && window.sessionStorage) {
  154. var xml = Blockly.Xml.workspaceToDom(Code.workspace);
  155. var text = Blockly.Xml.domToText(xml);
  156. window.sessionStorage.loadOnceBlocks = text;
  157. }
  158. var languageMenu = document.getElementById('languageMenu');
  159. var newLang = encodeURIComponent(
  160. languageMenu.options[languageMenu.selectedIndex].value);
  161. var search = window.location.search;
  162. if (search.length <= 1) {
  163. search = '?lang=' + newLang;
  164. } else if (search.match(/[?&]lang=[^&]*/)) {
  165. search = search.replace(/([?&]lang=)[^&]*/, '$1' + newLang);
  166. } else {
  167. search = search.replace(/\?/, '?lang=' + newLang + '&');
  168. }
  169. window.location = window.location.protocol + '//' +
  170. window.location.host + window.location.pathname + search;
  171. };
  172. /**
  173. * Bind a function to a button's click event.
  174. * On touch enabled browsers, ontouchend is treated as equivalent to onclick.
  175. * @param {!Element|string} el Button element or ID thereof.
  176. * @param {!Function} func Event handler to bind.
  177. */
  178. Code.bindClick = function(el, func) {
  179. if (typeof el == 'string') {
  180. el = document.getElementById(el);
  181. }
  182. el.addEventListener('click', func, true);
  183. el.addEventListener('touchend', func, true);
  184. };
  185. /**
  186. * Load the Prettify CSS and JavaScript.
  187. */
  188. Code.importPrettify = function() {
  189. //<link rel="stylesheet" href="../prettify.css">
  190. //<script src="../prettify.js"></script>
  191. var link = document.createElement('link');
  192. link.setAttribute('rel', 'stylesheet');
  193. link.setAttribute('href', '../prettify.css');
  194. document.head.appendChild(link);
  195. var script = document.createElement('script');
  196. script.setAttribute('src', '../prettify.js');
  197. document.head.appendChild(script);
  198. };
  199. /**
  200. * Compute the absolute coordinates and dimensions of an HTML element.
  201. * @param {!Element} element Element to match.
  202. * @return {!Object} Contains height, width, x, and y properties.
  203. * @private
  204. */
  205. Code.getBBox_ = function(element) {
  206. var height = element.offsetHeight;
  207. var width = element.offsetWidth;
  208. var x = 0;
  209. var y = 0;
  210. do {
  211. x += element.offsetLeft;
  212. y += element.offsetTop;
  213. element = element.offsetParent;
  214. } while (element);
  215. return {
  216. height: height,
  217. width: width,
  218. x: x,
  219. y: y
  220. };
  221. };
  222. /**
  223. * User's language (e.g. "en").
  224. * @type {string}
  225. */
  226. Code.LANG = Code.getLang();
  227. /**
  228. * List of tab names.
  229. * @private
  230. */
  231. Code.TABS_ = ['blocks', 'javascript', 'php', 'python', 'dart', 'lua', 'xml'];
  232. Code.selected = 'blocks';
  233. /**
  234. * Switch the visible pane when a tab is clicked.
  235. * @param {string} clickedName Name of tab clicked.
  236. */
  237. Code.tabClick = function(clickedName) {
  238. // If the XML tab was open, save and render the content.
  239. if (document.getElementById('tab_xml').className == 'tabon') {
  240. var xmlTextarea = document.getElementById('content_xml');
  241. var xmlText = xmlTextarea.value;
  242. var xmlDom = null;
  243. try {
  244. xmlDom = Blockly.Xml.textToDom(xmlText);
  245. } catch (e) {
  246. var q =
  247. window.confirm(MSG['badXml'].replace('%1', e));
  248. if (!q) {
  249. // Leave the user on the XML tab.
  250. return;
  251. }
  252. }
  253. if (xmlDom) {
  254. Code.workspace.clear();
  255. Blockly.Xml.domToWorkspace(xmlDom, Code.workspace);
  256. }
  257. }
  258. if (document.getElementById('tab_blocks').className == 'tabon') {
  259. Code.workspace.setVisible(false);
  260. }
  261. // Deselect all tabs and hide all panes.
  262. for (var i = 0; i < Code.TABS_.length; i++) {
  263. var name = Code.TABS_[i];
  264. document.getElementById('tab_' + name).className = 'taboff';
  265. document.getElementById('content_' + name).style.visibility = 'hidden';
  266. }
  267. // Select the active tab.
  268. Code.selected = clickedName;
  269. document.getElementById('tab_' + clickedName).className = 'tabon';
  270. // Show the selected pane.
  271. document.getElementById('content_' + clickedName).style.visibility =
  272. 'visible';
  273. Code.renderContent();
  274. if (clickedName == 'blocks') {
  275. Code.workspace.setVisible(true);
  276. }
  277. Blockly.svgResize(Code.workspace);
  278. };
  279. /**
  280. * Populate the currently selected pane with content generated from the blocks.
  281. */
  282. Code.renderContent = function() {
  283. var content = document.getElementById('content_' + Code.selected);
  284. // Initialize the pane.
  285. if (content.id == 'content_xml') {
  286. var xmlTextarea = document.getElementById('content_xml');
  287. var xmlDom = Blockly.Xml.workspaceToDom(Code.workspace);
  288. var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
  289. xmlTextarea.value = xmlText;
  290. xmlTextarea.focus();
  291. } else if (content.id == 'content_javascript') {
  292. var code = Blockly.JavaScript.workspaceToCode(Code.workspace);
  293. content.textContent = code;
  294. if (typeof prettyPrintOne == 'function') {
  295. code = content.textContent;
  296. code = prettyPrintOne(code, 'js');
  297. content.innerHTML = code;
  298. }
  299. } else if (content.id == 'content_python') {
  300. code = Blockly.Python.workspaceToCode(Code.workspace);
  301. content.textContent = code;
  302. if (typeof prettyPrintOne == 'function') {
  303. code = content.textContent;
  304. code = prettyPrintOne(code, 'py');
  305. content.innerHTML = code;
  306. }
  307. } else if (content.id == 'content_php') {
  308. code = Blockly.PHP.workspaceToCode(Code.workspace);
  309. content.textContent = code;
  310. if (typeof prettyPrintOne == 'function') {
  311. code = content.textContent;
  312. code = prettyPrintOne(code, 'php');
  313. content.innerHTML = code;
  314. }
  315. } else if (content.id == 'content_dart') {
  316. code = Blockly.Dart.workspaceToCode(Code.workspace);
  317. content.textContent = code;
  318. if (typeof prettyPrintOne == 'function') {
  319. code = content.textContent;
  320. code = prettyPrintOne(code, 'dart');
  321. content.innerHTML = code;
  322. }
  323. } else if (content.id == 'content_lua') {
  324. code = Blockly.Lua.workspaceToCode(Code.workspace);
  325. content.textContent = code;
  326. if (typeof prettyPrintOne == 'function') {
  327. code = content.textContent;
  328. code = prettyPrintOne(code, 'lua');
  329. content.innerHTML = code;
  330. }
  331. }
  332. };
  333. /**
  334. * Initialize Blockly. Called on page load.
  335. */
  336. Code.init = function() {
  337. Code.initLanguage();
  338. var rtl = Code.isRtl();
  339. var container = document.getElementById('content_area');
  340. var onresize = function(e) {
  341. var bBox = Code.getBBox_(container);
  342. for (var i = 0; i < Code.TABS_.length; i++) {
  343. var el = document.getElementById('content_' + Code.TABS_[i]);
  344. el.style.top = bBox.y + 'px';
  345. el.style.left = bBox.x + 'px';
  346. // Height and width need to be set, read back, then set again to
  347. // compensate for scrollbars.
  348. el.style.height = bBox.height + 'px';
  349. el.style.height = (2 * bBox.height - el.offsetHeight) + 'px';
  350. el.style.width = bBox.width + 'px';
  351. el.style.width = (2 * bBox.width - el.offsetWidth) + 'px';
  352. }
  353. // Make the 'Blocks' tab line up with the toolbox.
  354. if (Code.workspace && Code.workspace.toolbox_.width) {
  355. document.getElementById('tab_blocks').style.minWidth =
  356. (Code.workspace.toolbox_.width - 38) + 'px';
  357. // Account for the 19 pixel margin and on each side.
  358. }
  359. };
  360. window.addEventListener('resize', onresize, false);
  361. // Interpolate translated messages into toolbox.
  362. var toolboxText = document.getElementById('toolbox').outerHTML;
  363. toolboxText = toolboxText.replace(/{(\w+)}/g,
  364. function(m, p1) {return MSG[p1]});
  365. var toolboxXml = Blockly.Xml.textToDom(toolboxText);
  366. Code.workspace = Blockly.inject('content_blocks',
  367. {grid:
  368. {spacing: 25,
  369. length: 3,
  370. colour: '#ccc',
  371. snap: true},
  372. media: '../../media/',
  373. rtl: rtl,
  374. toolbox: toolboxXml,
  375. zoom:
  376. {controls: true,
  377. wheel: true}
  378. });
  379. // Add to reserved word list: Local variables in execution environment (runJS)
  380. // and the infinite loop detection function.
  381. Blockly.JavaScript.addReservedWords('code,timeouts,checkTimeout');
  382. Code.loadBlocks('');
  383. if ('BlocklyStorage' in window) {
  384. // Hook a save function onto unload.
  385. BlocklyStorage.backupOnUnload(Code.workspace);
  386. }
  387. Code.tabClick(Code.selected);
  388. Code.bindClick('trashButton',
  389. function() {Code.discard(); Code.renderContent();});
  390. Code.bindClick('runButton', Code.runJS);
  391. // Disable the link button if page isn't backed by App Engine storage.
  392. var linkButton = document.getElementById('linkButton');
  393. if ('BlocklyStorage' in window) {
  394. BlocklyStorage['HTTPREQUEST_ERROR'] = MSG['httpRequestError'];
  395. BlocklyStorage['LINK_ALERT'] = MSG['linkAlert'];
  396. BlocklyStorage['HASH_ERROR'] = MSG['hashError'];
  397. BlocklyStorage['XML_ERROR'] = MSG['xmlError'];
  398. Code.bindClick(linkButton,
  399. function() {BlocklyStorage.link(Code.workspace);});
  400. } else if (linkButton) {
  401. linkButton.className = 'disabled';
  402. }
  403. for (var i = 0; i < Code.TABS_.length; i++) {
  404. var name = Code.TABS_[i];
  405. Code.bindClick('tab_' + name,
  406. function(name_) {return function() {Code.tabClick(name_);};}(name));
  407. }
  408. onresize();
  409. Blockly.svgResize(Code.workspace);
  410. // Lazy-load the syntax-highlighting.
  411. window.setTimeout(Code.importPrettify, 1);
  412. };
  413. /**
  414. * Initialize the page language.
  415. */
  416. Code.initLanguage = function() {
  417. // Set the HTML's language and direction.
  418. var rtl = Code.isRtl();
  419. document.dir = rtl ? 'rtl' : 'ltr';
  420. document.head.parentElement.setAttribute('lang', Code.LANG);
  421. // Sort languages alphabetically.
  422. var languages = [];
  423. for (var lang in Code.LANGUAGE_NAME) {
  424. languages.push([Code.LANGUAGE_NAME[lang], lang]);
  425. }
  426. var comp = function(a, b) {
  427. // Sort based on first argument ('English', 'Русский', '简体字', etc).
  428. if (a[0] > b[0]) return 1;
  429. if (a[0] < b[0]) return -1;
  430. return 0;
  431. };
  432. languages.sort(comp);
  433. // Populate the language selection menu.
  434. var languageMenu = document.getElementById('languageMenu');
  435. languageMenu.options.length = 0;
  436. for (var i = 0; i < languages.length; i++) {
  437. var tuple = languages[i];
  438. var lang = tuple[tuple.length - 1];
  439. var option = new Option(tuple[0], lang);
  440. if (lang == Code.LANG) {
  441. option.selected = true;
  442. }
  443. languageMenu.options.add(option);
  444. }
  445. languageMenu.addEventListener('change', Code.changeLanguage, true);
  446. // Inject language strings.
  447. document.title += ' ' + MSG['title'];
  448. document.getElementById('title').textContent = MSG['title'];
  449. document.getElementById('tab_blocks').textContent = MSG['blocks'];
  450. document.getElementById('linkButton').title = MSG['linkTooltip'];
  451. document.getElementById('runButton').title = MSG['runTooltip'];
  452. document.getElementById('trashButton').title = MSG['trashTooltip'];
  453. };
  454. /**
  455. * Execute the user's code.
  456. * Just a quick and dirty eval. Catch infinite loops.
  457. */
  458. Code.runJS = function() {
  459. Blockly.JavaScript.INFINITE_LOOP_TRAP = ' checkTimeout();\n';
  460. var timeouts = 0;
  461. var checkTimeout = function() {
  462. if (timeouts++ > 1000000) {
  463. throw MSG['timeout'];
  464. }
  465. };
  466. var code = Blockly.JavaScript.workspaceToCode(Code.workspace);
  467. Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
  468. try {
  469. eval(code);
  470. } catch (e) {
  471. alert(MSG['badCode'].replace('%1', e));
  472. }
  473. };
  474. /**
  475. * Discard all blocks from the workspace.
  476. */
  477. Code.discard = function() {
  478. var count = Code.workspace.getAllBlocks().length;
  479. if (count < 2 ||
  480. window.confirm(Blockly.Msg.DELETE_ALL_BLOCKS.replace('%1', count))) {
  481. Code.workspace.clear();
  482. if (window.location.hash) {
  483. window.location.hash = '';
  484. }
  485. }
  486. };
  487. // Load the Code demo's language strings.
  488. document.write('<script src="msg/' + Code.LANG + '.js"></script>\n');
  489. // Load Blockly's language strings.
  490. document.write('<script src="../../msg/js/' + Code.LANG + '.js"></script>\n');
  491. window.addEventListener('load', Code.init);