-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfield_dropdown.js
240 lines (226 loc) · 8.42 KB
/
field_dropdown.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
/**
* Visual Blocks Editor
*
* Copyright 2012 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 Dropdown input field. Used for editable titles and variables.
* In the interests of a consistent UI, the toolbox shares some functions and
* properties with the context menu.
* @author [email protected] (Neil Fraser)
*/
/**
* Class for an editable dropdown field.
* @param {(!Array.<string>|!Function)} menuGenerator An array of options
* for a dropdown list, or a function which generates these options.
* @param {Function} opt_changeHandler A function that is executed when a new
* option is selected.
* @constructor
*/
Blockly.FieldDropdown = function(menuGenerator, opt_changeHandler) {
this.menuGenerator_ = menuGenerator;
this.changeHandler_ = opt_changeHandler;
var firstText = this.getOptions_()[0][0];
// Call parent's constructor.
Blockly.Field.call(this, firstText);
};
// FieldDropdown is a subclass of Field.
Blockly.FieldDropdown.prototype = new Blockly.Field(null);
/**
* Create the dropdown field's elements. Only needs to be called once.
* @return {!Element} The field's SVG group.
*/
Blockly.FieldDropdown.createDom = function() {
/*
<g class="blocklyHidden">
<rect class="blocklyDropdownMenuShadow" x="0" y="1" rx="2" ry="2"/>
<rect x="-2" y="-1" rx="2" ry="2"/>
<g class="blocklyDropdownMenuOptions">
</g>
</g>
*/
var svgGroup = Blockly.createSvgElement('g', {'class': 'blocklyHidden'},
null);
Blockly.FieldDropdown.svgGroup_ = svgGroup;
Blockly.FieldDropdown.svgShadow_ = Blockly.createSvgElement('rect',
{'class': 'blocklyDropdownMenuShadow',
x: 0, y: 1, rx: 2, ry: 2}, svgGroup);
Blockly.FieldDropdown.svgBackground_ = Blockly.createSvgElement('rect',
{x: -2, y: -1, rx: 2, ry: 2,
filter: 'url(#blocklyEmboss)'}, svgGroup);
Blockly.FieldDropdown.svgOptions_ = Blockly.createSvgElement('g',
{'class': 'blocklyDropdownMenuOptions'}, svgGroup);
return svgGroup;
};
/**
* Corner radius of the dropdown background.
*/
Blockly.FieldDropdown.CORNER_RADIUS = 2;
/**
* Mouse cursor style when over the hotspot that initiates the editor.
*/
Blockly.FieldDropdown.prototype.CURSOR = 'default';
/**
* Create a dropdown menu under the text.
* @private
*/
Blockly.FieldDropdown.prototype.showEditor_ = function() {
var svgGroup = Blockly.FieldDropdown.svgGroup_;
var svgOptions = Blockly.FieldDropdown.svgOptions_;
var svgBackground = Blockly.FieldDropdown.svgBackground_;
var svgShadow = Blockly.FieldDropdown.svgShadow_;
// Erase all existing options.
Blockly.removeChildren_(svgOptions);
// The menu must be made visible early since otherwise BBox and
// getComputedTextLength will return 0.
svgGroup.style.display = 'block';
function callbackFactory(text) {
return function(e) {
if (this.changeHandler_) {
this.changeHandler_(text);
} else {
this.setText(text);
}
// This mouse click has been handled, don't bubble up to document.
e.stopPropagation();
};
}
var maxWidth = 0;
var resizeList = [];
var checkElement = null;
var options = this.getOptions_();
for (var x = 0; x < options.length; x++) {
var text = options[x][0]; // Human-readable text.
var value = options[x][1]; // Language-neutral value.
var gElement = Blockly.ContextMenu.optionToDom(text);
var rectElement = gElement.firstChild;
var textElement = gElement.lastChild;
svgOptions.appendChild(gElement);
// Add a checkmark next to the current item.
if (!checkElement && text == this.text_) {
checkElement = Blockly.createSvgElement('text',
{'class': 'blocklyMenuText', y: 15}, null);
// Insert the checkmark between the rect and text, thus preserving the
// ability to reference them as firstChild and lastChild respectively.
gElement.insertBefore(checkElement, textElement);
checkElement.appendChild(Blockly.svgDoc.createTextNode('\u2713'));
}
gElement.setAttribute('transform',
'translate(0, ' + (x * Blockly.ContextMenu.Y_HEIGHT) + ')');
resizeList.push(rectElement);
Blockly.bindEvent_(gElement, 'mousedown', null, Blockly.noEvent);
Blockly.bindEvent_(gElement, 'mouseup', this, callbackFactory(text));
Blockly.bindEvent_(gElement, 'mouseup', null,
Blockly.FieldDropdown.hideMenu);
// 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 = svgOptions.childNodes[x]; x++) {
var textElement = gElement.lastChild;
textElement.setAttribute('x', maxWidth -
textElement.getComputedTextLength() - Blockly.ContextMenu.X_PADDING);
}
}
if (checkElement) {
if (Blockly.RTL) {
// Research indicates that RTL checkmarks are supposed to be drawn the
// same in the same direction as LTR checkmarks. It's only the alignment
// that needs to change.
checkElement.setAttribute('x',
maxWidth - 5 - checkElement.getComputedTextLength());
} else {
checkElement.setAttribute('x', 5);
}
}
var width = maxWidth + Blockly.FieldDropdown.CORNER_RADIUS * 2;
var height = options.length * Blockly.ContextMenu.Y_HEIGHT +
Blockly.FieldDropdown.CORNER_RADIUS + 1;
svgShadow.setAttribute('width', width);
svgShadow.setAttribute('height', height);
svgBackground.setAttribute('width', width);
svgBackground.setAttribute('height', height);
var hexColour = Blockly.makeColour(this.sourceBlock_.getColour());
svgBackground.setAttribute('fill', hexColour);
// Position the dropdown to line up with the field.
var xy = Blockly.getAbsoluteXY_(this.borderRect_);
var borderBBox = this.borderRect_.getBBox();
var x;
if (Blockly.RTL) {
x = xy.x - maxWidth + Blockly.ContextMenu.X_PADDING + borderBBox.width -
Blockly.BlockSvg.SEP_SPACE_X / 2;
} else {
x = xy.x - Blockly.ContextMenu.X_PADDING + Blockly.BlockSvg.SEP_SPACE_X / 2;
}
svgGroup.setAttribute('transform',
'translate(' + x + ', ' + (xy.y + borderBBox.height) + ')');
};
/**
* Return a list of the options for this dropdown.
* @return {!Array.<!Array.<string>>} Array of option tuples:
* (human-readable text, language-neutral name).
* @private
*/
Blockly.FieldDropdown.prototype.getOptions_ = function() {
if (typeof this.menuGenerator_ == 'function') {
return this.menuGenerator_.call(this);
}
return this.menuGenerator_;
};
/**
* Get the language-neutral value from this dropdown menu.
* @return {string} Current text.
*/
Blockly.FieldDropdown.prototype.getValue = function() {
var selectedText = this.text_;
var options = this.getOptions_();
for (var x = 0; x < options.length; x++) {
// Options are tuples of human-readable text and language-neutral values.
if (options[x][0] == selectedText) {
return options[x][1];
}
}
throw '"' + selectedText + '" not valid in dropdown.';
};
/**
* Set the language-neutral value for this dropdown menu.
* @param {string} newValue New value to set.
*/
Blockly.FieldDropdown.prototype.setValue = function(newValue) {
var options = this.getOptions_();
for (var x = 0; x < options.length; x++) {
// Options are tuples of human-readable text and language-neutral values.
if (options[x][1] == newValue) {
this.setText(options[x][0]);
return;
}
}
// Value not found. Add it, maybe it will become valid once set
// (like variable names).
this.setText(newValue);
};
/**
* Hide the dropdown menu.
*/
Blockly.FieldDropdown.hideMenu = function() {
Blockly.FieldDropdown.svgGroup_.style.display = 'none';
};