1 | define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/window", "dojo/on", "dojo/touch", "dojo/_base/fx", "dojox/gfx", "dojox/widget/_Invalidating", "./IndicatorBase"], |
---|
2 | function(lang, declare, win, on, touch, fx, gfx, _Invalidating, IndicatorBase){ |
---|
3 | return declare("dojox.dgauges.ScaleIndicatorBase", IndicatorBase, { |
---|
4 | // summary: |
---|
5 | // The base class for indicators that rely on a scale for their rendering. |
---|
6 | // Typically, value indicators and range indicators are subclasses of ScaleIndicatorBase. |
---|
7 | |
---|
8 | // summary: |
---|
9 | // The scale associated with this indicator. |
---|
10 | scale: null, |
---|
11 | // summary: |
---|
12 | // The value of the indicator. Default is 0. |
---|
13 | value: 0, |
---|
14 | |
---|
15 | // interactionArea: String |
---|
16 | // How to interact with the indicator using mouse or touch interactions. |
---|
17 | // Can be "indicator", "gauge", "area" or "none". The default value is "gauge". |
---|
18 | // If set to "indicator", the indicator shape reacts to mouse and touch events. |
---|
19 | // If set to "gauge", the whole gauge reacts to mouse and touch events. |
---|
20 | // If set to "area", the whole bounding box of the widget reacts to mouse and touch events. |
---|
21 | // If "none", interactions are disabled. |
---|
22 | interactionArea: "gauge", |
---|
23 | |
---|
24 | // interactionMode: String |
---|
25 | // Deprecated. Can be "mouse" or "touch". |
---|
26 | interactionMode: "mouse", |
---|
27 | |
---|
28 | // animationDuration: Number |
---|
29 | // The duration of the value change animation in milliseconds. Default is 0. |
---|
30 | // The animation occurs on both user interactions and programmatic value changes. |
---|
31 | // Set this property to 0 to disable animation. |
---|
32 | animationDuration: 0, |
---|
33 | |
---|
34 | // animationEaser: Object |
---|
35 | // The easer function of the value change animation. Default is fx._defaultEasing. |
---|
36 | animationEaser: null, |
---|
37 | |
---|
38 | _indicatorShapeFuncFlag: true, |
---|
39 | |
---|
40 | _interactionAreaFlag: true, |
---|
41 | |
---|
42 | _downListeners: null, |
---|
43 | |
---|
44 | _cursorListeners: null, |
---|
45 | |
---|
46 | _moveAndUpListeners: null, |
---|
47 | |
---|
48 | _transitionValue: NaN, |
---|
49 | |
---|
50 | _preventAnimation: false, |
---|
51 | |
---|
52 | _animation: null, |
---|
53 | |
---|
54 | constructor: function(){ |
---|
55 | |
---|
56 | // watches changed happening on the "value" property |
---|
57 | this.watch("value", lang.hitch(this, function(){ |
---|
58 | this.valueChanged(this); |
---|
59 | })); |
---|
60 | this.watch("value", lang.hitch(this, this._startAnimation)); |
---|
61 | |
---|
62 | this.watch("interactionArea", lang.hitch(this, function(){ |
---|
63 | this._interactionAreaFlag = true; |
---|
64 | })); |
---|
65 | this.watch("interactionMode", lang.hitch(this, function(){ |
---|
66 | this._interactionAreaFlag = true; |
---|
67 | })); |
---|
68 | |
---|
69 | this.watch("indicatorShapeFunc", lang.hitch(this, function(){ |
---|
70 | this._indicatorShapeFuncFlag = true; |
---|
71 | })); |
---|
72 | |
---|
73 | this.addInvalidatingProperties(["scale", "value", "indicatorShapeFunc", "interactionArea", "interactionMode"]); |
---|
74 | |
---|
75 | this._downListeners = []; |
---|
76 | this._moveAndUpListeners = []; |
---|
77 | this._cursorListeners = []; |
---|
78 | }, |
---|
79 | |
---|
80 | _startAnimation: function(prop, oldValue, newValue){ |
---|
81 | // summary: |
---|
82 | // Internal method. |
---|
83 | // tags: |
---|
84 | // private |
---|
85 | if(this.animationDuration == 0){ |
---|
86 | return; |
---|
87 | } |
---|
88 | if(this._animation && (this._preventAnimation || oldValue == newValue)){ |
---|
89 | this._animation.stop(); |
---|
90 | return; |
---|
91 | } |
---|
92 | this._animation = new fx.Animation({curve: [oldValue, newValue], |
---|
93 | duration: this.animationDuration, |
---|
94 | easing: this.animationEaser ? this.animationEaser : fx._defaultEasing, |
---|
95 | onAnimate: lang.hitch(this, this._updateAnimation), |
---|
96 | onEnd: lang.hitch(this, this._endAnimation), |
---|
97 | onStop: lang.hitch(this, this._endAnimation)}); |
---|
98 | |
---|
99 | this._animation.play(); |
---|
100 | }, |
---|
101 | |
---|
102 | _updateAnimation: function(v){ |
---|
103 | // summary: |
---|
104 | // Internal method. |
---|
105 | // tags: |
---|
106 | // private |
---|
107 | this._transitionValue = v; |
---|
108 | this.invalidateRendering(); |
---|
109 | }, |
---|
110 | |
---|
111 | _endAnimation: function(){ |
---|
112 | // summary: |
---|
113 | // Internal method. |
---|
114 | // tags: |
---|
115 | // private |
---|
116 | this._transitionValue = NaN; |
---|
117 | this.invalidateRendering(); |
---|
118 | }, |
---|
119 | |
---|
120 | refreshRendering: function(){ |
---|
121 | if(this._gfxGroup == null || this.scale == null){ |
---|
122 | return; |
---|
123 | }else{ |
---|
124 | if(this._indicatorShapeFuncFlag && lang.isFunction(this.indicatorShapeFunc)){ |
---|
125 | this._gfxGroup.clear(); |
---|
126 | this.indicatorShapeFunc(this._gfxGroup, this); |
---|
127 | this._indicatorShapeFuncFlag = false; |
---|
128 | } |
---|
129 | |
---|
130 | if(this._interactionAreaFlag){ |
---|
131 | this._interactionAreaFlag = this._connectDownListeners(); |
---|
132 | } |
---|
133 | } |
---|
134 | }, |
---|
135 | |
---|
136 | valueChanged: function(indicator){ |
---|
137 | // summary: |
---|
138 | // Invoked when the value of the indicator changes. |
---|
139 | // User can connect an listener on this function: |
---|
140 | // | theIndicator.on("valueChanged", lang.hitch(this, function(){ |
---|
141 | // | //do something |
---|
142 | // | })); |
---|
143 | on.emit(this, "valueChanged", { |
---|
144 | target: this, |
---|
145 | bubbles: true, |
---|
146 | cancelable: true |
---|
147 | }); |
---|
148 | }, |
---|
149 | |
---|
150 | _disconnectDownListeners: function(){ |
---|
151 | // summary: |
---|
152 | // Internal method. |
---|
153 | // tags: |
---|
154 | // private |
---|
155 | for(var i = 0; i < this._downListeners.length; i++){ |
---|
156 | this._downListeners[i].remove(); |
---|
157 | } |
---|
158 | this._downListeners = []; |
---|
159 | }, |
---|
160 | |
---|
161 | _disconnectMoveAndUpListeners: function(){ |
---|
162 | // summary: |
---|
163 | // Internal method. |
---|
164 | // tags: |
---|
165 | // private |
---|
166 | for(var i = 0; i < this._moveAndUpListeners.length; i++){ |
---|
167 | this._moveAndUpListeners[i].remove(); |
---|
168 | } |
---|
169 | this._moveAndUpListeners = []; |
---|
170 | }, |
---|
171 | |
---|
172 | _disconnectListeners: function(){ |
---|
173 | // summary: |
---|
174 | // Internal method. |
---|
175 | // tags: |
---|
176 | // private |
---|
177 | this._disconnectDownListeners(); |
---|
178 | this._disconnectMoveAndUpListeners(); |
---|
179 | this._disconnectCursorListeners(); |
---|
180 | }, |
---|
181 | |
---|
182 | _connectCursorListeners: function(target){ |
---|
183 | // summary: |
---|
184 | // Internal method. |
---|
185 | // tags: |
---|
186 | // private |
---|
187 | var listener = target.on(touch.over , lang.hitch(this, function(){ |
---|
188 | this.scale._gauge._setCursor("pointer"); |
---|
189 | })); |
---|
190 | this._cursorListeners.push(listener); |
---|
191 | listener = target.on(touch.out, lang.hitch(this, function(event){ |
---|
192 | this.scale._gauge._setCursor(""); |
---|
193 | } |
---|
194 | )); |
---|
195 | this._cursorListeners.push(listener); |
---|
196 | }, |
---|
197 | |
---|
198 | _disconnectCursorListeners: function(){ |
---|
199 | // summary: |
---|
200 | // Internal method. |
---|
201 | // tags: |
---|
202 | // private |
---|
203 | for(var i = 0; i < this._cursorListeners.length; i++){ |
---|
204 | this._cursorListeners[i].remove(); |
---|
205 | } |
---|
206 | this._cursorListeners = []; |
---|
207 | }, |
---|
208 | |
---|
209 | _connectDownListeners: function(){ |
---|
210 | // summary: |
---|
211 | // Internal method. |
---|
212 | // tags: |
---|
213 | // private |
---|
214 | this._disconnectDownListeners(); |
---|
215 | this._disconnectCursorListeners(); |
---|
216 | var listener = null; |
---|
217 | |
---|
218 | if(this.interactionMode == "mouse" || this.interactionMode == "touch"){ |
---|
219 | if(this.interactionArea == "indicator"){ |
---|
220 | listener = this._gfxGroup.on(touch.press, lang.hitch(this, this._onMouseDown)); |
---|
221 | this._downListeners.push(listener); |
---|
222 | this._connectCursorListeners(this._gfxGroup); |
---|
223 | }else if(this.interactionArea == "gauge"){ |
---|
224 | if(!this.scale || !this.scale._gauge || !this.scale._gauge._gfxGroup){ |
---|
225 | return true; |
---|
226 | } |
---|
227 | listener = this.scale._gauge._gfxGroup.on(touch.press, lang.hitch(this, this._onMouseDown)); |
---|
228 | this._downListeners.push(listener); |
---|
229 | listener = this._gfxGroup.on(touch.press, lang.hitch(this, this._onMouseDown)); |
---|
230 | this._downListeners.push(listener); |
---|
231 | this._connectCursorListeners(this.scale._gauge._gfxGroup); |
---|
232 | }else if(this.interactionArea == "area"){ |
---|
233 | if(!this.scale || !this.scale._gauge || !this.scale._gauge._baseGroup){ |
---|
234 | return true; |
---|
235 | } |
---|
236 | listener = this.scale._gauge._baseGroup.on(touch.press, lang.hitch(this, this._onMouseDown)); |
---|
237 | this._downListeners.push(listener); |
---|
238 | listener = this._gfxGroup.on(touch.press, lang.hitch(this, this._onMouseDown)); |
---|
239 | this._downListeners.push(listener); |
---|
240 | this._connectCursorListeners(this.scale._gauge._baseGroup); |
---|
241 | } |
---|
242 | } |
---|
243 | return false; |
---|
244 | }, |
---|
245 | |
---|
246 | _connectMoveAndUpListeners: function(){ |
---|
247 | // summary: |
---|
248 | // Internal method. |
---|
249 | // tags: |
---|
250 | // private |
---|
251 | var listener = null; |
---|
252 | |
---|
253 | listener = on(win.doc, touch.move, lang.hitch(this, this._onMouseMove)); |
---|
254 | this._moveAndUpListeners.push(listener); |
---|
255 | |
---|
256 | listener = on(win.doc, touch.release, lang.hitch(this, this._onMouseUp)); |
---|
257 | this._moveAndUpListeners.push(listener); |
---|
258 | }, |
---|
259 | |
---|
260 | _onMouseDown: function(event){ |
---|
261 | // summary: |
---|
262 | // Internal method. |
---|
263 | // tags: |
---|
264 | // private |
---|
265 | this._connectMoveAndUpListeners(); |
---|
266 | this._startEditing(); |
---|
267 | }, |
---|
268 | |
---|
269 | _onMouseMove: function(event){ |
---|
270 | // summary: |
---|
271 | // Internal method. |
---|
272 | // tags: |
---|
273 | // private |
---|
274 | this._preventAnimation = true; |
---|
275 | if(this._animation){ |
---|
276 | this._animation.stop(); |
---|
277 | } |
---|
278 | }, |
---|
279 | |
---|
280 | _onMouseUp: function(event){ |
---|
281 | // summary: |
---|
282 | // Internal method. |
---|
283 | // tags: |
---|
284 | // private |
---|
285 | this._disconnectMoveAndUpListeners(); |
---|
286 | this._preventAnimation = false; |
---|
287 | this._endEditing(); |
---|
288 | }, |
---|
289 | |
---|
290 | _startEditing: function(){ |
---|
291 | // summary: |
---|
292 | // Internal method. |
---|
293 | // tags: |
---|
294 | // private |
---|
295 | if(!this.scale || !this.scale._gauge){ |
---|
296 | return; |
---|
297 | }else{ |
---|
298 | this.scale._gauge.onStartEditing({ |
---|
299 | indicator: this |
---|
300 | }); |
---|
301 | } |
---|
302 | }, |
---|
303 | |
---|
304 | _endEditing: function(){ |
---|
305 | // summary: |
---|
306 | // Internal method. |
---|
307 | // tags: |
---|
308 | // private |
---|
309 | if(!this.scale || !this.scale._gauge){ |
---|
310 | return; |
---|
311 | }else{ |
---|
312 | this.scale._gauge.onEndEditing({ |
---|
313 | indicator: this |
---|
314 | }); |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | }); |
---|
319 | }); |
---|