input.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Object representing an input (value, statement, or dummy).
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Input');
  26. goog.require('Blockly.Connection');
  27. goog.require('Blockly.FieldLabel');
  28. goog.require('goog.asserts');
  29. /**
  30. * Class for an input with an optional field.
  31. * @param {number} type The type of the input.
  32. * @param {string} name Language-neutral identifier which may used to find this
  33. * input again.
  34. * @param {!Blockly.Block} block The block containing this input.
  35. * @param {Blockly.Connection} connection Optional connection for this input.
  36. * @constructor
  37. */
  38. Blockly.Input = function(type, name, block, connection) {
  39. /** @type {number} */
  40. this.type = type;
  41. /** @type {string} */
  42. this.name = name;
  43. /**
  44. * @type {!Blockly.Block}
  45. * @private
  46. */
  47. this.sourceBlock_ = block;
  48. /** @type {Blockly.Connection} */
  49. this.connection = connection;
  50. /** @type {!Array.<!Blockly.Field>} */
  51. this.fieldRow = [];
  52. };
  53. /**
  54. * Alignment of input's fields (left, right or centre).
  55. * @type {number}
  56. */
  57. Blockly.Input.prototype.align = Blockly.ALIGN_LEFT;
  58. /**
  59. * Is the input visible?
  60. * @type {boolean}
  61. * @private
  62. */
  63. Blockly.Input.prototype.visible_ = true;
  64. /**
  65. * Add an item to the end of the input's field row.
  66. * @param {string|!Blockly.Field} field Something to add as a field.
  67. * @param {string=} opt_name Language-neutral identifier which may used to find
  68. * this field again. Should be unique to the host block.
  69. * @return {!Blockly.Input} The input being append to (to allow chaining).
  70. */
  71. Blockly.Input.prototype.appendField = function(field, opt_name) {
  72. // Empty string, Null or undefined generates no field, unless field is named.
  73. if (!field && !opt_name) {
  74. return this;
  75. }
  76. // Generate a FieldLabel when given a plain text field.
  77. if (goog.isString(field)) {
  78. field = new Blockly.FieldLabel(/** @type {string} */ (field));
  79. }
  80. field.setSourceBlock(this.sourceBlock_);
  81. if (this.sourceBlock_.rendered) {
  82. field.init();
  83. }
  84. field.name = opt_name;
  85. if (field.prefixField) {
  86. // Add any prefix.
  87. this.appendField(field.prefixField);
  88. }
  89. // Add the field to the field row.
  90. this.fieldRow.push(field);
  91. if (field.suffixField) {
  92. // Add any suffix.
  93. this.appendField(field.suffixField);
  94. }
  95. if (this.sourceBlock_.rendered) {
  96. this.sourceBlock_.render();
  97. // Adding a field will cause the block to change shape.
  98. this.sourceBlock_.bumpNeighbours_();
  99. }
  100. return this;
  101. };
  102. /**
  103. * Add an item to the end of the input's field row.
  104. * @param {*} field Something to add as a field.
  105. * @param {string=} opt_name Language-neutral identifier which may used to find
  106. * this field again. Should be unique to the host block.
  107. * @return {!Blockly.Input} The input being append to (to allow chaining).
  108. * @deprecated December 2013
  109. */
  110. Blockly.Input.prototype.appendTitle = function(field, opt_name) {
  111. console.warn('Deprecated call to appendTitle, use appendField instead.');
  112. return this.appendField(field, opt_name);
  113. };
  114. /**
  115. * Remove a field from this input.
  116. * @param {string} name The name of the field.
  117. * @throws {goog.asserts.AssertionError} if the field is not present.
  118. */
  119. Blockly.Input.prototype.removeField = function(name) {
  120. for (var i = 0, field; field = this.fieldRow[i]; i++) {
  121. if (field.name === name) {
  122. field.dispose();
  123. this.fieldRow.splice(i, 1);
  124. if (this.sourceBlock_.rendered) {
  125. this.sourceBlock_.render();
  126. // Removing a field will cause the block to change shape.
  127. this.sourceBlock_.bumpNeighbours_();
  128. }
  129. return;
  130. }
  131. }
  132. goog.asserts.fail('Field "%s" not found.', name);
  133. };
  134. /**
  135. * Gets whether this input is visible or not.
  136. * @return {boolean} True if visible.
  137. */
  138. Blockly.Input.prototype.isVisible = function() {
  139. return this.visible_;
  140. };
  141. /**
  142. * Sets whether this input is visible or not.
  143. * Used to collapse/uncollapse a block.
  144. * @param {boolean} visible True if visible.
  145. * @return {!Array.<!Blockly.Block>} List of blocks to render.
  146. */
  147. Blockly.Input.prototype.setVisible = function(visible) {
  148. var renderList = [];
  149. if (this.visible_ == visible) {
  150. return renderList;
  151. }
  152. this.visible_ = visible;
  153. var display = visible ? 'block' : 'none';
  154. for (var y = 0, field; field = this.fieldRow[y]; y++) {
  155. field.setVisible(visible);
  156. }
  157. if (this.connection) {
  158. // Has a connection.
  159. if (visible) {
  160. renderList = this.connection.unhideAll();
  161. } else {
  162. this.connection.hideAll();
  163. }
  164. var child = this.connection.targetBlock();
  165. if (child) {
  166. child.getSvgRoot().style.display = display;
  167. if (!visible) {
  168. child.rendered = false;
  169. }
  170. }
  171. }
  172. return renderList;
  173. };
  174. /**
  175. * Change a connection's compatibility.
  176. * @param {string|Array.<string>|null} check Compatible value type or
  177. * list of value types. Null if all types are compatible.
  178. * @return {!Blockly.Input} The input being modified (to allow chaining).
  179. */
  180. Blockly.Input.prototype.setCheck = function(check) {
  181. if (!this.connection) {
  182. throw 'This input does not have a connection.';
  183. }
  184. this.connection.setCheck(check);
  185. return this;
  186. };
  187. /**
  188. * Change the alignment of the connection's field(s).
  189. * @param {number} align One of Blockly.ALIGN_LEFT, ALIGN_CENTRE, ALIGN_RIGHT.
  190. * In RTL mode directions are reversed, and ALIGN_RIGHT aligns to the left.
  191. * @return {!Blockly.Input} The input being modified (to allow chaining).
  192. */
  193. Blockly.Input.prototype.setAlign = function(align) {
  194. this.align = align;
  195. if (this.sourceBlock_.rendered) {
  196. this.sourceBlock_.render();
  197. }
  198. return this;
  199. };
  200. /**
  201. * Initialize the fields on this input.
  202. */
  203. Blockly.Input.prototype.init = function() {
  204. if (!this.sourceBlock_.workspace.rendered) {
  205. return; // Headless blocks don't need fields initialized.
  206. }
  207. for (var i = 0; i < this.fieldRow.length; i++) {
  208. this.fieldRow[i].init();
  209. }
  210. };
  211. /**
  212. * Sever all links to this input.
  213. */
  214. Blockly.Input.prototype.dispose = function() {
  215. for (var i = 0, field; field = this.fieldRow[i]; i++) {
  216. field.dispose();
  217. }
  218. if (this.connection) {
  219. this.connection.dispose();
  220. }
  221. this.sourceBlock_ = null;
  222. };