PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/libraries/blockly/demos/code/code.js

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