Debian Codegraph @

dojox/mobile/Slider.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/connect",
4 "dojo/_base/declare",
5 "dojo/_base/lang",
6 "dojo/_base/window",
7 "dojo/sniff",
8 "dojo/dom-class",
9 "dojo/dom-construct",
10 "dojo/dom-geometry",
11 "dojo/dom-style",
12 "dojo/keys",
13 "dojo/touch",
14 "dijit/_WidgetBase",
15 "dijit/form/_FormValueMixin",
16 "./common"
17 ],
18 function(array, connect, declare, lang, win, has, domClass, domConstruct, domGeometry, domStyle, keys, touch, WidgetBase, FormValueMixin, common){
19
20 return declare("dojox.mobile.Slider", [WidgetBase, FormValueMixin], {
21 // summary:
22 // A non-templated Slider widget similar to the HTML5 INPUT type=range.
23
24 // value: [const] Number
25 // The current slider value.
26 value: 0,
27
28 // min: [const] Number
29 // The first value the slider can be set to.
30 min: 0,
31
32 // max: [const] Number
33 // The last value the slider can be set to.
34 max: 100,
35
36 // step: [const] Number
37 // The delta from 1 value to another.
38 // This causes the slider handle to snap/jump to the closest possible value.
39 // A value of 0 means continuous (as much as allowed by pixel resolution).
40 step: 1,
41
42 // baseClass: String
43 // The name of the CSS class of this widget.
44 baseClass: "mblSlider",
45
46 // flip: [const] Boolean
47 // Specifies if the slider should change its default: ascending <--> descending.
48 flip: false,
49
50 // orientation: [const] String
51 // The slider direction.
52 //
53 // - "H": horizontal
54 // - "V": vertical
55 // - "auto": use width/height comparison at instantiation time (default is "H" if width/height are 0)
56 orientation: "auto",
57
58 // halo: Number
59 // Size of the boundary that extends beyond the edges of the slider
60 // to make it easier to touch.
61 halo: "8pt",
62
63 buildRendering: function(){
64 if(!this.templateString){ // true if this widget is not templated
65 this.focusNode = this.domNode = domConstruct.create("div", {});
66 this.valueNode = domConstruct.create("input", (this.srcNodeRef && this.srcNodeRef.name) ? { type: "hidden", name: this.srcNodeRef.name } : { type: "hidden" }, this.domNode, "last");
67 var relativeParent = domConstruct.create("div", { style: { position:"relative", height:"100%", width:"100%" } }, this.domNode, "last");
68 this.progressBar = domConstruct.create("div", { style:{ position:"absolute" }, "class":"mblSliderProgressBar" }, relativeParent, "last");
69 this.touchBox = domConstruct.create("div", { style:{ position:"absolute" }, "class":"mblSliderTouchBox" }, relativeParent, "last");
70 this.handle = domConstruct.create("div", { style:{ position:"absolute" }, "class":"mblSliderHandle" }, relativeParent, "last");
71 this.handle.setAttribute("role", "slider");
72 this.handle.setAttribute("tabindex", 0);
73 }
74 this.inherited(arguments);
75 // prevent browser scrolling on IE10 (evt.preventDefault() is not enough)
76 common._setTouchAction(this.domNode, "none");
77 },
78
79 _setMinAttr: function(/*Number*/ min){
80 this.handle.setAttribute("aria-valuemin", min);
81 this._set("min",min);
82 },
83 _setMaxAttr: function(/*Number*/ max){
84 this.handle.setAttribute("aria-valuemax", max);
85 this._set("max",max);
86 },
87 _setValueAttr: function(/*Number*/ value, /*Boolean?*/ priorityChange){
88 // summary:
89 // Hook such that set('value', value) works.
90 // tags:
91 // private
92 value = Math.max(Math.min(value, this.max), this.min);
93 var fromPercent = (this.value - this.min) * 100 / (this.max - this.min);
94 this.valueNode.value = value;
95 this.inherited(arguments);
96 if(!this._started){ return; } // don't move images until all the properties are set
97 var toPercent = (value - this.min) * 100 / (this.max - this.min);
98 // now perform visual slide
99 var horizontal = this.orientation != "V";
100 if(priorityChange === true){
101 domClass.add(this.handle, "mblSliderTransition");
102 domClass.add(this.progressBar, "mblSliderTransition");
103 }else{
104 domClass.remove(this.handle, "mblSliderTransition");
105 domClass.remove(this.progressBar, "mblSliderTransition");
106 }
107 domStyle.set(this.handle, this._attrs.handleLeft, (this._reversed ? (100-toPercent) : toPercent) + "%");
108 domStyle.set(this.progressBar, this._attrs.width, toPercent + "%");
109 this.handle.setAttribute("aria-valuenow", value);
110 },
111
112 postCreate: function(){
113 this.inherited(arguments);
114
115 function beginDrag(e){
116 e.stopPropagation(); // in case of slider enclosed in a scrollable container, this prevents the widget from scrolling while it's being used
117 e.target.focus();
118 function getEventData(e){
119 point = isMouse ? e[this._attrs.pageX] : (e.touches ? e.touches[0][this._attrs.pageX] : e[this._attrs.clientX]);
120 pixelValue = point - startPixel;
121 pixelValue = Math.min(Math.max(pixelValue, 0), maxPixels);
122 var discreteValues = this.step ? ((this.max - this.min) / this.step) : maxPixels;
123 if(discreteValues <= 1 || discreteValues == Infinity ){ discreteValues = maxPixels; }
124 var wholeIncrements = Math.round(pixelValue * discreteValues / maxPixels);
125 value = (this.max - this.min) * wholeIncrements / discreteValues;
126 value = this._reversed ? (this.max - value) : (this.min + value);
127 }
128 function continueDrag(e){
129 e.preventDefault();
130 lang.hitch(this, getEventData)(e);
131 this.set('value', value, false);
132 }
133
134 function endDrag(e){
135 e.preventDefault();
136 array.forEach(actionHandles, lang.hitch(this, "disconnect"));
137 actionHandles = [];
138 this.set('value', this.value, true);
139 }
140
141 e.preventDefault();
142 var isMouse = e.type == "mousedown";
143 var box = domGeometry.position(node, false); // can't use true since the added docScroll and the returned x are body-zoom incompatible
144 var bodyZoom = (has("ie") || has("trident") > 6) ? 1 : (domStyle.get(win.body(), "zoom") || 1);
145 if(isNaN(bodyZoom)){ bodyZoom = 1; }
146 var nodeZoom = (has("ie") || has("trident") > 6) ? 1 : (domStyle.get(node, "zoom") || 1);
147 if(isNaN(nodeZoom)){ nodeZoom = 1; }
148 var startPixel = box[this._attrs.x] * nodeZoom * bodyZoom + domGeometry.docScroll()[this._attrs.x];
149 var maxPixels = box[this._attrs.w] * nodeZoom * bodyZoom;
150 lang.hitch(this, getEventData)(e);
151 if(e.target == this.touchBox){
152 this.set('value', value, true);
153 }
154 array.forEach(actionHandles, connect.disconnect);
155 var root = win.doc.documentElement;
156 var actionHandles = [
157 this.connect(root, touch.move, continueDrag),
158 this.connect(root, touch.release, endDrag)
159 ];
160 }
161
162 function keyPress(/*Event*/ e){
163 if(this.disabled || this.readOnly || e.altKey || e.ctrlKey || e.metaKey){ return; }
164 var step = this.step,
165 multiplier = 1,
166 newValue;
167 switch(e.keyCode){
168 case keys.HOME:
169 newValue = this.min;
170 break;
171 case keys.END:
172 newValue = this.max;
173 break;
174 case keys.RIGHT_ARROW:
175 multiplier = -1;
176 case keys.LEFT_ARROW:
177 newValue = this.value + multiplier * ((flip && horizontal) ? step : -step);
178 break;
179 case keys.DOWN_ARROW:
180 multiplier = -1;
181 case keys.UP_ARROW:
182 newValue = this.value + multiplier * ((!flip || horizontal) ? step : -step);
183 break;
184 default:
185 return;
186 }
187 e.preventDefault();
188 this._setValueAttr(newValue, false);
189 }
190
191 function keyUp(/*Event*/ e){
192 if(this.disabled || this.readOnly || e.altKey || e.ctrlKey || e.metaKey){ return; }
193 this._setValueAttr(this.value, true);
194 }
195
196 var point, pixelValue, value,
197 node = this.domNode;
198 if(this.orientation == "auto"){
199 this.orientation = node.offsetHeight <= node.offsetWidth ? "H" : "V";
200 }
201 // add V or H suffix to baseClass for styling purposes
202 domClass.add(this.domNode, array.map(this.baseClass.split(" "), lang.hitch(this, function(c){ return c+this.orientation; })));
203 var horizontal = this.orientation != "V",
204 ltr = horizontal ? this.isLeftToRight() : false,
205 flip = !!this.flip;
206 // _reversed is complicated since you can have flipped right-to-left and vertical is upside down by default
207 this._reversed = !((horizontal && ((ltr && !flip) || (!ltr && flip))) || (!horizontal && flip));
208 this._attrs = horizontal ? { x:'x', w:'w', l:'l', r:'r', pageX:'pageX', clientX:'clientX', handleLeft:"left", left:this._reversed ? "right" : "left", width:"width" } : { x:'y', w:'h', l:'t', r:'b', pageX:'pageY', clientX:'clientY', handleLeft:"top", left:this._reversed ? "bottom" : "top", width:"height" };
209 this.progressBar.style[this._attrs.left] = "0px";
210 this.connect(this.touchBox, touch.press, beginDrag);
211 this.connect(this.handle, touch.press, beginDrag);
212 this.connect(this.domNode, "onkeypress", keyPress); // for desktop a11y
213 this.connect(this.domNode, "onkeyup", keyUp); // fire onChange on desktop
214 this.startup();
215 this.set('value', this.value);
216 }
217 });
218 });