| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/dnd/move", |
| 5 |
"dojo/_base/fx", |
| 6 |
"dojo/dom-geometry", |
| 7 |
"dojo/dom-style", |
| 8 |
"dojo/keys", |
| 9 |
"dojo/_base/lang", |
| 10 |
"dojo/sniff", |
| 11 |
"dojo/dnd/Moveable", |
| 12 |
"dojo/dnd/Mover", |
| 13 |
"dojo/query", |
| 14 |
"dojo/mouse", |
| 15 |
"dojo/on", |
| 16 |
"../_base/manager", |
| 17 |
"../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 |
|
| 27 |
|
| 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 |
|
| 52 |
|
| 53 |
|
| 54 |
templateString: template, |
| 55 |
|
| 56 |
|
| 57 |
value: 0, |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
showButtons: true, |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
minimum: 0, |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
maximum: 100, |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
discreteValues: Infinity, |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pageIncrement: 2, |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
clickSelect: true, |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
slideDuration: manager.defaultDuration, |
| 99 |
|
| 100 |
|
| 101 |
_setIdAttr: "", |
| 102 |
_setNameAttr: "valueNode", |
| 103 |
|
| 104 |
baseClass: "dijitSlider", |
| 105 |
|
| 106 |
|
| 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( 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( 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 |
|
| 138 |
|
| 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 |
|
| 162 |
|
| 163 |
focus.focus(this.sliderHandle); |
| 164 |
} |
| 165 |
e.stopPropagation(); |
| 166 |
e.preventDefault(); |
| 167 |
}, |
| 168 |
|
| 169 |
_isReversed: function(){ |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 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( pixelValue, maxPixels, 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( value, priorityChange){ |
| 205 |
|
| 206 |
|
| 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 |
|
| 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, 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 |
|
| 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( e){ |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
this._bumpValue(e.keyCode == keys.PAGE_DOWN ? -this.pageIncrement : -1); |
| 290 |
}, |
| 291 |
|
| 292 |
increment: function( e){ |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
this._bumpValue(e.keyCode == keys.PAGE_UP ? this.pageIncrement : 1); |
| 298 |
}, |
| 299 |
|
| 300 |
_mouseWheeled: function( evt){ |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
if(!this.focused){ |
| 305 |
|
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
evt.stopPropagation(); |
| 310 |
evt.preventDefault(); |
| 311 |
this._bumpValue(evt.wheelDelta < 0 ? -1 : 1, true); |
| 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( count, button, 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 |
|
| 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 |
|
| 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; |
| 388 |
|
| 389 |
return HorizontalSlider; |
| 390 |
}); |