forked from RaspberryPiFoundation/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield_checkbox.ts
More file actions
285 lines (252 loc) · 7.88 KB
/
field_checkbox.ts
File metadata and controls
285 lines (252 loc) · 7.88 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
/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Checkbox field. Checked or not checked.
*
* @class
*/
// Former goog.module ID: Blockly.FieldCheckbox
// Unused import preserved for side-effects. Remove if unneeded.
import './events/events_block_change.js';
import {Field, FieldConfig, FieldValidator} from './field.js';
import * as fieldRegistry from './field_registry.js';
import * as aria from './utils/aria.js';
import * as dom from './utils/dom.js';
type BoolString = 'TRUE' | 'FALSE';
type CheckboxBool = BoolString | boolean;
/**
* Class for a checkbox field.
*/
export class FieldCheckbox extends Field<CheckboxBool> {
/** Default character for the checkmark. */
static readonly CHECK_CHAR = '✓';
private checkChar: string;
/**
* Serializable fields are saved by the serializer, non-serializable fields
* are not. Editable fields should also be serializable.
*/
override SERIALIZABLE = true;
/**
* NOTE: The default value is set in `Field`, so maintain that value instead
* of overwriting it here or in the constructor.
*/
override value_: boolean | null = this.value_;
/**
* @param value The initial value of the field. Should either be 'TRUE',
* 'FALSE' or a boolean. Defaults to 'FALSE'. Also accepts
* Field.SKIP_SETUP if you wish to skip setup (only used by subclasses
* that want to handle configuration and setting the field value after
* their own constructors have run).
* @param validator A function that is called to validate changes to the
* field's value. Takes in a value ('TRUE' or 'FALSE') & returns a
* validated value ('TRUE' or 'FALSE'), or null to abort the change.
* @param config A map of options used to configure the field.
* See the [field creation documentation]{@link
* https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/checkbox#creation}
* for a list of properties this parameter supports.
*/
constructor(
value?: CheckboxBool | typeof Field.SKIP_SETUP,
validator?: FieldCheckboxValidator,
config?: FieldCheckboxConfig,
) {
super(Field.SKIP_SETUP);
/**
* Character for the check mark. Used to apply a different check mark
* character to individual fields.
*/
this.checkChar = FieldCheckbox.CHECK_CHAR;
if (value === Field.SKIP_SETUP) return;
if (config) {
this.configure_(config);
}
this.setValue(value);
if (validator) {
this.setValidator(validator);
}
}
/**
* Configure the field based on the given map of options.
*
* @param config A map of options to configure the field based on.
*/
protected override configure_(config: FieldCheckboxConfig) {
super.configure_(config);
if (config.checkCharacter) this.checkChar = config.checkCharacter;
}
/**
* Saves this field's value.
*
* @returns The boolean value held by this field.
* @internal
*/
override saveState(): AnyDuringMigration {
const legacyState = this.saveLegacyState(FieldCheckbox);
if (legacyState !== null) {
return legacyState;
}
return this.getValueBoolean();
}
/**
* Create the block UI for this checkbox.
*/
override initView() {
super.initView();
const textElement = this.getTextElement();
dom.addClass(this.fieldGroup_!, 'blocklyCheckboxField');
textElement.style.display = this.value_ ? 'block' : 'none';
this.recomputeAria();
}
override getAriaValue(): string {
return this.value_ ? 'checked' : 'not checked';
}
private recomputeAria() {
const element = this.getFocusableElement();
aria.setRole(element, aria.Role.CHECKBOX);
aria.setState(
element,
aria.State.LABEL,
this.getAriaTypeName() ?? 'Checkbox',
);
aria.setState(element, aria.State.CHECKED, !!this.value_);
}
override render_() {
if (this.textContent_) {
this.textContent_.nodeValue = this.getDisplayText_();
}
this.updateSize_(this.getConstants()!.FIELD_CHECKBOX_X_OFFSET);
}
override getDisplayText_() {
return this.checkChar;
}
/**
* Set the character used for the check mark.
*
* @param character The character to use for the check mark, or null to use
* the default.
*/
setCheckCharacter(character: string | null) {
this.checkChar = character || FieldCheckbox.CHECK_CHAR;
this.forceRerender();
}
/** Toggle the state of the checkbox on click. */
protected override showEditor_() {
this.setValue(!this.value_);
this.recomputeAria();
}
/**
* Ensure that the input value is valid ('TRUE' or 'FALSE').
*
* @param newValue The input value.
* @returns A valid value ('TRUE' or 'FALSE), or null if invalid.
*/
protected override doClassValidation_(
newValue?: AnyDuringMigration,
): BoolString | null {
if (newValue === true || newValue === 'TRUE') {
return 'TRUE';
}
if (newValue === false || newValue === 'FALSE') {
return 'FALSE';
}
return null;
}
/**
* Update the value of the field, and update the checkElement.
*
* @param newValue The value to be saved. The default validator guarantees
* that this is a either 'TRUE' or 'FALSE'.
*/
protected override doValueUpdate_(newValue: BoolString) {
this.value_ = this.convertValueToBool(newValue);
// Update visual.
if (this.textElement_) {
this.textElement_.style.display = this.value_ ? 'block' : 'none';
}
}
/**
* Get the value of this field, either 'TRUE' or 'FALSE'.
*
* @returns The value of this field.
*/
override getValue(): BoolString {
return this.value_ ? 'TRUE' : 'FALSE';
}
/**
* Get the boolean value of this field.
*
* @returns The boolean value of this field.
*/
getValueBoolean(): boolean | null {
return this.value_;
}
/**
* Get the text of this field. Used when the block is collapsed.
*
* @returns Text representing the value of this field ('true' or 'false').
*/
override getText(): string {
return String(this.convertValueToBool(this.value_));
}
/**
* Convert a value into a pure boolean.
*
* Converts 'TRUE' to true and 'FALSE' to false correctly, everything else
* is cast to a boolean.
*
* @param value The value to convert.
* @returns The converted value.
*/
private convertValueToBool(value: CheckboxBool | null): boolean {
if (typeof value === 'string') return value === 'TRUE';
return !!value;
}
/**
* Construct a FieldCheckbox from a JSON arg object.
*
* @param options A JSON object with options (checked).
* @returns The new field instance.
* @nocollapse
* @internal
*/
static override fromJson(
options: FieldCheckboxFromJsonConfig,
): FieldCheckbox {
// `this` might be a subclass of FieldCheckbox if that class doesn't
// 'override' the static fromJson method.
return new this(options.checked, undefined, options);
}
}
fieldRegistry.register('field_checkbox', FieldCheckbox);
FieldCheckbox.prototype.DEFAULT_VALUE = false;
/**
* Config options for the checkbox field.
*/
export interface FieldCheckboxConfig extends FieldConfig {
checkCharacter?: string;
}
/**
* fromJson config options for the checkbox field.
*/
export interface FieldCheckboxFromJsonConfig extends FieldCheckboxConfig {
checked?: boolean;
}
/**
* A function that is called to validate changes to the field's value before
* they are set.
*
* @see {@link https://developers.google.com/blockly/guides/create-custom-blocks/fields/validators#return_values}
* @param newValue The value to be validated.
* @returns One of three instructions for setting the new value: `T`, `null`,
* or `undefined`.
*
* - `T` to set this function's returned value instead of `newValue`.
*
* - `null` to invoke `doValueInvalid_` and not set a value.
*
* - `undefined` to set `newValue` as is.
*/
export type FieldCheckboxValidator = FieldValidator<CheckboxBool>;