custom_blocks.js.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: custom_blocks.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: custom_blocks.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>Blockly.Blocks['dicts_create_with_container'] = {
  20. /**
  21. * Dictionary block container
  22. * @this Blockly.Block
  23. */
  24. init: function() {
  25. this.setColour(Blockly.Blocks.dicts.HUE);
  26. this.appendDummyInput()
  27. .appendField(Blockly.Msg.DICTS_CREATE_WITH_CONTAINER_TITLE_ADD);
  28. this.appendStatementInput('STACK');
  29. this.setTooltip(Blockly.Msg.DICTS_CREATE_WITH_CONTAINER_TOOLTIP);
  30. this.contextMenu = false;
  31. }
  32. };
  33. Blockly.Blocks['dicts_create_with_item'] = {
  34. // Add items.
  35. init: function() {
  36. this.setColour(Blockly.Blocks.dicts.HUE);
  37. this.appendDummyInput()
  38. .appendField(Blockly.Msg.DICTS_CREATE_WITH_ITEM_TITLE);
  39. this.setPreviousStatement(true);
  40. this.setNextStatement(true);
  41. this.setTooltip(Blockly.Msg.DICTS_CREATE_WITH_ITEM_TOOLTIP);
  42. this.contextMenu = false;
  43. }
  44. };
  45. Blockly.Blocks['dicts_create_with'] = {
  46. /**
  47. * Block for creating a dict with any number of elements of any type.
  48. * @this Blockly.Block
  49. */
  50. init: function() {
  51. console.log("init");
  52. this.setInputsInline(false);
  53. this.setColour(Blockly.Blocks.dicts.HUE);
  54. this.itemCount_ = 1;
  55. this.updateShape_();
  56. this.setOutput(true, 'dict');
  57. this.setMutator(new Blockly.Mutator(['dicts_create_with_item']));
  58. this.setTooltip(Blockly.Msg.DICTS_CREATE_WITH_TOOLTIP);
  59. },
  60. /**
  61. * Create XML to represent dict inputs.
  62. * @return {Element} XML storage element.
  63. * @this Blockly.Block
  64. */
  65. mutationToDom: function(workspace) {
  66. console.log("mutationToDom");
  67. var container = document.createElement('mutation');
  68. container.setAttribute('items', this.itemCount_);
  69. return container;
  70. },
  71. /**
  72. * Parse XML to restore the dict inputs.
  73. * @param {!Element} xmlElement XML storage element.
  74. * @this Blockly.Block
  75. */
  76. domToMutation: function(xmlElement) {
  77. console.log("domToMutation");
  78. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  79. this.updateShape_();
  80. },
  81. /**
  82. * Modify this block to have the correct number of inputs.
  83. * @private
  84. * @this Blockly.Block
  85. */
  86. updateShape_: function() {
  87. console.log("updateShape");
  88. // Delete everything.
  89. if (this.getInput("EMPTY")) {
  90. this.removeInput('EMPTY');
  91. }
  92. var keyNames = [];
  93. for (var i = 0; this.getInput('VALUE' + i); i++) {
  94. //this.getInput('VALUE' + i).removeField("KEY"+i);
  95. keyNames.push(this.getFieldValue("KEY"+i))
  96. this.removeInput('VALUE' + i);
  97. }
  98. // Rebuild block.
  99. if (this.itemCount_ == 0) {
  100. this.appendDummyInput('EMPTY')
  101. .appendField(Blockly.Msg.DICTS_CREATE_EMPTY_TITLE);
  102. } else {
  103. this.appendDummyInput('EMPTY')
  104. .appendField(Blockly.Msg.DICTS_CREATE_WITH_INPUT_WITH);
  105. for (var i = 0; i &lt; this.itemCount_; i++) {
  106. this.appendValueInput('VALUE' + i)
  107. .setCheck(null)
  108. .setAlign(Blockly.ALIGN_RIGHT)
  109. .appendField(
  110. new Blockly.FieldTextInput(
  111. keyNames.length > i
  112. ? keyNames[i]
  113. : Blockly.Msg.DICTS_CREATE_WITH_ITEM_KEY),
  114. 'KEY'+i)
  115. .appendField(Blockly.Msg.DICTS_CREATE_WITH_ITEM_MAPPING);
  116. }
  117. }
  118. },
  119. /**
  120. * Populate the mutator's dialog with this block's components.
  121. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  122. * @return {!Blockly.Block} Root block in mutator.
  123. * @this Blockly.Block
  124. */
  125. decompose: function(workspace) {
  126. console.log("Decompose");
  127. var containerBlock = workspace.newBlock('dicts_create_with_container');
  128. containerBlock.initSvg();
  129. var connection = containerBlock.getInput('STACK').connection;
  130. for (var x = 0; x &lt; this.itemCount_; x++) {
  131. var itemBlock = workspace.newBlock('dicts_create_with_item');
  132. itemBlock.initSvg();
  133. connection.connect(itemBlock.previousConnection);
  134. connection = itemBlock.nextConnection;
  135. }
  136. return containerBlock;
  137. },
  138. /**
  139. * Reconfigure this block based on the mutator dialog's components.
  140. * @param {!Blockly.Block} containerBlock Root block in mutator.
  141. * @this Blockly.Block
  142. */
  143. compose: function(containerBlock) {
  144. console.log("Compose");
  145. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  146. // Count number of inputs.
  147. var connections = [];
  148. var i = 0;
  149. while (itemBlock) {
  150. connections[i] = itemBlock.valueConnection_;
  151. itemBlock = itemBlock.nextConnection &amp;&amp;
  152. itemBlock.nextConnection.targetBlock();
  153. i++;
  154. }
  155. this.itemCount_ = i;
  156. this.updateShape_();
  157. // Reconnect any child blocks.
  158. for (var i = 0; i &lt; this.itemCount_; i++) {
  159. if (connections[i]) {
  160. this.getInput('VALUE' + i).connection.connect(connections[i]);
  161. }
  162. }
  163. },
  164. /**
  165. * Store pointers to any connected child blocks.
  166. * @param {!Blockly.Block} containerBlock Root block in mutator.
  167. * @this Blockly.Block
  168. */
  169. saveConnections: function(containerBlock) {
  170. console.log("SaveConnections");
  171. // Store a pointer to any connected child blocks.
  172. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  173. var x = 0;
  174. while (itemBlock) {
  175. var value_input = this.getInput('VALUE' + x);
  176. itemBlock.valueConnection_ = value_input &amp;&amp; value_input.connection.targetConnection;
  177. x++;
  178. itemBlock = itemBlock.nextConnection &amp;&amp;
  179. itemBlock.nextConnection.targetBlock();
  180. }
  181. }
  182. };
  183. </code></pre>
  184. </article>
  185. </section>
  186. </div>
  187. <nav>
  188. <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>
  189. </nav>
  190. <br class="clear">
  191. <footer>
  192. 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)
  193. </footer>
  194. <script> prettyPrint(); </script>
  195. <script src="scripts/linenumber.js"> </script>
  196. </body>
  197. </html>