| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
value: 0, |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
min: 0, |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
max: 100, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
step: 1, |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
baseClass: "mblSlider", |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
flip: false, |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
orientation: "auto", |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
halo: "8pt", |
| 62 |
|
| 63 |
buildRendering: function(){ |
| 64 |
if(!this.templateString){ |
| 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 |
|
| 76 |
common._setTouchAction(this.domNode, "none"); |
| 77 |
}, |
| 78 |
|
| 79 |
_setMinAttr: function( min){ |
| 80 |
this.handle.setAttribute("aria-valuemin", min); |
| 81 |
this._set("min",min); |
| 82 |
}, |
| 83 |
_setMaxAttr: function( max){ |
| 84 |
this.handle.setAttribute("aria-valuemax", max); |
| 85 |
this._set("max",max); |
| 86 |
}, |
| 87 |
_setValueAttr: function( value, priorityChange){ |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 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; } |
| 97 |
var toPercent = (value - this.min) * 100 / (this.max - this.min); |
| 98 |
|
| 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(); |
| 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); |
| 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( 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( 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 |
|
| 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 |
|
| 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); |
| 213 |
this.connect(this.domNode, "onkeyup", keyUp); |
| 214 |
this.startup(); |
| 215 |
this.set('value', this.value); |
| 216 |
} |
| 217 |
}); |
| 218 |
}); |