forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield_toggle.ts
More file actions
297 lines (264 loc) · 12.3 KB
/
field_toggle.ts
File metadata and controls
297 lines (264 loc) · 12.3 KB
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
/// <reference path="../../localtypings/blockly.d.ts" />
namespace pxtblockly {
export class FieldToggle extends Blockly.FieldNumber implements Blockly.FieldCustom {
public isFieldCustom_ = true;
private params: any;
private state_: boolean;
private checkElement_: SVGElement;
private toggleThumb_: SVGElement;
public CURSOR = 'pointer';
private type_: string;
constructor(state: string, params: Blockly.FieldCustomOptions, opt_validator?: Function) {
super(state, undefined, undefined, undefined, opt_validator);
this.params = params;
this.setValue(state);
this.addArgType('toggle');
this.type_ = params.type;
}
init() {
if (this.fieldGroup_) {
// Field has already been initialized once.
return;
}
// Build the DOM.
this.fieldGroup_ = Blockly.utils.dom.createSvgElement('g', {}, null) as SVGGElement;
if (!this.visible_) {
(this.fieldGroup_ as any).style.display = 'none';
}
// Add an attribute to cassify the type of field.
if ((this as any).getArgTypes() !== null) {
if (this.sourceBlock_.isShadow()) {
(this.sourceBlock_ as any).svgGroup_.setAttribute('data-argument-type',
(this as any).getArgTypes());
} else {
// Fields without a shadow wrapper, like square dropdowns.
this.fieldGroup_.setAttribute('data-argument-type', (this as any).getArgTypes());
}
}
// If not in a shadow block, and has more than one input, draw a box.
if (!this.sourceBlock_.isShadow()
&& (this.sourceBlock_.inputList && this.sourceBlock_.inputList.length > 1)) {
this.borderRect_ = Blockly.utils.dom.createSvgElement('rect', {
'rx': (Blockly as any).BlockSvg.CORNER_RADIUS,
'ry': (Blockly as any).BlockSvg.CORNER_RADIUS,
'x': 0,
'y': 0,
'width': this.size_.width,
'height': this.size_.height,
'fill': (this.sourceBlock_ as Blockly.BlockSvg).getColour(),
'stroke': (this.sourceBlock_ as Blockly.BlockSvg).getColourTertiary()
}, null) as SVGRectElement;
this.fieldGroup_.insertBefore(this.borderRect_, this.textElement_);
}
// Adjust X to be flipped for RTL. Position is relative to horizontal start of source block.
const size = this.getSize();
this.checkElement_ = Blockly.utils.dom.createSvgElement('g',
{
'class': `blocklyToggle ${this.state_ ? 'blocklyToggleOn' : 'blocklyToggleOff'}`,
'transform': `translate(8, ${size.height / 2})`,
}, this.fieldGroup_);
switch (this.getOutputShape()) {
case Blockly.OUTPUT_SHAPE_HEXAGONAL:
this.toggleThumb_ = Blockly.utils.dom.createSvgElement('polygon',
{
'class': 'blocklyToggleRect',
'points': '-7,-14 -21,0 -7,14 7,14 21,0 7,-14',
'cursor': 'pointer'
},
this.checkElement_);
break;
case Blockly.OUTPUT_SHAPE_ROUND:
this.toggleThumb_ = Blockly.utils.dom.createSvgElement('rect',
{
'class': 'blocklyToggleCircle',
'x': -6, 'y': -14, 'height': 28,
'width': 28, 'rx': 14, 'ry': 14,
'cursor': 'pointer'
},
this.checkElement_);
break;
case Blockly.OUTPUT_SHAPE_SQUARE:
this.toggleThumb_ = Blockly.utils.dom.createSvgElement('rect',
{
'class': 'blocklyToggleRect',
'x': -6, 'y': -14, 'height': 28,
'width': 28, 'rx': 3, 'ry': 3,
'cursor': 'pointer'
},
this.checkElement_);
break;
}
let fieldX = (this.sourceBlock_.RTL) ? -size.width / 2 : size.width / 2;
/** @type {!Element} */
this.textElement_ = Blockly.utils.dom.createSvgElement('text',
{
'class': 'blocklyText',
'x': fieldX,
'dy': '0.6ex',
'y': size.height / 2
},
this.fieldGroup_) as SVGTextElement;
this.updateEditable();
const svgRoot = (this.sourceBlock_ as Blockly.BlockSvg).getSvgRoot();
svgRoot.appendChild(this.fieldGroup_);
svgRoot.querySelector(".blocklyBlockBackground").setAttribute('fill', (this.sourceBlock_ as Blockly.BlockSvg).getColourTertiary())
this.switchToggle(this.state_);
this.setValue(this.getValue());
// Force a render.
this.render_();
this.size_.width = 0;
(this as any).mouseDownWrapper_ =
Blockly.bindEventWithChecks_((this as any).getClickTarget_(), 'mousedown', this,
(this as any).onMouseDown_);
}
getDisplayText_() {
return this.state_ ? this.getTrueText() : this.getFalseText();
}
getTrueText() {
return lf("True");
}
getFalseText() {
return lf("False");
}
updateSize_() {
switch (this.getOutputShape()) {
case Blockly.OUTPUT_SHAPE_ROUND:
this.size_.width = this.getInnerWidth() * 2 - 7; break;
case Blockly.OUTPUT_SHAPE_HEXAGONAL:
this.size_.width = this.getInnerWidth() * 2 + 8 - Math.floor(this.getInnerWidth() / 2); break;
case Blockly.OUTPUT_SHAPE_SQUARE:
this.size_.width = 9 + this.getInnerWidth() * 2; break;
}
}
getInnerWidth() {
return this.getMaxLength() * 10;
}
getMaxLength() {
return Math.max(this.getTrueText().length, this.getFalseText().length);
}
getOutputShape() {
return this.sourceBlock_.isShadow() ? this.sourceBlock_.getOutputShape() : Blockly.OUTPUT_SHAPE_SQUARE;
}
doClassValidation_(newBool: string) {
return typeof this.fromVal(newBool) == "boolean" ? newBool : "false";
}
applyColour() {
let color = (this.sourceBlock_ as Blockly.BlockSvg).getColourTertiary();
if (this.borderRect_) {
this.borderRect_.setAttribute('stroke', color);
} else {
(this.sourceBlock_ as any).pathObject.svgPath.setAttribute('fill', color);
}
};
/**
* Return 'TRUE' if the toggle is ON, 'FALSE' otherwise.
* @return {string} Current state.
*/
getValue() {
return this.toVal(this.state_);
};
/**
* Set the checkbox to be checked if newBool is 'TRUE' or true,
* unchecks otherwise.
* @param {string|boolean} newBool New state.
*/
doValueUpdate_(newBool: string) {
let newState = this.fromVal(newBool);
if (this.state_ !== newState) {
if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
Blockly.Events.fire(new (Blockly.Events as any).BlockChange(
this.sourceBlock_, 'field', this.name, this.state_, newState));
}
this.state_ = newState;
this.switchToggle(this.state_);
this.isDirty_ = true;
}
}
switchToggle(newState: boolean) {
if (this.checkElement_) {
this.updateSize_();
const size = this.getSize();
const innerWidth = this.getInnerWidth();
if (newState) {
pxt.BrowserUtils.addClass(this.checkElement_, 'blocklyToggleOn');
pxt.BrowserUtils.removeClass(this.checkElement_, 'blocklyToggleOff');
} else {
pxt.BrowserUtils.removeClass(this.checkElement_, 'blocklyToggleOn');
pxt.BrowserUtils.addClass(this.checkElement_, 'blocklyToggleOff');
}
const outputShape = this.getOutputShape();
let width = 0, halfWidth = 0;
let leftPadding = 0, rightPadding = 0;
switch (outputShape) {
case Blockly.OUTPUT_SHAPE_HEXAGONAL:
width = innerWidth;
halfWidth = width / 2;
let quarterWidth = halfWidth / 2;
// TODO: the left padding calculation is a hack, we should calculate left padding based on width (generic case)
leftPadding = -halfWidth + quarterWidth;
rightPadding = -quarterWidth;
const topLeftPoint = -quarterWidth;
const bottomRightPoint = halfWidth;
this.toggleThumb_.setAttribute('points', `${topLeftPoint},-14 ${topLeftPoint - 14},0 ${topLeftPoint},14 ${bottomRightPoint},14 ${bottomRightPoint + 14},0 ${bottomRightPoint},-14`);
break;
case Blockly.OUTPUT_SHAPE_ROUND:
case Blockly.OUTPUT_SHAPE_SQUARE:
width = 5 + innerWidth;
halfWidth = width / 2;
this.toggleThumb_.setAttribute('width', "" + width);
this.toggleThumb_.setAttribute('x', `-${halfWidth}`);
leftPadding = rightPadding = outputShape == Blockly.OUTPUT_SHAPE_SQUARE ? 2 : -6;
break;
}
this.checkElement_.setAttribute('transform', `translate(${newState ? rightPadding + innerWidth + halfWidth : halfWidth + leftPadding}, ${size.height / 2})`);
}
}
render_() {
if (this.visible_ && this.textElement_) {
// Replace the text.
goog.dom.removeChildren(/** @type {!Element} */(this.textElement_));
let textNode = document.createTextNode(this.getDisplayText_());
this.textElement_.appendChild(textNode);
pxt.BrowserUtils.addClass(this.textElement_ as SVGElement, 'blocklyToggleText');
this.updateSize_();
// Update text centering, based on newly calculated width.
let width = this.size_.width;
let centerTextX = this.state_ ? (width + width / 8) : width / 2;
// Apply new text element x position.
let newX = centerTextX - width / 2;
this.textElement_.setAttribute('x', `${newX}`);
}
// Update any drawn box to the correct width and height.
if (this.borderRect_) {
this.borderRect_.setAttribute('width', `${this.size_.width}`);
this.borderRect_.setAttribute('height', `${this.size_.height}`);
}
}
/**
* Toggle the state of the toggle.
* @private
*/
showEditor_() {
let newState = !this.state_;
/*
if (this.sourceBlock_) {
// Call any validation function, and allow it to override.
newState = this.callValidator(newState);
}*/
if (newState !== null) {
this.setValue(this.toVal(newState));
}
}
private toVal(newState: boolean): string {
if (this.type_ == "number") return String(newState ? '1' : '0');
else return String(newState ? 'true' : 'false');
}
private fromVal(val: string): boolean {
if (typeof val == "string") {
if (val == "1" || val.toUpperCase() == "TRUE") return true;
return false;
}
return !!val;
}
}
}