imported.js.html 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: imported.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: imported.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/*
  20. Blockly.Blocks['classics_get_all'] = {
  21. init: function() {
  22. this.setHelpUrl('http://www.example.com/');
  23. this.setColour(WEATHER_HUE);
  24. this.appendDummyInput()
  25. .appendField("classics.get all books");
  26. this.setInputsInline(false);
  27. this.setOutput(true, "Number");
  28. this.setTooltip('Returns all the books');
  29. }
  30. };
  31. Blockly.Python['classics_get_all'] = function(block) {
  32. Blockly.Python.definitions_['import_classics'] = 'import classics';
  33. var code = 'classics.get_all()';
  34. return [code, Blockly.Python.ORDER_ATOMIC];
  35. };
  36. PythonToBlocks.KNOWN_MODULES['classics'] = {
  37. "get_all": ["classics_get_all"]
  38. };
  39. */
  40. function newBlock(name) {
  41. var block = blockpy.components.editor.blockly.newBlock(name);
  42. block.initSvg();
  43. block.render();
  44. }
  45. var DAYS = [
  46. ["Monday", "MON"],
  47. ["Tuesday", "TUE"],
  48. ["Wednesday", "WED"],
  49. ["Thursday", "THU"],
  50. ["Friday", "FRI"],
  51. ["Saturday", "SAT"],
  52. ["Sunday", "SUN"]
  53. ]
  54. var DAYS_MAP = {
  55. 'mon': 'parking.Day("MON")',
  56. 'tue': 'parking.Day("TUE")',
  57. 'wed': 'parking.Day("WED")',
  58. 'thu': 'parking.Day("THU")',
  59. 'fri': 'parking.Day("FRI")',
  60. 'sat': 'parking.Day("SAT")',
  61. 'sun': 'parking.Day("SUN")',
  62. 'tod': 'parking.today()'
  63. };
  64. Blockly.Blocks['datetime_day'] = {
  65. /**
  66. * Block for datetime day.
  67. * @this Blockly.Block
  68. */
  69. init: function() {
  70. this.jsonInit({
  71. "message0": "%1",
  72. "args0": [
  73. {
  74. "type": "field_dropdown",
  75. "name": "DAY",
  76. "options": [["Today", "TODAY"]].concat(DAYS)
  77. }
  78. ],
  79. "output": "DatetimeDay",
  80. "colour": DATA_HUE,
  81. "tooltip": "Returns a day of the week",
  82. "helpUrl": ""
  83. });
  84. }
  85. };
  86. Blockly.Python['datetime_day'] = function(block) {
  87. Blockly.Python.definitions_['import_parking'] = 'import parking';
  88. var operator = DAYS_MAP[block.getFieldValue('DAY').slice(0, 3).toLowerCase()];
  89. return [operator, Blockly.Python.ORDER_ATOMIC];
  90. };
  91. var HOURS = [["1", "1"], ["2", "2"], ["3", "3"],
  92. ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"],
  93. ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"],
  94. ["12", "12"]];
  95. var MINUTES = [["00", "00"], ["30", "30"]];
  96. var MERIDIANS = [["am", "AM"], ["pm", "PM"]];
  97. Blockly.Blocks['datetime_time'] = {
  98. /**
  99. * Block for datetime day.
  100. * @this Blockly.Block
  101. */
  102. init: function() {
  103. this.setColour(DATA_HUE);
  104. var dropdown = new Blockly.FieldDropdown([["Now", "NOW"]].concat(HOURS), function(opt) {
  105. var isNow = (opt == 'NOW');
  106. this.sourceBlock_.updateShape_(isNow);
  107. });
  108. this.appendDummyInput()
  109. .appendField(dropdown, 'HOUR');
  110. this.setInputsInline(true);
  111. this.setOutput(true, 'DatetimeTime');
  112. this.setTooltip("Returns a time of day");
  113. },
  114. mutationToDom: function() {
  115. var container = document.createElement('mutation');
  116. var isNow = (this.getFieldValue('HOUR').toUpperCase() == 'NOW');
  117. container.setAttribute('isnow', isNow);
  118. return container;
  119. },
  120. domToMutation: function(xmlElement) {
  121. var isNow = (xmlElement.getAttribute('isnow') == 'true');
  122. this.updateShape_(isNow);
  123. },
  124. updateShape_: function(isNow) {
  125. // Add or remove a Value Input.
  126. var inputExists = this.getInput('EXTENDED');
  127. if (!isNow) {
  128. if (!inputExists) {
  129. var minuteMenu = new Blockly.FieldDropdown(MINUTES);
  130. var meridianMenu = new Blockly.FieldDropdown(MERIDIANS);
  131. this.appendDummyInput('EXTENDED')
  132. .appendField(':')
  133. .appendField(minuteMenu, 'MINUTE')
  134. .appendField(meridianMenu, 'MERIDIAN');
  135. }
  136. } else if (inputExists) {
  137. this.removeInput('EXTENDED');
  138. }
  139. }
  140. };
  141. var HOURS_MAP = {
  142. '1': 'parking.one()',
  143. '2': 'parking.two()',
  144. '3': 'parking.three()',
  145. '4': 'parking.four()',
  146. '5': 'parking.five()',
  147. '6': 'parking.six()',
  148. '7': 'parking.seven()',
  149. '8': 'parking.eight()',
  150. '9': 'parking.nine()',
  151. '10': 'parking.ten()',
  152. '11': 'parking.eleven()',
  153. '12': 'parking.twelve()',
  154. 'NOW': 'parking.now()'
  155. };
  156. var MINUTES_MAP = {
  157. '00': 'parking.exactly()',
  158. '30': 'parking.half()'
  159. }
  160. var MERIDIANS_MAP = {
  161. 'AM': 'parking.am()',
  162. 'PM': 'parking.pm()'
  163. }
  164. Blockly.Python['datetime_time'] = function(block) {
  165. Blockly.Python.definitions_['import_parking'] = 'import parking';
  166. var hour = block.getFieldValue('HOUR');
  167. var code;
  168. if (hour == "NOW") {
  169. code = "parking.now()";
  170. } else {
  171. var minute = parseInt(block.getFieldValue('MINUTE'));
  172. var meridian = Blockly.Python.quote_(block.getFieldValue('MERIDIAN'));
  173. code = 'parking.Time('+hour+','+minute+','+meridian+')';
  174. }
  175. return [code, Blockly.Python.ORDER_ATOMIC];
  176. };
  177. var convertDate = function(date) {
  178. date = date.slice(0, 3).toLowerCase();
  179. switch (date) {
  180. case "mon": return "Monday";
  181. case "tue": return "Tuesday";
  182. case "wed": return "Wednesday";
  183. case "thu": return "Thursday";
  184. case "fri": return "Friday";
  185. case "sat": return "Saturday";
  186. case "sun": return "Sunday";
  187. default: return date;
  188. }
  189. }
  190. var convertMinute = function(minute) {
  191. if (minute &lt; 10) {
  192. return "0"+minute;
  193. } else {
  194. return ""+minute;
  195. }
  196. }
  197. PythonToBlocks.KNOWN_MODULES['parking'] = {
  198. "today": ["datetime_day", ["DAY", "TODAY"]],
  199. "day_compare": ["datetime_check_day", "OP",
  200. {"type": "variable", "mode": "value", "name": "LEFT"},
  201. {"type": "mapper", "name": "VALUE", "method": convertDate}],
  202. "Day": ["datetime_day", {"type": "mapper", "name": "DAY", "method": convertDate}],
  203. "now": ["datetime_time", ["HOUR", "NOW"]],
  204. "Time": ["datetime_time", {"type": "integer", "name": "HOUR", "add_mutation": {"name": "@isnow", "value": "false"}},
  205. {"type": "integer_mapper", "name": "MINUTE", "method": convertMinute},
  206. "MERIDIAN"],
  207. "time_compare": ["datetime_check_time", "OP",
  208. {"type": "variable", "mode": "value", "name": "LEFT"},
  209. {"type": "integer", "name": "HOURS"},
  210. {"type": "integer_mapper", "name": "MINUTES", "method": convertMinute},
  211. "MERIDIANS"]
  212. };
  213. var equalityOperators = [
  214. ["==", "IS"],
  215. ["&lt;", "BEFORE"],
  216. [">", "AFTER"],
  217. ["&lt;=", "BEFORE_EQUAL"],
  218. ["=>", "AFTER_EQUAL"],
  219. ["!=", "IS_NOT"]
  220. ];
  221. var equalityOperatorsConversions = {
  222. "IS": "parking.equal",
  223. "BEFORE": "parking.before",
  224. "AFTER": "parking.after",
  225. "BEFORE_EQUAL": "parking.before_equal",
  226. "AFTER_EQUAL": "parking.after_equal",
  227. "IS_NOT": "parking.not_equal",
  228. }
  229. // FINISH _time and _day
  230. // Add in numbers and days to KNOWN_MODULES
  231. Blockly.Blocks['datetime_check_day'] = {
  232. /**
  233. * Block for testing if something contains something.
  234. * @this Blockly.Block
  235. */
  236. init: function() {
  237. this.setColour(Blockly.Blocks.logic.HUE);
  238. this.setOutput(true, 'Boolean');
  239. this.appendValueInput('LEFT')
  240. .setCheck('DatetimeDay')
  241. .appendField(new Blockly.FieldDropdown(DAYS), 'VALUE')
  242. .appendField(new Blockly.FieldDropdown(equalityOperators), 'OP');
  243. this.setInputsInline(false);
  244. }
  245. };
  246. Blockly.Python['datetime_check_day'] = function(block) {
  247. Blockly.Python.definitions_['import_parking'] = 'import parking';
  248. var value = Blockly.Python.quote_(block.getFieldValue('VALUE'));
  249. var operator = Blockly.Python.quote_(block.getFieldValue('OP'));
  250. var left = Blockly.Python.valueToCode(block, 'LEFT', Blockly.Python.ORDER_ATOMIC) || "___";
  251. var code = "parking.day_compare(" + operator + ", " + left + ', ' + value + ")";
  252. return [code, Blockly.Python.ORDER_ATOMIC];
  253. };
  254. Blockly.Blocks['datetime_check_time'] = {
  255. /**
  256. * Block for testing if something contains something.
  257. * @this Blockly.Block
  258. */
  259. init: function() {
  260. this.setColour(Blockly.Blocks.logic.HUE);
  261. this.setOutput(true, 'Boolean');
  262. this.appendValueInput('LEFT')
  263. .setCheck('DatetimeTime')
  264. .appendField(new Blockly.FieldDropdown(HOURS), 'HOURS')
  265. .appendField(':')
  266. .appendField(new Blockly.FieldDropdown(MINUTES), 'MINUTES')
  267. .appendField(new Blockly.FieldDropdown(MERIDIANS), 'MERIDIANS')
  268. .appendField(new Blockly.FieldDropdown(equalityOperators), 'OP');
  269. //this.setInputsInline(true);
  270. }
  271. };
  272. Blockly.Python['datetime_check_time'] = function(block) {
  273. Blockly.Python.definitions_['import_parking'] = 'import parking';
  274. var hour = parseInt(block.getFieldValue('HOURS'));
  275. var minute = parseInt(block.getFieldValue('MINUTES'));
  276. var meridian = Blockly.Python.quote_(block.getFieldValue('MERIDIANS'));
  277. var operator = Blockly.Python.quote_(block.getFieldValue('OP'));
  278. var left = Blockly.Python.valueToCode(block, 'LEFT', Blockly.Python.ORDER_ATOMIC)
  279. var code = "parking.time_compare(" + operator+", "+left + ',' + hour + ',' + minute + ',' +meridian + ")";
  280. return [code, Blockly.Python.ORDER_ATOMIC];
  281. };
  282. Blockly.Blocks['controls_forEach'] = {
  283. /**
  284. * Block for 'for each' loop.
  285. * @this Blockly.Block
  286. */
  287. init: function() {
  288. this.jsonInit({
  289. "message0": "for each item %1 in list %2 : ", //Blockly.Msg.CONTROLS_FOREACH_TITLE,
  290. "args0": [
  291. {
  292. "type": "input_value",
  293. "name": "VAR",
  294. "check": "Tuple"
  295. },
  296. {
  297. "type": "input_value",
  298. "name": "LIST",
  299. "check": "Array"
  300. }
  301. ],
  302. "inputsInline": true,
  303. "previousStatement": null,
  304. "nextStatement": null,
  305. "colour": Blockly.Blocks.loops.HUE,
  306. "helpUrl": Blockly.Msg.CONTROLS_FOREACH_HELPURL
  307. });
  308. this.appendStatementInput('DO')
  309. .appendField(Blockly.Msg.CONTROLS_FOREACH_INPUT_DO);
  310. this.setInputsInline(true);
  311. // Assign 'this' to a variable for use in the tooltip closure below.
  312. var thisBlock = this;
  313. this.setTooltip(function() {
  314. return Blockly.Msg.CONTROLS_FOREACH_TOOLTIP.replace('%1',
  315. Blockly.Python.valueToCode(thisBlock, 'VAR', Blockly.Python.ORDER_RELATIONAL) || '___');
  316. });
  317. },
  318. customContextMenu: Blockly.Blocks['controls_for'].customContextMenu
  319. };
  320. Blockly.Python['controls_forEach'] = function(block) {
  321. // For each loop.
  322. var variable0 = Blockly.Python.valueToCode(block, 'VAR',
  323. Blockly.Python.ORDER_RELATIONAL) || '___';
  324. var argument0 = Blockly.Python.valueToCode(block, 'LIST',
  325. Blockly.Python.ORDER_RELATIONAL) || '___';
  326. var branch = Blockly.Python.statementToCode(block, 'DO');
  327. branch = Blockly.Python.addLoopTrap(branch, block.id) ||
  328. Blockly.Python.PASS;
  329. var code = 'for ' + variable0 + ' in ' + argument0 + ':\n' + branch;
  330. return code;
  331. };
  332. Blockly.Blocks['class_creation'] = {
  333. init: function() {
  334. this.appendDummyInput()
  335. .appendField("Create class")
  336. .appendField(new Blockly.FieldVariable("new class"), "CLASS");
  337. /*
  338. this.appendDummyInput()
  339. .appendField("Inherits from")
  340. .appendField(new Blockly.FieldVariable("j"), "NAME")
  341. .appendField(",")
  342. .appendField(new Blockly.FieldVariable("k"), "NAME");
  343. */
  344. this.appendStatementInput("BODY")
  345. .setCheck(null);
  346. this.setPreviousStatement(true, null);
  347. this.setNextStatement(true, null);
  348. this.setColour(230);
  349. this.setTooltip('');
  350. this.setHelpUrl('http://www.example.com/');
  351. }
  352. };
  353. Blockly.Python['class_creation'] = function(block) {
  354. var class_name = Blockly.Python.variableDB_.getName(block.getFieldValue('CLASS'), Blockly.Variables.NAME_TYPE) || '___';
  355. var body = Blockly.Python.statementToCode(block, 'BODY') ||
  356. Blockly.Python.PASS;
  357. // TODO: Assemble Python into code variable.
  358. var code = 'class ' + class_name + ':\n' + body;
  359. return code;
  360. };
  361. Blockly.Blocks['list_comprehension'] = {
  362. init: function() {
  363. this.appendValueInput("body")
  364. .setCheck(null)
  365. .appendField("[");
  366. this.appendValueInput("var")
  367. .setCheck(null)
  368. .appendField("for");
  369. this.appendValueInput("list")
  370. .setCheck(null)
  371. .appendField("in");
  372. this.appendDummyInput()
  373. .appendField("]");
  374. this.setInputsInline(true);
  375. this.setOutput(true, null);
  376. this.setTooltip('');
  377. this.setHelpUrl('http://www.example.com/');
  378. }
  379. };
  380. Blockly.Python['list_comprehension'] = function(block) {
  381. var value_body = Blockly.Python.valueToCode(block, 'body', Blockly.Python.ORDER_ATOMIC) || '___';
  382. var value_var = Blockly.Python.valueToCode(block, 'var', Blockly.Python.ORDER_ATOMIC) || '___';
  383. var value_list = Blockly.Python.valueToCode(block, 'list', Blockly.Python.ORDER_ATOMIC) || '___';
  384. // TODO: Assemble Python into code variable.
  385. var code = '['+value_body+' for '+value_var+' in '+value_list+']';
  386. // TODO: Change ORDER_NONE to the correct strength.
  387. return [code, Blockly.Python.ORDER_NONE];
  388. };
  389. /**
  390. * Decode an XML DOM and create blocks on the workspace, clearing out old blocks.
  391. * @param {!Element} xml XML DOM.
  392. * @param {!Blockly.Workspace} workspace The workspace.
  393. */
  394. Blockly.Xml.domToWorkspaceDestructive = function(xml, workspace, errorXml) {
  395. if (xml instanceof Blockly.Workspace) {
  396. var swap = xml;
  397. xml = workspace;
  398. workspace = swap;
  399. console.warn('Deprecated call to Blockly.Xml.domToWorkspace, ' +
  400. 'swap the arguments.');
  401. }
  402. var width; // Not used in LTR.
  403. if (workspace.RTL) {
  404. width = workspace.getWidth();
  405. }
  406. Blockly.Field.startCache();
  407. // Safari 7.1.3 is known to provide node lists with extra references to
  408. // children beyond the lists' length. Trust the length, do not use the
  409. // looping pattern of checking the index for an object.
  410. var childCount = xml.childNodes.length;
  411. var existingGroup = Blockly.Events.getGroup();
  412. if (!existingGroup) {
  413. Blockly.Events.setGroup(true);
  414. }
  415. Blockly.Events.disable();
  416. while (workspace.topBlocks_.length) {
  417. workspace.topBlocks_[0].dispose();
  418. }
  419. workspace.variableList.length = 0;
  420. Blockly.Events.enable();
  421. // Disable workspace resizes as an optimization.
  422. if (workspace.setResizesEnabled) {
  423. workspace.setResizesEnabled(false);
  424. }
  425. for (var i = 0; i &lt; childCount; i++) {
  426. var xmlChild = xml.childNodes[i];
  427. var name = xmlChild.nodeName.toLowerCase();
  428. if (name == 'block' ||
  429. (name == 'shadow' &amp;&amp; !Blockly.Events.recordUndo)) {
  430. // Allow top-level shadow blocks if recordUndo is disabled since
  431. // that means an undo is in progress. Such a block is expected
  432. // to be moved to a nested destination in the next operation.
  433. var block = Blockly.Xml.domToBlock(xmlChild, workspace);
  434. var blockX = parseInt(xmlChild.getAttribute('x'), 10);
  435. var blockY = parseInt(xmlChild.getAttribute('y'), 10);
  436. if (!isNaN(blockX) &amp;&amp; !isNaN(blockY)) {
  437. block.moveBy(workspace.RTL ? width - blockX : blockX, blockY);
  438. }
  439. } else if (name == 'shadow') {
  440. goog.asserts.fail('Shadow block cannot be a top-level block.');
  441. }
  442. }
  443. if (!existingGroup) {
  444. Blockly.Events.setGroup(false);
  445. }
  446. Blockly.Field.stopCache();
  447. workspace.updateVariableList(false);
  448. // Re-enable workspace resizing.
  449. if (workspace.setResizesEnabled) {
  450. workspace.setResizesEnabled(true);
  451. }
  452. }
  453. Blockly.Blocks['comment_single'] = {
  454. init: function() {
  455. this.appendDummyInput()
  456. .appendField("Comment:")
  457. .appendField(new Blockly.FieldTextInput("will be ignored"), "BODY");
  458. this.setInputsInline(true);
  459. this.setPreviousStatement(true, null);
  460. this.setNextStatement(true, null);
  461. this.setColour(60);
  462. this.setTooltip('This is a comment, which will be ignored when you execute your code.');
  463. this.setHelpUrl('');
  464. }
  465. };
  466. Blockly.Python['comment_single'] = function(block) {
  467. var text_body = block.getFieldValue('BODY');
  468. // TODO: Assemble JavaScript into code variable.
  469. var code = '# '+text_body+'\n';
  470. return code;
  471. };
  472. Blockly.Blocks['string_multiline'] = {
  473. // Container.
  474. init: function() {
  475. this.appendDummyInput()
  476. .appendField('Multiline String:');
  477. this.appendDummyInput()
  478. .appendField(this.newQuote_(true))
  479. .appendField(new Blockly.FieldTextArea(''), 'TEXT')
  480. .appendField(this.newQuote_(false));
  481. this.setColour(Blockly.Blocks.texts.HUE);
  482. this.setOutput(true, 'String');
  483. },
  484. newQuote_: function(open) {
  485. if (open == this.RTL) {
  486. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
  487. } else {
  488. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
  489. }
  490. return new Blockly.FieldImage(file, 12, 12, '"');
  491. }
  492. };
  493. Blockly.Python['string_multiline'] = function(block) {
  494. var text_body = block.getFieldValue('TEXT');
  495. // TODO: Assemble JavaScript into code variable.
  496. var code = '"""'+text_body+'"""\n';
  497. return [code, Blockly.Python.ORDER_ATOMIC];
  498. };
  499. Blockly.Blocks['attribute_access'] = {
  500. init: function() {
  501. this.appendValueInput("MODULE")
  502. .setCheck(null);
  503. this.appendValueInput("NAME")
  504. .setCheck(null)
  505. .appendField(".");
  506. this.setInputsInline(true);
  507. this.setOutput(true, null);
  508. this.setColour(230);
  509. this.setTooltip('');
  510. this.setHelpUrl('');
  511. }
  512. };
  513. Blockly.Python['attribute_access'] = function(block) {
  514. var value_module = Blockly.Python.valueToCode(block, 'MODULE', Blockly.Python.ORDER_ATOMIC);
  515. var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  516. // TODO: Assemble JavaScript into code variable.
  517. var code = value_module+'.'+value_name;
  518. // TODO: Change ORDER_NONE to the correct strength.
  519. return [code, Blockly.Python.ORDER_NONE];
  520. };
  521. /**
  522. * Turtles!
  523. */
  524. Blockly.Blocks['turtle_create'] = {
  525. init: function() {
  526. this.appendDummyInput()
  527. .appendField("create new turtle");
  528. this.setOutput(true, 'Turtle');
  529. this.setColour(180);
  530. this.setTooltip('Creates a new turtle');
  531. this.setHelpUrl('');
  532. }
  533. };
  534. Blockly.Python['turtle_create'] = function(block) {
  535. Blockly.Python.definitions_['import_turtle'] = 'import turtle';
  536. var code = 'turtle.Turtle()\n';
  537. // TODO: Change ORDER_NONE to the correct strength.
  538. return [code, Blockly.Python.ORDER_NONE];
  539. };
  540. PythonToBlocks.KNOWN_MODULES['turtle'] = {
  541. "Turtle": ["turtle_create"]
  542. }
  543. Blockly.Blocks['turtle_color'] = {
  544. init: function() {
  545. this.appendValueInput("TURTLE")
  546. .setCheck("Turtle")
  547. .appendField("make turtle");
  548. this.appendValueInput("COLOR")
  549. .setCheck(null)
  550. .appendField("color");
  551. this.setPreviousStatement(true);
  552. this.setNextStatement(true);
  553. this.setOutput(false);
  554. this.setInputsInline(true);
  555. this.setColour(180);
  556. this.setTooltip('');
  557. this.setHelpUrl('');
  558. }
  559. };
  560. Blockly.Python['turtle_color'] = function(block) {
  561. var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  562. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  563. // TODO: Assemble Python into code variable.
  564. var code = turtle+'.color('+color+')\n';
  565. return code;
  566. };
  567. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['color'] = function(func, args, keywords, starargs, kwargs, node) {
  568. if (args.length &lt; 1 || args.length > 2) {
  569. throw new Error("Incorrect number of arguments to turtle.color!");
  570. }
  571. return [block("turtle_color", func.lineno, {}, {
  572. "COLOR": this.convert(args[0]),
  573. "TURTLE": this.convert(func.value)
  574. }, {"inline": "true"})];
  575. }
  576. Blockly.Blocks['turtle_forward'] = {
  577. init: function() {
  578. this.appendValueInput("TURTLE")
  579. .setCheck("Turtle")
  580. .appendField("make turtle");
  581. this.appendValueInput("DISTANCE")
  582. .setCheck(null)
  583. .appendField("move forward by");
  584. this.setPreviousStatement(true);
  585. this.setNextStatement(true);
  586. this.setInputsInline(true);
  587. this.setOutput(false);
  588. this.setColour(180);
  589. this.setTooltip('');
  590. this.setHelpUrl('');
  591. }
  592. };
  593. Blockly.Python['turtle_forward'] = function(block) {
  594. var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  595. var distance = Blockly.Python.valueToCode(block, 'DISTANCE', Blockly.Python.ORDER_ATOMIC);
  596. // TODO: Assemble Python into code variable.
  597. var code = turtle+'.forward('+distance+')\n';
  598. return code;
  599. };
  600. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['forward'] = function(func, args, keywords, starargs, kwargs, node) {
  601. if (args.length != 1) {
  602. throw new Error("Incorrect number of arguments to turtle.forward!");
  603. }
  604. return [block("turtle_forward", func.lineno, {}, {
  605. "DISTANCE": this.convert(args[0]),
  606. "TURTLE": this.convert(func.value)
  607. }, {"inline": "true"})];
  608. }
  609. Blockly.Blocks['turtle_backward'] = {
  610. init: function() {
  611. this.appendValueInput("TURTLE")
  612. .setCheck("Turtle")
  613. .appendField("make turtle");
  614. this.appendValueInput("DISTANCE")
  615. .setCheck(null)
  616. .appendField("move backward by");
  617. this.setPreviousStatement(true);
  618. this.setInputsInline(true);
  619. this.setNextStatement(true);
  620. this.setOutput(false);
  621. this.setColour(180);
  622. this.setTooltip('');
  623. this.setHelpUrl('');
  624. }
  625. };
  626. Blockly.Python['turtle_backward'] = function(block) {
  627. var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  628. var distance = Blockly.Python.valueToCode(block, 'DISTANCE', Blockly.Python.ORDER_ATOMIC);
  629. // TODO: Assemble Python into code variable.
  630. var code = turtle+'.backward('+distance+')\n';
  631. return code;
  632. };
  633. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['backward'] = function(func, args, keywords, starargs, kwargs, node) {
  634. if (args.length != 1) {
  635. throw new Error("Incorrect number of arguments to turtle.backward!");
  636. }
  637. return [block("turtle_backward", func.lineno, {}, {
  638. "DISTANCE": this.convert(args[0]),
  639. "TURTLE": this.convert(func.value)
  640. }, {"inline": "true"})];
  641. }
  642. Blockly.Blocks['turtle_left'] = {
  643. init: function() {
  644. this.appendValueInput("TURTLE")
  645. .setCheck("Turtle")
  646. .appendField("make turtle");
  647. this.appendValueInput("ANGLE")
  648. .setCheck(null)
  649. .appendField("turn left by");
  650. this.setPreviousStatement(true);
  651. this.setNextStatement(true);
  652. this.setInputsInline(true);
  653. this.setOutput(false);
  654. this.setColour(180);
  655. this.setTooltip('');
  656. this.setHelpUrl('');
  657. }
  658. };
  659. Blockly.Python['turtle_left'] = function(block) {
  660. var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  661. var angle = Blockly.Python.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_ATOMIC);
  662. // TODO: Assemble Python into code variable.
  663. var code = turtle+'.left('+angle+')\n';
  664. return code;
  665. };
  666. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['left'] = function(func, args, keywords, starargs, kwargs, node) {
  667. if (args.length != 1) {
  668. throw new Error("Incorrect number of arguments to turtle.left!");
  669. }
  670. return [block("turtle_left", func.lineno, {}, {
  671. "ANGLE": this.convert(args[0]),
  672. "TURTLE": this.convert(func.value)
  673. }, {"inline": "true"})];
  674. }
  675. Blockly.Blocks['turtle_right'] = {
  676. init: function() {
  677. this.appendValueInput("TURTLE")
  678. .setCheck("Turtle")
  679. .appendField("make turtle");
  680. this.appendValueInput("ANGLE")
  681. .setCheck(null)
  682. .appendField("turn right by");
  683. this.setPreviousStatement(true);
  684. this.setNextStatement(true);
  685. this.setInputsInline(true);
  686. this.setOutput(false);
  687. this.setColour(180);
  688. this.setTooltip('');
  689. this.setHelpUrl('');
  690. }
  691. };
  692. Blockly.Python['turtle_right'] = function(block) {
  693. var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
  694. var angle = Blockly.Python.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_ATOMIC);
  695. // TODO: Assemble Python into code variable.
  696. var code = turtle+'.right('+angle+')\n';
  697. return code;
  698. };
  699. PythonToBlocks.KNOWN_ATTR_FUNCTIONS['right'] = function(func, args, keywords, starargs, kwargs, node) {
  700. if (args.length != 1) {
  701. throw new Error("Incorrect number of arguments to turtle.right!");
  702. }
  703. return [block("turtle_right", func.lineno, {}, {
  704. "ANGLE": this.convert(args[0]),
  705. "TURTLE": this.convert(func.value)
  706. }, {"inline": "true"})];
  707. }</code></pre>
  708. </article>
  709. </section>
  710. </div>
  711. <nav>
  712. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BlockPy.html">BlockPy</a></li><li><a href="BlockPyCorgis.html">BlockPyCorgis</a></li><li><a href="BlockPyDialog.html">BlockPyDialog</a></li><li><a href="BlockPyEditor.html">BlockPyEditor</a></li><li><a href="BlockPyEngine.html">BlockPyEngine</a></li><li><a href="BlockPyEnglish.html">BlockPyEnglish</a></li><li><a href="BlockPyFeedback.html">BlockPyFeedback</a></li><li><a href="BlockPyHistory.html">BlockPyHistory</a></li><li><a href="BlockPyPresentation.html">BlockPyPresentation</a></li><li><a href="BlockPyPrinter.html">BlockPyPrinter</a></li><li><a href="BlockPyServer.html">BlockPyServer</a></li><li><a href="BlockPyToolbar.html">BlockPyToolbar</a></li><li><a href="LocalStorageWrapper.html">LocalStorageWrapper</a></li><li><a href="PythonToBlocks.html">PythonToBlocks</a></li></ul><h3>Global</h3><ul><li><a href="global.html#BlockPyInterface">BlockPyInterface</a></li><li><a href="global.html#cloneNode">cloneNode</a></li><li><a href="global.html#encodeHTML">encodeHTML</a></li><li><a href="global.html#expandArray">expandArray</a></li><li><a href="global.html#EXTENDED_ERROR_EXPLANATION">EXTENDED_ERROR_EXPLANATION</a></li><li><a href="global.html#indent">indent</a></li><li><a href="global.html#instructor_module">instructor_module</a></li><li><a href="global.html#prettyPrintDateTime">prettyPrintDateTime</a></li><li><a href="global.html#randomInteger">randomInteger</a></li><li><a href="global.html#set_button_loaded">set_button_loaded</a></li><li><a href="global.html#timerGuard">timerGuard</a></li></ul>
  713. </nav>
  714. <br class="clear">
  715. <footer>
  716. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Sun Mar 26 2017 09:45:03 GMT-0400 (Eastern Daylight Time)
  717. </footer>
  718. <script> prettyPrint(); </script>
  719. <script src="scripts/linenumber.js"> </script>
  720. </body>
  721. </html>