@blockdia/scratch-blocks 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (243) hide show
  1. package/.nvmrc +1 -0
  2. package/LICENSE +674 -0
  3. package/README.md +57 -0
  4. package/TRADEMARK +1 -0
  5. package/blockly_compressed_horizontal.js +2268 -0
  6. package/blockly_compressed_vertical.js +2296 -0
  7. package/blockly_uncompressed_horizontal.js +1212 -0
  8. package/blockly_uncompressed_vertical.js +1212 -0
  9. package/blocks_common/colour.js +61 -0
  10. package/blocks_common/math.js +159 -0
  11. package/blocks_common/matrix.js +54 -0
  12. package/blocks_common/note.js +58 -0
  13. package/blocks_common/text.js +57 -0
  14. package/blocks_compressed.js +37 -0
  15. package/blocks_compressed_horizontal.js +68 -0
  16. package/blocks_compressed_vertical.js +216 -0
  17. package/blocks_horizontal/control.js +212 -0
  18. package/blocks_horizontal/default_toolbox.js +139 -0
  19. package/blocks_horizontal/event.js +190 -0
  20. package/blocks_horizontal/wedo.js +325 -0
  21. package/blocks_vertical/control.js +532 -0
  22. package/blocks_vertical/data.js +666 -0
  23. package/blocks_vertical/default_toolbox.js +571 -0
  24. package/blocks_vertical/event.js +329 -0
  25. package/blocks_vertical/extensions.js +294 -0
  26. package/blocks_vertical/looks.js +591 -0
  27. package/blocks_vertical/motion.js +587 -0
  28. package/blocks_vertical/operators.js +470 -0
  29. package/blocks_vertical/procedures.js +1051 -0
  30. package/blocks_vertical/sensing.js +555 -0
  31. package/blocks_vertical/sound.js +246 -0
  32. package/blocks_vertical/vertical_extensions.js +289 -0
  33. package/build/gen_blocks.js +1 -0
  34. package/build/test_expect.js +1 -0
  35. package/build/test_input.js +1 -0
  36. package/build.py +647 -0
  37. package/cleanup.sh +101 -0
  38. package/core/block.js +1837 -0
  39. package/core/block_animations.js +107 -0
  40. package/core/block_drag_surface.js +299 -0
  41. package/core/block_dragger.js +424 -0
  42. package/core/block_events.js +531 -0
  43. package/core/block_render_svg_horizontal.js +892 -0
  44. package/core/block_render_svg_vertical.js +1734 -0
  45. package/core/block_svg.js +1355 -0
  46. package/core/blockly.js +620 -0
  47. package/core/blocks.js +37 -0
  48. package/core/bubble.js +664 -0
  49. package/core/bubble_dragger.js +285 -0
  50. package/core/colours.js +177 -0
  51. package/core/comment.js +293 -0
  52. package/core/comment_events.js +539 -0
  53. package/core/connection.js +776 -0
  54. package/core/connection_db.js +300 -0
  55. package/core/constants.js +408 -0
  56. package/core/contextmenu.js +534 -0
  57. package/core/css.js +1354 -0
  58. package/core/data_category.js +490 -0
  59. package/core/dragged_connection_manager.js +260 -0
  60. package/core/dropdowndiv.js +408 -0
  61. package/core/events.js +429 -0
  62. package/core/events_abstract.js +113 -0
  63. package/core/extensions.js +450 -0
  64. package/core/field.js +810 -0
  65. package/core/field_angle.js +410 -0
  66. package/core/field_checkbox.js +133 -0
  67. package/core/field_colour.js +253 -0
  68. package/core/field_colour_slider.js +387 -0
  69. package/core/field_date.js +353 -0
  70. package/core/field_dropdown.js +447 -0
  71. package/core/field_iconmenu.js +309 -0
  72. package/core/field_image.js +200 -0
  73. package/core/field_label.js +136 -0
  74. package/core/field_label_serializable.js +125 -0
  75. package/core/field_matrix.js +566 -0
  76. package/core/field_note.js +850 -0
  77. package/core/field_number.js +366 -0
  78. package/core/field_numberdropdown.js +77 -0
  79. package/core/field_textdropdown.js +164 -0
  80. package/core/field_textinput.js +675 -0
  81. package/core/field_textinput_removable.js +105 -0
  82. package/core/field_variable.js +385 -0
  83. package/core/field_variable_getter.js +185 -0
  84. package/core/field_vertical_separator.js +161 -0
  85. package/core/flyout_base.js +935 -0
  86. package/core/flyout_button.js +324 -0
  87. package/core/flyout_dragger.js +83 -0
  88. package/core/flyout_extension_category_header.js +159 -0
  89. package/core/flyout_horizontal.js +475 -0
  90. package/core/flyout_vertical.js +770 -0
  91. package/core/generator.js +426 -0
  92. package/core/gesture.js +1010 -0
  93. package/core/grid.js +227 -0
  94. package/core/icon.js +205 -0
  95. package/core/inject.js +496 -0
  96. package/core/input.js +285 -0
  97. package/core/insertion_marker_manager.js +678 -0
  98. package/core/intersection_observer.js +102 -0
  99. package/core/msg.js +62 -0
  100. package/core/mutator.js +426 -0
  101. package/core/names.js +198 -0
  102. package/core/options.js +244 -0
  103. package/core/procedures.js +739 -0
  104. package/core/rendered_connection.js +417 -0
  105. package/core/scratch_block_comment.js +646 -0
  106. package/core/scratch_blocks_utils.js +246 -0
  107. package/core/scratch_bubble.js +699 -0
  108. package/core/scratch_events.js +131 -0
  109. package/core/scratch_msgs.js +85 -0
  110. package/core/scrollbar.js +875 -0
  111. package/core/toolbox.js +803 -0
  112. package/core/tooltip.js +337 -0
  113. package/core/touch.js +226 -0
  114. package/core/trashcan.js +343 -0
  115. package/core/ui_events.js +91 -0
  116. package/core/ui_menu_utils.js +68 -0
  117. package/core/utils.js +825 -0
  118. package/core/variable_events.js +259 -0
  119. package/core/variable_map.js +415 -0
  120. package/core/variable_model.js +116 -0
  121. package/core/variables.js +674 -0
  122. package/core/warning.js +199 -0
  123. package/core/widgetdiv.js +344 -0
  124. package/core/workspace.js +673 -0
  125. package/core/workspace_audio.js +170 -0
  126. package/core/workspace_comment.js +426 -0
  127. package/core/workspace_comment_render_svg.js +706 -0
  128. package/core/workspace_comment_svg.js +609 -0
  129. package/core/workspace_drag_surface_svg.js +195 -0
  130. package/core/workspace_dragger.js +132 -0
  131. package/core/workspace_svg.js +2382 -0
  132. package/core/xml.js +930 -0
  133. package/core/zoom_controls.js +301 -0
  134. package/dist/horizontal.js +222 -0
  135. package/dist/vertical.js +222 -0
  136. package/dist/web/horizontal.js +232 -0
  137. package/dist/web/vertical.js +232 -0
  138. package/i18n/common.py +234 -0
  139. package/i18n/create_messages.py +162 -0
  140. package/i18n/create_scratch_msgs.js +69 -0
  141. package/i18n/dedup_json.py +73 -0
  142. package/i18n/js_to_json.js +46 -0
  143. package/i18n/js_to_json.py +136 -0
  144. package/i18n/json_to_js.js +52 -0
  145. package/i18n/json_to_js.py +185 -0
  146. package/i18n/sync_tx_translations.js +111 -0
  147. package/i18n/test_scratch_msgs.js +87 -0
  148. package/i18n/tests.py +47 -0
  149. package/i18n/xliff_to_json.py +232 -0
  150. package/local_build.sh +70 -0
  151. package/media/click.mp3 +0 -0
  152. package/media/click.ogg +0 -0
  153. package/media/click.wav +0 -0
  154. package/media/comment-arrow-down.svg +10 -0
  155. package/media/comment-arrow-up.svg +10 -0
  156. package/media/delete-x.svg +10 -0
  157. package/media/delete.mp3 +0 -0
  158. package/media/delete.ogg +0 -0
  159. package/media/delete.wav +0 -0
  160. package/media/dropdown-arrow-dark.svg +1 -0
  161. package/media/dropdown-arrow.svg +1 -0
  162. package/media/extensions/microbit-block-icon.svg +130 -0
  163. package/media/extensions/music-block-icon.svg +17 -0
  164. package/media/extensions/pen-block-icon.svg +19 -0
  165. package/media/extensions/wedo2-block-icon.svg +36 -0
  166. package/media/eyedropper.svg +22 -0
  167. package/media/green-flag.svg +17 -0
  168. package/media/handclosed.cur +0 -0
  169. package/media/handdelete.cur +0 -0
  170. package/media/handopen.cur +0 -0
  171. package/media/icons/arrow.svg +12 -0
  172. package/media/icons/arrow_button.svg +1 -0
  173. package/media/icons/control_forever.svg +1 -0
  174. package/media/icons/control_repeat.svg +1 -0
  175. package/media/icons/control_stop.svg +1 -0
  176. package/media/icons/control_wait.svg +1 -0
  177. package/media/icons/event_broadcast_blue.svg +1 -0
  178. package/media/icons/event_broadcast_coral.svg +1 -0
  179. package/media/icons/event_broadcast_green.svg +1 -0
  180. package/media/icons/event_broadcast_magenta.svg +1 -0
  181. package/media/icons/event_broadcast_orange.svg +1 -0
  182. package/media/icons/event_broadcast_purple.svg +1 -0
  183. package/media/icons/event_when-broadcast-received_blue.svg +1 -0
  184. package/media/icons/event_when-broadcast-received_coral.svg +1 -0
  185. package/media/icons/event_when-broadcast-received_green.svg +1 -0
  186. package/media/icons/event_when-broadcast-received_magenta.svg +1 -0
  187. package/media/icons/event_when-broadcast-received_orange.svg +1 -0
  188. package/media/icons/event_when-broadcast-received_purple.svg +1 -0
  189. package/media/icons/event_whenflagclicked.svg +1 -0
  190. package/media/icons/remove.svg +19 -0
  191. package/media/icons/set-led_blue.svg +1 -0
  192. package/media/icons/set-led_coral.svg +1 -0
  193. package/media/icons/set-led_green.svg +1 -0
  194. package/media/icons/set-led_magenta.svg +1 -0
  195. package/media/icons/set-led_mystery.svg +1 -0
  196. package/media/icons/set-led_orange.svg +1 -0
  197. package/media/icons/set-led_purple.svg +1 -0
  198. package/media/icons/set-led_white.svg +1 -0
  199. package/media/icons/set-led_yellow.svg +1 -0
  200. package/media/icons/wedo_motor-clockwise.svg +1 -0
  201. package/media/icons/wedo_motor-counterclockwise.svg +1 -0
  202. package/media/icons/wedo_motor-speed_fast.svg +1 -0
  203. package/media/icons/wedo_motor-speed_med.svg +1 -0
  204. package/media/icons/wedo_motor-speed_slow.svg +1 -0
  205. package/media/icons/wedo_when-distance_close.svg +1 -0
  206. package/media/icons/wedo_when-tilt-backward.svg +1 -0
  207. package/media/icons/wedo_when-tilt-forward.svg +1 -0
  208. package/media/icons/wedo_when-tilt-left.svg +1 -0
  209. package/media/icons/wedo_when-tilt-right.svg +1 -0
  210. package/media/icons/wedo_when-tilt.svg +1 -0
  211. package/media/repeat.svg +18 -0
  212. package/media/rotate-left.svg +1 -0
  213. package/media/rotate-right.svg +1 -0
  214. package/media/sprites.png +0 -0
  215. package/media/status-not-ready.svg +13 -0
  216. package/media/status-ready.svg +13 -0
  217. package/media/zoom-in.svg +1 -0
  218. package/media/zoom-out.svg +1 -0
  219. package/media/zoom-reset.svg +1 -0
  220. package/msg/js/en.js +290 -0
  221. package/msg/json/en.json +285 -0
  222. package/msg/messages.js +359 -0
  223. package/msg/scratch_msgs.js +22969 -0
  224. package/package.json +60 -0
  225. package/pull_from_blockly.sh +151 -0
  226. package/renovate.json5 +15 -0
  227. package/shim/blockly_compressed_horizontal-blocks_compressed.js +1 -0
  228. package/shim/blockly_compressed_horizontal.Blockly.js +1 -0
  229. package/shim/blockly_compressed_horizontal.goog.js +1 -0
  230. package/shim/blockly_compressed_horizontal.js +1 -0
  231. package/shim/blockly_compressed_vertical-blocks_compressed.js +1 -0
  232. package/shim/blockly_compressed_vertical.Blockly.js +1 -0
  233. package/shim/blockly_compressed_vertical.goog.js +1 -0
  234. package/shim/blockly_compressed_vertical.js +1 -0
  235. package/shim/blocks_compressed_horizontal-blockly_compressed_horizontal-messages.js +1 -0
  236. package/shim/blocks_compressed_horizontal.js +1 -0
  237. package/shim/blocks_compressed_vertical-blockly_compressed_vertical-messages.js +1 -0
  238. package/shim/blocks_compressed_vertical.js +1 -0
  239. package/shim/gh-pages.js +1 -0
  240. package/shim/horizontal.js +1 -0
  241. package/shim/index.js +17 -0
  242. package/shim/vertical.js +1 -0
  243. package/universal-python.js +82 -0
@@ -0,0 +1,1355 @@
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
+ /**
22
+ * @fileoverview Methods for graphically rendering a block as SVG.
23
+ * @author fraser@google.com (Neil Fraser)
24
+ */
25
+ 'use strict';
26
+
27
+ goog.provide('Blockly.BlockSvg');
28
+
29
+ goog.require('Blockly.Block');
30
+ goog.require('Blockly.BlockAnimations');
31
+ goog.require('Blockly.ContextMenu');
32
+ goog.require('Blockly.Events.Ui');
33
+ goog.require('Blockly.Events.BlockMove');
34
+ goog.require('Blockly.Grid');
35
+ goog.require('Blockly.RenderedConnection');
36
+ goog.require('Blockly.scratchBlocksUtils');
37
+ goog.require('Blockly.Tooltip');
38
+ goog.require('Blockly.Touch');
39
+ goog.require('Blockly.utils');
40
+
41
+ goog.require('goog.Timer');
42
+ goog.require('goog.asserts');
43
+ goog.require('goog.dom');
44
+ goog.require('goog.math.Coordinate');
45
+
46
+
47
+ /**
48
+ * Class for a block's SVG representation.
49
+ * Not normally called directly, workspace.newBlock() is preferred.
50
+ * @param {!Blockly.Workspace} workspace The block's workspace.
51
+ * @param {?string} prototypeName Name of the language object containing
52
+ * type-specific functions for this block.
53
+ * @param {string=} opt_id Optional ID. Use this ID if provided, otherwise
54
+ * create a new ID. If the ID conflicts with an in-use ID, a new one will
55
+ * be generated.
56
+ * @extends {Blockly.Block}
57
+ * @constructor
58
+ */
59
+ Blockly.BlockSvg = function(workspace, prototypeName, opt_id) {
60
+ // Create core elements for the block.
61
+ /**
62
+ * @type {SVGElement}
63
+ * @private
64
+ */
65
+ this.svgGroup_ = Blockly.utils.createSvgElement('g', {}, null);
66
+ /** @type {SVGElement} */
67
+ this.svgPath_ = Blockly.utils.createSvgElement('path',
68
+ {'class': 'blocklyPath blocklyBlockBackground'},
69
+ this.svgGroup_);
70
+ this.svgPath_.tooltip = this;
71
+
72
+ /** @type {boolean} */
73
+ this.rendered = false;
74
+
75
+ /**
76
+ * Whether to move the block to the drag surface when it is dragged.
77
+ * True if it should move, false if it should be translated directly.
78
+ * @type {boolean}
79
+ * @private
80
+ */
81
+ this.useDragSurface_ = Blockly.utils.is3dSupported() && !!workspace.blockDragSurface_;
82
+
83
+ Blockly.Tooltip.bindMouseEvents(this.svgPath_);
84
+ Blockly.BlockSvg.superClass_.constructor.call(this,
85
+ workspace, prototypeName, opt_id);
86
+
87
+ // Expose this block's ID on its top-level SVG group.
88
+ if (this.svgGroup_.dataset) {
89
+ this.svgGroup_.dataset.id = this.id;
90
+ }
91
+ };
92
+ goog.inherits(Blockly.BlockSvg, Blockly.Block);
93
+
94
+ /**
95
+ * Height of this block, not including any statement blocks above or below.
96
+ * Height is in workspace units.
97
+ */
98
+ Blockly.BlockSvg.prototype.height = 0;
99
+
100
+ /**
101
+ * Width of this block, including any connected value blocks.
102
+ * Width is in workspace units.
103
+ */
104
+ Blockly.BlockSvg.prototype.width = 0;
105
+
106
+ /**
107
+ * Minimum width of block if insertion marker; comes from inserting block.
108
+ * @type {number}
109
+ */
110
+ Blockly.BlockSvg.prototype.insertionMarkerMinWidth_ = 0;
111
+
112
+ /**
113
+ * Opacity of this block between 0 and 1.
114
+ * @type {number}
115
+ * @private
116
+ */
117
+ Blockly.BlockSvg.prototype.opacity_ = 1;
118
+
119
+ /**
120
+ * Original location of block being dragged.
121
+ * @type {goog.math.Coordinate}
122
+ * @private
123
+ */
124
+ Blockly.BlockSvg.prototype.dragStartXY_ = null;
125
+
126
+ /**
127
+ * Whether the block glows as if running.
128
+ * @type {boolean}
129
+ * @private
130
+ */
131
+ Blockly.BlockSvg.prototype.isGlowingBlock_ = false;
132
+
133
+ /**
134
+ * Whether the block's whole stack glows as if running.
135
+ * @type {boolean}
136
+ * @private
137
+ */
138
+ Blockly.BlockSvg.prototype.isGlowingStack_ = false;
139
+
140
+ /**
141
+ * Constant for identifying rows that are to be rendered inline.
142
+ * Don't collide with Blockly.INPUT_VALUE and friends.
143
+ * @const
144
+ */
145
+ Blockly.BlockSvg.INLINE = -1;
146
+
147
+ /**
148
+ * Create and initialize the SVG representation of the block.
149
+ * May be called more than once.
150
+ */
151
+ Blockly.BlockSvg.prototype.initSvg = function() {
152
+ goog.asserts.assert(this.workspace.rendered, 'Workspace is headless.');
153
+ if (!this.isInsertionMarker()) { // Insertion markers not allowed to have inputs or icons
154
+ // Input shapes are empty holes drawn when a value input is not connected.
155
+ for (var i = 0, input; input = this.inputList[i]; i++) {
156
+ input.init();
157
+ input.initOutlinePath(this.svgGroup_);
158
+ }
159
+ var icons = this.getIcons();
160
+ for (i = 0; i < icons.length; i++) {
161
+ icons[i].createIcon();
162
+ }
163
+ }
164
+ this.updateColour();
165
+ this.updateMovable();
166
+ if (!this.workspace.options.readOnly && !this.eventsInit_) {
167
+ Blockly.bindEventWithChecks_(
168
+ this.getSvgRoot(), 'mousedown', this, this.onMouseDown_);
169
+ }
170
+ this.eventsInit_ = true;
171
+
172
+ if (!this.getSvgRoot().parentNode) {
173
+ this.workspace.getCanvas().appendChild(this.getSvgRoot());
174
+ }
175
+ };
176
+
177
+ /**
178
+ * Select this block. Highlight it visually.
179
+ */
180
+ Blockly.BlockSvg.prototype.select = function() {
181
+ if (this.isShadow() && this.getParent()) {
182
+ // Shadow blocks should not be selected.
183
+ this.getParent().select();
184
+ return;
185
+ }
186
+ if (Blockly.selected == this) {
187
+ return;
188
+ }
189
+ var oldId = null;
190
+ if (Blockly.selected) {
191
+ oldId = Blockly.selected.id;
192
+ // Unselect any previously selected block.
193
+ Blockly.Events.disable();
194
+ try {
195
+ Blockly.selected.unselect();
196
+ } finally {
197
+ Blockly.Events.enable();
198
+ }
199
+ }
200
+ var event = new Blockly.Events.Ui(null, 'selected', oldId, this.id);
201
+ event.workspaceId = this.workspace.id;
202
+ Blockly.Events.fire(event);
203
+ Blockly.selected = this;
204
+ this.addSelect();
205
+ };
206
+
207
+ /**
208
+ * Unselect this block. Remove its highlighting.
209
+ */
210
+ Blockly.BlockSvg.prototype.unselect = function() {
211
+ if (Blockly.selected != this) {
212
+ return;
213
+ }
214
+ var event = new Blockly.Events.Ui(null, 'selected', this.id, null);
215
+ event.workspaceId = this.workspace.id;
216
+ Blockly.Events.fire(event);
217
+ Blockly.selected = null;
218
+ this.removeSelect();
219
+ };
220
+
221
+ /**
222
+ * Glow only this particular block, to highlight it visually as if it's running.
223
+ * @param {boolean} isGlowingBlock Whether the block should glow.
224
+ */
225
+ Blockly.BlockSvg.prototype.setGlowBlock = function(isGlowingBlock) {
226
+ this.isGlowingBlock_ = isGlowingBlock;
227
+ this.updateColour();
228
+ };
229
+
230
+ /**
231
+ * Glow the stack starting with this block, to highlight it visually as if it's running.
232
+ * @param {boolean} isGlowingStack Whether the stack starting with this block should glow.
233
+ */
234
+ Blockly.BlockSvg.prototype.setGlowStack = function(isGlowingStack) {
235
+ this.isGlowingStack_ = isGlowingStack;
236
+ // Update the applied SVG filter if the property has changed
237
+ var svg = this.getSvgRoot();
238
+ if (this.isGlowingStack_ && !svg.hasAttribute('filter')) {
239
+ var stackGlowFilterId = this.workspace.options.stackGlowFilterId || 'blocklyStackGlowFilter';
240
+ svg.setAttribute('filter', 'url(#' + stackGlowFilterId + ')');
241
+ } else if (!this.isGlowingStack_ && svg.hasAttribute('filter')) {
242
+ svg.removeAttribute('filter');
243
+ }
244
+ };
245
+
246
+ /**
247
+ * Block's mutator icon (if any).
248
+ * @type {Blockly.Mutator}
249
+ */
250
+ Blockly.BlockSvg.prototype.mutator = null;
251
+
252
+ /**
253
+ * Block's comment icon (if any).
254
+ * @type {Blockly.Comment}
255
+ */
256
+ Blockly.BlockSvg.prototype.comment = null;
257
+
258
+ /**
259
+ * Block's warning icon (if any).
260
+ * @type {Blockly.Warning}
261
+ */
262
+ Blockly.BlockSvg.prototype.warning = null;
263
+
264
+ /**
265
+ * Returns a list of mutator, comment, and warning icons.
266
+ * @return {!Array} List of icons.
267
+ */
268
+ Blockly.BlockSvg.prototype.getIcons = function() {
269
+ var icons = [];
270
+ if (this.mutator) {
271
+ icons.push(this.mutator);
272
+ }
273
+ if (this.comment) {
274
+ icons.push(this.comment);
275
+ }
276
+ if (this.warning) {
277
+ icons.push(this.warning);
278
+ }
279
+ return icons;
280
+ };
281
+
282
+ Blockly.BlockSvg.prototype.intersects_ = true;
283
+
284
+ Blockly.BlockSvg.prototype.setIntersects = function(intersects) {
285
+ if (intersects === this.intersects_) {
286
+ return;
287
+ }
288
+ this.intersects_ = intersects;
289
+ var root = this.getSvgRoot();
290
+ if (!root) {
291
+ return;
292
+ }
293
+ if (intersects) {
294
+ root.style.display = '';
295
+ } else {
296
+ root.style.display = 'none';
297
+ }
298
+ };
299
+
300
+ Blockly.BlockSvg.prototype.updateIntersectionObserver = function() {
301
+ if (this.workspace.intersectionObserver) {
302
+ if (this.getParent()) {
303
+ this.workspace.intersectionObserver.unobserve(this);
304
+ if (!this.intersects_) {
305
+ this.setIntersects(true);
306
+ }
307
+ } else {
308
+ this.workspace.intersectionObserver.observe(this);
309
+ }
310
+ }
311
+ };
312
+
313
+ /**
314
+ * Set parent of this block to be a new block or null.
315
+ * @param {Blockly.BlockSvg} newParent New parent block.
316
+ */
317
+ Blockly.BlockSvg.prototype.setParent = function(newParent) {
318
+ var oldParent = this.parentBlock_;
319
+ if (newParent == oldParent) {
320
+ return;
321
+ }
322
+ Blockly.Field.startCache();
323
+ Blockly.BlockSvg.superClass_.setParent.call(this, newParent);
324
+ Blockly.Field.stopCache();
325
+
326
+ var svgRoot = this.getSvgRoot();
327
+
328
+ // Bail early if workspace is clearing, or we aren't rendered.
329
+ // We won't need to reattach ourselves anywhere.
330
+ if (this.workspace.isClearing || !svgRoot) {
331
+ return;
332
+ }
333
+
334
+ this.updateIntersectionObserver();
335
+
336
+ var oldXY = this.getRelativeToSurfaceXY();
337
+ if (newParent) {
338
+ newParent.getSvgRoot().appendChild(svgRoot);
339
+ var newXY = this.getRelativeToSurfaceXY();
340
+ // Move the connections to match the child's new position.
341
+ this.moveConnections_(newXY.x - oldXY.x, newXY.y - oldXY.y);
342
+ // If we are a shadow block, inherit tertiary colour.
343
+ if (this.isShadow()) {
344
+ this.setColour(this.getColour(), this.getColourSecondary(),
345
+ newParent.getColourTertiary(), this.getColourQuaternary());
346
+ }
347
+ }
348
+ // If we are losing a parent, we want to move our DOM element to the
349
+ // root of the workspace.
350
+ else if (oldParent) {
351
+ this.workspace.getCanvas().appendChild(svgRoot);
352
+ this.translate(oldXY.x, oldXY.y);
353
+ }
354
+
355
+ };
356
+
357
+ /**
358
+ * Return the coordinates of the top-left corner of this block relative to the
359
+ * drawing surface's origin (0,0), in workspace units.
360
+ * If the block is on the workspace, (0, 0) is the origin of the workspace
361
+ * coordinate system.
362
+ * This does not change with workspace scale.
363
+ * @return {!goog.math.Coordinate} Object with .x and .y properties in
364
+ * workspace coordinates.
365
+ */
366
+ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() {
367
+ // The drawing surface is relative to either the workspace canvas
368
+ // or to the drag surface group.
369
+ var x = 0;
370
+ var y = 0;
371
+
372
+ var dragSurfaceGroup = this.useDragSurface_ ?
373
+ this.workspace.blockDragSurface_.getGroup() : null;
374
+
375
+ var element = this.getSvgRoot();
376
+ if (element) {
377
+ do {
378
+ // Loop through this block and every parent.
379
+ var xy = Blockly.utils.getRelativeXY(element);
380
+ x += xy.x;
381
+ y += xy.y;
382
+ // If this element is the current element on the drag surface, include
383
+ // the translation of the drag surface itself.
384
+ if (this.useDragSurface_ &&
385
+ this.workspace.blockDragSurface_.getCurrentBlock() == element) {
386
+ var surfaceTranslation = this.workspace.blockDragSurface_.getSurfaceTranslation();
387
+ x += surfaceTranslation.x;
388
+ y += surfaceTranslation.y;
389
+ }
390
+ element = element.parentNode;
391
+ } while (element && element != this.workspace.getCanvas() &&
392
+ element != dragSurfaceGroup);
393
+ }
394
+ return new goog.math.Coordinate(x, y);
395
+ };
396
+
397
+ /**
398
+ * Move a block by a relative offset.
399
+ * @param {number} dx Horizontal offset in workspace units.
400
+ * @param {number} dy Vertical offset in workspace units.
401
+ */
402
+ Blockly.BlockSvg.prototype.moveBy = function(dx, dy) {
403
+ goog.asserts.assert(!this.parentBlock_, 'Block has parent.');
404
+ var eventsEnabled = Blockly.Events.isEnabled();
405
+ if (eventsEnabled) {
406
+ var event = new Blockly.Events.BlockMove(this);
407
+ }
408
+ var xy = this.getRelativeToSurfaceXY();
409
+ this.translate(xy.x + dx, xy.y + dy);
410
+ this.moveConnections_(dx, dy);
411
+ if (eventsEnabled) {
412
+ event.recordNew();
413
+ Blockly.Events.fire(event);
414
+ }
415
+ this.workspace.resizeContents();
416
+ };
417
+
418
+ /**
419
+ * Transforms a block by setting the translation on the transform attribute
420
+ * of the block's SVG.
421
+ * @param {number} x The x coordinate of the translation in workspace units.
422
+ * @param {number} y The y coordinate of the translation in workspace units.
423
+ */
424
+ Blockly.BlockSvg.prototype.translate = function(x, y) {
425
+ this.getSvgRoot().setAttribute('transform',
426
+ 'translate(' + x + ',' + y + ')');
427
+ };
428
+
429
+ /**
430
+ * Move this block to its workspace's drag surface, accounting for positioning.
431
+ * Generally should be called at the same time as setDragging_(true).
432
+ * Does nothing if useDragSurface_ is false.
433
+ * @private
434
+ */
435
+ Blockly.BlockSvg.prototype.moveToDragSurface_ = function() {
436
+ if (!this.useDragSurface_) {
437
+ return;
438
+ }
439
+ // The translation for drag surface blocks,
440
+ // is equal to the current relative-to-surface position,
441
+ // to keep the position in sync as it move on/off the surface.
442
+ // This is in workspace coordinates.
443
+ var xy = this.getRelativeToSurfaceXY();
444
+ this.clearTransformAttributes_();
445
+ this.workspace.blockDragSurface_.translateSurface(xy.x, xy.y);
446
+ // Execute the move on the top-level SVG component
447
+ this.workspace.blockDragSurface_.setBlocksAndShow(this.getSvgRoot());
448
+ };
449
+
450
+ /**
451
+ * Move this block back to the workspace block canvas.
452
+ * Generally should be called at the same time as setDragging_(false).
453
+ * Does nothing if useDragSurface_ is false.
454
+ * @param {!goog.math.Coordinate} newXY The position the block should take on
455
+ * on the workspace canvas, in workspace coordinates.
456
+ * @private
457
+ */
458
+ Blockly.BlockSvg.prototype.moveOffDragSurface_ = function(newXY) {
459
+ if (!this.useDragSurface_) {
460
+ return;
461
+ }
462
+ // Translate to current position, turning off 3d.
463
+ this.translate(newXY.x, newXY.y);
464
+ this.workspace.blockDragSurface_.clearAndHide(this.workspace.getCanvas());
465
+ };
466
+
467
+ /**
468
+ * Move this block during a drag, taking into account whether we are using a
469
+ * drag surface to translate blocks.
470
+ * This block must be a top-level block.
471
+ * @param {!goog.math.Coordinate} newLoc The location to translate to, in
472
+ * workspace coordinates.
473
+ * @package
474
+ */
475
+ Blockly.BlockSvg.prototype.moveDuringDrag = function(newLoc) {
476
+ if (this.useDragSurface_) {
477
+ this.workspace.blockDragSurface_.translateSurface(newLoc.x, newLoc.y);
478
+ } else {
479
+ this.svgGroup_.translate_ = 'translate(' + newLoc.x + ',' + newLoc.y + ')';
480
+ this.svgGroup_.setAttribute('transform',
481
+ this.svgGroup_.translate_ + this.svgGroup_.skew_);
482
+ }
483
+ };
484
+
485
+ /**
486
+ * Clear the block of transform="..." attributes.
487
+ * Used when the block is switching from 3d to 2d transform or vice versa.
488
+ * @private
489
+ */
490
+ Blockly.BlockSvg.prototype.clearTransformAttributes_ = function() {
491
+ Blockly.utils.removeAttribute(this.getSvgRoot(), 'transform');
492
+ };
493
+
494
+ /**
495
+ * Snap this block to the nearest grid point.
496
+ */
497
+ Blockly.BlockSvg.prototype.snapToGrid = function() {
498
+ if (!this.workspace) {
499
+ return; // Deleted block.
500
+ }
501
+ if (this.workspace.isDragging()) {
502
+ return; // Don't bump blocks during a drag.
503
+ }
504
+ if (this.getParent()) {
505
+ return; // Only snap top-level blocks.
506
+ }
507
+ if (this.isInFlyout) {
508
+ return; // Don't move blocks around in a flyout.
509
+ }
510
+ var grid = this.workspace.getGrid();
511
+ if (!grid || !grid.shouldSnap()) {
512
+ return; // Config says no snapping.
513
+ }
514
+ var spacing = grid.getSpacing();
515
+ var half = spacing / 2;
516
+ var xy = this.getRelativeToSurfaceXY();
517
+ var dx = Math.round((xy.x - half) / spacing) * spacing + half - xy.x;
518
+ var dy = Math.round((xy.y - half) / spacing) * spacing + half - xy.y;
519
+ dx = Math.round(dx);
520
+ dy = Math.round(dy);
521
+ if (dx != 0 || dy != 0) {
522
+ this.moveBy(dx, dy);
523
+ }
524
+ };
525
+
526
+ /**
527
+ * Returns the coordinates of a bounding box describing the dimensions of this
528
+ * block and any blocks stacked below it.
529
+ * Coordinate system: workspace coordinates.
530
+ * @return {!{topLeft: goog.math.Coordinate, bottomRight: goog.math.Coordinate}}
531
+ * Object with top left and bottom right coordinates of the bounding box.
532
+ */
533
+ Blockly.BlockSvg.prototype.getBoundingRectangle = function() {
534
+ var blockXY = this.getRelativeToSurfaceXY(this);
535
+ var blockBounds = this.getHeightWidth();
536
+ var topLeft;
537
+ var bottomRight;
538
+ if (this.RTL) {
539
+ topLeft = new goog.math.Coordinate(blockXY.x - blockBounds.width,
540
+ blockXY.y);
541
+ bottomRight = new goog.math.Coordinate(blockXY.x,
542
+ blockXY.y + blockBounds.height);
543
+ } else {
544
+ topLeft = new goog.math.Coordinate(blockXY.x, blockXY.y);
545
+ bottomRight = new goog.math.Coordinate(blockXY.x + blockBounds.width,
546
+ blockXY.y + blockBounds.height);
547
+ }
548
+
549
+ return {topLeft: topLeft, bottomRight: bottomRight};
550
+ };
551
+
552
+ /**
553
+ * Set block opacity for SVG rendering.
554
+ * @param {number} opacity Intended opacity, betweeen 0 and 1
555
+ */
556
+ Blockly.BlockSvg.prototype.setOpacity = function(opacity) {
557
+ this.opacity_ = opacity;
558
+ if (this.rendered) {
559
+ this.updateColour();
560
+ }
561
+ };
562
+
563
+ /**
564
+ * Get block opacity for SVG rendering.
565
+ * @return {number} Intended opacity, betweeen 0 and 1
566
+ */
567
+ Blockly.BlockSvg.prototype.getOpacity = function() {
568
+ return this.opacity_;
569
+ };
570
+
571
+ /**
572
+ * Set whether the block is collapsed or not.
573
+ * @param {boolean} collapsed True if collapsed.
574
+ */
575
+ Blockly.BlockSvg.prototype.setCollapsed = function(collapsed) {
576
+ if (this.collapsed_ == collapsed) {
577
+ return;
578
+ }
579
+ var renderList = [];
580
+ // Show/hide the inputs.
581
+ for (var i = 0, input; input = this.inputList[i]; i++) {
582
+ renderList.push.apply(renderList, input.setVisible(!collapsed));
583
+ }
584
+
585
+ var COLLAPSED_INPUT_NAME = '_TEMP_COLLAPSED_INPUT';
586
+ if (collapsed) {
587
+ var icons = this.getIcons();
588
+ for (var i = 0; i < icons.length; i++) {
589
+ icons[i].setVisible(false);
590
+ }
591
+ var text = this.toString(Blockly.COLLAPSE_CHARS);
592
+ this.appendDummyInput(COLLAPSED_INPUT_NAME).appendField(text).init();
593
+ } else {
594
+ this.removeInput(COLLAPSED_INPUT_NAME);
595
+ // Clear any warnings inherited from enclosed blocks.
596
+ this.setWarningText(null);
597
+ }
598
+ Blockly.BlockSvg.superClass_.setCollapsed.call(this, collapsed);
599
+
600
+ if (!renderList.length) {
601
+ // No child blocks, just render this block.
602
+ renderList[0] = this;
603
+ }
604
+ if (this.rendered) {
605
+ for (var i = 0, block; block = renderList[i]; i++) {
606
+ block.render();
607
+ }
608
+ // Don't bump neighbours.
609
+ // Although bumping neighbours would make sense, users often collapse
610
+ // all their functions and store them next to each other. Expanding and
611
+ // bumping causes all their definitions to go out of alignment.
612
+ }
613
+ };
614
+
615
+ /**
616
+ * Open the next (or previous) FieldTextInput.
617
+ * @param {Blockly.Field|Blockly.Block} start Current location.
618
+ * @param {boolean} forward If true go forward, otherwise backward.
619
+ */
620
+ Blockly.BlockSvg.prototype.tab = function(start, forward) {
621
+ var list = this.createTabList_();
622
+ var i = list.indexOf(start);
623
+ if (i == -1) {
624
+ // No start location, start at the beginning or end.
625
+ i = forward ? -1 : list.length;
626
+ }
627
+ var target = list[forward ? i + 1 : i - 1];
628
+ if (!target) {
629
+ // Ran off of list.
630
+ // If there is an output, tab up to that block.
631
+ var outputBlock = this.outputConnection && this.outputConnection.targetBlock();
632
+ if (outputBlock) {
633
+ outputBlock.tab(this, forward);
634
+ } else { // Otherwise, go to next / previous block, depending on value of `forward`
635
+ var block = forward ? this.getNextBlock() : this.getPreviousBlock();
636
+ if (block) {
637
+ block.tab(this, forward);
638
+ }
639
+ }
640
+ } else if (target instanceof Blockly.Field) {
641
+ target.showEditor_();
642
+ } else {
643
+ target.tab(null, forward);
644
+ }
645
+ };
646
+
647
+ /**
648
+ * Create an ordered list of all text fields and connected inputs.
649
+ * @return {!Array.<!Blockly.FieldTextInput|!Blockly.Input>} The ordered list.
650
+ * @private
651
+ */
652
+ Blockly.BlockSvg.prototype.createTabList_ = function() {
653
+ // This function need not be efficient since it runs once on a keypress.
654
+ var list = [];
655
+ for (var i = 0, input; input = this.inputList[i]; i++) {
656
+ for (var j = 0, field; field = input.fieldRow[j]; j++) {
657
+ if (field instanceof Blockly.FieldTextInput) {
658
+ // TODO(# 1276): Also support dropdown fields.
659
+ list.push(field);
660
+ }
661
+ }
662
+ if (input.connection) {
663
+ var block = input.connection.targetBlock();
664
+ if (block) {
665
+ list.push(block);
666
+ }
667
+ }
668
+ }
669
+ return list;
670
+ };
671
+
672
+ /**
673
+ * Handle a mouse-down on an SVG block.
674
+ * @param {!Event} e Mouse down event or touch start event.
675
+ * @private
676
+ */
677
+ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) {
678
+ if (this.workspace && this.workspace.isDragging()) {
679
+ return;
680
+ }
681
+ var gesture = this.workspace && this.workspace.getGesture(e);
682
+ if (gesture) {
683
+ gesture.handleBlockStart(e, this);
684
+ }
685
+ };
686
+
687
+ /**
688
+ * Load the block's help page in a new window.
689
+ * @private
690
+ */
691
+ Blockly.BlockSvg.prototype.showHelp_ = function() {
692
+ var url = goog.isFunction(this.helpUrl) ? this.helpUrl() : this.helpUrl;
693
+ if (url) {
694
+ // @todo rewrite
695
+ alert(url);
696
+ }
697
+ };
698
+
699
+
700
+ /**
701
+ * Show the context menu for this block.
702
+ * @param {!Event} e Mouse event.
703
+ * @private
704
+ */
705
+ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
706
+ if (this.workspace.options.readOnly || !this.contextMenu) {
707
+ return;
708
+ }
709
+ // Save the current block in a variable for use in closures.
710
+ var block = this;
711
+ var menuOptions = [];
712
+ if (this.isDeletable() && this.isMovable() && !block.isInFlyout) {
713
+ menuOptions.push(
714
+ Blockly.ContextMenu.blockDuplicateOption(block, e));
715
+ if (this.isEditable() && this.workspace.options.comments) {
716
+ menuOptions.push(Blockly.ContextMenu.blockCommentOption(block));
717
+ }
718
+ menuOptions.push(Blockly.ContextMenu.blockDeleteOption(block));
719
+ } else if (this.parentBlock_ && this.isShadow_) {
720
+ this.parentBlock_.showContextMenu_(e);
721
+ return;
722
+ }
723
+
724
+ // Allow the block to add or modify menuOptions.
725
+ if (this.customContextMenu) {
726
+ this.customContextMenu(menuOptions);
727
+ }
728
+ Blockly.ContextMenu.show(e, menuOptions, this.RTL);
729
+ Blockly.ContextMenu.currentBlock = this;
730
+ };
731
+
732
+ /**
733
+ * Move the connections for this block and all blocks attached under it.
734
+ * Also update any attached bubbles.
735
+ * @param {number} dx Horizontal offset from current location, in workspace
736
+ * units.
737
+ * @param {number} dy Vertical offset from current location, in workspace
738
+ * units.
739
+ * @private
740
+ */
741
+ Blockly.BlockSvg.prototype.moveConnections_ = function(dx, dy) {
742
+ if (!this.rendered) {
743
+ // Rendering is required to lay out the blocks.
744
+ // This is probably an invisible block attached to a collapsed block.
745
+ return;
746
+ }
747
+ var myConnections = this.getConnections_(false);
748
+ for (var i = 0; i < myConnections.length; i++) {
749
+ myConnections[i].moveBy(dx, dy);
750
+ }
751
+ var icons = this.getIcons();
752
+ for (i = 0; i < icons.length; i++) {
753
+ icons[i].computeIconLocation();
754
+ }
755
+
756
+ // Recurse through all blocks attached under this one.
757
+ for (i = 0; i < this.childBlocks_.length; i++) {
758
+ this.childBlocks_[i].moveConnections_(dx, dy);
759
+ }
760
+ };
761
+
762
+ /**
763
+ * Recursively adds or removes the dragging class to this node and its children.
764
+ * @param {boolean} adding True if adding, false if removing.
765
+ * @package
766
+ */
767
+ Blockly.BlockSvg.prototype.setDragging = function(adding) {
768
+ if (adding) {
769
+ var group = this.getSvgRoot();
770
+ group.translate_ = '';
771
+ group.skew_ = '';
772
+ Blockly.draggingConnections_ =
773
+ Blockly.draggingConnections_.concat(this.getConnections_(true));
774
+ Blockly.utils.addClass(
775
+ /** @type {!Element} */ (this.svgGroup_), 'blocklyDragging');
776
+ } else {
777
+ Blockly.draggingConnections_ = [];
778
+ Blockly.utils.removeClass(
779
+ /** @type {!Element} */ (this.svgGroup_), 'blocklyDragging');
780
+ }
781
+ // Recurse through all blocks attached under this one.
782
+ for (var i = 0; i < this.childBlocks_.length; i++) {
783
+ this.childBlocks_[i].setDragging(adding);
784
+ }
785
+ };
786
+
787
+ /**
788
+ * Add or remove the UI indicating if this block is movable or not.
789
+ */
790
+ Blockly.BlockSvg.prototype.updateMovable = function() {
791
+ if (this.isMovable()) {
792
+ Blockly.utils.addClass(
793
+ /** @type {!Element} */ (this.svgGroup_), 'blocklyDraggable');
794
+ } else {
795
+ Blockly.utils.removeClass(
796
+ /** @type {!Element} */ (this.svgGroup_), 'blocklyDraggable');
797
+ }
798
+ };
799
+
800
+ /**
801
+ * Set whether this block is movable or not.
802
+ * @param {boolean} movable True if movable.
803
+ */
804
+ Blockly.BlockSvg.prototype.setMovable = function(movable) {
805
+ Blockly.BlockSvg.superClass_.setMovable.call(this, movable);
806
+ this.updateMovable();
807
+ };
808
+
809
+ /**
810
+ * Set whether this block is editable or not.
811
+ * @param {boolean} editable True if editable.
812
+ */
813
+ Blockly.BlockSvg.prototype.setEditable = function(editable) {
814
+ Blockly.BlockSvg.superClass_.setEditable.call(this, editable);
815
+ var icons = this.getIcons();
816
+ for (var i = 0; i < icons.length; i++) {
817
+ icons[i].updateEditable();
818
+ }
819
+ };
820
+
821
+ /**
822
+ * Set whether this block is a shadow block or not.
823
+ * @param {boolean} shadow True if a shadow.
824
+ */
825
+ Blockly.BlockSvg.prototype.setShadow = function(shadow) {
826
+ Blockly.BlockSvg.superClass_.setShadow.call(this, shadow);
827
+ this.updateColour();
828
+ };
829
+
830
+ /**
831
+ * Set whether this block is an insertion marker block or not.
832
+ * @param {boolean} insertionMarker True if an insertion marker.
833
+ * @param {Number=} opt_minWidth Optional minimum width of the marker.
834
+ */
835
+ Blockly.BlockSvg.prototype.setInsertionMarker = function(insertionMarker, opt_minWidth) {
836
+ Blockly.BlockSvg.superClass_.setInsertionMarker.call(this, insertionMarker);
837
+ this.insertionMarkerMinWidth_ = opt_minWidth;
838
+ this.updateColour();
839
+ };
840
+
841
+ /**
842
+ * Return the root node of the SVG or null if none exists.
843
+ * @return {Element} The root SVG node (probably a group).
844
+ */
845
+ Blockly.BlockSvg.prototype.getSvgRoot = function() {
846
+ return this.svgGroup_;
847
+ };
848
+
849
+ /**
850
+ * Dispose of this block.
851
+ * @param {boolean} healStack If true, then try to heal any gap by connecting
852
+ * the next statement with the previous statement. Otherwise, dispose of
853
+ * all children of this block.
854
+ * @param {boolean} animate If true, show a disposal animation and sound.
855
+ */
856
+ Blockly.BlockSvg.prototype.dispose = function(healStack, animate) {
857
+ if (!this.workspace) {
858
+ // The block has already been deleted.
859
+ return;
860
+ }
861
+ Blockly.Tooltip.hide();
862
+ Blockly.Field.startCache();
863
+ // Save the block's workspace temporarily so we can resize the
864
+ // contents once the block is disposed.
865
+ var blockWorkspace = this.workspace;
866
+ // If this block is being dragged, unlink the mouse events.
867
+ if (Blockly.selected == this) {
868
+ this.unselect();
869
+ this.workspace.cancelCurrentGesture();
870
+ }
871
+ // If this block has a context menu open, close it.
872
+ if (Blockly.ContextMenu.currentBlock == this) {
873
+ Blockly.ContextMenu.hide();
874
+ }
875
+
876
+ if (animate && this.rendered) {
877
+ this.unplug(healStack);
878
+ Blockly.BlockAnimations.disposeUiEffect(this);
879
+ }
880
+ // Stop rerendering.
881
+ this.rendered = false;
882
+
883
+ Blockly.Events.disable();
884
+ try {
885
+ var icons = this.getIcons();
886
+ for (var i = 0; i < icons.length; i++) {
887
+ icons[i].dispose();
888
+ }
889
+ } finally {
890
+ Blockly.Events.enable();
891
+ }
892
+ Blockly.BlockSvg.superClass_.dispose.call(this, healStack);
893
+
894
+ if (blockWorkspace.intersectionObserver) {
895
+ blockWorkspace.intersectionObserver.unobserve(this);
896
+ }
897
+ goog.dom.removeNode(this.svgGroup_);
898
+ blockWorkspace.resizeContents();
899
+ // Sever JavaScript to DOM connections.
900
+ this.svgGroup_ = null;
901
+ this.svgPath_ = null;
902
+ Blockly.Field.stopCache();
903
+ };
904
+
905
+ /**
906
+ * Enable or disable a block.
907
+ */
908
+ Blockly.BlockSvg.prototype.updateDisabled = function() {
909
+ // not supported
910
+ };
911
+
912
+ /**
913
+ * Returns the comment on this block (or '' if none).
914
+ * @return {string} Block's comment.
915
+ */
916
+ Blockly.BlockSvg.prototype.getCommentText = function() {
917
+ if (this.comment) {
918
+ var comment = this.comment.getText();
919
+ // Trim off trailing whitespace.
920
+ return comment.replace(/\s+$/, '').replace(/ +\n/g, '\n');
921
+ }
922
+ return '';
923
+ };
924
+
925
+ /**
926
+ * Set this block's comment text.
927
+ * @param {?string} text The text, or null to delete.
928
+ * @param {string=} commentId Id of the comment, or a new one will be generated if not provided.
929
+ * @param {number=} commentX Optional x position for scratch comment in workspace coordinates
930
+ * @param {number=} commentY Optional y position for scratch comment in workspace coordinates
931
+ * @param {boolean=} minimized Optional minimized state for scratch comment, defaults to false
932
+ */
933
+ Blockly.BlockSvg.prototype.setCommentText = function(text, commentId,
934
+ commentX, commentY, minimized) {
935
+ var changedState = false;
936
+ if (goog.isString(text)) {
937
+ if (!this.comment) {
938
+ this.comment = new Blockly.ScratchBlockComment(this, text, commentId,
939
+ commentX, commentY, minimized);
940
+ changedState = true;
941
+ } else {
942
+ this.comment.setText(/** @type {string} */ (text));
943
+ }
944
+ } else {
945
+ if (this.comment) {
946
+ this.comment.dispose();
947
+ changedState = true;
948
+ }
949
+ }
950
+ if (changedState && this.rendered) {
951
+ this.render();
952
+ if (goog.isString(text)) {
953
+ this.comment.setVisible(true);
954
+ }
955
+ // Adding or removing a comment icon will cause the block to change shape.
956
+ this.bumpNeighbours_();
957
+ }
958
+ };
959
+
960
+ /**
961
+ * Set this block's warning text.
962
+ * @param {?string} text The text, or null to delete.
963
+ * @param {string=} opt_id An optional ID for the warning text to be able to
964
+ * maintain multiple warnings.
965
+ */
966
+ Blockly.BlockSvg.prototype.setWarningText = function(text, opt_id) {
967
+ if (!this.setWarningText.pid_) {
968
+ // Create a database of warning PIDs.
969
+ // Only runs once per block (and only those with warnings).
970
+ this.setWarningText.pid_ = Object.create(null);
971
+ }
972
+ var id = opt_id || '';
973
+ if (!id) {
974
+ // Kill all previous pending processes, this edit supersedes them all.
975
+ for (var n in this.setWarningText.pid_) {
976
+ clearTimeout(this.setWarningText.pid_[n]);
977
+ delete this.setWarningText.pid_[n];
978
+ }
979
+ } else if (this.setWarningText.pid_[id]) {
980
+ // Only queue up the latest change. Kill any earlier pending process.
981
+ clearTimeout(this.setWarningText.pid_[id]);
982
+ delete this.setWarningText.pid_[id];
983
+ }
984
+ if (this.workspace.isDragging()) {
985
+ // Don't change the warning text during a drag.
986
+ // Wait until the drag finishes.
987
+ var thisBlock = this;
988
+ this.setWarningText.pid_[id] = setTimeout(function() {
989
+ if (thisBlock.workspace) { // Check block wasn't deleted.
990
+ delete thisBlock.setWarningText.pid_[id];
991
+ thisBlock.setWarningText(text, id);
992
+ }
993
+ }, 100);
994
+ return;
995
+ }
996
+ if (this.isInFlyout) {
997
+ text = null;
998
+ }
999
+
1000
+ var changedState = false;
1001
+ if (goog.isString(text)) {
1002
+ if (!this.warning) {
1003
+ this.warning = new Blockly.Warning(this);
1004
+ changedState = true;
1005
+ }
1006
+ this.warning.setText(/** @type {string} */ (text), id);
1007
+ } else {
1008
+ // Dispose all warnings if no ID is given.
1009
+ if (this.warning && !id) {
1010
+ this.warning.dispose();
1011
+ changedState = true;
1012
+ } else if (this.warning) {
1013
+ var oldText = this.warning.getText();
1014
+ this.warning.setText('', id);
1015
+ var newText = this.warning.getText();
1016
+ if (!newText) {
1017
+ this.warning.dispose();
1018
+ }
1019
+ changedState = oldText != newText;
1020
+ }
1021
+ }
1022
+ if (changedState && this.rendered) {
1023
+ this.render();
1024
+ // Adding or removing a warning icon will cause the block to change shape.
1025
+ this.bumpNeighbours_();
1026
+ }
1027
+ };
1028
+
1029
+ /**
1030
+ * Give this block a mutator dialog.
1031
+ * @param {Blockly.Mutator} mutator A mutator dialog instance or null to remove.
1032
+ */
1033
+ Blockly.BlockSvg.prototype.setMutator = function(mutator) {
1034
+ if (this.mutator && this.mutator !== mutator) {
1035
+ this.mutator.dispose();
1036
+ }
1037
+ if (mutator) {
1038
+ mutator.block_ = this;
1039
+ this.mutator = mutator;
1040
+ mutator.createIcon();
1041
+ }
1042
+ };
1043
+
1044
+ /**
1045
+ * Select this block. Highlight it visually.
1046
+ */
1047
+ Blockly.BlockSvg.prototype.addSelect = function() {
1048
+ Blockly.utils.addClass(
1049
+ /** @type {!Element} */ (this.svgGroup_), 'blocklySelected');
1050
+ };
1051
+
1052
+ /**
1053
+ * Unselect this block. Remove its highlighting.
1054
+ */
1055
+ Blockly.BlockSvg.prototype.removeSelect = function() {
1056
+ Blockly.utils.removeClass(
1057
+ /** @type {!Element} */ (this.svgGroup_), 'blocklySelected');
1058
+ };
1059
+
1060
+ /**
1061
+ * Update the cursor over this block by adding or removing a class.
1062
+ * @param {boolean} letMouseThrough True if the blocks should ignore pointer
1063
+ * events, false otherwise.
1064
+ * @package
1065
+ */
1066
+ Blockly.BlockSvg.prototype.setMouseThroughStyle = function(letMouseThrough) {
1067
+ if (letMouseThrough) {
1068
+ Blockly.utils.addClass(/** @type {!Element} */ (this.svgGroup_),
1069
+ 'blocklyDraggingMouseThrough');
1070
+ } else {
1071
+ Blockly.utils.removeClass(/** @type {!Element} */ (this.svgGroup_),
1072
+ 'blocklyDraggingMouseThrough');
1073
+ }
1074
+ };
1075
+
1076
+ /**
1077
+ * Update the cursor over this block by adding or removing a class.
1078
+ * @param {boolean} enable True if the delete cursor should be shown, false
1079
+ * otherwise.
1080
+ * @package
1081
+ */
1082
+ Blockly.BlockSvg.prototype.setDeleteStyle = function(enable) {
1083
+ if (enable) {
1084
+ Blockly.utils.addClass(/** @type {!Element} */ (this.svgGroup_),
1085
+ 'blocklyDraggingDelete');
1086
+ } else {
1087
+ Blockly.utils.removeClass(/** @type {!Element} */ (this.svgGroup_),
1088
+ 'blocklyDraggingDelete');
1089
+ }
1090
+ };
1091
+
1092
+ // Overrides of functions on Blockly.Block that take into account whether the
1093
+ // block has been rendered.
1094
+
1095
+ /**
1096
+ * Change the colour of a block.
1097
+ * @param {number|string} colour HSV hue value, or #RRGGBB string.
1098
+ * @param {number|string} colourSecondary Secondary HSV hue value, or #RRGGBB
1099
+ * string.
1100
+ * @param {number|string} colourTertiary Tertiary HSV hue value, or #RRGGBB
1101
+ * string.
1102
+ * @param {number|string} colourQuaternary Quaternary HSV hue value, or #RRGGBB
1103
+ * string.
1104
+ */
1105
+ Blockly.BlockSvg.prototype.setColour = function(colour, colourSecondary,
1106
+ colourTertiary, colourQuaternary) {
1107
+ Blockly.BlockSvg.superClass_.setColour.call(this, colour, colourSecondary,
1108
+ colourTertiary, colourQuaternary);
1109
+
1110
+ if (this.rendered) {
1111
+ this.updateColour();
1112
+ }
1113
+ };
1114
+
1115
+ /**
1116
+ * Move this block to the front of the visible workspace.
1117
+ * <g> tags do not respect z-index so SVG renders them in the
1118
+ * order that they are in the DOM. By placing this block first within the
1119
+ * block group's <g>, it will render on top of any other blocks.
1120
+ * @package
1121
+ */
1122
+ Blockly.BlockSvg.prototype.bringToFront = function() {
1123
+ var block = this;
1124
+ do {
1125
+ var root = block.getSvgRoot();
1126
+ root.parentNode.appendChild(root);
1127
+ block = block.getParent();
1128
+ } while (block);
1129
+ };
1130
+
1131
+ /**
1132
+ * Set whether this block can chain onto the bottom of another block.
1133
+ * @param {boolean} newBoolean True if there can be a previous statement.
1134
+ * @param {(string|Array.<string>|null)=} opt_check Statement type or
1135
+ * list of statement types. Null/undefined if any type could be connected.
1136
+ */
1137
+ Blockly.BlockSvg.prototype.setPreviousStatement = function(newBoolean,
1138
+ opt_check) {
1139
+ Blockly.BlockSvg.superClass_.setPreviousStatement.call(this, newBoolean,
1140
+ opt_check);
1141
+
1142
+ if (this.rendered) {
1143
+ this.render();
1144
+ this.bumpNeighbours_();
1145
+ }
1146
+ };
1147
+
1148
+ /**
1149
+ * Set whether another block can chain onto the bottom of this block.
1150
+ * @param {boolean} newBoolean True if there can be a next statement.
1151
+ * @param {(string|Array.<string>|null)=} opt_check Statement type or
1152
+ * list of statement types. Null/undefined if any type could be connected.
1153
+ */
1154
+ Blockly.BlockSvg.prototype.setNextStatement = function(newBoolean, opt_check) {
1155
+ Blockly.BlockSvg.superClass_.setNextStatement.call(this, newBoolean,
1156
+ opt_check);
1157
+
1158
+ if (this.rendered) {
1159
+ this.render();
1160
+ this.bumpNeighbours_();
1161
+ }
1162
+ };
1163
+
1164
+ /**
1165
+ * Set whether this block returns a value.
1166
+ * @param {boolean} newBoolean True if there is an output.
1167
+ * @param {(string|Array.<string>|null)=} opt_check Returned type or list
1168
+ * of returned types. Null or undefined if any type could be returned
1169
+ * (e.g. variable get).
1170
+ */
1171
+ Blockly.BlockSvg.prototype.setOutput = function(newBoolean, opt_check) {
1172
+ Blockly.BlockSvg.superClass_.setOutput.call(this, newBoolean, opt_check);
1173
+
1174
+ if (this.rendered) {
1175
+ this.render();
1176
+ this.bumpNeighbours_();
1177
+ }
1178
+ };
1179
+
1180
+ /**
1181
+ * Set whether value inputs are arranged horizontally or vertically.
1182
+ * @param {boolean} newBoolean True if inputs are horizontal.
1183
+ */
1184
+ Blockly.BlockSvg.prototype.setInputsInline = function(newBoolean) {
1185
+ Blockly.BlockSvg.superClass_.setInputsInline.call(this, newBoolean);
1186
+
1187
+ if (this.rendered) {
1188
+ this.render();
1189
+ this.bumpNeighbours_();
1190
+ }
1191
+ };
1192
+
1193
+ /**
1194
+ * Remove an input from this block.
1195
+ * @param {string} name The name of the input.
1196
+ * @param {boolean=} opt_quiet True to prevent error if input is not present.
1197
+ * @throws {goog.asserts.AssertionError} if the input is not present and
1198
+ * opt_quiet is not true.
1199
+ */
1200
+ Blockly.BlockSvg.prototype.removeInput = function(name, opt_quiet) {
1201
+ Blockly.BlockSvg.superClass_.removeInput.call(this, name, opt_quiet);
1202
+
1203
+ if (this.rendered) {
1204
+ this.render();
1205
+ // Removing an input will cause the block to change shape.
1206
+ this.bumpNeighbours_();
1207
+ }
1208
+ };
1209
+
1210
+ /**
1211
+ * Move a numbered input to a different location on this block.
1212
+ * @param {number} inputIndex Index of the input to move.
1213
+ * @param {number} refIndex Index of input that should be after the moved input.
1214
+ */
1215
+ Blockly.BlockSvg.prototype.moveNumberedInputBefore = function(
1216
+ inputIndex, refIndex) {
1217
+ Blockly.BlockSvg.superClass_.moveNumberedInputBefore.call(this, inputIndex,
1218
+ refIndex);
1219
+
1220
+ if (this.rendered) {
1221
+ this.render();
1222
+ // Moving an input will cause the block to change shape.
1223
+ this.bumpNeighbours_();
1224
+ }
1225
+ };
1226
+
1227
+ /**
1228
+ * Add a value input, statement input or local variable to this block.
1229
+ * @param {number} type Either Blockly.INPUT_VALUE or Blockly.NEXT_STATEMENT or
1230
+ * Blockly.DUMMY_INPUT.
1231
+ * @param {string} name Language-neutral identifier which may used to find this
1232
+ * input again. Should be unique to this block.
1233
+ * @return {!Blockly.Input} The input object created.
1234
+ * @private
1235
+ */
1236
+ Blockly.BlockSvg.prototype.appendInput_ = function(type, name) {
1237
+ var input = Blockly.BlockSvg.superClass_.appendInput_.call(this, type, name);
1238
+
1239
+ if (this.rendered) {
1240
+ this.render();
1241
+ // Adding an input will cause the block to change shape.
1242
+ this.bumpNeighbours_();
1243
+ }
1244
+ return input;
1245
+ };
1246
+
1247
+ /**
1248
+ * Returns connections originating from this block.
1249
+ * @param {boolean} all If true, return all connections even hidden ones.
1250
+ * Otherwise, for a non-rendered block return an empty list, and for a
1251
+ * collapsed block don't return inputs connections.
1252
+ * @return {!Array.<!Blockly.Connection>} Array of connections.
1253
+ * @package
1254
+ */
1255
+ Blockly.BlockSvg.prototype.getConnections_ = function(all) {
1256
+ var myConnections = [];
1257
+ if (all || this.rendered) {
1258
+ if (this.outputConnection) {
1259
+ myConnections.push(this.outputConnection);
1260
+ }
1261
+ if (this.previousConnection) {
1262
+ myConnections.push(this.previousConnection);
1263
+ }
1264
+ if (this.nextConnection) {
1265
+ myConnections.push(this.nextConnection);
1266
+ }
1267
+ if (all || !this.collapsed_) {
1268
+ for (var i = 0, input; input = this.inputList[i]; i++) {
1269
+ if (input.connection) {
1270
+ myConnections.push(input.connection);
1271
+ }
1272
+ }
1273
+ }
1274
+ }
1275
+ return myConnections;
1276
+ };
1277
+
1278
+ /**
1279
+ * Create a connection of the specified type.
1280
+ * @param {number} type The type of the connection to create.
1281
+ * @return {!Blockly.RenderedConnection} A new connection of the specified type.
1282
+ * @private
1283
+ */
1284
+ Blockly.BlockSvg.prototype.makeConnection_ = function(type) {
1285
+ return new Blockly.RenderedConnection(this, type);
1286
+ };
1287
+
1288
+ /**
1289
+ * Bump unconnected blocks out of alignment. Two blocks which aren't actually
1290
+ * connected should not coincidentally line up on screen.
1291
+ * @private
1292
+ */
1293
+ Blockly.BlockSvg.prototype.bumpNeighbours_ = function() {
1294
+ if (!this.workspace) {
1295
+ return; // Deleted block.
1296
+ }
1297
+ if (this.workspace.isDragging()) {
1298
+ return; // Don't bump blocks during a drag.
1299
+ }
1300
+ var rootBlock = this.getRootBlock();
1301
+ if (rootBlock.isInFlyout) {
1302
+ return; // Don't move blocks around in a flyout.
1303
+ }
1304
+ // Loop through every connection on this block.
1305
+ var myConnections = this.getConnections_(false);
1306
+ for (var i = 0, connection; connection = myConnections[i]; i++) {
1307
+
1308
+ // Spider down from this block bumping all sub-blocks.
1309
+ if (connection.isConnected() && connection.isSuperior()) {
1310
+ connection.targetBlock().bumpNeighbours_();
1311
+ }
1312
+
1313
+ var neighbours = connection.neighbours_(Blockly.SNAP_RADIUS);
1314
+ for (var j = 0, otherConnection; otherConnection = neighbours[j]; j++) {
1315
+
1316
+ // If both connections are connected, that's probably fine. But if
1317
+ // either one of them is unconnected, then there could be confusion.
1318
+ if (!connection.isConnected() || !otherConnection.isConnected()) {
1319
+ // Only bump blocks if they are from different tree structures.
1320
+ if (otherConnection.getSourceBlock().getRootBlock() != rootBlock) {
1321
+
1322
+ // Always bump the inferior block.
1323
+ if (connection.isSuperior()) {
1324
+ otherConnection.bumpAwayFrom_(connection);
1325
+ } else {
1326
+ connection.bumpAwayFrom_(otherConnection);
1327
+ }
1328
+ }
1329
+ }
1330
+ }
1331
+ }
1332
+ };
1333
+
1334
+ /**
1335
+ * Schedule snapping to grid and bumping neighbours to occur after a brief
1336
+ * delay.
1337
+ * @package
1338
+ */
1339
+ Blockly.BlockSvg.prototype.scheduleSnapAndBump = function() {
1340
+ var block = this;
1341
+ // Ensure that any snap and bump are part of this move's event group.
1342
+ var group = Blockly.Events.getGroup();
1343
+
1344
+ setTimeout(function() {
1345
+ Blockly.Events.setGroup(group);
1346
+ block.snapToGrid();
1347
+ Blockly.Events.setGroup(false);
1348
+ }, Blockly.BUMP_DELAY / 2);
1349
+
1350
+ setTimeout(function() {
1351
+ Blockly.Events.setGroup(group);
1352
+ block.bumpNeighbours_();
1353
+ Blockly.Events.setGroup(false);
1354
+ }, Blockly.BUMP_DELAY);
1355
+ };