-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflyout.js
311 lines (290 loc) · 10.3 KB
/
flyout.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* 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 Flyout tray containing blocks which may be created.
* @author [email protected] (Neil Fraser)
*/
/**
* Class for a flyout.
* @constructor
*/
Blockly.Flyout = function() {
this.workspace_ = new Blockly.Workspace(false);
};
/**
* Does the flyout automatically close when a block is created?
*/
Blockly.Flyout.prototype.autoClose = true;
/**
* Corner radius of the flyout background.
*/
Blockly.Flyout.prototype.CORNER_RADIUS = 8;
/**
* Creates the flyout's DOM. Only needs to be called once.
* @return {!Element} The flyout's SVG group.
*/
Blockly.Flyout.prototype.createDom = function() {
/*
<g>
<path class="blocklyFlyoutBackground"/>
<g></g>
</g>
*/
this.svgGroup_ = Blockly.createSvgElement('g', {}, null);
this.svgBackground_ = Blockly.createSvgElement('path',
{'class': 'blocklyFlyoutBackground'}, this.svgGroup_);
this.svgOptions_ = Blockly.createSvgElement('g', {}, this.svgGroup_);
this.svgOptions_.appendChild(this.workspace_.createDom());
return this.svgGroup_;
};
/**
* Return an object with all the metrics required to size scrollbars for the
* flyout. 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 flyout.
*/
Blockly.Flyout.prototype.getMetrics = function() {
if (!this.isVisible()) {
// Flyout is hidden.
return null;
}
var viewHeight = this.height_ - 2 * this.CORNER_RADIUS;
var viewWidth = this.width_;
try {
var optionBox = this.svgOptions_.getBBox();
} catch (e) {
// Firefox has trouble with hidden elements (Bug 528969).
var optionBox = {height: 0, y: 0};
}
return {
viewHeight: viewHeight,
viewWidth: viewWidth,
contentHeight: optionBox.height + optionBox.y,
viewTop: -this.svgOptions_.scrollY,
contentTop: 0,
absoluteTop: this.CORNER_RADIUS,
absoluteLeft: 0
};
};
/**
* Sets the Y translation of the flyout 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.Flyout.prototype.setMetrics = function(yRatio) {
var metrics = this.getMetrics();
if (typeof yRatio.y == 'number') {
this.svgOptions_.scrollY =
-metrics.contentHeight * yRatio.y - metrics.contentTop;
}
var y = this.svgOptions_.scrollY + metrics.absoluteTop;
this.svgOptions_.setAttribute('transform', 'translate(0,' + y + ')');
};
/**
* Initializes the flyout.
* @param {!Blockly.Workspace} workspace The workspace in which to create new
* blocks.
* @param {!Function} workspaceMetrics Function which returns size information
* regarding the flyout's target workspace.
*/
Blockly.Flyout.prototype.init = function(workspace, workspaceMetrics) {
this.targetWorkspace_ = workspace;
this.targetWorkspaceMetrics_ = workspaceMetrics;
// Add scrollbars.
this.width_ = 0;
this.height_ = 0;
var flyout = this;
new Blockly.Scrollbar(this.svgOptions_,
function() {return flyout.getMetrics();},
function(ratio) {return flyout.setMetrics(ratio);},
false, false);
// List of background buttons that lurk behind each block to catch clicks
// landing in the blocks' lakes and bays.
this.buttons_ = [];
this.position_();
// If the document resizes, reposition the toolbox.
Blockly.bindEvent_(window, 'resize', this, this.position_);
};
/**
* Move the toolbox to the edge of the workspace.
* @private
*/
Blockly.Flyout.prototype.position_ = function() {
var metrics = this.targetWorkspaceMetrics_();
if (!metrics) {
// Hidden components will return null.
return;
}
var edgeWidth = this.width_ - this.CORNER_RADIUS;
if (Blockly.RTL) {
edgeWidth *= -1;
}
var path = ['M ' + (Blockly.RTL ? this.width_ : 0) + ',0'];
path.push('h', edgeWidth);
path.push('a', this.CORNER_RADIUS, this.CORNER_RADIUS, 0, 0,
Blockly.RTL ? 0 : 1,
Blockly.RTL ? -this.CORNER_RADIUS : this.CORNER_RADIUS,
this.CORNER_RADIUS);
path.push('v', Math.max(0, metrics.viewHeight - this.CORNER_RADIUS * 2));
path.push('a', this.CORNER_RADIUS, this.CORNER_RADIUS, 0, 0,
Blockly.RTL ? 0 : 1,
Blockly.RTL ? this.CORNER_RADIUS : -this.CORNER_RADIUS,
this.CORNER_RADIUS);
path.push('h', -edgeWidth);
path.push('z');
this.svgBackground_.setAttribute('d', path.join(' '));
var x = metrics.absoluteLeft;
if (Blockly.RTL) {
x -= this.width_;
}
this.svgGroup_.setAttribute('transform',
'translate(' + x + ',' + metrics.absoluteTop + ')');
// Record the height for Blockly.Flyout.getMetrics.
this.height_ = metrics.viewHeight;
};
/**
* Is the flyout visisble?
* @return {boolean} True if visible.
*/
Blockly.Flyout.prototype.isVisible = function() {
return this.svgGroup_.style.display != 'none';
};
/**
* Hide and empty the flyout.
*/
Blockly.Flyout.prototype.hide = function() {
this.svgGroup_.style.display = 'none';
// Delete all the blocks.
var blocks = this.workspace_.getTopBlocks(false);
for (var x = 0, block; block = blocks[x]; x++) {
block.destroy();
}
// Delete all the background buttons.
for (var x = 0, rect; rect = this.buttons_[x];
x++) {
Blockly.unbindEvent_(rect, 'mousedown', rect.wrapper_);
rect.parentNode.removeChild(rect);
}
this.buttons_ = [];
};
/**
* Show and populate the flyout.
* @param {!Array.<string>|string} names List of type names of blocks to show.
* Variables and procedures have a custom set of blocks.
*/
Blockly.Flyout.prototype.show = function(names) {
var margin = this.CORNER_RADIUS;
this.svgGroup_.style.display = 'block';
// Create the blocks to be shown in this flyout.
var blocks = [];
var gaps = [];
if (names == Blockly.MSG_VARIABLE_CATEGORY) {
// Special category for variables.
Blockly.Variables.flyoutCategory(blocks, gaps, margin, this.workspace_);
} else if (names == Blockly.MSG_PROCEDURE_CATEGORY) {
// Special category for procedures.
Blockly.Procedures.flyoutCategory(blocks, gaps, margin, this.workspace_);
} else {
for (var i = 0, name; name = names[i]; i++) {
var block = new Blockly.Block(this.workspace_, name);
block.initSvg();
blocks[i] = block;
gaps[i] = margin * 2;
}
}
// Lay out the blocks vertically.
var flyoutWidth = 0;
var cursorY = margin;
for (var i = 0, block; block = blocks[i]; i++) {
// Mark blocks as being inside a flyout. This is used to detect and prevent
// the closure of the flyout if the user right-clicks on such a block.
block.isInFlyout = true;
// There is no good way to handle comment bubbles inside the flyout.
// Blocks shouldn't come with predefined comments, but someone will
// try this, I'm sure. Kill the comment.
Blockly.Comment && block.setCommentText(null);
block.render();
var bBox = block.getSvgRoot().getBBox();
var x = Blockly.RTL ? 0 : margin + Blockly.BlockSvg.TAB_WIDTH;
block.moveBy(x, cursorY);
flyoutWidth = Math.max(flyoutWidth, bBox.width);
cursorY += bBox.height + gaps[i];
Blockly.bindEvent_(block.getSvgRoot(), 'mousedown', null,
Blockly.Flyout.createBlockFunc_(this, block));
}
flyoutWidth += margin + Blockly.BlockSvg.TAB_WIDTH + margin / 2 +
Blockly.Scrollbar.scrollbarThickness;
for (var i = 0, block; block = blocks[i]; i++) {
if (Blockly.RTL) {
// With the flyoutWidth known, reposition the blocks to the right-aligned.
block.moveBy(flyoutWidth - margin - Blockly.BlockSvg.TAB_WIDTH, 0);
}
// Create an invisible rectangle over the block to act as a button. Just
// using the block as a button is poor, since blocks have holes in them.
var bBox = block.getSvgRoot().getBBox();
var xy = block.getRelativeToSurfaceXY();
var rect = Blockly.createSvgElement('rect',
{width: bBox.width, height: bBox.height,
x: xy.x + bBox.x, y: xy.y + bBox.y,
'fill-opacity': 0}, null);
// Add the rectangles under the blocks, so that the blocks' tooltips work.
this.svgOptions_.insertBefore(rect, this.svgOptions_.firstChild);
rect.wrapper_ = Blockly.bindEvent_(rect, 'mousedown', null,
Blockly.Flyout.createBlockFunc_(this, block));
this.buttons_[i] = rect;
}
// Record the width for .getMetrics and .position_.
this.width_ = flyoutWidth;
// Fire a resize event to update the flyout's scrollbar.
Blockly.fireUiEvent(Blockly.svgDoc, window, 'resize');
};
/**
* Create a copy of this block on the workspace.
* @param {!Blockly.Flyout} flyout Instance of the flyout.
* @param {!Blockly.Block} originBlock The flyout block to copy.
* @return {!Function} Function to call when block is clicked.
* @private
*/
Blockly.Flyout.createBlockFunc_ = function(flyout, originBlock) {
return function(e) {
if (e.button == 2) {
// Right-click. Don't create a block, let the context menu show.
return;
}
// Create the new block by cloning the block in the flyout (via XML).
var xml = Blockly.Xml.blockToDom_(originBlock);
var block = Blockly.Xml.domToBlock_(flyout.targetWorkspace_, xml);
// Place it in the same spot as the flyout copy.
var xyOld = Blockly.getAbsoluteXY_(originBlock.getSvgRoot());
var xyNew = Blockly.getAbsoluteXY_(flyout.targetWorkspace_.getCanvas());
block.moveBy(xyOld.x - xyNew.x, xyOld.y - xyNew.y);
block.render();
if (flyout.autoClose) {
flyout.hide();
}
// Start a dragging operation on the new block.
block.onMouseDown_(e);
};
};