| 1 |
define([ |
| 2 |
"dojo/_base/kernel", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojo/_base/declare", |
| 5 |
"dojo/_base/window", |
| 6 |
"dojo/dom-class", |
| 7 |
"dojo/dom-construct", |
| 8 |
"dojo/has", |
| 9 |
"dojo/has!dojo-bidi?dojox/mobile/bidi/SpinWheelSlot", |
| 10 |
"dojo/touch", |
| 11 |
"dojo/on", |
| 12 |
"dijit/_Contained", |
| 13 |
"dijit/_WidgetBase", |
| 14 |
"./scrollable", |
| 15 |
"./common" |
| 16 |
], function(dojo, array, declare, win, domClass, domConstruct, has, BidiSpinWheelSlot, |
| 17 |
touch, on, Contained, WidgetBase, Scrollable){ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
var SpinWheelSlot = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiSpinWheelSlot" : "dojox.mobile.SpinWheelSlot", [WidgetBase, Contained, Scrollable], { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
items: [], |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
labels: [], |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
labelFrom: 0, |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
labelTo: 0, |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
zeroPad: 0, |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
value: "", |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
step: 1, |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
pageSteps: 1, |
| 66 |
|
| 67 |
|
| 68 |
baseClass: "mblSpinWheelSlot", |
| 69 |
|
| 70 |
|
| 71 |
maxSpeed: 500, |
| 72 |
|
| 73 |
|
| 74 |
minItems: 15, |
| 75 |
|
| 76 |
|
| 77 |
centerPos: 0, |
| 78 |
|
| 79 |
|
| 80 |
scrollBar: false, |
| 81 |
|
| 82 |
|
| 83 |
constraint: false, |
| 84 |
|
| 85 |
|
| 86 |
propagatable: false, |
| 87 |
|
| 88 |
|
| 89 |
androidWorkaroud: false, |
| 90 |
|
| 91 |
buildRendering: function(){ |
| 92 |
this.inherited(arguments); |
| 93 |
|
| 94 |
this.initLabels(); |
| 95 |
var i, j; |
| 96 |
if(this.labels.length > 0){ |
| 97 |
this.items = []; |
| 98 |
for(i = 0; i < this.labels.length; i++){ |
| 99 |
this.items.push([i, this.labels[i]]); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
this.containerNode = domConstruct.create("div", {className:"mblSpinWheelSlotContainer"}); |
| 104 |
this.containerNode.style.height |
| 105 |
= (win.global.innerHeight||win.doc.documentElement.clientHeight) * 2 + "px"; |
| 106 |
this.panelNodes = []; |
| 107 |
for(var k = 0; k < 3; k++){ |
| 108 |
this.panelNodes[k] = domConstruct.create("div", {className:"mblSpinWheelSlotPanel"}); |
| 109 |
this.panelNodes[k].setAttribute("aria-hidden", "true"); |
| 110 |
var len = this.items.length; |
| 111 |
if(len > 0){ |
| 112 |
var n = Math.ceil(this.minItems / len); |
| 113 |
for(j = 0; j < n; j++){ |
| 114 |
for(i = 0; i < len; i++){ |
| 115 |
domConstruct.create("div", { |
| 116 |
className: "mblSpinWheelSlotLabel", |
| 117 |
name: this.items[i][0], |
| 118 |
"data-mobile-val": this.items[i][1], |
| 119 |
innerHTML: this._cv ? this._cv(this.items[i][1]) : this.items[i][1] |
| 120 |
}, this.panelNodes[k]); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
this.containerNode.appendChild(this.panelNodes[k]); |
| 125 |
} |
| 126 |
this.domNode.appendChild(this.containerNode); |
| 127 |
this.touchNode = domConstruct.create("div", {className:"mblSpinWheelSlotTouch"}, this.domNode); |
| 128 |
this.setSelectable(this.domNode, false); |
| 129 |
|
| 130 |
this.touchNode.setAttribute("tabindex", 0); |
| 131 |
this.touchNode.setAttribute("role", "slider"); |
| 132 |
|
| 133 |
if(this.value === "" && this.items.length > 0){ |
| 134 |
this.value = this.items[0][1]; |
| 135 |
} |
| 136 |
this._initialValue = this.value; |
| 137 |
|
| 138 |
if(has("windows-theme")){ |
| 139 |
var self = this, |
| 140 |
containerNode = this.containerNode, |
| 141 |
threshold = 5; |
| 142 |
|
| 143 |
this.own(on(self.touchNode, touch.press, function(e){ |
| 144 |
var posY = e.pageY, |
| 145 |
slots = self.getParent().getChildren(); |
| 146 |
|
| 147 |
for(var i = 0, ln = slots.length; i < ln; i++){ |
| 148 |
var container = slots[i].containerNode; |
| 149 |
|
| 150 |
if(containerNode !== container){ |
| 151 |
domClass.remove(container, "mblSelectedSlot"); |
| 152 |
container.selected = false; |
| 153 |
}else{ |
| 154 |
domClass.add(containerNode, "mblSelectedSlot"); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
var moveHandler = on(self.touchNode, touch.move, function(e){ |
| 159 |
if(Math.abs(e.pageY - posY) < threshold){ |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
moveHandler.remove(); |
| 164 |
releaseHandler.remove(); |
| 165 |
containerNode.selected = true; |
| 166 |
|
| 167 |
var item = self.getCenterItem(); |
| 168 |
|
| 169 |
if(item){ |
| 170 |
domClass.remove(item, "mblSelectedSlotItem"); |
| 171 |
} |
| 172 |
}); |
| 173 |
|
| 174 |
var releaseHandler = on(self.touchNode, touch.release, function(){ |
| 175 |
releaseHandler.remove(); |
| 176 |
moveHandler.remove(); |
| 177 |
containerNode.selected ? |
| 178 |
domClass.remove(containerNode, "mblSelectedSlot") : |
| 179 |
domClass.add(containerNode, "mblSelectedSlot"); |
| 180 |
|
| 181 |
containerNode.selected = !containerNode.selected; |
| 182 |
}); |
| 183 |
})); |
| 184 |
|
| 185 |
this.on("flickAnimationEnd", function(){ |
| 186 |
var item = self.getCenterItem(); |
| 187 |
|
| 188 |
if(self.previousCenterItem) { |
| 189 |
domClass.remove(self.previousCenterItem, "mblSelectedSlotItem"); |
| 190 |
} |
| 191 |
|
| 192 |
domClass.add(item, "mblSelectedSlotItem"); |
| 193 |
self.previousCenterItem = item; |
| 194 |
}); |
| 195 |
} |
| 196 |
}, |
| 197 |
|
| 198 |
startup: function(){ |
| 199 |
if(this._started){ return; } |
| 200 |
this.inherited(arguments); |
| 201 |
this.noResize = true; |
| 202 |
if(this.items.length > 0){ |
| 203 |
this.init(); |
| 204 |
this.centerPos = this.getParent().centerPos; |
| 205 |
var items = this.panelNodes[1].childNodes; |
| 206 |
this._itemHeight = items[0].offsetHeight; |
| 207 |
this.adjust(); |
| 208 |
this.connect(this.touchNode, "onkeydown", "_onKeyDown"); |
| 209 |
} |
| 210 |
if(has("windows-theme")){ |
| 211 |
this.previousCenterItem = this.getCenterItem(); |
| 212 |
if(this.previousCenterItem){ |
| 213 |
domClass.add(this.previousCenterItem, "mblSelectedSlotItem"); |
| 214 |
} |
| 215 |
} |
| 216 |
}, |
| 217 |
|
| 218 |
initLabels: function(){ |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
if(this.labelFrom !== this.labelTo){ |
| 224 |
var a = this.labels = [], |
| 225 |
zeros = this.zeroPad && Array(this.zeroPad).join("0"); |
| 226 |
for(var i = this.labelFrom; i <= this.labelTo; i += this.step){ |
| 227 |
a.push(this.zeroPad ? (zeros + i).slice(-this.zeroPad) : i + ""); |
| 228 |
} |
| 229 |
} |
| 230 |
}, |
| 231 |
|
| 232 |
onTouchStart: function(e) { |
| 233 |
this.touchNode.focus(); |
| 234 |
this.inherited(arguments); |
| 235 |
}, |
| 236 |
|
| 237 |
adjust: function(){ |
| 238 |
|
| 239 |
|
| 240 |
var items = this.panelNodes[1].childNodes; |
| 241 |
var adjustY; |
| 242 |
for(var i = 0, len = items.length; i < len; i++){ |
| 243 |
var item = items[i]; |
| 244 |
if(item.offsetTop <= this.centerPos && this.centerPos < item.offsetTop + item.offsetHeight){ |
| 245 |
adjustY = this.centerPos - (item.offsetTop + Math.round(item.offsetHeight/2)); |
| 246 |
break; |
| 247 |
} |
| 248 |
} |
| 249 |
var h = this.panelNodes[0].offsetHeight; |
| 250 |
this.panelNodes[0].style.top = -h + adjustY + "px"; |
| 251 |
this.panelNodes[1].style.top = adjustY + "px"; |
| 252 |
this.panelNodes[2].style.top = h + adjustY + "px"; |
| 253 |
}, |
| 254 |
|
| 255 |
setInitialValue: function(){ |
| 256 |
|
| 257 |
|
| 258 |
this.set("value", this._initialValue); |
| 259 |
this.touchNode.setAttribute("aria-valuetext", this._initialValue); |
| 260 |
}, |
| 261 |
|
| 262 |
_onKeyDown: function(e){ |
| 263 |
if(!e || e.type !== "keydown" || e.altKey || e.ctrlKey || e.shiftKey){ |
| 264 |
return true; |
| 265 |
} |
| 266 |
switch(e.keyCode){ |
| 267 |
case 38: |
| 268 |
case 39: |
| 269 |
this.spin(1); |
| 270 |
e.stopPropagation(); |
| 271 |
return false; |
| 272 |
case 40: |
| 273 |
case 37: |
| 274 |
this.spin(-1); |
| 275 |
e.stopPropagation(); |
| 276 |
return false; |
| 277 |
case 33: |
| 278 |
this.spin(this.pageSteps); |
| 279 |
e.stopPropagation(); |
| 280 |
return false; |
| 281 |
case 34: |
| 282 |
this.spin(-1 * this.pageSteps); |
| 283 |
e.stopPropagation(); |
| 284 |
return false; |
| 285 |
} |
| 286 |
return true; |
| 287 |
}, |
| 288 |
|
| 289 |
_getCenterPanel: function(){ |
| 290 |
|
| 291 |
|
| 292 |
var pos = this.getPos(); |
| 293 |
for(var i = 0, len = this.panelNodes.length; i < len; i++){ |
| 294 |
var top = pos.y + this.panelNodes[i].offsetTop; |
| 295 |
if(top <= this.centerPos && this.centerPos < top + this.panelNodes[i].offsetHeight){ |
| 296 |
return this.panelNodes[i]; |
| 297 |
} |
| 298 |
} |
| 299 |
return null; |
| 300 |
}, |
| 301 |
|
| 302 |
setColor: function(value, color){ |
| 303 |
|
| 304 |
|
| 305 |
array.forEach(this.panelNodes, function(panel){ |
| 306 |
array.forEach(panel.childNodes, function(node, i){ |
| 307 |
domClass.toggle(node, color || "mblSpinWheelSlotLabelBlue", node.innerHTML === value); |
| 308 |
}, this); |
| 309 |
}, this); |
| 310 |
}, |
| 311 |
|
| 312 |
disableValues: function(n){ |
| 313 |
|
| 314 |
|
| 315 |
array.forEach(this.panelNodes, function(panel){ |
| 316 |
for(var i = 0; i < panel.childNodes.length; i++){ |
| 317 |
domClass.toggle(panel.childNodes[i], "mblSpinWheelSlotLabelGray", i >= n); |
| 318 |
} |
| 319 |
}); |
| 320 |
}, |
| 321 |
|
| 322 |
getCenterItem: function(){ |
| 323 |
|
| 324 |
|
| 325 |
var pos = this.getPos(); |
| 326 |
var centerPanel = this._getCenterPanel(); |
| 327 |
if(centerPanel){ |
| 328 |
var top = pos.y + centerPanel.offsetTop; |
| 329 |
var items = centerPanel.childNodes; |
| 330 |
for(var i = 0, len = items.length; i < len; i++){ |
| 331 |
if(top + items[i].offsetTop <= this.centerPos && this.centerPos < top + items[i].offsetTop + items[i].offsetHeight){ |
| 332 |
return items[i]; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
return null; |
| 337 |
|
| 338 |
}, |
| 339 |
|
| 340 |
_getKeyAttr: function(){ |
| 341 |
|
| 342 |
|
| 343 |
if(!this._started){ |
| 344 |
if(this.items){ |
| 345 |
var v = this.value; |
| 346 |
for(var i = 0; i < this.items.length; i++){ |
| 347 |
if(this.items[i][1] == this.value){ |
| 348 |
return this.items[i][0]; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
return null; |
| 353 |
} |
| 354 |
var item = this.getCenterItem(); |
| 355 |
return (item && item.getAttribute("name")); |
| 356 |
}, |
| 357 |
|
| 358 |
_getValueAttr: function(){ |
| 359 |
|
| 360 |
|
| 361 |
if(!this._started){ |
| 362 |
return this.value; |
| 363 |
} |
| 364 |
if(this.items.length > 0){ |
| 365 |
var item = this.getCenterItem(); |
| 366 |
return (item && item.getAttribute("data-mobile-val")); |
| 367 |
}else{ |
| 368 |
return this._initialValue; |
| 369 |
} |
| 370 |
}, |
| 371 |
|
| 372 |
_setValueAttr: function(value){ |
| 373 |
|
| 374 |
|
| 375 |
if(this.items.length > 0){ |
| 376 |
this._spinToValue(value, true); |
| 377 |
} |
| 378 |
}, |
| 379 |
|
| 380 |
_spinToValue: function(value, applyValue){ |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
var idx0, idx1; |
| 386 |
var curValue = this.get("value"); |
| 387 |
if(!curValue){ |
| 388 |
this._pendingValue = value; |
| 389 |
return; |
| 390 |
} |
| 391 |
if(curValue == value){ |
| 392 |
return; |
| 393 |
} |
| 394 |
this._pendingValue = undefined; |
| 395 |
|
| 396 |
|
| 397 |
if(applyValue){ |
| 398 |
this._set("value", value); |
| 399 |
} |
| 400 |
var n = this.items.length; |
| 401 |
for(var i = 0; i < n; i++){ |
| 402 |
if(this.items[i][1] === String(curValue)){ |
| 403 |
idx0 = i; |
| 404 |
} |
| 405 |
if(this.items[i][1] === String(value)){ |
| 406 |
idx1 = i; |
| 407 |
} |
| 408 |
if(idx0 !== undefined && idx1 !== undefined){ |
| 409 |
break; |
| 410 |
} |
| 411 |
} |
| 412 |
var d = idx1 - (idx0 || 0); |
| 413 |
var m; |
| 414 |
if(d > 0){ |
| 415 |
m = (d < n - d) ? -d : n - d; |
| 416 |
}else{ |
| 417 |
m = (-d < n + d) ? -d : -(n + d); |
| 418 |
} |
| 419 |
this.spin(m); |
| 420 |
}, |
| 421 |
|
| 422 |
onFlickAnimationStart: function(e){ |
| 423 |
|
| 424 |
|
| 425 |
this._onFlickAnimationStartCalled = true; |
| 426 |
this.inherited(arguments); |
| 427 |
}, |
| 428 |
|
| 429 |
onFlickAnimationEnd: function(e){ |
| 430 |
|
| 431 |
|
| 432 |
this._duringSlideTo = false; |
| 433 |
this._onFlickAnimationStartCalled = false; |
| 434 |
this.inherited(arguments); |
| 435 |
this.touchNode.setAttribute("aria-valuetext", this.get("value")); |
| 436 |
}, |
| 437 |
|
| 438 |
spin: function(steps){ |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
if(!this._started || this._duringSlideTo){ |
| 444 |
return; |
| 445 |
} |
| 446 |
var to = this.getPos(); |
| 447 |
to.y += steps * this._itemHeight; |
| 448 |
this.slideTo(to, 1); |
| 449 |
}, |
| 450 |
|
| 451 |
getSpeed: function(){ |
| 452 |
|
| 453 |
|
| 454 |
var y = 0, n = this._time.length; |
| 455 |
var delta = (new Date()).getTime() - this.startTime - this._time[n - 1]; |
| 456 |
if(n >= 2 && delta < 200){ |
| 457 |
var dy = this._posY[n - 1] - this._posY[(n - 6) >= 0 ? n - 6 : 0]; |
| 458 |
var dt = this._time[n - 1] - this._time[(n - 6) >= 0 ? n - 6 : 0]; |
| 459 |
y = this.calcSpeed(dy, dt); |
| 460 |
} |
| 461 |
return {x:0, y:y}; |
| 462 |
}, |
| 463 |
|
| 464 |
calcSpeed: function(d, t){ |
| 465 |
|
| 466 |
|
| 467 |
var speed = this.inherited(arguments); |
| 468 |
if(!speed){ return 0; } |
| 469 |
var v = Math.abs(speed); |
| 470 |
var ret = speed; |
| 471 |
if(v > this.maxSpeed){ |
| 472 |
ret = this.maxSpeed*(speed/v); |
| 473 |
} |
| 474 |
return ret; |
| 475 |
}, |
| 476 |
|
| 477 |
adjustDestination: function(to, pos, dim){ |
| 478 |
|
| 479 |
|
| 480 |
var h = this._itemHeight; |
| 481 |
var j = to.y + Math.round(h/2); |
| 482 |
var r = j >= 0 ? j % h : j % h + h; |
| 483 |
to.y = j - r; |
| 484 |
return true; |
| 485 |
}, |
| 486 |
|
| 487 |
resize: function(e){ |
| 488 |
|
| 489 |
if(this.panelNodes && this.panelNodes.length > 0){ |
| 490 |
var items = this.panelNodes[1].childNodes; |
| 491 |
|
| 492 |
|
| 493 |
if(items.length > 0 && !has("windows-theme")){ |
| 494 |
var parent = this.getParent(); |
| 495 |
if(parent){ |
| 496 |
this._itemHeight = items[0].offsetHeight; |
| 497 |
this.centerPos = parent.centerPos; |
| 498 |
if(!this.panelNodes[0].style.top){ |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
this.adjust(); |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
if(this._pendingValue){ |
| 508 |
this.set("value", this._pendingValue); |
| 509 |
} |
| 510 |
}, |
| 511 |
|
| 512 |
slideTo: function(to, duration, easing){ |
| 513 |
|
| 514 |
|
| 515 |
this._duringSlideTo = true; |
| 516 |
var pos = this.getPos(); |
| 517 |
var top = pos.y + this.panelNodes[1].offsetTop; |
| 518 |
var bottom = top + this.panelNodes[1].offsetHeight; |
| 519 |
var vh = this.domNode.parentNode.offsetHeight; |
| 520 |
var t; |
| 521 |
if(pos.y < to.y){ |
| 522 |
if(bottom > vh){ |
| 523 |
|
| 524 |
t = this.panelNodes[2]; |
| 525 |
t.style.top = this.panelNodes[0].offsetTop - this.panelNodes[0].offsetHeight + "px"; |
| 526 |
this.panelNodes[2] = this.panelNodes[1]; |
| 527 |
this.panelNodes[1] = this.panelNodes[0]; |
| 528 |
this.panelNodes[0] = t; |
| 529 |
} |
| 530 |
}else if(pos.y > to.y){ |
| 531 |
if(top < 0){ |
| 532 |
|
| 533 |
t = this.panelNodes[0]; |
| 534 |
t.style.top = this.panelNodes[2].offsetTop + this.panelNodes[2].offsetHeight + "px"; |
| 535 |
this.panelNodes[0] = this.panelNodes[1]; |
| 536 |
this.panelNodes[1] = this.panelNodes[2]; |
| 537 |
this.panelNodes[2] = t; |
| 538 |
} |
| 539 |
} |
| 540 |
if(this.getParent()._duringStartup){ |
| 541 |
duration = 0; |
| 542 |
|
| 543 |
|
| 544 |
}else if(Math.abs(this._speed.y) < 40){ |
| 545 |
duration = 0.2; |
| 546 |
} |
| 547 |
if(duration && duration > 0){ |
| 548 |
this.inherited(arguments, [to, duration, easing]); |
| 549 |
if(!this._onFlickAnimationStartCalled){ |
| 550 |
|
| 551 |
this._duringSlideTo = false; |
| 552 |
|
| 553 |
} |
| 554 |
}else{ |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
this.onFlickAnimationStart(); |
| 560 |
this.scrollTo(to, true); |
| 561 |
this.onFlickAnimationEnd(); |
| 562 |
} |
| 563 |
} |
| 564 |
}); |
| 565 |
|
| 566 |
return has("dojo-bidi") ? declare("dojox.mobile.SpinWheelSlot", [SpinWheelSlot, BidiSpinWheelSlot]) : SpinWheelSlot; |
| 567 |
}); |