@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,875 @@
1
+ /**
2
+ * @license
3
+ * Visual Blocks Editor
4
+ *
5
+ * Copyright 2011 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 Library for creating scrollbars.
23
+ * @author fraser@google.com (Neil Fraser)
24
+ */
25
+ 'use strict';
26
+
27
+ goog.provide('Blockly.Scrollbar');
28
+ goog.provide('Blockly.ScrollbarPair');
29
+
30
+ goog.require('goog.dom');
31
+ goog.require('goog.events');
32
+
33
+
34
+ /**
35
+ * A note on units: most of the numbers that are in CSS pixels are scaled if the
36
+ * scrollbar is in a mutator.
37
+ */
38
+
39
+ /**
40
+ * Class for a pair of scrollbars. Horizontal and vertical.
41
+ * @param {!Blockly.Workspace} workspace Workspace to bind the scrollbars to.
42
+ * @constructor
43
+ */
44
+ Blockly.ScrollbarPair = function(workspace) {
45
+ this.workspace_ = workspace;
46
+ this.hScroll = new Blockly.Scrollbar(
47
+ workspace, true, true, 'blocklyMainWorkspaceScrollbar');
48
+ this.vScroll = new Blockly.Scrollbar(
49
+ workspace, false, true, 'blocklyMainWorkspaceScrollbar');
50
+ this.corner_ = Blockly.utils.createSvgElement(
51
+ 'rect',
52
+ {
53
+ 'height': Blockly.Scrollbar.scrollbarThickness,
54
+ 'width': Blockly.Scrollbar.scrollbarThickness,
55
+ 'class': 'blocklyScrollbarBackground'
56
+ },
57
+ null);
58
+ Blockly.utils.insertAfter(this.corner_, workspace.getBubbleCanvas());
59
+ };
60
+
61
+ /**
62
+ * Previously recorded metrics from the workspace.
63
+ * @type {Object}
64
+ * @private
65
+ */
66
+ Blockly.ScrollbarPair.prototype.oldHostMetrics_ = null;
67
+
68
+ /**
69
+ * Dispose of this pair of scrollbars.
70
+ * Unlink from all DOM elements to prevent memory leaks.
71
+ */
72
+ Blockly.ScrollbarPair.prototype.dispose = function() {
73
+ goog.dom.removeNode(this.corner_);
74
+ this.corner_ = null;
75
+ this.workspace_ = null;
76
+ this.oldHostMetrics_ = null;
77
+ this.hScroll.dispose();
78
+ this.hScroll = null;
79
+ this.vScroll.dispose();
80
+ this.vScroll = null;
81
+ };
82
+
83
+ /**
84
+ * Recalculate both of the scrollbars' locations and lengths.
85
+ * Also reposition the corner rectangle.
86
+ */
87
+ Blockly.ScrollbarPair.prototype.resize = function() {
88
+ // Look up the host metrics once, and use for both scrollbars.
89
+ var hostMetrics = this.workspace_.getMetrics();
90
+ if (!hostMetrics) {
91
+ // Host element is likely not visible.
92
+ return;
93
+ }
94
+
95
+ // Only change the scrollbars if there has been a change in metrics.
96
+ var resizeH = false;
97
+ var resizeV = false;
98
+ if (!this.oldHostMetrics_ ||
99
+ this.oldHostMetrics_.viewWidth != hostMetrics.viewWidth ||
100
+ this.oldHostMetrics_.viewHeight != hostMetrics.viewHeight ||
101
+ this.oldHostMetrics_.absoluteTop != hostMetrics.absoluteTop ||
102
+ this.oldHostMetrics_.absoluteLeft != hostMetrics.absoluteLeft) {
103
+ // The window has been resized or repositioned.
104
+ resizeH = true;
105
+ resizeV = true;
106
+ } else {
107
+ // Has the content been resized or moved?
108
+ if (!this.oldHostMetrics_ ||
109
+ this.oldHostMetrics_.contentWidth != hostMetrics.contentWidth ||
110
+ this.oldHostMetrics_.viewLeft != hostMetrics.viewLeft ||
111
+ this.oldHostMetrics_.contentLeft != hostMetrics.contentLeft) {
112
+ resizeH = true;
113
+ }
114
+ if (!this.oldHostMetrics_ ||
115
+ this.oldHostMetrics_.contentHeight != hostMetrics.contentHeight ||
116
+ this.oldHostMetrics_.viewTop != hostMetrics.viewTop ||
117
+ this.oldHostMetrics_.contentTop != hostMetrics.contentTop) {
118
+ resizeV = true;
119
+ }
120
+ }
121
+ if (resizeH) {
122
+ this.hScroll.resize(hostMetrics);
123
+ }
124
+ if (resizeV) {
125
+ this.vScroll.resize(hostMetrics);
126
+ }
127
+
128
+ // Reposition the corner square.
129
+ if (!this.oldHostMetrics_ ||
130
+ this.oldHostMetrics_.viewWidth != hostMetrics.viewWidth ||
131
+ this.oldHostMetrics_.absoluteLeft != hostMetrics.absoluteLeft) {
132
+ this.corner_.setAttribute('x', this.vScroll.position_.x);
133
+ }
134
+ if (!this.oldHostMetrics_ ||
135
+ this.oldHostMetrics_.viewHeight != hostMetrics.viewHeight ||
136
+ this.oldHostMetrics_.absoluteTop != hostMetrics.absoluteTop) {
137
+ this.corner_.setAttribute('y', this.hScroll.position_.y);
138
+ }
139
+
140
+ // Cache the current metrics to potentially short-cut the next resize event.
141
+ this.oldHostMetrics_ = hostMetrics;
142
+ };
143
+
144
+ /**
145
+ * Set the handles of both scrollbars to be at a certain position in CSS pixels
146
+ * relative to their parents.
147
+ * @param {number} x Horizontal scroll value.
148
+ * @param {number} y Vertical scroll value.
149
+ */
150
+ Blockly.ScrollbarPair.prototype.set = function(x, y) {
151
+ // This function is equivalent to:
152
+ // this.hScroll.set(x);
153
+ // this.vScroll.set(y);
154
+ // However, that calls setMetrics twice which causes a chain of
155
+ // getAttribute->setAttribute->getAttribute resulting in an extra layout pass.
156
+ // Combining them speeds up rendering.
157
+ var xyRatio = {};
158
+
159
+ var hHandlePosition = x * this.hScroll.ratio_;
160
+ var vHandlePosition = y * this.vScroll.ratio_;
161
+
162
+ var hBarLength = this.hScroll.scrollViewSize_;
163
+ var vBarLength = this.vScroll.scrollViewSize_;
164
+
165
+ xyRatio.x = this.getRatio_(hHandlePosition, hBarLength);
166
+ xyRatio.y = this.getRatio_(vHandlePosition, vBarLength);
167
+ this.workspace_.setMetrics(xyRatio);
168
+
169
+ this.hScroll.setHandlePosition(hHandlePosition);
170
+ this.vScroll.setHandlePosition(vHandlePosition);
171
+ };
172
+
173
+ /**
174
+ * Helper to calculate the ratio of handle position to scrollbar view size.
175
+ * @param {number} handlePosition The value of the handle.
176
+ * @param {number} viewSize The total size of the scrollbar's view.
177
+ * @return {number} Ratio.
178
+ * @private
179
+ */
180
+ Blockly.ScrollbarPair.prototype.getRatio_ = function(handlePosition, viewSize) {
181
+ var ratio = handlePosition / viewSize;
182
+ if (isNaN(ratio)) {
183
+ return 0;
184
+ }
185
+ return ratio;
186
+ };
187
+
188
+ // --------------------------------------------------------------------
189
+
190
+ /**
191
+ * Class for a pure SVG scrollbar.
192
+ * This technique offers a scrollbar that is guaranteed to work, but may not
193
+ * look or behave like the system's scrollbars.
194
+ * @param {!Blockly.Workspace} workspace Workspace to bind the scrollbar to.
195
+ * @param {boolean} horizontal True if horizontal, false if vertical.
196
+ * @param {boolean=} opt_pair True if scrollbar is part of a horiz/vert pair.
197
+ * @param {string=} opt_class A class to be applied to this scrollbar.
198
+ * @constructor
199
+ */
200
+ Blockly.Scrollbar = function(workspace, horizontal, opt_pair, opt_class) {
201
+ this.workspace_ = workspace;
202
+ this.pair_ = opt_pair || false;
203
+ this.horizontal_ = horizontal;
204
+ this.oldHostMetrics_ = null;
205
+
206
+ this.createDom_(opt_class);
207
+
208
+ /**
209
+ * The upper left corner of the scrollbar's SVG group in CSS pixels relative
210
+ * to the scrollbar's origin. This is usually relative to the injection div
211
+ * origin.
212
+ * @type {goog.math.Coordinate}
213
+ * @private
214
+ */
215
+ this.position_ = new goog.math.Coordinate(0, 0);
216
+
217
+ // Store the thickness in a temp variable for readability.
218
+ var scrollbarThickness = Blockly.Scrollbar.scrollbarThickness;
219
+ if (horizontal) {
220
+ this.svgBackground_.setAttribute('height', scrollbarThickness);
221
+ this.outerSvg_.setAttribute('height', scrollbarThickness);
222
+ this.svgHandle_.setAttribute('height', scrollbarThickness - 5);
223
+ this.svgHandle_.setAttribute('y', 2.5);
224
+
225
+ this.lengthAttribute_ = 'width';
226
+ this.positionAttribute_ = 'x';
227
+ } else {
228
+ this.svgBackground_.setAttribute('width', scrollbarThickness);
229
+ this.outerSvg_.setAttribute('width', scrollbarThickness);
230
+ this.svgHandle_.setAttribute('width', scrollbarThickness - 5);
231
+ this.svgHandle_.setAttribute('x', 2.5);
232
+
233
+ this.lengthAttribute_ = 'height';
234
+ this.positionAttribute_ = 'y';
235
+ }
236
+ var scrollbar = this;
237
+ this.onMouseDownBarWrapper_ = Blockly.bindEventWithChecks_(
238
+ this.svgBackground_, 'mousedown', scrollbar, scrollbar.onMouseDownBar_);
239
+ this.onMouseDownHandleWrapper_ = Blockly.bindEventWithChecks_(this.svgHandle_,
240
+ 'mousedown', scrollbar, scrollbar.onMouseDownHandle_);
241
+ };
242
+
243
+ /**
244
+ * The location of the origin of the workspace that the scrollbar is in,
245
+ * measured in CSS pixels relative to the injection div origin. This is usually
246
+ * (0, 0). When the scrollbar is in a flyout it may have a different origin.
247
+ * @type {goog.math.Coordinate}
248
+ * @private
249
+ */
250
+ Blockly.Scrollbar.prototype.origin_ = new goog.math.Coordinate(0, 0);
251
+
252
+ /**
253
+ * Whether or not the origin of the scrollbar has changed. Used
254
+ * to help decide whether or not the reflow/resize calls need to happen.
255
+ * @type {boolean}
256
+ * @private
257
+ */
258
+ Blockly.Scrollbar.prototype.originHasChanged_ = true;
259
+
260
+ /**
261
+ * The size of the area within which the scrollbar handle can move, in CSS
262
+ * pixels.
263
+ * @type {number}
264
+ * @private
265
+ */
266
+ Blockly.Scrollbar.prototype.scrollViewSize_ = 0;
267
+
268
+ /**
269
+ * The length of the scrollbar handle in CSS pixels.
270
+ * @type {number}
271
+ * @private
272
+ */
273
+ Blockly.Scrollbar.prototype.handleLength_ = 0;
274
+
275
+ /**
276
+ * The offset of the start of the handle from the scrollbar position, in CSS
277
+ * pixels.
278
+ * @type {number}
279
+ * @private
280
+ */
281
+ Blockly.Scrollbar.prototype.handlePosition_ = 0;
282
+
283
+ /**
284
+ * Whether the scrollbar handle is visible.
285
+ * @type {boolean}
286
+ * @private
287
+ */
288
+ Blockly.Scrollbar.prototype.isVisible_ = true;
289
+
290
+ /**
291
+ * Whether the workspace containing this scrollbar is visible.
292
+ * @type {boolean}
293
+ * @private
294
+ */
295
+ Blockly.Scrollbar.prototype.containerVisible_ = true;
296
+
297
+ /**
298
+ * Width of vertical scrollbar or height of horizontal scrollbar in CSS pixels.
299
+ * Scrollbars should be larger on touch devices.
300
+ */
301
+ Blockly.Scrollbar.scrollbarThickness = 11;
302
+ if (goog.events.BrowserFeature.TOUCH_ENABLED) {
303
+ Blockly.Scrollbar.scrollbarThickness = 14;
304
+ }
305
+
306
+ /**
307
+ * @param {!Object} first An object containing computed measurements of a
308
+ * workspace.
309
+ * @param {!Object} second Another object containing computed measurements of a
310
+ * workspace.
311
+ * @return {boolean} Whether the two sets of metrics are equivalent.
312
+ * @private
313
+ */
314
+ Blockly.Scrollbar.metricsAreEquivalent_ = function(first, second) {
315
+ if (!(first && second)) {
316
+ return false;
317
+ }
318
+
319
+ if (first.viewWidth != second.viewWidth ||
320
+ first.viewHeight != second.viewHeight ||
321
+ first.viewLeft != second.viewLeft ||
322
+ first.viewTop != second.viewTop ||
323
+ first.absoluteTop != second.absoluteTop ||
324
+ first.absoluteLeft != second.absoluteLeft ||
325
+ first.contentWidth != second.contentWidth ||
326
+ first.contentHeight != second.contentHeight ||
327
+ first.contentLeft != second.contentLeft ||
328
+ first.contentTop != second.contentTop) {
329
+ return false;
330
+ }
331
+
332
+ return true;
333
+ };
334
+
335
+ /**
336
+ * Dispose of this scrollbar.
337
+ * Unlink from all DOM elements to prevent memory leaks.
338
+ */
339
+ Blockly.Scrollbar.prototype.dispose = function() {
340
+ this.cleanUp_();
341
+ Blockly.unbindEvent_(this.onMouseDownBarWrapper_);
342
+ this.onMouseDownBarWrapper_ = null;
343
+ Blockly.unbindEvent_(this.onMouseDownHandleWrapper_);
344
+ this.onMouseDownHandleWrapper_ = null;
345
+
346
+ goog.dom.removeNode(this.outerSvg_);
347
+ this.outerSvg_ = null;
348
+ this.svgGroup_ = null;
349
+ this.svgBackground_ = null;
350
+ this.svgHandle_ = null;
351
+ this.workspace_ = null;
352
+ };
353
+
354
+ /**
355
+ * Set the length of the scrollbar's handle and change the SVG attribute
356
+ * accordingly.
357
+ * @param {number} newLength The new scrollbar handle length in CSS pixels.
358
+ */
359
+ Blockly.Scrollbar.prototype.setHandleLength_ = function(newLength) {
360
+ this.handleLength_ = newLength;
361
+ this.svgHandle_.setAttribute(this.lengthAttribute_, this.handleLength_);
362
+ };
363
+
364
+ /**
365
+ * Set the offset of the scrollbar's handle from the scrollbar's position, and
366
+ * change the SVG attribute accordingly.
367
+ * @param {number} newPosition The new scrollbar handle offset in CSS pixels.
368
+ */
369
+ Blockly.Scrollbar.prototype.setHandlePosition = function(newPosition) {
370
+ this.handlePosition_ = newPosition;
371
+ this.svgHandle_.setAttribute(this.positionAttribute_, this.handlePosition_);
372
+ };
373
+
374
+ /**
375
+ * Set the size of the scrollbar's background and change the SVG attribute
376
+ * accordingly.
377
+ * @param {number} newSize The new scrollbar background length in CSS pixels.
378
+ * @private
379
+ */
380
+ Blockly.Scrollbar.prototype.setScrollViewSize_ = function(newSize) {
381
+ this.scrollViewSize_ = newSize;
382
+ this.outerSvg_.setAttribute(this.lengthAttribute_, this.scrollViewSize_);
383
+ this.svgBackground_.setAttribute(this.lengthAttribute_, this.scrollViewSize_);
384
+ };
385
+
386
+ /**
387
+ * Set whether this scrollbar's container is visible.
388
+ * @param {boolean} visible Whether the container is visible.
389
+ */
390
+ Blockly.ScrollbarPair.prototype.setContainerVisible = function(visible) {
391
+ this.hScroll.setContainerVisible(visible);
392
+ this.vScroll.setContainerVisible(visible);
393
+ };
394
+
395
+ /**
396
+ * Set the position of the scrollbar's SVG group in CSS pixels relative to the
397
+ * scrollbar's origin. This sets the scrollbar's location within the workspace.
398
+ * @param {number} x The new x coordinate.
399
+ * @param {number} y The new y coordinate.
400
+ * @private
401
+ */
402
+ Blockly.Scrollbar.prototype.setPosition_ = function(x, y) {
403
+ this.position_.x = x;
404
+ this.position_.y = y;
405
+
406
+ var tempX = this.position_.x + this.origin_.x;
407
+ var tempY = this.position_.y + this.origin_.y;
408
+ var transform = 'translate(' + tempX + 'px,' + tempY + 'px)';
409
+ Blockly.utils.setCssTransform(this.outerSvg_, transform);
410
+ };
411
+
412
+ /**
413
+ * Recalculate the scrollbar's location and its length.
414
+ * @param {Object=} opt_metrics A data structure of from the describing all the
415
+ * required dimensions. If not provided, it will be fetched from the host
416
+ * object.
417
+ */
418
+ Blockly.Scrollbar.prototype.resize = function(opt_metrics) {
419
+ // Determine the location, height and width of the host element.
420
+ var hostMetrics = opt_metrics;
421
+ if (!hostMetrics) {
422
+ hostMetrics = this.workspace_.getMetrics();
423
+ if (!hostMetrics) {
424
+ // Host element is likely not visible.
425
+ return;
426
+ }
427
+ }
428
+
429
+ // If the origin has changed (e.g. the toolbox is moving from start to end)
430
+ // we want to continue with the resize even if workspace metrics haven't.
431
+ if (this.originHasChanged_) {
432
+ this.originHasChanged_ = false;
433
+ } else if (Blockly.Scrollbar.metricsAreEquivalent_(hostMetrics,
434
+ this.oldHostMetrics_)) {
435
+ return;
436
+ }
437
+ this.oldHostMetrics_ = hostMetrics;
438
+
439
+ /* hostMetrics is an object with the following properties.
440
+ * .viewHeight: Height of the visible rectangle,
441
+ * .viewWidth: Width of the visible rectangle,
442
+ * .contentHeight: Height of the contents,
443
+ * .contentWidth: Width of the content,
444
+ * .viewTop: Offset of top edge of visible rectangle from parent,
445
+ * .viewLeft: Offset of left edge of visible rectangle from parent,
446
+ * .contentTop: Offset of the top-most content from the y=0 coordinate,
447
+ * .contentLeft: Offset of the left-most content from the x=0 coordinate,
448
+ * .absoluteTop: Top-edge of view.
449
+ * .absoluteLeft: Left-edge of view.
450
+ */
451
+ if (this.horizontal_) {
452
+ this.resizeHorizontal_(hostMetrics);
453
+ } else {
454
+ this.resizeVertical_(hostMetrics);
455
+ }
456
+ // Resizing may have caused some scrolling.
457
+ this.onScroll_();
458
+ };
459
+
460
+ /**
461
+ * Recalculate a horizontal scrollbar's location and length.
462
+ * @param {!Object} hostMetrics A data structure describing all the
463
+ * required dimensions, possibly fetched from the host object.
464
+ * @private
465
+ */
466
+ Blockly.Scrollbar.prototype.resizeHorizontal_ = function(hostMetrics) {
467
+ // TODO: Inspect metrics to determine if we can get away with just a content
468
+ // resize.
469
+ this.resizeViewHorizontal(hostMetrics);
470
+ };
471
+
472
+ /**
473
+ * Recalculate a horizontal scrollbar's location on the screen and path length.
474
+ * This should be called when the layout or size of the window has changed.
475
+ * @param {!Object} hostMetrics A data structure describing all the
476
+ * required dimensions, possibly fetched from the host object.
477
+ */
478
+ Blockly.Scrollbar.prototype.resizeViewHorizontal = function(hostMetrics) {
479
+ var viewSize = hostMetrics.viewWidth - 1;
480
+ if (this.pair_) {
481
+ // Shorten the scrollbar to make room for the corner square.
482
+ viewSize -= Blockly.Scrollbar.scrollbarThickness;
483
+ }
484
+ this.setScrollViewSize_(Math.max(0, viewSize));
485
+
486
+ var xCoordinate = hostMetrics.absoluteLeft + 0.5;
487
+ if (this.pair_ && this.workspace_.RTL) {
488
+ xCoordinate += Blockly.Scrollbar.scrollbarThickness;
489
+ }
490
+
491
+ // Horizontal toolbar should always be just above the bottom of the workspace.
492
+ var yCoordinate = hostMetrics.absoluteTop + hostMetrics.viewHeight -
493
+ Blockly.Scrollbar.scrollbarThickness - 0.5;
494
+ this.setPosition_(xCoordinate, yCoordinate);
495
+
496
+ // If the view has been resized, a content resize will also be necessary. The
497
+ // reverse is not true.
498
+ this.resizeContentHorizontal(hostMetrics);
499
+ };
500
+
501
+ /**
502
+ * Recalculate a horizontal scrollbar's location within its path and length.
503
+ * This should be called when the contents of the workspace have changed.
504
+ * @param {!Object} hostMetrics A data structure describing all the
505
+ * required dimensions, possibly fetched from the host object.
506
+ */
507
+ Blockly.Scrollbar.prototype.resizeContentHorizontal = function(hostMetrics) {
508
+ if (!this.pair_) {
509
+ // Only show the scrollbar if needed.
510
+ // Ideally this would also apply to scrollbar pairs, but that's a bigger
511
+ // headache (due to interactions with the corner square).
512
+ this.setVisible(this.scrollViewSize_ < hostMetrics.contentWidth);
513
+ }
514
+
515
+ this.ratio_ = this.scrollViewSize_ / hostMetrics.contentWidth;
516
+ if (this.ratio_ == -Infinity || this.ratio_ == Infinity ||
517
+ isNaN(this.ratio_)) {
518
+ this.ratio_ = 0;
519
+ }
520
+
521
+ var handleLength = hostMetrics.viewWidth * this.ratio_;
522
+ this.setHandleLength_(Math.max(0, handleLength));
523
+
524
+ var handlePosition = (hostMetrics.viewLeft - hostMetrics.contentLeft) *
525
+ this.ratio_;
526
+ this.setHandlePosition(this.constrainHandle_(handlePosition));
527
+ };
528
+
529
+ /**
530
+ * Recalculate a vertical scrollbar's location and length.
531
+ * @param {!Object} hostMetrics A data structure describing all the
532
+ * required dimensions, possibly fetched from the host object.
533
+ * @private
534
+ */
535
+ Blockly.Scrollbar.prototype.resizeVertical_ = function(hostMetrics) {
536
+ // TODO: Inspect metrics to determine if we can get away with just a content
537
+ // resize.
538
+ this.resizeViewVertical(hostMetrics);
539
+ };
540
+
541
+ /**
542
+ * Recalculate a vertical scrollbar's location on the screen and path length.
543
+ * This should be called when the layout or size of the window has changed.
544
+ * @param {!Object} hostMetrics A data structure describing all the
545
+ * required dimensions, possibly fetched from the host object.
546
+ */
547
+ Blockly.Scrollbar.prototype.resizeViewVertical = function(hostMetrics) {
548
+ var viewSize = hostMetrics.viewHeight - 1;
549
+ if (this.pair_) {
550
+ // Shorten the scrollbar to make room for the corner square.
551
+ viewSize -= Blockly.Scrollbar.scrollbarThickness;
552
+ }
553
+ this.setScrollViewSize_(Math.max(0, viewSize));
554
+
555
+ var xCoordinate = hostMetrics.absoluteLeft + 0.5;
556
+ if (!this.workspace_.RTL) {
557
+ xCoordinate += hostMetrics.viewWidth -
558
+ Blockly.Scrollbar.scrollbarThickness - 1;
559
+ }
560
+ var yCoordinate = hostMetrics.absoluteTop + 0.5;
561
+ this.setPosition_(xCoordinate, yCoordinate);
562
+
563
+ // If the view has been resized, a content resize will also be necessary. The
564
+ // reverse is not true.
565
+ this.resizeContentVertical(hostMetrics);
566
+ };
567
+
568
+ /**
569
+ * Recalculate a vertical scrollbar's location within its path and length.
570
+ * This should be called when the contents of the workspace have changed.
571
+ * @param {!Object} hostMetrics A data structure describing all the
572
+ * required dimensions, possibly fetched from the host object.
573
+ */
574
+ Blockly.Scrollbar.prototype.resizeContentVertical = function(hostMetrics) {
575
+ if (!this.pair_) {
576
+ // Only show the scrollbar if needed.
577
+ this.setVisible(this.scrollViewSize_ < hostMetrics.contentHeight);
578
+ }
579
+
580
+ this.ratio_ = this.scrollViewSize_ / hostMetrics.contentHeight;
581
+ if (this.ratio_ == -Infinity || this.ratio_ == Infinity ||
582
+ isNaN(this.ratio_)) {
583
+ this.ratio_ = 0;
584
+ }
585
+
586
+ var handleLength = hostMetrics.viewHeight * this.ratio_;
587
+ this.setHandleLength_(Math.max(0, handleLength));
588
+
589
+ var handlePosition = (hostMetrics.viewTop - hostMetrics.contentTop) *
590
+ this.ratio_;
591
+ this.setHandlePosition(this.constrainHandle_(handlePosition));
592
+ };
593
+
594
+ /**
595
+ * Create all the DOM elements required for a scrollbar.
596
+ * The resulting widget is not sized.
597
+ * @param {string=} opt_class A class to be applied to this scrollbar.
598
+ * @private
599
+ */
600
+ Blockly.Scrollbar.prototype.createDom_ = function(opt_class) {
601
+ /* Create the following DOM:
602
+ <svg class="blocklyScrollbarHorizontal optionalClass">
603
+ <g>
604
+ <rect class="blocklyScrollbarBackground" />
605
+ <rect class="blocklyScrollbarHandle" rx="8" ry="8" />
606
+ </g>
607
+ </svg>
608
+ */
609
+ var className = 'blocklyScrollbar' +
610
+ (this.horizontal_ ? 'Horizontal' : 'Vertical');
611
+ if (opt_class) {
612
+ className += ' ' + opt_class;
613
+ }
614
+ this.outerSvg_ = Blockly.utils.createSvgElement(
615
+ 'svg', {'class': className}, null);
616
+ this.svgGroup_ = Blockly.utils.createSvgElement('g', {}, this.outerSvg_);
617
+ this.svgBackground_ = Blockly.utils.createSvgElement(
618
+ 'rect', {'class': 'blocklyScrollbarBackground'}, this.svgGroup_);
619
+ var radius = Math.floor((Blockly.Scrollbar.scrollbarThickness - 5) / 2);
620
+ this.svgHandle_ = Blockly.utils.createSvgElement(
621
+ 'rect',
622
+ {
623
+ 'class': 'blocklyScrollbarHandle',
624
+ 'rx': radius,
625
+ 'ry': radius
626
+ },
627
+ this.svgGroup_);
628
+ Blockly.utils.insertAfter(this.outerSvg_, this.workspace_.getParentSvg());
629
+ };
630
+
631
+ /**
632
+ * Is the scrollbar visible. Non-paired scrollbars disappear when they aren't
633
+ * needed.
634
+ * @return {boolean} True if visible.
635
+ */
636
+ Blockly.Scrollbar.prototype.isVisible = function() {
637
+ return this.isVisible_;
638
+ };
639
+
640
+ /**
641
+ * Set whether the scrollbar's container is visible and update
642
+ * display accordingly if visibility has changed.
643
+ * @param {boolean} visible Whether the container is visible
644
+ */
645
+ Blockly.Scrollbar.prototype.setContainerVisible = function(visible) {
646
+ var visibilityChanged = (visible != this.containerVisible_);
647
+
648
+ this.containerVisible_ = visible;
649
+ if (visibilityChanged) {
650
+ this.updateDisplay_();
651
+ }
652
+ };
653
+
654
+ /**
655
+ * Set whether the scrollbar is visible.
656
+ * Only applies to non-paired scrollbars.
657
+ * @param {boolean} visible True if visible.
658
+ */
659
+ Blockly.Scrollbar.prototype.setVisible = function(visible) {
660
+ var visibilityChanged = (visible != this.isVisible());
661
+
662
+ // Ideally this would also apply to scrollbar pairs, but that's a bigger
663
+ // headache (due to interactions with the corner square).
664
+ if (this.pair_) {
665
+ throw 'Unable to toggle visibility of paired scrollbars.';
666
+ }
667
+ this.isVisible_ = visible;
668
+ if (visibilityChanged) {
669
+ this.updateDisplay_();
670
+ }
671
+ };
672
+
673
+ /**
674
+ * Update visibility of scrollbar based on whether it thinks it should
675
+ * be visible and whether its containing workspace is visible.
676
+ * We cannot rely on the containing workspace being hidden to hide us
677
+ * because it is not necessarily our parent in the DOM.
678
+ */
679
+ Blockly.Scrollbar.prototype.updateDisplay_ = function() {
680
+ var show = true;
681
+ // Check whether our parent/container is visible.
682
+ if (!this.containerVisible_) {
683
+ show = false;
684
+ } else {
685
+ show = this.isVisible();
686
+ }
687
+ if (show) {
688
+ this.outerSvg_.setAttribute('display', 'block');
689
+ } else {
690
+ this.outerSvg_.setAttribute('display', 'none');
691
+ }
692
+ };
693
+
694
+ /**
695
+ * Scroll by one pageful.
696
+ * Called when scrollbar background is clicked.
697
+ * @param {!Event} e Mouse down event.
698
+ * @private
699
+ */
700
+ Blockly.Scrollbar.prototype.onMouseDownBar_ = function(e) {
701
+ this.workspace_.markFocused();
702
+ Blockly.Touch.clearTouchIdentifier(); // This is really a click.
703
+ this.cleanUp_();
704
+ if (Blockly.utils.isRightButton(e)) {
705
+ // Right-click.
706
+ // Scrollbars have no context menu.
707
+ e.stopPropagation();
708
+ return;
709
+ }
710
+ var mouseXY = Blockly.utils.mouseToSvg(e, this.workspace_.getParentSvg(),
711
+ this.workspace_.getInverseScreenCTM());
712
+ var mouseLocation = this.horizontal_ ? mouseXY.x : mouseXY.y;
713
+
714
+ var handleXY = Blockly.utils.getInjectionDivXY_(this.svgHandle_);
715
+ var handleStart = this.horizontal_ ? handleXY.x : handleXY.y;
716
+ var handlePosition = this.handlePosition_;
717
+
718
+ var pageLength = this.handleLength_ * 0.95;
719
+ if (mouseLocation <= handleStart) {
720
+ // Decrease the scrollbar's value by a page.
721
+ handlePosition -= pageLength;
722
+ } else if (mouseLocation >= handleStart + this.handleLength_) {
723
+ // Increase the scrollbar's value by a page.
724
+ handlePosition += pageLength;
725
+ }
726
+ // When the scrollbars are clicked, hide the WidgetDiv/DropDownDiv without
727
+ // animation in anticipation of a workspace move.
728
+ Blockly.WidgetDiv.hide(true);
729
+ Blockly.DropDownDiv.hideWithoutAnimation();
730
+
731
+ this.setHandlePosition(this.constrainHandle_(handlePosition));
732
+ this.onScroll_();
733
+ e.stopPropagation();
734
+ e.preventDefault();
735
+ };
736
+
737
+ /**
738
+ * Start a dragging operation.
739
+ * Called when scrollbar handle is clicked.
740
+ * @param {!Event} e Mouse down event.
741
+ * @private
742
+ */
743
+ Blockly.Scrollbar.prototype.onMouseDownHandle_ = function(e) {
744
+ this.workspace_.markFocused();
745
+ this.cleanUp_();
746
+ if (Blockly.utils.isRightButton(e)) {
747
+ // Right-click.
748
+ // Scrollbars have no context menu.
749
+ e.stopPropagation();
750
+ return;
751
+ }
752
+ // Look up the current translation and record it.
753
+ this.startDragHandle = this.handlePosition_;
754
+
755
+ // Tell the workspace to setup its drag surface since it is about to move.
756
+ // onMouseMoveHandle will call onScroll which actually tells the workspace
757
+ // to move.
758
+ this.workspace_.setupDragSurface();
759
+
760
+ // Record the current mouse position.
761
+ this.startDragMouse_ = this.horizontal_ ? e.clientX : e.clientY;
762
+ Blockly.Scrollbar.onMouseUpWrapper_ = Blockly.bindEventWithChecks_(document,
763
+ 'mouseup', this, this.onMouseUpHandle_);
764
+ Blockly.Scrollbar.onMouseMoveWrapper_ = Blockly.bindEventWithChecks_(document,
765
+ 'mousemove', this, this.onMouseMoveHandle_);
766
+ // When the scrollbars are clicked, hide the WidgetDiv/DropDownDiv without
767
+ // animation in anticipation of a workspace move.
768
+ Blockly.WidgetDiv.hide(true);
769
+ Blockly.DropDownDiv.hideWithoutAnimation();
770
+
771
+ e.stopPropagation();
772
+ e.preventDefault();
773
+ };
774
+
775
+ /**
776
+ * Drag the scrollbar's handle.
777
+ * @param {!Event} e Mouse up event.
778
+ * @private
779
+ */
780
+ Blockly.Scrollbar.prototype.onMouseMoveHandle_ = function(e) {
781
+ var currentMouse = this.horizontal_ ? e.clientX : e.clientY;
782
+ var mouseDelta = currentMouse - this.startDragMouse_;
783
+ var handlePosition = this.startDragHandle + mouseDelta;
784
+ // Position the bar.
785
+ this.setHandlePosition(this.constrainHandle_(handlePosition));
786
+ this.onScroll_();
787
+ };
788
+
789
+ /**
790
+ * Release the scrollbar handle and reset state accordingly.
791
+ * @private
792
+ */
793
+ Blockly.Scrollbar.prototype.onMouseUpHandle_ = function() {
794
+ // Tell the workspace to clean up now that the workspace is done moving.
795
+ this.workspace_.resetDragSurface();
796
+ Blockly.Touch.clearTouchIdentifier();
797
+ this.cleanUp_();
798
+ };
799
+
800
+ /**
801
+ * Hide chaff and stop binding to mouseup and mousemove events. Call this to
802
+ * wrap up lose ends associated with the scrollbar.
803
+ * @private
804
+ */
805
+ Blockly.Scrollbar.prototype.cleanUp_ = function() {
806
+ Blockly.hideChaff(true);
807
+ if (Blockly.Scrollbar.onMouseUpWrapper_) {
808
+ Blockly.unbindEvent_(Blockly.Scrollbar.onMouseUpWrapper_);
809
+ Blockly.Scrollbar.onMouseUpWrapper_ = null;
810
+ }
811
+ if (Blockly.Scrollbar.onMouseMoveWrapper_) {
812
+ Blockly.unbindEvent_(Blockly.Scrollbar.onMouseMoveWrapper_);
813
+ Blockly.Scrollbar.onMouseMoveWrapper_ = null;
814
+ }
815
+ };
816
+
817
+ /**
818
+ * Constrain the handle's position within the minimum (0) and maximum
819
+ * (length of scrollbar) values allowed for the scrollbar.
820
+ * @param {number} value Value that is potentially out of bounds, in CSS pixels.
821
+ * @return {number} Constrained value, in CSS pixels.
822
+ * @private
823
+ */
824
+ Blockly.Scrollbar.prototype.constrainHandle_ = function(value) {
825
+ if (value <= 0 || isNaN(value) || this.scrollViewSize_ < this.handleLength_) {
826
+ value = 0;
827
+ } else {
828
+ value = Math.min(value, this.scrollViewSize_ - this.handleLength_);
829
+ }
830
+ return value;
831
+ };
832
+
833
+ /**
834
+ * Called when scrollbar is moved.
835
+ * @private
836
+ */
837
+ Blockly.Scrollbar.prototype.onScroll_ = function() {
838
+ var ratio = this.handlePosition_ / this.scrollViewSize_;
839
+ if (isNaN(ratio)) {
840
+ ratio = 0;
841
+ }
842
+ var xyRatio = {};
843
+ if (this.horizontal_) {
844
+ xyRatio.x = ratio;
845
+ } else {
846
+ xyRatio.y = ratio;
847
+ }
848
+ this.workspace_.setMetrics(xyRatio);
849
+ };
850
+
851
+ /**
852
+ * Set the scrollbar handle's position.
853
+ * @param {number} value The distance from the top/left end of the bar, in CSS
854
+ * pixels. It may be larger than the maximum allowable position of the
855
+ * scrollbar handle.
856
+ */
857
+ Blockly.Scrollbar.prototype.set = function(value) {
858
+ this.setHandlePosition(this.constrainHandle_(value * this.ratio_));
859
+ this.onScroll_();
860
+ };
861
+
862
+ /**
863
+ * Record the origin of the workspace that the scrollbar is in, in pixels
864
+ * relative to the injection div origin. This is for times when the scrollbar is
865
+ * used in an object whose origin isn't the same as the main workspace
866
+ * (e.g. in a flyout.)
867
+ * @param {number} x The x coordinate of the scrollbar's origin, in CSS pixels.
868
+ * @param {number} y The y coordinate of the scrollbar's origin, in CSS pixels.
869
+ */
870
+ Blockly.Scrollbar.prototype.setOrigin = function(x, y) {
871
+ if (x != this.origin_.x || y != this.origin_.y) {
872
+ this.origin_ = new goog.math.Coordinate(x, y);
873
+ this.originHasChanged_ = true;
874
+ }
875
+ };