Debian Codegraph @

dojox/mobile/SpinWheelSlot.js

raw symbols SCIP
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 // module:
20 // dojox/mobile/SpinWheelSlot
21
22 var SpinWheelSlot = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiSpinWheelSlot" : "dojox.mobile.SpinWheelSlot", [WidgetBase, Contained, Scrollable], {
23 // summary:
24 // A slot of a SpinWheel.
25 // description:
26 // SpinWheelSlot is a slot that is placed in the SpinWheel widget.
27
28 // items: Array
29 // An array of array of key-label pairs
30 // (e.g. [[0, "Jan"], [1, "Feb"], ...]). If key values for each label
31 // are not necessary, labels can be used instead.
32 items: [],
33
34 // labels: Array
35 // An array of labels to be displayed on the slot
36 // (e.g. ["Jan", "Feb", ...]). This is a simplified version of the
37 // items property.
38 labels: [],
39
40 // labelFrom: Number
41 // The start value of display values of the slot. This parameter is
42 // especially useful when the slot has serial values.
43 labelFrom: 0,
44
45 // labelTo: Number
46 // The end value of display values of the slot.
47 labelTo: 0,
48
49 // zeroPad: Number
50 // Length of zero padding numbers.
51 // Ex. zeroPad=2 -> "00", "01", ...
52 // Ex. zeroPad=3 -> "000", "001", ...
53 zeroPad: 0,
54
55 // value: String
56 // The initial value of the slot.
57 value: "",
58
59 // step: Number
60 // The steps between labelFrom and labelTo.
61 step: 1,
62
63 // pageStep: Number
64 // The number of items in a page when using pageup/pagedown keys to navigate with the keyboard.
65 pageSteps: 1,
66
67 /* internal properties */
68 baseClass: "mblSpinWheelSlot",
69 // maxSpeed: [private] Number
70 // Maximum speed.
71 maxSpeed: 500,
72 // minItems: [private] int
73 // Minimum number of items.
74 minItems: 15,
75 // centerPos: [private] Number
76 // Inherited from parent.
77 centerPos: 0,
78 // scrollbar: [private] Boolean
79 // False: no scrollbars must be shown.
80 scrollBar: false,
81 // constraint: [private] Boolean
82 // False: no scroll constraint.
83 constraint: false,
84 // propagatable: [private] Boolean
85 // False: stop touchstart event propagation.
86 propagatable: false, // stop touchstart event propagation to make spin wheel work inside scrollable
87 // androidWorkaroud: [private] Boolean
88 // False.
89 androidWorkaroud: false, // disable workaround in SpinWheel TODO:remove this line later
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"; // must bigger than the screen
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){ // if the slot is not empty
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){ // if the slot is not empty
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"); // for desktop browsers
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 // summary:
220 // Initializes the slot labels according to the labelFrom/labelTo properties.
221 // tags:
222 // private
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(); // give focus to enable key navigation
234 this.inherited(arguments);
235 },
236
237 adjust: function(){
238 // summary:
239 // Adjusts the position of slot panels.
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 // summary:
257 // Sets the initial value using this.value or the first item.
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: // up arrow key (fallthrough)
268 case 39: // right arrow key
269 this.spin(1);
270 e.stopPropagation();
271 return false;
272 case 40: // down arrow key (fallthrough)
273 case 37: // left arrow key
274 this.spin(-1);
275 e.stopPropagation();
276 return false;
277 case 33: // pageup
278 this.spin(this.pageSteps);
279 e.stopPropagation();
280 return false;
281 case 34: // pagedown
282 this.spin(-1 * this.pageSteps);
283 e.stopPropagation();
284 return false;
285 }
286 return true;
287 },
288
289 _getCenterPanel: function(){
290 // summary:
291 // Gets a panel that contains the currently selected item.
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(/*String*/value, /*String?*/color){
303 // summary:
304 // Sets the color of the specified item as blue.
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(/*Number*/n){
313 // summary:
314 // Grays out the items with an index higher or equal to the specified number.
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 // summary:
324 // Gets the currently selected item.
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 // summary:
342 // Gets the key for the currently selected value.
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 // summary:
360 // Gets the currently selected value.
361 if(!this._started){
362 return this.value;
363 }
364 if(this.items.length > 0){ // if the slot is not empty
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 // summary:
374 // Sets the value to this slot.
375 if(this.items.length > 0){ // no-op for empty slots
376 this._spinToValue(value, true);
377 }
378 },
379
380 _spinToValue: function(value, applyValue){
381 // summary:
382 // Spins the slot to the specified value.
383 // tags:
384 // private
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; // no change; avoid notification
393 }
394 this._pendingValue = undefined;
395 // to avoid unnecessary notifications, applyValue is false when
396 // _spinToValue is called by _DatePickerMixin.
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 // summary:
424 // Overrides dojox/mobile/scrollable.onFlickAnimationStart().
425 this._onFlickAnimationStartCalled = true;
426 this.inherited(arguments);
427 },
428
429 onFlickAnimationEnd: function(e){
430 // summary:
431 // Overrides dojox/mobile/scrollable.onFlickAnimationEnd().
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(/*Number*/steps){
439 // summary:
440 // Spins the slot as specified by steps.
441
442 // do nothing before startup and during slide
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 // summary:
453 // Overrides dojox/mobile/scrollable.getSpeed().
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(/*Number*/d, /*Number*/t){
465 // summary:
466 // Overrides dojox/mobile/scrollable.calcSpeed().
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 // summary:
479 // Overrides dojox/mobile/scrollable.adjustDestination().
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 // Correct internal variables & adjust slot panels
489 if(this.panelNodes && this.panelNodes.length > 0){
490 var items = this.panelNodes[1].childNodes;
491 // TODO investigate - the position is calculated incorrectly for
492 // windows theme, disable this logic for now.
493 if(items.length > 0 && !has("windows-theme")){ // empty slot?
494 var parent = this.getParent();
495 if(parent){ // #18012: null in same cases on IE8/9
496 this._itemHeight = items[0].offsetHeight;
497 this.centerPos = parent.centerPos;
498 if(!this.panelNodes[0].style.top){
499 // (#17339) to avoid messing up the layout of the panels, call adjust()
500 // only if it didn't manage yet to set the style.top (this happens
501 // typically because the slot was initially hidden).
502 this.adjust();
503 }
504 }
505 }
506 }
507 if(this._pendingValue){
508 this.set("value", this._pendingValue);
509 }
510 },
511
512 slideTo: function(/*Object*/to, /*Number*/duration, /*String*/easing){
513 // summary:
514 // Overrides dojox/mobile/scrollable.slideTo().
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){ // going down
522 if(bottom > vh){
523 // move up the bottom panel
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){ // going up
531 if(top < 0){
532 // move down the top panel
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; // to reduce flickers at start-up especially on android
542 // No scroll animation at startup. This avoids flickering especially on Android,
543 // and avoids the issue in #17775.
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]); // 2nd arg is to avoid excessive optimization by closure compiler
549 if(!this._onFlickAnimationStartCalled){
550 // if slideTo() didn't call itself (synchronously) onFlickAnimationEnd():
551 this._duringSlideTo = false;
552 // (otherwise, wait for onFlickAnimationEnd which deletes the flag)
553 }
554 }else{
555 // #17775: at startup, no scroll animation, because it is not needed ergonomically,
556 // and because the animation would imply an asynchrouns notification of
557 // onFlickAnimationEnd() which would forbid resetting the value right after startup.
558 // this.onFlickAnimationStart(); // not called by scrollTo()
559 this.onFlickAnimationStart(); // not called by scrollTo()
560 this.scrollTo(to, true);
561 this.onFlickAnimationEnd(); // not called by scrollTo()
562 }
563 }
564 });
565
566 return has("dojo-bidi") ? declare("dojox.mobile.SpinWheelSlot", [SpinWheelSlot, BidiSpinWheelSlot]) : SpinWheelSlot;
567 });