if.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. Blockly.Blocks['controls_if_better'] = {
  2. /**
  3. * Block for if/elseif/else condition.
  4. * @this Blockly.Block
  5. */
  6. init: function() {
  7. this.setHelpUrl(Blockly.Msg.CONTROLS_IF_HELPURL);
  8. this.setColour(Blockly.Blocks.logic.HUE);
  9. this.appendValueInput('IF0')
  10. .setCheck('Boolean')
  11. .appendField(Blockly.Msg.CONTROLS_IF_MSG_IF);
  12. this.appendStatementInput('DO0')
  13. .appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
  14. this.setPreviousStatement(true);
  15. this.setNextStatement(true);
  16. this.updateShape_();
  17. // Assign 'this' to a variable for use in the tooltip closure below.
  18. var thisBlock = this;
  19. this.setTooltip(function() {
  20. if (!thisBlock.elseifCount_ && !thisBlock.elseCount_) {
  21. return Blockly.Msg.CONTROLS_IF_TOOLTIP_1;
  22. } else if (!thisBlock.elseifCount_ && thisBlock.elseCount_) {
  23. return Blockly.Msg.CONTROLS_IF_TOOLTIP_2;
  24. } else if (thisBlock.elseifCount_ && !thisBlock.elseCount_) {
  25. return Blockly.Msg.CONTROLS_IF_TOOLTIP_3;
  26. } else if (thisBlock.elseifCount_ && thisBlock.elseCount_) {
  27. return Blockly.Msg.CONTROLS_IF_TOOLTIP_4;
  28. }
  29. return '';
  30. });
  31. this.elseifCount_ = 0;
  32. this.elseCount_ = 0;
  33. },
  34. /**
  35. * Create XML to represent the number of else-if and else inputs.
  36. * @return {Element} XML storage element.
  37. * @this Blockly.Block
  38. */
  39. mutationToDom: function() {
  40. if (!this.elseifCount_ && !this.elseCount_) {
  41. return null;
  42. }
  43. var container = document.createElement('mutation');
  44. if (this.elseifCount_) {
  45. container.setAttribute('elseif', this.elseifCount_);
  46. }
  47. if (this.elseCount_) {
  48. container.setAttribute('else', 1);
  49. }
  50. return container;
  51. },
  52. /**
  53. * Parse XML to restore the else-if and else inputs.
  54. * @param {!Element} xmlElement XML storage element.
  55. * @this Blockly.Block
  56. */
  57. domToMutation: function(xmlElement) {
  58. this.elseifCount_ = parseInt(xmlElement.getAttribute('elseif'), 10) || 0;
  59. this.elseCount_ = parseInt(xmlElement.getAttribute('else'), 10) || 0;
  60. this.updateShape_();
  61. },
  62. /**
  63. * Populate the mutator's dialog with this block's components.
  64. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  65. * @return {!Blockly.Block} Root block in mutator.
  66. * @this Blockly.Block
  67. */
  68. /*
  69. decompose: function(workspace) {
  70. var containerBlock = workspace.newBlock('controls_if_if');
  71. containerBlock.initSvg();
  72. var connection = containerBlock.nextConnection;
  73. for (var i = 1; i <= this.elseifCount_; i++) {
  74. var elseifBlock = workspace.newBlock('controls_if_elseif');
  75. elseifBlock.initSvg();
  76. connection.connect(elseifBlock.previousConnection);
  77. connection = elseifBlock.nextConnection;
  78. }
  79. if (this.elseCount_) {
  80. var elseBlock = workspace.newBlock('controls_if_else');
  81. elseBlock.initSvg();
  82. connection.connect(elseBlock.previousConnection);
  83. }
  84. return containerBlock;
  85. },*/
  86. /**
  87. * Reconfigure this block based on the mutator dialog's components.
  88. * @param {!Blockly.Block} containerBlock Root block in mutator.
  89. * @this Blockly.Block
  90. *//*
  91. compose: function(containerBlock) {
  92. var clauseBlock = containerBlock.nextConnection.targetBlock();
  93. // Count number of inputs.
  94. this.elseifCount_ = 0;
  95. this.elseCount_ = 0;
  96. var valueConnections = [null];
  97. var statementConnections = [null];
  98. var elseStatementConnection = null;
  99. while (clauseBlock) {
  100. switch (clauseBlock.type) {
  101. case 'controls_if_elseif':
  102. this.elseifCount_++;
  103. valueConnections.push(clauseBlock.valueConnection_);
  104. statementConnections.push(clauseBlock.statementConnection_);
  105. break;
  106. case 'controls_if_else':
  107. this.elseCount_++;
  108. elseStatementConnection = clauseBlock.statementConnection_;
  109. break;
  110. default:
  111. throw 'Unknown block type.';
  112. }
  113. clauseBlock = clauseBlock.nextConnection &&
  114. clauseBlock.nextConnection.targetBlock();
  115. }
  116. this.updateShape_();
  117. // Reconnect any child blocks.
  118. for (var i = 1; i <= this.elseifCount_; i++) {
  119. Blockly.Mutator.reconnect(valueConnections[i], this, 'IF' + i);
  120. Blockly.Mutator.reconnect(statementConnections[i], this, 'DO' + i);
  121. }
  122. Blockly.Mutator.reconnect(elseStatementConnection, this, 'ELSE');
  123. },*/
  124. /**
  125. * Store pointers to any connected child blocks.
  126. * @param {!Blockly.Block} containerBlock Root block in mutator.
  127. * @this Blockly.Block
  128. *//*
  129. saveConnections: function(containerBlock) {
  130. var clauseBlock = containerBlock.nextConnection.targetBlock();
  131. var i = 1;
  132. while (clauseBlock) {
  133. switch (clauseBlock.type) {
  134. case 'controls_if_elseif':
  135. var inputIf = this.getInput('IF' + i);
  136. var inputDo = this.getInput('DO' + i);
  137. clauseBlock.valueConnection_ =
  138. inputIf && inputIf.connection.targetConnection;
  139. clauseBlock.statementConnection_ =
  140. inputDo && inputDo.connection.targetConnection;
  141. i++;
  142. break;
  143. case 'controls_if_else':
  144. var inputDo = this.getInput('ELSE');
  145. clauseBlock.statementConnection_ =
  146. inputDo && inputDo.connection.targetConnection;
  147. break;
  148. default:
  149. throw 'Unknown block type.';
  150. }
  151. clauseBlock = clauseBlock.nextConnection &&
  152. clauseBlock.nextConnection.targetBlock();
  153. }
  154. },*/
  155. /**
  156. * Modify this block to have the correct number of inputs.
  157. * @private
  158. * @this Blockly.Block
  159. */
  160. updateShape_: function() {
  161. // Delete everything.
  162. if (this.getInput('ELSE')) {
  163. this.removeInput('ELSE');
  164. }
  165. var i = 1;
  166. while (this.getInput('IF' + i)) {
  167. this.removeInput('IF' + i);
  168. this.removeInput('DO' + i);
  169. i++;
  170. }
  171. // Rebuild block.
  172. for (var i = 1; i <= this.elseifCount_; i++) {
  173. this.appendValueInput('IF' + i)
  174. .setCheck('Boolean')
  175. .appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSEIF);
  176. this.appendStatementInput('DO' + i)
  177. .appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
  178. }
  179. if (this.elseCount_) {
  180. this.appendStatementInput('ELSE')
  181. .appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE);
  182. }
  183. }
  184. };