blockly 7.20211209.4 → 8.0.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 (262) hide show
  1. package/blockly.d.ts +18963 -18432
  2. package/blockly.min.js +852 -844
  3. package/blockly_compressed.js +669 -664
  4. package/blockly_compressed.js.map +1 -1
  5. package/blocks/blocks.js +47 -0
  6. package/blocks/colour.js +13 -3
  7. package/blocks/lists.js +22 -13
  8. package/blocks/logic.js +13 -3
  9. package/blocks/loops.js +24 -11
  10. package/blocks/math.js +12 -3
  11. package/blocks/procedures.js +41 -27
  12. package/blocks/text.js +22 -13
  13. package/blocks/variables.js +14 -3
  14. package/blocks/variables_dynamic.js +13 -3
  15. package/blocks_compressed.js +146 -141
  16. package/blocks_compressed.js.map +1 -1
  17. package/core/block.js +1869 -1814
  18. package/core/block_drag_surface.js +201 -200
  19. package/core/block_dragger.js +377 -373
  20. package/core/block_svg.js +1593 -1479
  21. package/core/blockly.js +8 -22
  22. package/core/blocks.js +9 -2
  23. package/core/browser_events.js +22 -5
  24. package/core/bubble.js +841 -797
  25. package/core/bubble_dragger.js +213 -206
  26. package/core/bump_objects.js +2 -2
  27. package/core/clipboard.js +9 -9
  28. package/core/comment.js +353 -332
  29. package/core/common.js +46 -17
  30. package/core/component_manager.js +181 -174
  31. package/core/config.js +87 -0
  32. package/core/connection.js +595 -584
  33. package/core/connection_checker.js +242 -244
  34. package/core/connection_db.js +235 -230
  35. package/core/contextmenu.js +9 -6
  36. package/core/contextmenu_items.js +1 -2
  37. package/core/contextmenu_registry.js +93 -89
  38. package/core/css.js +474 -474
  39. package/core/delete_area.js +45 -42
  40. package/core/drag_target.js +57 -56
  41. package/core/dropdowndiv.js +153 -163
  42. package/core/events/events.js +2 -2
  43. package/core/events/events_abstract.js +89 -77
  44. package/core/events/events_block_base.js +37 -36
  45. package/core/events/events_block_change.js +130 -124
  46. package/core/events/events_block_create.js +73 -71
  47. package/core/events/events_block_delete.js +84 -82
  48. package/core/events/events_block_drag.js +50 -49
  49. package/core/events/events_block_move.js +147 -140
  50. package/core/events/events_bubble_open.js +51 -50
  51. package/core/events/events_click.js +48 -44
  52. package/core/events/events_comment_base.js +72 -69
  53. package/core/events/events_comment_change.js +63 -61
  54. package/core/events/events_comment_create.js +44 -42
  55. package/core/events/events_comment_delete.js +42 -40
  56. package/core/events/events_comment_move.js +106 -104
  57. package/core/events/events_marker_move.js +65 -64
  58. package/core/events/events_selected.js +46 -45
  59. package/core/events/events_theme_change.js +36 -35
  60. package/core/events/events_toolbox_item_select.js +46 -45
  61. package/core/events/events_trashcan_open.js +37 -36
  62. package/core/events/events_ui.js +47 -46
  63. package/core/events/events_ui_base.js +30 -29
  64. package/core/events/events_var_base.js +37 -36
  65. package/core/events/events_var_create.js +50 -48
  66. package/core/events/events_var_delete.js +50 -48
  67. package/core/events/events_var_rename.js +51 -49
  68. package/core/events/events_viewport.js +66 -65
  69. package/core/events/utils.js +29 -14
  70. package/core/events/workspace_events.js +49 -55
  71. package/core/extensions.js +4 -3
  72. package/core/field.js +1061 -997
  73. package/core/field_angle.js +462 -442
  74. package/core/field_checkbox.js +194 -182
  75. package/core/field_colour.js +519 -505
  76. package/core/field_dropdown.js +617 -598
  77. package/core/field_image.js +229 -220
  78. package/core/field_label.js +102 -91
  79. package/core/field_label_serializable.js +42 -41
  80. package/core/field_multilineinput.js +372 -358
  81. package/core/field_number.js +272 -253
  82. package/core/field_textinput.js +499 -467
  83. package/core/field_variable.js +458 -420
  84. package/core/flyout_base.js +1005 -952
  85. package/core/flyout_button.js +277 -260
  86. package/core/flyout_horizontal.js +304 -302
  87. package/core/flyout_metrics_manager.js +64 -64
  88. package/core/flyout_vertical.js +306 -300
  89. package/core/generator.js +459 -446
  90. package/core/gesture.js +829 -813
  91. package/core/grid.js +166 -163
  92. package/core/icon.js +168 -159
  93. package/core/inject.js +7 -5
  94. package/core/input.js +257 -248
  95. package/core/insertion_marker_manager.js +655 -624
  96. package/core/internal_constants.js +0 -129
  97. package/core/keyboard_nav/ast_node.js +605 -596
  98. package/core/keyboard_nav/basic_cursor.js +166 -165
  99. package/core/keyboard_nav/cursor.js +99 -97
  100. package/core/keyboard_nav/marker.js +83 -79
  101. package/core/keyboard_nav/tab_navigate_cursor.js +18 -23
  102. package/core/marker_manager.js +153 -141
  103. package/core/menu.js +377 -372
  104. package/core/menuitem.js +223 -217
  105. package/core/metrics_manager.js +403 -390
  106. package/core/mutator.js +468 -437
  107. package/core/names.js +229 -188
  108. package/core/options.js +290 -284
  109. package/core/procedures.js +29 -17
  110. package/core/registry.js +19 -16
  111. package/core/rendered_connection.js +482 -463
  112. package/core/renderers/common/block_rendering.js +9 -3
  113. package/core/renderers/common/constants.js +1119 -1112
  114. package/core/renderers/common/debug.js +14 -0
  115. package/core/renderers/common/debugger.js +338 -316
  116. package/core/renderers/common/drawer.js +380 -370
  117. package/core/renderers/common/i_path_object.js +2 -2
  118. package/core/renderers/common/info.js +626 -618
  119. package/core/renderers/common/marker_svg.js +579 -541
  120. package/core/renderers/common/path_object.js +203 -200
  121. package/core/renderers/common/renderer.js +220 -218
  122. package/core/renderers/geras/constants.js +36 -36
  123. package/core/renderers/geras/drawer.js +155 -147
  124. package/core/renderers/geras/highlight_constants.js +244 -238
  125. package/core/renderers/geras/highlighter.js +231 -179
  126. package/core/renderers/geras/info.js +392 -369
  127. package/core/renderers/geras/measurables/inline_input.js +25 -19
  128. package/core/renderers/geras/measurables/statement_input.js +23 -17
  129. package/core/renderers/geras/path_object.js +106 -121
  130. package/core/renderers/geras/renderer.js +96 -98
  131. package/core/renderers/measurables/base.js +30 -18
  132. package/core/renderers/measurables/bottom_row.js +83 -80
  133. package/core/renderers/measurables/connection.js +22 -15
  134. package/core/renderers/measurables/external_value_input.js +35 -22
  135. package/core/renderers/measurables/field.js +35 -20
  136. package/core/renderers/measurables/hat.js +18 -13
  137. package/core/renderers/measurables/icon.js +24 -17
  138. package/core/renderers/measurables/in_row_spacer.js +15 -13
  139. package/core/renderers/measurables/inline_input.js +43 -33
  140. package/core/renderers/measurables/input_connection.js +41 -28
  141. package/core/renderers/measurables/input_row.js +50 -44
  142. package/core/renderers/measurables/jagged_edge.js +14 -12
  143. package/core/renderers/measurables/next_connection.js +16 -14
  144. package/core/renderers/measurables/output_connection.js +26 -20
  145. package/core/renderers/measurables/previous_connection.js +16 -15
  146. package/core/renderers/measurables/round_corner.js +20 -18
  147. package/core/renderers/measurables/row.js +184 -168
  148. package/core/renderers/measurables/spacer_row.js +38 -23
  149. package/core/renderers/measurables/square_corner.js +18 -16
  150. package/core/renderers/measurables/statement_input.js +23 -20
  151. package/core/renderers/measurables/top_row.js +88 -85
  152. package/core/renderers/minimalist/constants.js +8 -7
  153. package/core/renderers/minimalist/drawer.js +11 -10
  154. package/core/renderers/minimalist/info.js +18 -18
  155. package/core/renderers/minimalist/renderer.js +40 -39
  156. package/core/renderers/thrasos/info.js +258 -248
  157. package/core/renderers/thrasos/renderer.js +20 -20
  158. package/core/renderers/zelos/constants.js +898 -873
  159. package/core/renderers/zelos/drawer.js +186 -169
  160. package/core/renderers/zelos/info.js +502 -479
  161. package/core/renderers/zelos/marker_svg.js +129 -115
  162. package/core/renderers/zelos/measurables/bottom_row.js +31 -30
  163. package/core/renderers/zelos/measurables/inputs.js +22 -21
  164. package/core/renderers/zelos/measurables/row_elements.js +14 -13
  165. package/core/renderers/zelos/measurables/top_row.js +34 -33
  166. package/core/renderers/zelos/path_object.js +181 -180
  167. package/core/renderers/zelos/renderer.js +91 -92
  168. package/core/scrollbar.js +759 -713
  169. package/core/scrollbar_pair.js +250 -245
  170. package/core/serialization/blocks.js +19 -9
  171. package/core/serialization/workspaces.js +3 -2
  172. package/core/shortcut_registry.js +286 -277
  173. package/core/sprites.js +31 -0
  174. package/core/theme.js +135 -141
  175. package/core/theme_manager.js +147 -143
  176. package/core/toolbox/category.js +602 -576
  177. package/core/toolbox/collapsible_category.js +226 -227
  178. package/core/toolbox/separator.js +70 -61
  179. package/core/toolbox/toolbox.js +934 -927
  180. package/core/toolbox/toolbox_item.js +115 -99
  181. package/core/tooltip.js +108 -35
  182. package/core/touch.js +8 -3
  183. package/core/touch_gesture.js +254 -251
  184. package/core/trashcan.js +606 -595
  185. package/core/utils/coordinate.js +97 -95
  186. package/core/utils/dom.js +2 -2
  187. package/core/utils/global.js +2 -0
  188. package/core/utils/rect.js +41 -37
  189. package/core/utils/sentinel.js +25 -0
  190. package/core/utils/size.js +30 -27
  191. package/core/utils/svg.js +18 -16
  192. package/core/variable_map.js +325 -341
  193. package/core/variable_model.js +55 -54
  194. package/core/variables.js +9 -2
  195. package/core/variables_dynamic.js +3 -1
  196. package/core/warning.js +126 -120
  197. package/core/widgetdiv.js +4 -4
  198. package/core/workspace.js +685 -664
  199. package/core/workspace_audio.js +124 -118
  200. package/core/workspace_comment.js +308 -298
  201. package/core/workspace_comment_svg.js +1029 -951
  202. package/core/workspace_drag_surface_svg.js +147 -140
  203. package/core/workspace_dragger.js +70 -71
  204. package/core/workspace_svg.js +2322 -2297
  205. package/core/xml.js +30 -20
  206. package/core/zoom_controls.js +431 -439
  207. package/dart_compressed.js +40 -43
  208. package/dart_compressed.js.map +1 -1
  209. package/generators/dart/colour.js +56 -64
  210. package/generators/dart/lists.js +61 -50
  211. package/generators/dart/math.js +160 -148
  212. package/generators/dart/text.js +83 -61
  213. package/generators/javascript/colour.js +37 -34
  214. package/generators/javascript/lists.js +50 -43
  215. package/generators/javascript/math.js +123 -139
  216. package/generators/javascript/text.js +67 -81
  217. package/generators/lua/colour.js +25 -23
  218. package/generators/lua/lists.js +97 -69
  219. package/generators/lua/logic.js +1 -2
  220. package/generators/lua/math.js +182 -144
  221. package/generators/lua/text.js +116 -99
  222. package/generators/php/colour.js +38 -32
  223. package/generators/php/lists.js +109 -89
  224. package/generators/php/math.js +90 -81
  225. package/generators/php/text.js +63 -61
  226. package/generators/python/colour.js +18 -18
  227. package/generators/python/lists.js +38 -30
  228. package/generators/python/loops.js +12 -8
  229. package/generators/python/math.js +104 -106
  230. package/generators/python/text.js +34 -30
  231. package/javascript_compressed.js +37 -39
  232. package/javascript_compressed.js.map +1 -1
  233. package/lua_compressed.js +39 -42
  234. package/lua_compressed.js.map +1 -1
  235. package/msg/az.js +2 -2
  236. package/msg/be.js +4 -4
  237. package/msg/cs.js +15 -15
  238. package/msg/de.js +1 -1
  239. package/msg/diq.js +1 -1
  240. package/msg/eo.js +1 -1
  241. package/msg/es.js +1 -1
  242. package/msg/fa.js +1 -1
  243. package/msg/fr.js +4 -4
  244. package/msg/he.js +1 -1
  245. package/msg/hr.js +2 -2
  246. package/msg/hy.js +2 -2
  247. package/msg/id.js +12 -12
  248. package/msg/inh.js +14 -14
  249. package/msg/ja.js +7 -7
  250. package/msg/lv.js +29 -29
  251. package/msg/pa.js +3 -3
  252. package/msg/smn.js +436 -0
  253. package/msg/te.js +1 -1
  254. package/msg/yue.js +1 -1
  255. package/msg/zh-hans.js +3 -3
  256. package/msg/zh-hant.js +3 -3
  257. package/package.json +7 -6
  258. package/php_compressed.js +38 -42
  259. package/php_compressed.js.map +1 -1
  260. package/python_compressed.js +26 -25
  261. package/python_compressed.js.map +1 -1
  262. package/blocks/all.js +0 -23
@@ -37,390 +37,374 @@ goog.require('Blockly.Events.VarRename');
37
37
  * Class for a variable map. This contains a dictionary data structure with
38
38
  * variable types as keys and lists of variables as values. The list of
39
39
  * variables are the type indicated by the key.
40
- * @param {!Workspace} workspace The workspace this map belongs to.
41
- * @constructor
42
40
  * @alias Blockly.VariableMap
43
41
  */
44
- const VariableMap = function(workspace) {
42
+ class VariableMap {
45
43
  /**
46
- * A map from variable type to list of variable names. The lists contain all
47
- * of the named variables in the workspace, including variables
48
- * that are not currently in use.
49
- * @type {!Object<string, !Array<VariableModel>>}
50
- * @private
44
+ * @param {!Workspace} workspace The workspace this map belongs to.
51
45
  */
52
- this.variableMap_ = Object.create(null);
53
-
46
+ constructor(workspace) {
47
+ /**
48
+ * A map from variable type to list of variable names. The lists contain
49
+ * all of the named variables in the workspace, including variables that are
50
+ * not currently in use.
51
+ * @type {!Object<string, !Array<VariableModel>>}
52
+ * @private
53
+ */
54
+ this.variableMap_ = Object.create(null);
55
+
56
+ /**
57
+ * The workspace this map belongs to.
58
+ * @type {!Workspace}
59
+ */
60
+ this.workspace = workspace;
61
+ }
54
62
  /**
55
- * The workspace this map belongs to.
56
- * @type {!Workspace}
63
+ * Clear the variable map.
57
64
  */
58
- this.workspace = workspace;
59
- };
60
-
61
- /**
62
- * Clear the variable map.
63
- */
64
- VariableMap.prototype.clear = function() {
65
- this.variableMap_ = Object.create(null);
66
- };
67
-
68
- /* Begin functions for renaming variables. */
69
-
70
- /**
71
- * Rename the given variable by updating its name in the variable map.
72
- * @param {!VariableModel} variable Variable to rename.
73
- * @param {string} newName New variable name.
74
- * @package
75
- */
76
- VariableMap.prototype.renameVariable = function(variable, newName) {
77
- const type = variable.type;
78
- const conflictVar = this.getVariable(newName, type);
79
- const blocks = this.workspace.getAllBlocks(false);
80
- eventUtils.setGroup(true);
81
- try {
82
- // The IDs may match if the rename is a simple case change (name1 -> Name1).
83
- if (!conflictVar || conflictVar.getId() === variable.getId()) {
84
- this.renameVariableAndUses_(variable, newName, blocks);
85
- } else {
86
- this.renameVariableWithConflict_(variable, newName, conflictVar, blocks);
65
+ clear() {
66
+ this.variableMap_ = Object.create(null);
67
+ }
68
+ /* Begin functions for renaming variables. */
69
+ /**
70
+ * Rename the given variable by updating its name in the variable map.
71
+ * @param {!VariableModel} variable Variable to rename.
72
+ * @param {string} newName New variable name.
73
+ * @package
74
+ */
75
+ renameVariable(variable, newName) {
76
+ const type = variable.type;
77
+ const conflictVar = this.getVariable(newName, type);
78
+ const blocks = this.workspace.getAllBlocks(false);
79
+ eventUtils.setGroup(true);
80
+ try {
81
+ // The IDs may match if the rename is a simple case change (name1 ->
82
+ // Name1).
83
+ if (!conflictVar || conflictVar.getId() === variable.getId()) {
84
+ this.renameVariableAndUses_(variable, newName, blocks);
85
+ } else {
86
+ this.renameVariableWithConflict_(
87
+ variable, newName, conflictVar, blocks);
88
+ }
89
+ } finally {
90
+ eventUtils.setGroup(false);
87
91
  }
88
- } finally {
89
- eventUtils.setGroup(false);
90
92
  }
91
- };
93
+ /**
94
+ * Rename a variable by updating its name in the variable map. Identify the
95
+ * variable to rename with the given ID.
96
+ * @param {string} id ID of the variable to rename.
97
+ * @param {string} newName New variable name.
98
+ */
99
+ renameVariableById(id, newName) {
100
+ const variable = this.getVariableById(id);
101
+ if (!variable) {
102
+ throw Error('Tried to rename a variable that didn\'t exist. ID: ' + id);
103
+ }
92
104
 
93
- /**
94
- * Rename a variable by updating its name in the variable map. Identify the
95
- * variable to rename with the given ID.
96
- * @param {string} id ID of the variable to rename.
97
- * @param {string} newName New variable name.
98
- */
99
- VariableMap.prototype.renameVariableById = function(id, newName) {
100
- const variable = this.getVariableById(id);
101
- if (!variable) {
102
- throw Error('Tried to rename a variable that didn\'t exist. ID: ' + id);
105
+ this.renameVariable(variable, newName);
103
106
  }
104
-
105
- this.renameVariable(variable, newName);
106
- };
107
-
108
- /**
109
- * Update the name of the given variable and refresh all references to it.
110
- * The new name must not conflict with any existing variable names.
111
- * @param {!VariableModel} variable Variable to rename.
112
- * @param {string} newName New variable name.
113
- * @param {!Array<!Block>} blocks The list of all blocks in the
114
- * workspace.
115
- * @private
116
- */
117
- VariableMap.prototype.renameVariableAndUses_ = function(
118
- variable, newName, blocks) {
119
- eventUtils.fire(
120
- new (eventUtils.get(eventUtils.VAR_RENAME))(variable, newName));
121
- variable.name = newName;
122
- for (let i = 0; i < blocks.length; i++) {
123
- blocks[i].updateVarName(variable);
107
+ /**
108
+ * Update the name of the given variable and refresh all references to it.
109
+ * The new name must not conflict with any existing variable names.
110
+ * @param {!VariableModel} variable Variable to rename.
111
+ * @param {string} newName New variable name.
112
+ * @param {!Array<!Block>} blocks The list of all blocks in the
113
+ * workspace.
114
+ * @private
115
+ */
116
+ renameVariableAndUses_(variable, newName, blocks) {
117
+ eventUtils.fire(
118
+ new (eventUtils.get(eventUtils.VAR_RENAME))(variable, newName));
119
+ variable.name = newName;
120
+ for (let i = 0; i < blocks.length; i++) {
121
+ blocks[i].updateVarName(variable);
122
+ }
124
123
  }
125
- };
124
+ /**
125
+ * Update the name of the given variable to the same name as an existing
126
+ * variable. The two variables are coalesced into a single variable with the
127
+ * ID of the existing variable that was already using newName. Refresh all
128
+ * references to the variable.
129
+ * @param {!VariableModel} variable Variable to rename.
130
+ * @param {string} newName New variable name.
131
+ * @param {!VariableModel} conflictVar The variable that was already
132
+ * using newName.
133
+ * @param {!Array<!Block>} blocks The list of all blocks in the
134
+ * workspace.
135
+ * @private
136
+ */
137
+ renameVariableWithConflict_(variable, newName, conflictVar, blocks) {
138
+ const type = variable.type;
139
+ const oldCase = conflictVar.name;
126
140
 
127
- /**
128
- * Update the name of the given variable to the same name as an existing
129
- * variable. The two variables are coalesced into a single variable with the ID
130
- * of the existing variable that was already using newName.
131
- * Refresh all references to the variable.
132
- * @param {!VariableModel} variable Variable to rename.
133
- * @param {string} newName New variable name.
134
- * @param {!VariableModel} conflictVar The variable that was already
135
- * using newName.
136
- * @param {!Array<!Block>} blocks The list of all blocks in the
137
- * workspace.
138
- * @private
139
- */
140
- VariableMap.prototype.renameVariableWithConflict_ = function(
141
- variable, newName, conflictVar, blocks) {
142
- const type = variable.type;
143
- const oldCase = conflictVar.name;
141
+ if (newName !== oldCase) {
142
+ // Simple rename to change the case and update references.
143
+ this.renameVariableAndUses_(conflictVar, newName, blocks);
144
+ }
144
145
 
145
- if (newName !== oldCase) {
146
- // Simple rename to change the case and update references.
147
- this.renameVariableAndUses_(conflictVar, newName, blocks);
148
- }
146
+ // These blocks now refer to a different variable.
147
+ // These will fire change events.
148
+ for (let i = 0; i < blocks.length; i++) {
149
+ blocks[i].renameVarById(variable.getId(), conflictVar.getId());
150
+ }
149
151
 
150
- // These blocks now refer to a different variable.
151
- // These will fire change events.
152
- for (let i = 0; i < blocks.length; i++) {
153
- blocks[i].renameVarById(variable.getId(), conflictVar.getId());
152
+ // Finally delete the original variable, which is now unreferenced.
153
+ eventUtils.fire(new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
154
+ // And remove it from the list.
155
+ arrayUtils.removeElem(this.variableMap_[type], variable);
154
156
  }
155
-
156
- // Finally delete the original variable, which is now unreferenced.
157
- eventUtils.fire(new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
158
- // And remove it from the list.
159
- arrayUtils.removeElem(this.variableMap_[type], variable);
160
- };
161
-
162
- /* End functions for renaming variables. */
163
-
164
- /**
165
- * Create a variable with a given name, optional type, and optional ID.
166
- * @param {string} name The name of the variable. This must be unique across
167
- * variables and procedures.
168
- * @param {?string=} opt_type The type of the variable like 'int' or 'string'.
169
- * Does not need to be unique. Field_variable can filter variables based on
170
- * their type. This will default to '' which is a specific type.
171
- * @param {?string=} opt_id The unique ID of the variable. This will default to
172
- * a UUID.
173
- * @return {!VariableModel} The newly created variable.
174
- */
175
- VariableMap.prototype.createVariable = function(name, opt_type, opt_id) {
176
- let variable = this.getVariable(name, opt_type);
177
- if (variable) {
178
- if (opt_id && variable.getId() !== opt_id) {
179
- throw Error(
180
- 'Variable "' + name + '" is already in use and its id is "' +
181
- variable.getId() + '" which conflicts with the passed in ' +
182
- 'id, "' + opt_id + '".');
157
+ /* End functions for renaming variables. */
158
+ /**
159
+ * Create a variable with a given name, optional type, and optional ID.
160
+ * @param {string} name The name of the variable. This must be unique across
161
+ * variables and procedures.
162
+ * @param {?string=} opt_type The type of the variable like 'int' or 'string'.
163
+ * Does not need to be unique. Field_variable can filter variables based
164
+ * on their type. This will default to '' which is a specific type.
165
+ * @param {?string=} opt_id The unique ID of the variable. This will default
166
+ * to a UUID.
167
+ * @return {!VariableModel} The newly created variable.
168
+ */
169
+ createVariable(name, opt_type, opt_id) {
170
+ let variable = this.getVariable(name, opt_type);
171
+ if (variable) {
172
+ if (opt_id && variable.getId() !== opt_id) {
173
+ throw Error(
174
+ 'Variable "' + name + '" is already in use and its id is "' +
175
+ variable.getId() + '" which conflicts with the passed in ' +
176
+ 'id, "' + opt_id + '".');
177
+ }
178
+ // The variable already exists and has the same ID.
179
+ return variable;
183
180
  }
184
- // The variable already exists and has the same ID.
185
- return variable;
186
- }
187
- if (opt_id && this.getVariableById(opt_id)) {
188
- throw Error('Variable id, "' + opt_id + '", is already in use.');
189
- }
190
- const id = opt_id || idGenerator.genUid();
191
- const type = opt_type || '';
192
- variable = new VariableModel(this.workspace, name, type, id);
193
-
194
- const variables = this.variableMap_[type] || [];
195
- variables.push(variable);
196
- // Delete the list of variables of this type, and re-add it so that
197
- // the most recent addition is at the end.
198
- // This is used so the toolbox's set block is set to the most recent variable.
199
- delete this.variableMap_[type];
200
- this.variableMap_[type] = variables;
201
-
202
- return variable;
203
- };
204
-
205
- /* Begin functions for variable deletion. */
206
-
207
- /**
208
- * Delete a variable.
209
- * @param {!VariableModel} variable Variable to delete.
210
- */
211
- VariableMap.prototype.deleteVariable = function(variable) {
212
- const variableId = variable.getId();
213
- const variableList = this.variableMap_[variable.type];
214
- for (let i = 0; i < variableList.length; i++) {
215
- const tempVar = variableList[i];
216
- if (tempVar.getId() === variableId) {
217
- variableList.splice(i, 1);
218
- eventUtils.fire(new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
219
- return;
181
+ if (opt_id && this.getVariableById(opt_id)) {
182
+ throw Error('Variable id, "' + opt_id + '", is already in use.');
220
183
  }
221
- }
222
- };
184
+ const id = opt_id || idGenerator.genUid();
185
+ const type = opt_type || '';
186
+ variable = new VariableModel(this.workspace, name, type, id);
187
+
188
+ const variables = this.variableMap_[type] || [];
189
+ variables.push(variable);
190
+ // Delete the list of variables of this type, and re-add it so that
191
+ // the most recent addition is at the end.
192
+ // This is used so the toolbox's set block is set to the most recent
193
+ // variable.
194
+ delete this.variableMap_[type];
195
+ this.variableMap_[type] = variables;
223
196
 
224
- /**
225
- * Delete a variables by the passed in ID and all of its uses from this
226
- * workspace. May prompt the user for confirmation.
227
- * @param {string} id ID of variable to delete.
228
- */
229
- VariableMap.prototype.deleteVariableById = function(id) {
230
- const variable = this.getVariableById(id);
231
- if (variable) {
232
- // Check whether this variable is a function parameter before deleting.
233
- const variableName = variable.name;
234
- const uses = this.getVariableUsesById(id);
235
- for (let i = 0, block; (block = uses[i]); i++) {
236
- if (block.type === 'procedures_defnoreturn' ||
237
- block.type === 'procedures_defreturn') {
238
- const procedureName = String(block.getFieldValue('NAME'));
239
- const deleteText = Msg['CANNOT_DELETE_VARIABLE_PROCEDURE']
240
- .replace('%1', variableName)
241
- .replace('%2', procedureName);
242
- dialog.alert(deleteText);
197
+ return variable;
198
+ }
199
+ /* Begin functions for variable deletion. */
200
+ /**
201
+ * Delete a variable.
202
+ * @param {!VariableModel} variable Variable to delete.
203
+ */
204
+ deleteVariable(variable) {
205
+ const variableId = variable.getId();
206
+ const variableList = this.variableMap_[variable.type];
207
+ for (let i = 0; i < variableList.length; i++) {
208
+ const tempVar = variableList[i];
209
+ if (tempVar.getId() === variableId) {
210
+ variableList.splice(i, 1);
211
+ eventUtils.fire(new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
243
212
  return;
244
213
  }
245
214
  }
246
-
247
- const map = this;
248
- if (uses.length > 1) {
249
- // Confirm before deleting multiple blocks.
250
- const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION']
251
- .replace('%1', String(uses.length))
252
- .replace('%2', variableName);
253
- dialog.confirm(confirmText, function(ok) {
254
- if (ok && variable) {
255
- map.deleteVariableInternal(variable, uses);
215
+ }
216
+ /**
217
+ * Delete a variables by the passed in ID and all of its uses from this
218
+ * workspace. May prompt the user for confirmation.
219
+ * @param {string} id ID of variable to delete.
220
+ */
221
+ deleteVariableById(id) {
222
+ const variable = this.getVariableById(id);
223
+ if (variable) {
224
+ // Check whether this variable is a function parameter before deleting.
225
+ const variableName = variable.name;
226
+ const uses = this.getVariableUsesById(id);
227
+ for (let i = 0, block; (block = uses[i]); i++) {
228
+ if (block.type === 'procedures_defnoreturn' ||
229
+ block.type === 'procedures_defreturn') {
230
+ const procedureName = String(block.getFieldValue('NAME'));
231
+ const deleteText = Msg['CANNOT_DELETE_VARIABLE_PROCEDURE']
232
+ .replace('%1', variableName)
233
+ .replace('%2', procedureName);
234
+ dialog.alert(deleteText);
235
+ return;
256
236
  }
257
- });
237
+ }
238
+
239
+ const map = this;
240
+ if (uses.length > 1) {
241
+ // Confirm before deleting multiple blocks.
242
+ const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION']
243
+ .replace('%1', String(uses.length))
244
+ .replace('%2', variableName);
245
+ dialog.confirm(confirmText, function(ok) {
246
+ if (ok && variable) {
247
+ map.deleteVariableInternal(variable, uses);
248
+ }
249
+ });
250
+ } else {
251
+ // No confirmation necessary for a single block.
252
+ map.deleteVariableInternal(variable, uses);
253
+ }
258
254
  } else {
259
- // No confirmation necessary for a single block.
260
- map.deleteVariableInternal(variable, uses);
255
+ console.warn('Can\'t delete non-existent variable: ' + id);
261
256
  }
262
- } else {
263
- console.warn('Can\'t delete non-existent variable: ' + id);
264
- }
265
- };
266
-
267
- /**
268
- * Deletes a variable and all of its uses from this workspace without asking the
269
- * user for confirmation.
270
- * @param {!VariableModel} variable Variable to delete.
271
- * @param {!Array<!Block>} uses An array of uses of the variable.
272
- * @package
273
- */
274
- VariableMap.prototype.deleteVariableInternal = function(variable, uses) {
275
- const existingGroup = eventUtils.getGroup();
276
- if (!existingGroup) {
277
- eventUtils.setGroup(true);
278
257
  }
279
- try {
280
- for (let i = 0; i < uses.length; i++) {
281
- uses[i].dispose(true);
282
- }
283
- this.deleteVariable(variable);
284
- } finally {
258
+ /**
259
+ * Deletes a variable and all of its uses from this workspace without asking
260
+ * the user for confirmation.
261
+ * @param {!VariableModel} variable Variable to delete.
262
+ * @param {!Array<!Block>} uses An array of uses of the variable.
263
+ * @package
264
+ */
265
+ deleteVariableInternal(variable, uses) {
266
+ const existingGroup = eventUtils.getGroup();
285
267
  if (!existingGroup) {
286
- eventUtils.setGroup(false);
268
+ eventUtils.setGroup(true);
287
269
  }
288
- }
289
- };
290
-
291
- /* End functions for variable deletion. */
292
-
293
- /**
294
- * Find the variable by the given name and type and return it. Return null if
295
- * it is not found.
296
- * @param {string} name The name to check for.
297
- * @param {?string=} opt_type The type of the variable. If not provided it
298
- * defaults to the empty string, which is a specific type.
299
- * @return {?VariableModel} The variable with the given name, or null if
300
- * it was not found.
301
- */
302
- VariableMap.prototype.getVariable = function(name, opt_type) {
303
- const type = opt_type || '';
304
- const list = this.variableMap_[type];
305
- if (list) {
306
- for (let j = 0, variable; (variable = list[j]); j++) {
307
- if (Names.equals(variable.name, name)) {
308
- return variable;
270
+ try {
271
+ for (let i = 0; i < uses.length; i++) {
272
+ uses[i].dispose(true);
273
+ }
274
+ this.deleteVariable(variable);
275
+ } finally {
276
+ if (!existingGroup) {
277
+ eventUtils.setGroup(false);
309
278
  }
310
279
  }
311
280
  }
312
- return null;
313
- };
314
-
315
- /**
316
- * Find the variable by the given ID and return it. Return null if not found.
317
- * @param {string} id The ID to check for.
318
- * @return {?VariableModel} The variable with the given ID.
319
- */
320
- VariableMap.prototype.getVariableById = function(id) {
321
- const keys = Object.keys(this.variableMap_);
322
- for (let i = 0; i < keys.length; i++) {
323
- const key = keys[i];
324
- for (let j = 0, variable; (variable = this.variableMap_[key][j]); j++) {
325
- if (variable.getId() === id) {
326
- return variable;
281
+ /* End functions for variable deletion. */
282
+ /**
283
+ * Find the variable by the given name and type and return it. Return null if
284
+ * it is not found.
285
+ * @param {string} name The name to check for.
286
+ * @param {?string=} opt_type The type of the variable. If not provided it
287
+ * defaults to the empty string, which is a specific type.
288
+ * @return {?VariableModel} The variable with the given name, or null if
289
+ * it was not found.
290
+ */
291
+ getVariable(name, opt_type) {
292
+ const type = opt_type || '';
293
+ const list = this.variableMap_[type];
294
+ if (list) {
295
+ for (let j = 0, variable; (variable = list[j]); j++) {
296
+ if (Names.equals(variable.name, name)) {
297
+ return variable;
298
+ }
327
299
  }
328
300
  }
301
+ return null;
329
302
  }
330
- return null;
331
- };
332
-
333
- /**
334
- * Get a list containing all of the variables of a specified type. If type is
335
- * null, return list of variables with empty string type.
336
- * @param {?string} type Type of the variables to find.
337
- * @return {!Array<!VariableModel>} The sought after variables of the
338
- * passed in type. An empty array if none are found.
339
- */
340
- VariableMap.prototype.getVariablesOfType = function(type) {
341
- type = type || '';
342
- const variableList = this.variableMap_[type];
343
- if (variableList) {
344
- return variableList.slice();
345
- }
346
- return [];
347
- };
348
-
349
- /**
350
- * Return all variable and potential variable types. This list always contains
351
- * the empty string.
352
- * @param {?Workspace} ws The workspace used to look for potential
353
- * variables. This can be different than the workspace stored on this object
354
- * if the passed in ws is a flyout workspace.
355
- * @return {!Array<string>} List of variable types.
356
- * @package
357
- */
358
- VariableMap.prototype.getVariableTypes = function(ws) {
359
- const variableMap = {};
360
- object.mixin(variableMap, this.variableMap_);
361
- if (ws && ws.getPotentialVariableMap()) {
362
- object.mixin(variableMap, ws.getPotentialVariableMap().variableMap_);
303
+ /**
304
+ * Find the variable by the given ID and return it. Return null if not found.
305
+ * @param {string} id The ID to check for.
306
+ * @return {?VariableModel} The variable with the given ID.
307
+ */
308
+ getVariableById(id) {
309
+ const keys = Object.keys(this.variableMap_);
310
+ for (let i = 0; i < keys.length; i++) {
311
+ const key = keys[i];
312
+ for (let j = 0, variable; (variable = this.variableMap_[key][j]); j++) {
313
+ if (variable.getId() === id) {
314
+ return variable;
315
+ }
316
+ }
317
+ }
318
+ return null;
363
319
  }
364
- const types = Object.keys(variableMap);
365
- let hasEmpty = false;
366
- for (let i = 0; i < types.length; i++) {
367
- if (types[i] === '') {
368
- hasEmpty = true;
320
+ /**
321
+ * Get a list containing all of the variables of a specified type. If type is
322
+ * null, return list of variables with empty string type.
323
+ * @param {?string} type Type of the variables to find.
324
+ * @return {!Array<!VariableModel>} The sought after variables of the
325
+ * passed in type. An empty array if none are found.
326
+ */
327
+ getVariablesOfType(type) {
328
+ type = type || '';
329
+ const variableList = this.variableMap_[type];
330
+ if (variableList) {
331
+ return variableList.slice();
369
332
  }
333
+ return [];
370
334
  }
371
- if (!hasEmpty) {
372
- types.push('');
335
+ /**
336
+ * Return all variable and potential variable types. This list always
337
+ * contains the empty string.
338
+ * @param {?Workspace} ws The workspace used to look for potential
339
+ * variables. This can be different than the workspace stored on this object
340
+ * if the passed in ws is a flyout workspace.
341
+ * @return {!Array<string>} List of variable types.
342
+ * @package
343
+ */
344
+ getVariableTypes(ws) {
345
+ const variableMap = {};
346
+ object.mixin(variableMap, this.variableMap_);
347
+ if (ws && ws.getPotentialVariableMap()) {
348
+ object.mixin(variableMap, ws.getPotentialVariableMap().variableMap_);
349
+ }
350
+ const types = Object.keys(variableMap);
351
+ let hasEmpty = false;
352
+ for (let i = 0; i < types.length; i++) {
353
+ if (types[i] === '') {
354
+ hasEmpty = true;
355
+ }
356
+ }
357
+ if (!hasEmpty) {
358
+ types.push('');
359
+ }
360
+ return types;
373
361
  }
374
- return types;
375
- };
376
-
377
- /**
378
- * Return all variables of all types.
379
- * @return {!Array<!VariableModel>} List of variable models.
380
- */
381
- VariableMap.prototype.getAllVariables = function() {
382
- let allVariables = [];
383
- for (const key in this.variableMap_) {
384
- allVariables = allVariables.concat(this.variableMap_[key]);
362
+ /**
363
+ * Return all variables of all types.
364
+ * @return {!Array<!VariableModel>} List of variable models.
365
+ */
366
+ getAllVariables() {
367
+ let allVariables = [];
368
+ for (const key in this.variableMap_) {
369
+ allVariables = allVariables.concat(this.variableMap_[key]);
370
+ }
371
+ return allVariables;
385
372
  }
386
- return allVariables;
387
- };
388
-
389
- /**
390
- * Returns all of the variable names of all types.
391
- * @return {!Array<string>} All of the variable names of all types.
392
- */
393
- VariableMap.prototype.getAllVariableNames = function() {
394
- const allNames = [];
395
- for (const key in this.variableMap_) {
396
- const variables = this.variableMap_[key];
397
- for (let i = 0, variable; (variable = variables[i]); i++) {
398
- allNames.push(variable.name);
373
+ /**
374
+ * Returns all of the variable names of all types.
375
+ * @return {!Array<string>} All of the variable names of all types.
376
+ */
377
+ getAllVariableNames() {
378
+ const allNames = [];
379
+ for (const key in this.variableMap_) {
380
+ const variables = this.variableMap_[key];
381
+ for (let i = 0, variable; (variable = variables[i]); i++) {
382
+ allNames.push(variable.name);
383
+ }
399
384
  }
385
+ return allNames;
400
386
  }
401
- return allNames;
402
- };
403
-
404
- /**
405
- * Find all the uses of a named variable.
406
- * @param {string} id ID of the variable to find.
407
- * @return {!Array<!Block>} Array of block usages.
408
- */
409
- VariableMap.prototype.getVariableUsesById = function(id) {
410
- const uses = [];
411
- const blocks = this.workspace.getAllBlocks(false);
412
- // Iterate through every block and check the name.
413
- for (let i = 0; i < blocks.length; i++) {
414
- const blockVariables = blocks[i].getVarModels();
415
- if (blockVariables) {
416
- for (let j = 0; j < blockVariables.length; j++) {
417
- if (blockVariables[j].getId() === id) {
418
- uses.push(blocks[i]);
387
+ /**
388
+ * Find all the uses of a named variable.
389
+ * @param {string} id ID of the variable to find.
390
+ * @return {!Array<!Block>} Array of block usages.
391
+ */
392
+ getVariableUsesById(id) {
393
+ const uses = [];
394
+ const blocks = this.workspace.getAllBlocks(false);
395
+ // Iterate through every block and check the name.
396
+ for (let i = 0; i < blocks.length; i++) {
397
+ const blockVariables = blocks[i].getVarModels();
398
+ if (blockVariables) {
399
+ for (let j = 0; j < blockVariables.length; j++) {
400
+ if (blockVariables[j].getId() === id) {
401
+ uses.push(blocks[i]);
402
+ }
419
403
  }
420
404
  }
421
405
  }
406
+ return uses;
422
407
  }
423
- return uses;
424
- };
408
+ }
425
409
 
426
410
  exports.VariableMap = VariableMap;