Debian Codegraph @

dojox/form/_RangeSliderMixin.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare",
3 "dojo/_base/lang",
4 "dojo/_base/array",
5 "dojo/_base/fx",
6 "dojo/_base/event",
7 "dojo/_base/sniff",
8 "dojo/dom-style",
9 "dojo/dom-geometry",
10 "dojo/keys",
11 "dijit",
12 "dojo/dnd/Mover",
13 "dojo/dnd/Moveable",
14 "dijit/form/_FormValueWidget",
15 "dijit/focus",
16 "dojo/fx",
17 "dojox/fx" // unused?
18 ], function(declare, lang, array, fx, event, has, domStyle, domGeometry, keys, dijit, Mover, Moveable, FormValueWidget, FocusManager, fxUtils){
19
20 // make these functions once:
21 var sortReversed = function(a, b){ return b - a; },
22 sortForward = function(a, b){ return a - b; };
23
24
25 var RangeSliderMixin = declare("dojox.form._RangeSliderMixin", null, {
26
27 // list attach points that have tabIndex so they can be properly disabled
28 _setTabIndexAttr: ["sliderHandle", "sliderHandleMax"],
29
30 value: [0,100],
31 postMixInProperties: function(){
32 this.inherited(arguments);
33 this.value = array.map(this.value, function(i){ return parseInt(i, 10); });
34 },
35
36 postCreate: function(){
37 this.inherited(arguments);
38 // we sort the values!
39 // TODO: re-think, how to set the value
40 this.value.sort(sortForward);
41
42 // define a custom constructor for a SliderMoverMax that points back to me
43 var _self = this;
44 var mover = declare(SliderMoverMax, {
45 constructor: function(){
46 this.widget = _self;
47 }
48 });
49
50 this._movableMax = new Moveable(this.sliderHandleMax,{ mover: mover });
51
52 //The valuenow of the sliderHandle (min) usually determines the valuemin of sliderHandleMax
53 //and valuenow of sliderHandleMax usually determines the valueMax of sliderHandle
54 //However, in our RangeSlider dragging one handle past the other one causes both to
55 //'snap' together and move so both sliders will have the same min, max values
56
57 this.sliderHandle.setAttribute("aria-valuemin", this.minimum);
58 this.sliderHandle.setAttribute("aria-valuemax", this.maximum);
59 this.sliderHandleMax.setAttribute("aria-valuemin", this.minimum);
60 this.sliderHandleMax.setAttribute("aria-valuemax", this.maximum);
61
62 // a dnd for the bar!
63 var barMover = declare(SliderBarMover, {
64 constructor: function(){
65 this.widget = _self;
66 }
67 });
68 this._movableBar = new Moveable(this.progressBar,{ mover: barMover });
69
70 // Remove these from the progress bar since it isn't a slider
71 this.focusNode.removeAttribute("aria-valuemin");
72 this.focusNode.removeAttribute("aria-valuemax");
73 this.focusNode.removeAttribute("aria-valuenow");
74
75 },
76
77 destroy: function(){
78 this.inherited(arguments);
79 this._movableMax.destroy();
80 this._movableBar.destroy();
81 },
82
83 _onKeyPress: function(/*Event*/ e){
84 if(this.disabled || this.readOnly || e.altKey || e.ctrlKey){ return; }
85
86 var useMaxValue = e.target === this.sliderHandleMax;
87 var barFocus = e.target === this.progressBar;
88 var k = lang.delegate(keys, this.isLeftToRight() ? {PREV_ARROW: keys.LEFT_ARROW, NEXT_ARROW: keys.RIGHT_ARROW}
89 : {PREV_ARROW: keys.RIGHT_ARROW, NEXT_ARROW: keys.LEFT_ARROW});
90 var delta = 0;
91 var down = false;
92
93 switch(e.keyCode){
94 case k.HOME : this._setValueAttr(this.minimum, true, useMaxValue);event.stop(e);return;
95 case k.END : this._setValueAttr(this.maximum, true, useMaxValue);event.stop(e);return;
96 case k.PREV_ARROW :
97 case k.DOWN_ARROW : down = true;
98 case k.NEXT_ARROW :
99 case k.UP_ARROW : delta = 1; break;
100 case k.PAGE_DOWN : down = true;
101 case k.PAGE_UP : delta = this.pageIncrement; break;
102 default : this.inherited(arguments);return;
103 }
104
105 if(down){delta = -delta;}
106
107 if(delta){
108 if(barFocus){
109 this._bumpValue([
110 { change: delta, useMaxValue: false },
111 { change: delta, useMaxValue: true }
112 ]);
113 }else{
114 this._bumpValue(delta, useMaxValue);
115 }
116 event.stop(e);
117 }
118 },
119
120 _onHandleClickMax: function(e){
121 if(this.disabled || this.readOnly){ return; }
122 if(!has("ie")){
123 // make sure you get focus when dragging the handle
124 // (but don't do on IE because it causes a flicker on mouse up (due to blur then focus)
125 FocusManager.focus(this.sliderHandleMax);
126 }
127 event.stop(e);
128 },
129
130 _onClkIncBumper: function(){
131 this._setValueAttr(this._descending === false ? this.minimum : this.maximum, true, true);
132 },
133
134 _bumpValue: function(signedChange, useMaxValue){
135
136 // we pass an array to _setValueAttr when signedChange is an array
137 var value = lang.isArray(signedChange) ? [
138 this._getBumpValue(signedChange[0].change, signedChange[0].useMaxValue),
139 this._getBumpValue(signedChange[1].change, signedChange[1].useMaxValue)
140 ]
141 : this._getBumpValue(signedChange, useMaxValue);
142
143 this._setValueAttr(value, true, useMaxValue);
144 },
145
146 _getBumpValue: function(signedChange, useMaxValue){
147
148 var idx = useMaxValue ? 1 : 0;
149
150 var s = domStyle.getComputedStyle(this.sliderBarContainer),
151 c = domGeometry.getContentBox(this.sliderBarContainer, s),
152 count = this.discreteValues,
153 myValue = this.value[idx]
154 ;
155
156 if(count <= 1 || count == Infinity){ count = c[this._pixelCount]; }
157 count--;
158
159 var value = this.maximum > this.minimum ? ((myValue - this.minimum) * count / (this.maximum - this.minimum) + signedChange) : 0;
160 if(value < 0){ value = 0; }
161 if(value > count){ value = count; }
162
163 return value * (this.maximum - this.minimum) / count + this.minimum;
164 },
165
166 _onBarClick: function(e){
167 if(this.disabled || this.readOnly){ return; }
168 if(!has("ie")){
169 // make sure you get focus when dragging the handle
170 // (but don't do on IE because it causes a flicker on mouse up (due to blur then focus)
171 FocusManager.focus(this.progressBar);
172 }
173 event.stop(e);
174 },
175
176 _onRemainingBarClick: function(e){
177 if(this.disabled || this.readOnly){ return; }
178 if(!has("ie")){
179 // make sure you get focus when dragging the handle
180 // (but don't do on IE because it causes a flicker on mouse up (due to blur then focus)
181 FocusManager.focus(this.progressBar);
182 }
183
184 // now we set the min/max-value of the slider!
185 var abspos = domGeometry.position(this.sliderBarContainer, true),
186 bar = domGeometry.position(this.progressBar, true),
187 mousePos = e[this._mousePixelCoord],
188 leftPos = bar[this._startingPixelCoord],
189 rightPos = leftPos + bar[this._pixelCount],
190 isMaxVal = this._isReversed() ? mousePos <= leftPos : mousePos >= rightPos,
191 p = this._isReversed() ? (abspos[this._pixelCount] - mousePos + abspos[this._startingPixelCoord]) : (mousePos - abspos[this._startingPixelCoord])
192 ;
193
194 this._setPixelValue(p, abspos[this._pixelCount], true, isMaxVal);
195 event.stop(e);
196 },
197
198 _setPixelValue: function(/*Number*/ pixelValue, /*Number*/ maxPixels, /*Boolean*/ priorityChange, /*Boolean*/ isMaxVal){
199 if(this.disabled || this.readOnly){ return; }
200 var myValue = this._getValueByPixelValue(pixelValue, maxPixels);
201 this._setValueAttr(myValue, priorityChange, isMaxVal);
202 },
203
204 _getValueByPixelValue: function(/*Number*/ pixelValue, /*Number*/ maxPixels){
205 pixelValue = pixelValue < 0 ? 0 : maxPixels < pixelValue ? maxPixels : pixelValue;
206 var count = this.discreteValues;
207 if(count <= 1 || count == Infinity){ count = maxPixels; }
208 count--;
209 var pixelsPerValue = maxPixels / count;
210 var wholeIncrements = Math.round(pixelValue / pixelsPerValue);
211 return (this.maximum-this.minimum)*wholeIncrements/count + this.minimum;
212 },
213
214 _setValueAttr: function(/*Array or Number*/ value, /*Boolean, optional*/ priorityChange, /*Boolean, optional*/ isMaxVal){
215 // we pass an array, when we move the slider with the bar
216 var actValue = lang.clone(this.value);
217
218 if(!lang.isArray(value)){
219 actValue[isMaxVal ? 1 : 0] = value;
220 }else{
221 actValue = value;
222 }
223 // we have to reset this values. don't know the reason for that
224 this._lastValueReported = "";
225 this.valueNode.value = value = actValue;
226
227 actValue.sort(sortForward);
228
229 this.sliderHandle.setAttribute("aria-valuenow", actValue[0]);
230 this.sliderHandleMax.setAttribute("aria-valuenow", actValue[1]);
231
232 // not calling the _setValueAttr-function of Slider, but the super-super-class (needed for the onchange-event!)
233 FormValueWidget.prototype._setValueAttr.apply(this, arguments);
234
235 this._printSliderBar(priorityChange, isMaxVal);
236 },
237
238 _printSliderBar: function(priorityChange, isMaxVal){
239 var percentMin = this.maximum > this.minimum ? ((this.value[0] - this.minimum) / (this.maximum - this.minimum)) : 0;
240 var percentMax = this.maximum > this.minimum ? ((this.value[1] - this.minimum) / (this.maximum - this.minimum)) : 0;
241 var percentMinSave = percentMin;
242 if(percentMin > percentMax){
243 percentMin = percentMax;
244 percentMax = percentMinSave;
245 }
246 var sliderHandleVal = this._isReversed() ? ((1-percentMin)*100) : (percentMin * 100);
247 var sliderHandleMaxVal = this._isReversed() ? ((1-percentMax)*100) : (percentMax * 100);
248 var progressBarVal = this._isReversed() ? ((1-percentMax)*100) : (percentMin * 100);
249 if(priorityChange && this.slideDuration > 0 && this.progressBar.style[this._progressPixelSize]){
250 // animate the slider
251 var percent = isMaxVal ? percentMax : percentMin;
252 var _this = this;
253 var props = {};
254 var start = parseFloat(this.progressBar.style[this._handleOffsetCoord]);
255 var duration = this.slideDuration / 10; // * (percent-start/100);
256 if(duration === 0){ return; }
257 if(duration < 0){ duration = 0 - duration; }
258 var propsHandle = {};
259 var propsHandleMax = {};
260 var propsBar = {};
261 // hui, a lot of animations :-)
262 propsHandle[this._handleOffsetCoord] = { start: this.sliderHandle.parentNode.style[this._handleOffsetCoord], end: sliderHandleVal, units:"%"};
263 propsHandleMax[this._handleOffsetCoord] = { start: this.sliderHandleMax.parentNode.style[this._handleOffsetCoord], end: sliderHandleMaxVal, units:"%"};
264 propsBar[this._handleOffsetCoord] = { start: this.progressBar.style[this._handleOffsetCoord], end: progressBarVal, units:"%"};
265 propsBar[this._progressPixelSize] = { start: this.progressBar.style[this._progressPixelSize], end: (percentMax - percentMin) * 100, units:"%"};
266 var animHandle = fx.animateProperty({node: this.sliderHandle.parentNode, duration: duration, properties: propsHandle});
267 var animHandleMax = fx.animateProperty({node: this.sliderHandleMax.parentNode, duration: duration, properties: propsHandleMax});
268 var animBar = fx.animateProperty({node: this.progressBar, duration: duration, properties: propsBar});
269 var animCombine = fxUtils.combine([animHandle, animHandleMax, animBar]);
270 animCombine.play();
271 }else{
272 this.sliderHandle.parentNode.style[this._handleOffsetCoord] = sliderHandleVal + "%";
273 this.sliderHandleMax.parentNode.style[this._handleOffsetCoord] = sliderHandleMaxVal + "%";
274 this.progressBar.style[this._handleOffsetCoord] = progressBarVal + "%";
275 this.progressBar.style[this._progressPixelSize] = ((percentMax - percentMin) * 100) + "%";
276 }
277 }
278 });
279
280 var SliderMoverMax = declare("dijit.form._SliderMoverMax", Mover, {
281
282 onMouseMove: function(e){
283 var widget = this.widget;
284 var abspos = widget._abspos;
285 if(!abspos){
286 abspos = widget._abspos = domGeometry.position(widget.sliderBarContainer, true);
287 widget._setPixelValue_ = lang.hitch(widget, "_setPixelValue");
288 widget._isReversed_ = widget._isReversed();
289 }
290
291 var pixelValue = e[widget._mousePixelCoord] - abspos[widget._startingPixelCoord];
292 widget._setPixelValue_(widget._isReversed_ ? (abspos[widget._pixelCount]-pixelValue) : pixelValue, abspos[widget._pixelCount], false, true);
293 },
294
295 destroy: function(e){
296 Mover.prototype.destroy.apply(this, arguments);
297 var widget = this.widget;
298 widget._abspos = null;
299 widget._setValueAttr(widget.value, true);
300 }
301 });
302
303 var SliderBarMover = declare("dijit.form._SliderBarMover", Mover, {
304
305 onMouseMove: function(e){
306 var widget = this.widget;
307 if(widget.disabled || widget.readOnly){ return; }
308 var abspos = widget._abspos;
309 var bar = widget._bar;
310 var mouseOffset = widget._mouseOffset;
311 if(!abspos){
312 abspos = widget._abspos = domGeometry.position(widget.sliderBarContainer, true);
313 widget._setPixelValue_ = lang.hitch(widget, "_setPixelValue");
314 widget._getValueByPixelValue_ = lang.hitch(widget, "_getValueByPixelValue");
315 widget._isReversed_ = widget._isReversed();
316 }
317
318 if(!bar){
319 bar = widget._bar = domGeometry.position(widget.progressBar, true);
320 }
321
322 if(!mouseOffset){
323 mouseOffset = widget._mouseOffset = e[widget._mousePixelCoord] - bar[widget._startingPixelCoord];
324 }
325
326 var pixelValueMin = e[widget._mousePixelCoord] - abspos[widget._startingPixelCoord] - mouseOffset,
327 pixelValueMax = pixelValueMin + bar[widget._pixelCount];
328 // we don't narrow the slider when it reaches the bumper!
329 // maybe there is a simpler way
330 pixelValues = [pixelValueMin, pixelValueMax]
331 ;
332
333 pixelValues.sort(sortForward);
334
335 if(pixelValues[0] <= 0){
336 pixelValues[0] = 0;
337 pixelValues[1] = bar[widget._pixelCount];
338 }
339 if(pixelValues[1] >= abspos[widget._pixelCount]){
340 pixelValues[1] = abspos[widget._pixelCount];
341 pixelValues[0] = abspos[widget._pixelCount] - bar[widget._pixelCount];
342 }
343 // getting the real values by pixel
344 var myValues = [
345 widget._getValueByPixelValue(widget._isReversed_ ? (abspos[widget._pixelCount] - pixelValues[0]) : pixelValues[0], abspos[widget._pixelCount]),
346 widget._getValueByPixelValue(widget._isReversed_ ? (abspos[widget._pixelCount] - pixelValues[1]) : pixelValues[1], abspos[widget._pixelCount])
347 ];
348 // and setting the value of the widget
349 widget._setValueAttr(myValues, false, false);
350 },
351
352 destroy: function(){
353 Mover.prototype.destroy.apply(this, arguments);
354 var widget = this.widget;
355 widget._abspos = null;
356 widget._bar = null;
357 widget._mouseOffset = null;
358 widget._setValueAttr(widget.value, true);
359 }
360 });
361
362 return RangeSliderMixin;
363 });