forked from dojo/dgauges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScaleIndicatorBase.js
executable file
·344 lines (303 loc) · 9.67 KB
/
ScaleIndicatorBase.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/on", "dojo/_base/connect", "dojo/_base/fx", "dojox/gfx", "dojox/widget/_Invalidating", "./IndicatorBase"],
function(lang, declare, on, connect, fx, gfx, _Invalidating, IndicatorBase){
return declare("dojox.dgauges.ScaleIndicatorBase", IndicatorBase, {
// summary:
// The base class for indicators that rely on a scale for their rendering.
// Typically, value indicators and range indicators are subclasses of ScaleIndicatorBase.
// summary:
// The scale associated with this indicator.
scale: null,
// summary:
// The value of the indicator. Default is 0.
value: 0,
// interactionArea: String
// How to interact with the indicator using mouse or touch interactions.
// Can be "indicator", "gauge", "area" or "none". The default value is "gauge".
// If set to "indicator", the indicator shape reacts to mouse and touch events.
// If set to "gauge", the whole gauge reacts to mouse and touch events.
// If set to "area", the whole bounding box of the widget reacts to mouse and touch events.
// If "none", interactions are disabled.
interactionArea: "gauge",
// interactionMode: String
// Can be "mouse" or "touch".
interactionMode: "mouse",
// animationDuration: Number
// The duration of the value change animation in milliseconds. Default is 0.
// The animation occurs on both user interactions and programmatic value changes.
// Set this property to 0 to disable animation.
animationDuration: 0,
// animationEaser: Object
// The easer function of the value change animation. Default is fx._defaultEasing.
animationEaser: null,
_indicatorShapeFuncFlag: true,
_interactionAreaFlag: true,
_downListeners: null,
_cursorListeners: null,
_moveAndUpListeners: null,
_transitionValue: NaN,
_preventAnimation: false,
_animation: null,
constructor: function(){
// watches changed happening on the "value" property to call this.valueChanged() function which
// can be listen by user with connect.connect
this.watch("value", lang.hitch(this, function(){
this.valueChanged(this);
}));
this.watch("value", lang.hitch(this, this._startAnimation));
this.watch("interactionArea", lang.hitch(this, function(){
this._interactionAreaFlag = true;
}));
this.watch("interactionMode", lang.hitch(this, function(){
this._interactionAreaFlag = true;
}));
this.watch("indicatorShapeFunc", lang.hitch(this, function(){
this._indicatorShapeFuncFlag = true;
}));
this.addInvalidatingProperties(["scale", "value", "indicatorShapeFunc", "interactionArea", "interactionMode"]);
this._downListeners = [];
this._moveAndUpListeners = [];
this._cursorListeners = [];
},
_startAnimation: function(prop, oldValue, newValue){
// summary:
// Internal method.
// tags:
// private
if(this.animationDuration == 0){
return;
}
if(this._animation && (this._preventAnimation || oldValue == newValue)){
this._animation.stop();
return;
}
this._animation = new fx.Animation({curve: [oldValue, newValue],
duration: this.animationDuration,
easing: this.animationEaser ? this.animationEaser : fx._defaultEasing,
onAnimate: lang.hitch(this, this._updateAnimation),
onEnd: lang.hitch(this, this._endAnimation),
onStop: lang.hitch(this, this._endAnimation)});
this._animation.play();
},
_updateAnimation: function(v){
// summary:
// Internal method.
// tags:
// private
this._transitionValue = v;
this.invalidateRendering();
},
_endAnimation: function(){
// summary:
// Internal method.
// tags:
// private
this._transitionValue = NaN;
this.invalidateRendering();
},
refreshRendering: function(){
if(this._gfxGroup == null || this.scale == null){
return;
}else{
if(this._indicatorShapeFuncFlag && lang.isFunction(this.indicatorShapeFunc)){
this._gfxGroup.clear();
this.indicatorShapeFunc(this._gfxGroup, this);
this._indicatorShapeFuncFlag = false;
}
if(this._interactionAreaFlag){
this._interactionAreaFlag = this._connectDownListeners();
}
}
},
valueChanged: function(indicator){
// summary:
// Invoked when the value of the indicator changes.
// User can connect an listener on this function:
// | connect.connect(theIndicator, "valueChanged", lang.hitch(this, function(){
// | //do something
// | }));
on.emit(this, "valueChanged", {
target: this,
bubbles: true,
cancelable: true
});
},
_disconnectDownListeners: function(){
// summary:
// Internal method.
// tags:
// private
for(var i = 0; i < this._downListeners.length; i++){
connect.disconnect(this._downListeners[i]);
}
this._downListeners = [];
},
_disconnectMoveAndUpListeners: function(){
// summary:
// Internal method.
// tags:
// private
for(var i = 0; i < this._moveAndUpListeners.length; i++){
connect.disconnect(this._moveAndUpListeners[i]);
}
this._moveAndUpListeners = [];
},
_disconnectListeners: function(){
// summary:
// Internal method.
// tags:
// private
this._disconnectDownListeners();
this._disconnectMoveAndUpListeners();
this._disconnectCursorListeners();
},
_connectCursorListeners: function(target){
// summary:
// Internal method.
// tags:
// private
var listener = target.connect("onmouseover", this, function(){
this.scale._gauge._setCursor("pointer");
});
this._cursorListeners.push(listener);
listener = target.connect("onmouseout", this, function(event){
this.scale._gauge._setCursor("");
}
);
this._cursorListeners.push(listener);
},
_disconnectCursorListeners: function(){
// summary:
// Internal method.
// tags:
// private
for(var i = 0; i < this._cursorListeners.length; i++){
connect.disconnect(this._cursorListeners[i]);
}
this._cursorListeners = [];
},
_connectDownListeners: function(){
// summary:
// Internal method.
// tags:
// private
this._disconnectDownListeners();
this._disconnectCursorListeners();
var listener = null;
var downEventName;
if(this.interactionMode == "mouse"){
downEventName = "onmousedown";
}else if(this.interactionMode == "touch"){
downEventName = "ontouchstart";
}
if(this.interactionMode == "mouse" || this.interactionMode == "touch"){
if(this.interactionArea == "indicator"){
listener = this._gfxGroup.connect(downEventName, this, this._onMouseDown);
this._downListeners.push(listener);
if (this.interactionMode == "mouse") {
this._connectCursorListeners(this._gfxGroup);
}
}else if(this.interactionArea == "gauge"){
if(!this.scale || !this.scale._gauge || !this.scale._gauge._gfxGroup){
return true;
}
listener = this.scale._gauge._gfxGroup.connect(downEventName, this, this._onMouseDown);
this._downListeners.push(listener);
listener = this._gfxGroup.connect(downEventName, this, this._onMouseDown);
this._downListeners.push(listener);
if (this.interactionMode == "mouse") {
this._connectCursorListeners(this.scale._gauge._gfxGroup);
}
}else if(this.interactionArea == "area"){
if(!this.scale || !this.scale._gauge || !this.scale._gauge._baseGroup){
return true;
}
listener = this.scale._gauge._baseGroup.connect(downEventName, this, this._onMouseDown);
this._downListeners.push(listener);
listener = this._gfxGroup.connect(downEventName, this, this._onMouseDown);
this._downListeners.push(listener);
if (this.interactionMode == "mouse") {
this._connectCursorListeners(this.scale._gauge._baseGroup);
}
}
}
return false;
},
_connectMoveAndUpListeners: function(){
// summary:
// Internal method.
// tags:
// private
var listener = null;
var moveEventName;
var upEventName;
if(this.interactionMode == "mouse"){
moveEventName = "onmousemove";
upEventName = "onmouseup";
}else if(this.interactionMode == "touch"){
moveEventName = "ontouchmove";
upEventName = "ontouchend";
}
listener = this.scale._gauge._baseGroup.connect(moveEventName, this, this._onMouseMove);
this._moveAndUpListeners.push(listener);
listener = this._gfxGroup.connect(moveEventName, this, this._onMouseMove);
this._moveAndUpListeners.push(listener);
listener = this.scale._gauge._baseGroup.connect(upEventName, this, this._onMouseUp);
this._moveAndUpListeners.push(listener);
listener = this._gfxGroup.connect(upEventName, this, this._onMouseUp);
this._moveAndUpListeners.push(listener);
},
_onMouseDown: function(event){
// summary:
// Internal method.
// tags:
// private
this._connectMoveAndUpListeners();
this._startEditing();
},
_onMouseMove: function(event){
// summary:
// Internal method.
// tags:
// private
this._preventAnimation = true;
if(this._animation){
this._animation.stop();
}
},
_onMouseUp: function(event){
// summary:
// Internal method.
// tags:
// private
this._disconnectMoveAndUpListeners();
this._preventAnimation = false;
this._endEditing();
},
_startEditing: function(){
// summary:
// Internal method.
// tags:
// private
if(!this.scale || !this.scale._gauge){
return;
}else{
this.scale._gauge.onStartEditing({
indicator: this
});
}
},
_endEditing: function(){
// summary:
// Internal method.
// tags:
// private
if(!this.scale || !this.scale._gauge){
return;
}else{
this.scale._gauge.onEndEditing({
indicator: this
});
}
}
});
});