-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolbox.js
291 lines (268 loc) · 9.22 KB
/
toolbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Visual Blocks Editor
*
* Copyright 2011 Google Inc.
* http://code.google.com/p/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Toolbox from whence to create blocks.
* In the interests of a consistent UI, the toolbox shares some functions and
* properties with the context menu.
* @author fraser@google.com (Neil Fraser)
*/
// Name space for the toolbox.
Blockly.Toolbox = {};
/**
* Width of the toolbox.
*/
Blockly.Toolbox.width = 0;
/**
* The SVG group currently selected.
* @type {Element}
* @private
*/
Blockly.Toolbox.selectedOption_ = null;
/**
* Creates the toolbox's DOM. Only needs to be called once.
* @return {!Element} The toolbox's SVG group.
*/
Blockly.Toolbox.createDom = function() {
Blockly.Toolbox.flyout_ = new Blockly.Flyout();
/*
<g>
[flyout]
<rect class="blocklyToolboxBackground" height="100%"/>
<g class="blocklyToolboxOptions">
</g>
</g>
*/
var svgGroup = Blockly.createSvgElement('g', {}, null);
Blockly.Toolbox.svgGroup_ = svgGroup;
var flyoutGroup = Blockly.Toolbox.flyout_.createDom();
svgGroup.appendChild(flyoutGroup);
Blockly.Toolbox.svgBackground_ = Blockly.createSvgElement('rect',
{'class': 'blocklyToolboxBackground', height: '100%'}, svgGroup);
Blockly.Toolbox.svgOptions_ = Blockly.createSvgElement('g',
{'class': 'blocklyToolboxOptions'}, svgGroup);
return svgGroup;
};
/**
* Return an object with all the metrics required to size scrollbars for the
* toolbox. The following properties are computed:
* .viewHeight: Height of the visible rectangle,
* .viewWidth: Width of the visible rectangle,
* .contentHeight: Height of the contents,
* .viewTop: Offset of top edge of visible rectangle from parent,
* .contentTop: Offset of the top-most content from the y=0 coordinate,
* .absoluteTop: Top-edge of view.
* .absoluteLeft: Left-edge of view.
* @return {Object} Contains size and position metrics of the toolbox.
*/
Blockly.Toolbox.getMetrics = function() {
var viewHeight = Blockly.svgSize().height;
var viewWidth = Blockly.Toolbox.width;
try {
var optionBox = Blockly.Toolbox.svgOptions_.getBBox();
} catch (e) {
// Firefox has trouble with hidden elements (Bug 528969).
return null;
}
return {
viewHeight: viewHeight,
viewWidth: viewWidth,
contentHeight: optionBox.height + optionBox.y,
viewTop: -Blockly.Toolbox.svgOptions_.scrollY,
contentTop: 0,
absoluteTop: 0,
// absoluteLeft should be 0, but Firefox leaks by a pixel.
absoluteLeft: Blockly.RTL ? -1 : 1
};
};
/**
* Sets the Y translation of the toolbox to match the scrollbars.
* @param {!Object} yRatio Contains a y property which is a float
* between 0 and 1 specifying the degree of scrolling.
*/
Blockly.Toolbox.setMetrics = function(yRatio) {
var metrics = Blockly.Toolbox.getMetrics();
if (typeof yRatio.y == 'number') {
Blockly.Toolbox.svgOptions_.scrollY = -metrics.contentHeight * yRatio.y -
metrics.contentTop;
}
Blockly.Toolbox.svgOptions_.setAttribute('transform', 'translate(0,' +
(Blockly.Toolbox.svgOptions_.scrollY + metrics.absoluteTop) + ')');
};
/**
* Initializes the toolbox.
*/
Blockly.Toolbox.init = function() {
Blockly.Toolbox.flyout_.init(Blockly.mainWorkspace,
Blockly.getMainWorkspaceMetrics);
Blockly.Toolbox.languageTree = Blockly.Toolbox.buildTree_();
Blockly.Toolbox.redraw();
// Add scrollbars.
new Blockly.Scrollbar(Blockly.Toolbox.svgOptions_,
Blockly.Toolbox.getMetrics, Blockly.Toolbox.setMetrics,
false, false);
Blockly.Toolbox.position_();
// If the document resizes, reposition the toolbox.
Blockly.bindEvent_(window, 'resize', null, Blockly.Toolbox.position_);
};
/**
* Move the toolbox to the edge.
* @private
*/
Blockly.Toolbox.position_ = function() {
var svgSize = Blockly.svgSize();
if (Blockly.RTL) {
Blockly.Toolbox.svgGroup_.setAttribute('transform',
'translate(' + (svgSize.width - Blockly.Toolbox.width) + ',0)');
}
};
/**
* String to prefix on categories of each block in the toolbox.
* Used to prevent collisions with built-in properties like 'toString'.
* @private
*/
Blockly.Toolbox.PREFIX_ = 'cat_';
/**
* Build the hierarchical tree of block types.
* @return {!Object} Tree object.
* @private
*/
Blockly.Toolbox.buildTree_ = function() {
var tree = {};
// Populate the tree structure.
for (var name in Blockly.Language) {
var block = Blockly.Language[name];
// Blocks without a category are fragments used by the mutator dialog.
if (block.category) {
var cat = Blockly.Toolbox.PREFIX_ + window.encodeURI(block.category);
if (cat in tree) {
tree[cat].push(name);
} else {
tree[cat] = [name];
}
}
}
return tree;
};
/**
* Fill the toolbox with options.
*/
Blockly.Toolbox.redraw = function() {
// Create an option for each category.
var options = [];
for (var cat in Blockly.Toolbox.languageTree) {
var option = {};
option.text =
window.decodeURI(cat.substring(Blockly.Toolbox.PREFIX_.length));
option.cat = cat;
options.push(option);
}
var option = {};
if (Blockly.Language.variables_get || Blockly.Language.variables_set) {
// Variables have a special category that is dynamic.
options.push({text: Blockly.MSG_VARIABLE_CATEGORY,
cat: Blockly.MSG_VARIABLE_CATEGORY});
}
if (Blockly.Language.procedures_defnoreturn ||
Blockly.Language.procedures_defreturn) {
// Procedures have a special category that is dynamic.
options.push({text: Blockly.MSG_PROCEDURE_CATEGORY,
cat: Blockly.MSG_PROCEDURE_CATEGORY});
}
function callbackFactory(cat, element) {
return function(e) {
var oldSelectedOption = Blockly.Toolbox.selectedOption_;
Blockly.hideChaff();
if (oldSelectedOption != element) {
Blockly.Toolbox.selectOption_(cat, element);
}
// This mouse click has been handled, don't bubble up to document.
e.stopPropagation();
};
}
// Erase all existing options.
Blockly.removeChildren_(Blockly.Toolbox.svgOptions_);
var TOP_MARGIN = 4;
var maxWidth = 0;
var resizeList = [Blockly.Toolbox.svgBackground_];
for (var x = 0, option; option = options[x]; x++) {
var gElement = Blockly.ContextMenu.optionToDom(option.text);
var rectElement = gElement.firstChild;
var textElement = gElement.lastChild;
Blockly.Toolbox.svgOptions_.appendChild(gElement);
gElement.setAttribute('transform', 'translate(0, ' +
(x * Blockly.ContextMenu.Y_HEIGHT + TOP_MARGIN) + ')');
Blockly.bindEvent_(gElement, 'mousedown', null,
callbackFactory(option.cat, gElement));
resizeList.push(rectElement);
// Compute the length of the longest text length.
maxWidth = Math.max(maxWidth, textElement.getComputedTextLength());
}
// Run a second pass to resize all options to the required width.
maxWidth += Blockly.ContextMenu.X_PADDING * 2;
for (var x = 0; x < resizeList.length; x++) {
resizeList[x].setAttribute('width', maxWidth);
}
if (Blockly.RTL) {
// Right-align the text.
for (var x = 0, gElement;
gElement = Blockly.Toolbox.svgOptions_.childNodes[x]; x++) {
var textElement = gElement.lastChild;
textElement.setAttribute('x', maxWidth -
textElement.getComputedTextLength() - Blockly.ContextMenu.X_PADDING);
}
}
Blockly.Toolbox.width = maxWidth;
// Right-click on empty areas of the toolbox does not generate a context menu.
Blockly.bindEvent_(Blockly.Toolbox.svgGroup_, 'mousedown', null,
function(e) {
if (e.button == 2) {
Blockly.hideChaff(true);
e.stopPropagation();
}
});
// Fire a resize event since the toolbox may have changed width and height.
Blockly.fireUiEvent(Blockly.svgDoc, window, 'resize');
};
/**
* Highlight the specified option.
* @param {?string} cat The category name of the newly specified option,
* or null to select nothing.
* @param {Element} newSelectedOption The SVG group for the selected option,
* or null to select nothing.
* @private
*/
Blockly.Toolbox.selectOption_ = function(cat, newSelectedOption) {
Blockly.Toolbox.selectedOption_ = newSelectedOption;
if (newSelectedOption) {
Blockly.addClass_(newSelectedOption, 'blocklyMenuSelected');
var blockSet = Blockly.Toolbox.languageTree[cat] || cat;
Blockly.Toolbox.flyout_.show(blockSet);
}
};
/**
* Unhighlight any previously specified option. Hide the flyout.
*/
Blockly.Toolbox.clearSelection = function() {
var oldSelectedOption = Blockly.Toolbox.selectedOption_;
if (oldSelectedOption) {
Blockly.removeClass_(oldSelectedOption, 'blocklyMenuSelected');
Blockly.Toolbox.selectedOption_ = null;
}
Blockly.Toolbox.flyout_.hide();
};