Debian Codegraph @

dijit/form/HorizontalSlider.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.forEach
3 "dojo/_base/declare", // declare
4 "dojo/dnd/move",
5 "dojo/_base/fx", // fx.animateProperty
6 "dojo/dom-geometry", // domGeometry.position
7 "dojo/dom-style", // domStyle.getComputedStyle
8 "dojo/keys", // keys.DOWN_ARROW keys.END keys.HOME keys.LEFT_ARROW keys.PAGE_DOWN keys.PAGE_UP keys.RIGHT_ARROW keys.UP_ARROW
9 "dojo/_base/lang", // lang.hitch
10 "dojo/sniff", // has("ie") has("mozilla")
11 "dojo/dnd/Moveable", // Moveable
12 "dojo/dnd/Mover", // Mover Mover.prototype.destroy.apply
13 "dojo/query", // query
14 "dojo/mouse", // mouse.wheel
15 "dojo/on",
16 "../_base/manager", // defaultDuration
17 "../focus", // focus.focus()
18 "../typematic",
19 "./Button",
20 "./_FormValueWidget",
21 "../_Container",
22 "dojo/text!./templates/HorizontalSlider.html"
23 ], function(array, declare, move, fx, domGeometry, domStyle, keys, lang, has, Moveable, Mover, query, mouse, on,
24 manager, focus, typematic, Button, _FormValueWidget, _Container, template){
25
26 // module:
27 // dijit/form/HorizontalSlider
28
29 var _SliderMover = declare("dijit.form._SliderMover", Mover, {
30 onMouseMove: function(e){
31 var widget = this.widget;
32 var abspos = widget._abspos;
33 if(!abspos){
34 abspos = widget._abspos = domGeometry.position(widget.sliderBarContainer, true);
35 widget._setPixelValue_ = lang.hitch(widget, "_setPixelValue");
36 widget._isReversed_ = widget._isReversed();
37 }
38 var pixelValue = e[widget._mousePixelCoord] - abspos[widget._startingPixelCoord];
39 widget._setPixelValue_(widget._isReversed_ ? (abspos[widget._pixelCount] - pixelValue) : pixelValue, abspos[widget._pixelCount], false);
40 },
41
42 destroy: function(e){
43 Mover.prototype.destroy.apply(this, arguments);
44 var widget = this.widget;
45 widget._abspos = null;
46 widget._setValueAttr(widget.value, true);
47 }
48 });
49
50 var HorizontalSlider = declare("dijit.form.HorizontalSlider", [_FormValueWidget, _Container], {
51 // summary:
52 // A form widget that allows one to select a value with a horizontally draggable handle
53
54 templateString: template,
55
56 // Overrides FormValueWidget.value to indicate numeric value
57 value: 0,
58
59 // showButtons: [const] Boolean
60 // Show increment/decrement buttons at the ends of the slider?
61 showButtons: true,
62
63 // minimum: [const] Integer
64 // The minimum value the slider can be set to.
65 minimum: 0,
66
67 // maximum: [const] Integer
68 // The maximum value the slider can be set to.
69 maximum: 100,
70
71 // discreteValues: Integer
72 // If specified, indicates that the slider handle has only 'discreteValues' possible positions,
73 // and that after dragging the handle, it will snap to the nearest possible position.
74 // Thus, the slider has only 'discreteValues' possible values.
75 //
76 // For example, if minimum=10, maxiumum=30, and discreteValues=3, then the slider handle has
77 // three possible positions, representing values 10, 20, or 30.
78 //
79 // If discreteValues is not specified or if it's value is higher than the number of pixels
80 // in the slider bar, then the slider handle can be moved freely, and the slider's value will be
81 // computed/reported based on pixel position (in this case it will likely be fractional,
82 // such as 123.456789).
83 discreteValues: Infinity,
84
85 // pageIncrement: Integer
86 // If discreteValues is also specified, this indicates the amount of clicks (ie, snap positions)
87 // that the slider handle is moved via pageup/pagedown keys.
88 // If discreteValues is not specified, it indicates the number of pixels.
89 pageIncrement: 2,
90
91 // clickSelect: Boolean
92 // If clicking the slider bar changes the value or not
93 clickSelect: true,
94
95 // slideDuration: Number
96 // The time in ms to take to animate the slider handle from 0% to 100%,
97 // when clicking the slider bar to make the handle move.
98 slideDuration: manager.defaultDuration,
99
100 // Map widget attributes to DOMNode attributes.
101 _setIdAttr: "", // Override _FormWidget which sends id to focusNode
102 _setNameAttr: "valueNode", // Override default behavior to send to focusNode
103
104 baseClass: "dijitSlider",
105
106 // Apply CSS classes to up/down arrows and handle per mouse state
107 cssStateNodes: {
108 incrementButton: "dijitSliderIncrementButton",
109 decrementButton: "dijitSliderDecrementButton",
110 focusNode: "dijitSliderThumb"
111 },
112
113 _mousePixelCoord: "pageX",
114 _pixelCount: "w",
115 _startingPixelCoord: "x",
116 _handleOffsetCoord: "left",
117 _progressPixelSize: "width",
118
119 _onKeyUp: function(/*Event*/ e){
120 if(this.disabled || this.readOnly || e.altKey || e.ctrlKey || e.metaKey){
121 return;
122 }
123 this._setValueAttr(this.value, true);
124 },
125
126 _onKeyDown: function(/*Event*/ e){
127 if(this.disabled || this.readOnly || e.altKey || e.ctrlKey || e.metaKey){
128 return;
129 }
130 switch(e.keyCode){
131 case keys.HOME:
132 this._setValueAttr(this.minimum, false);
133 break;
134 case keys.END:
135 this._setValueAttr(this.maximum, false);
136 break;
137 // this._descending === false: if ascending vertical (min on top)
138 // (this._descending || this.isLeftToRight()): if left-to-right horizontal or descending vertical
139 case ((this._descending || this.isLeftToRight()) ? keys.RIGHT_ARROW : keys.LEFT_ARROW):
140 case (this._descending === false ? keys.DOWN_ARROW : keys.UP_ARROW):
141 case (this._descending === false ? keys.PAGE_DOWN : keys.PAGE_UP):
142 this.increment(e);
143 break;
144 case ((this._descending || this.isLeftToRight()) ? keys.LEFT_ARROW : keys.RIGHT_ARROW):
145 case (this._descending === false ? keys.UP_ARROW : keys.DOWN_ARROW):
146 case (this._descending === false ? keys.PAGE_UP : keys.PAGE_DOWN):
147 this.decrement(e);
148 break;
149 default:
150 return;
151 }
152 e.stopPropagation();
153 e.preventDefault();
154 },
155
156 _onHandleClick: function(e){
157 if(this.disabled || this.readOnly){
158 return;
159 }
160 if(!has("ie")){
161 // make sure you get focus when dragging the handle
162 // (but don't do on IE because it causes a flicker on mouse up (due to blur then focus)
163 focus.focus(this.sliderHandle);
164 }
165 e.stopPropagation();
166 e.preventDefault();
167 },
168
169 _isReversed: function(){
170 // summary:
171 // Returns true if direction is from right to left
172 // tags:
173 // protected extension
174 return !this.isLeftToRight();
175 },
176
177 _onBarClick: function(e){
178 if(this.disabled || this.readOnly || !this.clickSelect){
179 return;
180 }
181 focus.focus(this.sliderHandle);
182 e.stopPropagation();
183 e.preventDefault();
184 var abspos = domGeometry.position(this.sliderBarContainer, true);
185 var pixelValue = e[this._mousePixelCoord] - abspos[this._startingPixelCoord];
186 this._setPixelValue(this._isReversed() ? (abspos[this._pixelCount] - pixelValue) : pixelValue, abspos[this._pixelCount], true);
187 this._movable.onMouseDown(e);
188 },
189
190 _setPixelValue: function(/*Number*/ pixelValue, /*Number*/ maxPixels, /*Boolean?*/ priorityChange){
191 if(this.disabled || this.readOnly){
192 return;
193 }
194 var count = this.discreteValues;
195 if(count <= 1 || count == Infinity){
196 count = maxPixels;
197 }
198 count--;
199 var pixelsPerValue = maxPixels / count;
200 var wholeIncrements = Math.round(pixelValue / pixelsPerValue);
201 this._setValueAttr(Math.max(Math.min((this.maximum - this.minimum) * wholeIncrements / count + this.minimum, this.maximum), this.minimum), priorityChange);
202 },
203
204 _setValueAttr: function(/*Number*/ value, /*Boolean?*/ priorityChange){
205 // summary:
206 // Hook so set('value', value) works.
207 this._set("value", value);
208 this.valueNode.value = value;
209 this.focusNode.setAttribute("aria-valuenow", value);
210 this.inherited(arguments);
211 var percent = this.maximum > this.minimum ? ((value - this.minimum) / (this.maximum - this.minimum)) : 0;
212 var progressBar = (this._descending === false) ? this.remainingBar : this.progressBar;
213 var remainingBar = (this._descending === false) ? this.progressBar : this.remainingBar;
214 if(this._inProgressAnim && this._inProgressAnim.status != "stopped"){
215 this._inProgressAnim.stop(true);
216 }
217 if(priorityChange && this.slideDuration > 0 && progressBar.style[this._progressPixelSize]){
218 // animate the slider
219 var _this = this;
220 var props = {};
221 var start = parseFloat(progressBar.style[this._progressPixelSize]);
222 var duration = this.slideDuration * (percent - start / 100);
223 if(duration == 0){
224 return;
225 }
226 if(duration < 0){
227 duration = 0 - duration;
228 }
229 props[this._progressPixelSize] = { start: start, end: percent * 100, units: "%" };
230 this._inProgressAnim = fx.animateProperty({ node: progressBar, duration: duration,
231 onAnimate: function(v){
232 remainingBar.style[_this._progressPixelSize] = (100 - parseFloat(v[_this._progressPixelSize])) + "%";
233 },
234 onEnd: function(){
235 delete _this._inProgressAnim;
236 },
237 properties: props
238 });
239 this._inProgressAnim.play();
240 }else{
241 progressBar.style[this._progressPixelSize] = (percent * 100) + "%";
242 remainingBar.style[this._progressPixelSize] = ((1 - percent) * 100) + "%";
243 }
244 },
245
246 _bumpValue: function(signedChange, /*Boolean?*/ priorityChange){
247 if(this.disabled || this.readOnly || (this.maximum <= this.minimum)){
248 return;
249 }
250 var s = domStyle.getComputedStyle(this.sliderBarContainer);
251 var c = domGeometry.getContentBox(this.sliderBarContainer, s);
252 var count = this.discreteValues;
253 if(count <= 1 || count == Infinity){
254 count = c[this._pixelCount];
255 }
256 count--;
257 // the division is imprecise so the expression has to be rounded to avoid long floating numbers
258 var value = Math.round((this.value - this.minimum) * count / (this.maximum - this.minimum)) + signedChange;
259 if(value < 0){
260 value = 0;
261 }
262 if(value > count){
263 value = count;
264 }
265 value = value * (this.maximum - this.minimum) / count + this.minimum;
266 this._setValueAttr(value, priorityChange);
267 },
268
269 _onClkBumper: function(val){
270 if(this.disabled || this.readOnly || !this.clickSelect){
271 return;
272 }
273 this._setValueAttr(val, true);
274 },
275
276 _onClkIncBumper: function(){
277 this._onClkBumper(this._descending === false ? this.minimum : this.maximum);
278 },
279
280 _onClkDecBumper: function(){
281 this._onClkBumper(this._descending === false ? this.maximum : this.minimum);
282 },
283
284 decrement: function(/*Event*/ e){
285 // summary:
286 // Decrement slider
287 // tags:
288 // private
289 this._bumpValue(e.keyCode == keys.PAGE_DOWN ? -this.pageIncrement : -1);
290 },
291
292 increment: function(/*Event*/ e){
293 // summary:
294 // Increment slider
295 // tags:
296 // private
297 this._bumpValue(e.keyCode == keys.PAGE_UP ? this.pageIncrement : 1);
298 },
299
300 _mouseWheeled: function(/*Event*/ evt){
301 // summary:
302 // Event handler for mousewheel where supported
303
304 if(!this.focused){
305 // If use is scrolling over page and we happen to get the mouse wheel event, just ignore it.
306 return;
307 }
308
309 evt.stopPropagation();
310 evt.preventDefault();
311 this._bumpValue(evt.wheelDelta < 0 ? -1 : 1, true); // negative scroll acts like a decrement
312 },
313
314 startup: function(){
315 if(this._started){
316 return;
317 }
318
319 array.forEach(this.getChildren(), function(child){
320 if(this[child.container] != this.containerNode){
321 this[child.container].appendChild(child.domNode);
322 }
323 }, this);
324
325 this.inherited(arguments);
326 },
327
328 _typematicCallback: function(/*Number*/ count, /*Object*/ button, /*Event*/ e){
329 if(count == -1){
330 this._setValueAttr(this.value, true);
331 }else{
332 this[(button == (this._descending ? this.incrementButton : this.decrementButton)) ? "decrement" : "increment"](e);
333 }
334 },
335
336 buildRendering: function(){
337 this.inherited(arguments);
338 if(this.showButtons){
339 this.incrementButton.style.display = "";
340 this.decrementButton.style.display = "";
341 }
342
343 // find any associated label element and add to slider focusnode.
344 var label = query('label[for="' + this.id + '"]');
345 if(label.length){
346 if(!label[0].id){
347 label[0].id = this.id + "_label";
348 }
349 this.focusNode.setAttribute("aria-labelledby", label[0].id);
350 }
351
352 this.focusNode.setAttribute("aria-valuemin", this.minimum);
353 this.focusNode.setAttribute("aria-valuemax", this.maximum);
354 },
355
356 postCreate: function(){
357 this.inherited(arguments);
358
359 if(this.showButtons){
360 this.own(
361 typematic.addMouseListener(this.decrementButton, this, "_typematicCallback", 25, 500),
362 typematic.addMouseListener(this.incrementButton, this, "_typematicCallback", 25, 500)
363 );
364 }
365 this.own(
366 on(this.domNode, mouse.wheel, lang.hitch(this, "_mouseWheeled"))
367 );
368
369 // define a custom constructor for a SliderMover that points back to me
370 var mover = declare(_SliderMover, {
371 widget: this
372 });
373 this._movable = new Moveable(this.sliderHandle, {mover: mover});
374
375 this._layoutHackIE7();
376 },
377
378 destroy: function(){
379 this._movable.destroy();
380 if(this._inProgressAnim && this._inProgressAnim.status != "stopped"){
381 this._inProgressAnim.stop(true);
382 }
383 this.inherited(arguments);
384 }
385 });
386
387 HorizontalSlider._Mover = _SliderMover; // for monkey patching
388
389 return HorizontalSlider;
390 });