field_slider.js
4.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
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Number slider input field.
* @author kozbial@google.com (Monica Kozbial)
*/
'use strict';
goog.provide('Blockly.FieldSlider');
goog.require('Blockly.FieldNumber');
/**
* Class for an number slider field.
* @param {string|number=} opt_value The initial value of the field. Should
* cast to a number. Defaults to 0.
* @param {?(string|number)=} opt_min Minimum value.
* @param {?(string|number)=} opt_max Maximum value.
* @param {?(string|number)=} opt_precision Precision for value.
* @param {?Function=} opt_validator A function that is called to validate
* changes to the field's value. Takes in a number & returns a validated
* number, or null to abort the change.
* @extends {Blockly.FieldNumber}
* @constructor
*/
Blockly.FieldSlider = function(opt_value, opt_min, opt_max, opt_precision,
opt_validator) {
Blockly.FieldSlider.superClass_.constructor.call(
this, opt_value, opt_min, opt_max, opt_precision, opt_validator);
/**
* Array holding info needed to unbind events.
* Used for disposing.
* Ex: [[node, name, func], [node, name, func]].
* @type {!Array.<Array<?>>}
* @private
*/
this.boundEvents_ = [];
/**
* The HTML range input element.
* @type {?HTMLInputElement}
* @private
*/
this.sliderInput_ = null;
};
goog.inherits(Blockly.FieldSlider, Blockly.FieldNumber);
/**
* Constructs a FieldSlider from a JSON arg object.
* @param {!Object} options A JSON object with options (value, min, max, and
* precision).
* @return {!FieldSlider} The new field instance.
* @package
*/
Blockly.FieldSlider.fromJson = function(options) {
return new Blockly.FieldSlider(options['value'],
options['min'], options['max'], options['precision']);
};
/**
* Clean up this FieldAngle, as well as the inherited FieldTextInput.
* @return {!Function} Closure to call on destruction of the WidgetDiv.
* @private
*/
Blockly.FieldSlider.prototype.dispose_ = function() {
return function() {
for (var event in this.boundEvents_) {
Blockly.unbindEvent_(event);
}
this.sliderInput_ = null;
};
};
/**
* Show the inline free-text editor on top of the text along with the slider
* editor.
* @protected
* @override
*/
Blockly.FieldSlider.prototype.showEditor_ = function() {
Blockly.FieldSlider.superClass_.showEditor_.call(this, this.useTouchInteraction_);
// Build the DOM.
var editor = document.createElement('div');
editor.className = 'scratchSliderDiv';
var sliderInput = document.createElement('input');
sliderInput.setAttribute('type', 'range');
sliderInput.setAttribute('min', this.min_);
sliderInput.setAttribute('max', this.max_);
sliderInput.setAttribute('step', this.precision_);
sliderInput.setAttribute('value', this.getValue());
sliderInput.className = 'scratchFieldSlider';
sliderInput.style.setProperty('--trackColor', this.sourceBlock_.parentBlock_.getColourTertiary());
editor.appendChild(sliderInput);
this.sliderInput_ = sliderInput;
Blockly.DropDownDiv.getContentDiv().appendChild(editor);
Blockly.DropDownDiv.setColour(this.sourceBlock_.parentBlock_.getColour(),
this.sourceBlock_.getColourTertiary());
Blockly.DropDownDiv.setCategory(this.sourceBlock_.parentBlock_.getCategory());
Blockly.DropDownDiv.showPositionedByBlock(this, this.sourceBlock_);
this.boundEvents_.push(Blockly.bindEvent_(
sliderInput, 'input', this, this.onSliderChange_));
this.boundEvents_.push(Blockly.bindEventWithChecks_(
Blockly.FieldTextInput.htmlInput_, 'keyup', this, this.updateSlider_));
this.boundEvents_.push(Blockly.bindEventWithChecks_(
Blockly.FieldTextInput.htmlInput_, 'keypress', this, this.updateSlider_));
};
/**
* Sets the text to match the slider's position.
* @private
*/
Blockly.FieldSlider.prototype.onSliderChange_ = function() {
Blockly.FieldTextInput.htmlInput_.value = this.sliderInput_.value;
this.setValue(this.sliderInput_.value);
this.validate_();
this.resizeEditor_();
};
/**
* Updates the slider when the field rerenders.
* @private
*/
Blockly.FieldSlider.prototype.updateSlider_ = function() {
if (!this.sliderInput_) {
return;
}
this.sliderInput_.setAttribute('value', this.getValue());
};
Blockly.Field.register('field_slider', Blockly.FieldSlider);