1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
//>>built
define("dojox/mobile/SpinWheelSlot", [
"dojo/_base/declare",
"dojo/_base/window",
"dojo/dom-class",
"dojo/dom-construct",
"dijit/_Contained",
"dijit/_WidgetBase",
"./_ScrollableMixin"
], function(declare, win, domClass, domConstruct, Contained, WidgetBase, ScrollableMixin){
/*=====
var Contained = dijit._Contained;
var WidgetBase = dijit._WidgetBase;
var ScrollableMixin = dojox.mobile._ScrollableMixin;
=====*/
// module:
// dojox/mobile/SpinWheelSlot
// summary:
// A slot of a SpinWheel.
return declare("dojox.mobile.SpinWheelSlot", [WidgetBase, Contained, ScrollableMixin], {
// summary:
// A slot of a SpinWheel.
// description:
// SpinWheelSlot is a slot that is placed in the SpinWheel widget.
// items: Array
// An array of array of key-label paris.
// (e.g. [[0,"Jan"],[1,"Feb"],...] ) If key values for each label
// are not necessary, labels can be used instead.
items: [],
// labels: Array
// An array of labels to be displayed on the slot.
// (e.g. ["Jan","Feb",...] ) This is a simplified version of the
// items property.
labels: [],
// labelFrom: Number
// The start value of display values of the slot. This parameter is
// especially useful when slot has serial values.
labelFrom: 0,
// labelTo: Number
// The end value of display values of the slot.
labelTo: 0,
// value: String
// The initial value of the slot.
value: "",
/* internal properties */
maxSpeed: 500,
minItems: 15,
centerPos: 0,
scrollBar: false,
constraint: false,
allowNestedScrolls: false,
androidWorkaroud: false, // disable workaround in SpinWheel
buildRendering: function(){
this.inherited(arguments);
domClass.add(this.domNode, "mblSpinWheelSlot");
var i, j, idx;
if(this.labelFrom !== this.labelTo){
this.labels = [];
for(i = this.labelFrom, idx = 0; i <= this.labelTo; i++, idx++){
this.labels[idx] = String(i);
}
}
if(this.labels.length > 0){
this.items = [];
for(i = 0; i < this.labels.length; i++){
this.items.push([i, this.labels[i]]);
}
}
this.containerNode = domConstruct.create("DIV", {className:"mblSpinWheelSlotContainer"});
this.containerNode.style.height
= (win.global.innerHeight||win.doc.documentElement.clientHeight) * 2 + "px"; // must bigger than the screen
this.panelNodes = [];
for(var k = 0; k < 3; k++){
this.panelNodes[k] = domConstruct.create("DIV", {className:"mblSpinWheelSlotPanel"});
var len = this.items.length;
var n = Math.ceil(this.minItems / len);
for(j = 0; j < n; j++){
for(i = 0; i < len; i++){
domConstruct.create("DIV", {
className: "mblSpinWheelSlotLabel",
name: this.items[i][0],
innerHTML: this._cv ? this._cv(this.items[i][1]) : this.items[i][1]
}, this.panelNodes[k]);
}
}
this.containerNode.appendChild(this.panelNodes[k]);
}
this.domNode.appendChild(this.containerNode);
this.touchNode = domConstruct.create("DIV", {className:"mblSpinWheelSlotTouch"}, this.domNode);
this.setSelectable(this.domNode, false);
},
startup: function(){
this.inherited(arguments);
this.centerPos = this.getParent().centerPos;
var items = this.panelNodes[1].childNodes;
this._itemHeight = items[0].offsetHeight;
this.adjust();
},
adjust: function(){
// summary:
// Adjusts the position of slot panels.
var items = this.panelNodes[1].childNodes;
var adjustY;
for(var i = 0, len = items.length; i < len; i++){
var item = items[i];
if(item.offsetTop <= this.centerPos && this.centerPos < item.offsetTop + item.offsetHeight){
adjustY = this.centerPos - (item.offsetTop + Math.round(item.offsetHeight/2));
break;
}
}
var h = this.panelNodes[0].offsetHeight;
this.panelNodes[0].style.top = -h + adjustY + "px";
this.panelNodes[1].style.top = adjustY + "px";
this.panelNodes[2].style.top = h + adjustY + "px";
},
setInitialValue: function(){
// summary:
// Sets the initial value using this.value or the first item.
if(this.items.length > 0){
var val = (this.value !== "") ? this.value : this.items[0][1];
this.setValue(val);
}
},
getCenterPanel: function(){
// summary:
// Gets a panel that contains the currently selected item.
var pos = this.getPos();
for(var i = 0, len = this.panelNodes.length; i < len; i++){
var top = pos.y + this.panelNodes[i].offsetTop;
if(top <= this.centerPos && this.centerPos < top + this.panelNodes[i].offsetHeight){
return this.panelNodes[i];
}
}
return null;
},
setColor: function(/*String*/value){
// summary:
// Sets the color of the specified item as blue.
for(var i = 0, len = this.panelNodes.length; i < len; i++){
var items = this.panelNodes[i].childNodes;
for(var j = 0; j < items.length; j++){
if(items[j].innerHTML === String(value)){
domClass.add(items[j], "mblSpinWheelSlotLabelBlue");
}else{
domClass.remove(items[j], "mblSpinWheelSlotLabelBlue");
}
}
}
},
disableValues: function(/*Array*/values){
// summary:
// Makes the specified items grayed out.
for(var i = 0, len = this.panelNodes.length; i < len; i++){
var items = this.panelNodes[i].childNodes;
for(var j = 0; j < items.length; j++){
domClass.remove(items[j], "mblSpinWheelSlotLabelGray");
for(var k = 0; k < values.length; k++){
if(items[j].innerHTML === String(values[k])){
domClass.add(items[j], "mblSpinWheelSlotLabelGray");
break;
}
}
}
}
},
getCenterItem: function(){
// summary:
// Gets the currently selected item.
var pos = this.getPos();
var centerPanel = this.getCenterPanel();
if(centerPanel){
var top = pos.y + centerPanel.offsetTop;
var items = centerPanel.childNodes;
for(var i = 0, len = items.length; i < len; i++){
if(top + items[i].offsetTop <= this.centerPos && this.centerPos < top + items[i].offsetTop + items[i].offsetHeight){
return items[i];
}
}
}
return null;
},
getValue: function(){
// summary:
// Gets the currently selected value.
var item = this.getCenterItem();
return (item && item.innerHTML);
},
getKey: function(){
// summary:
// Gets the key for the currently selected value.
return this.getCenterItem().getAttribute("name");
},
setValue: function(newValue){
// summary:
// Sets the newValue to this slot.
var idx0, idx1;
var curValue = this.getValue();
if(!curValue){
this._penddingValue = newValue;
return;
}
this._penddingValue = undefined;
var n = this.items.length;
for(var i = 0; i < n; i++){
if(this.items[i][1] === String(curValue)){
idx0 = i;
}
if(this.items[i][1] === String(newValue)){
idx1 = i;
}
if(idx0 !== undefined && idx1 !== undefined){
break;
}
}
var d = idx1 - (idx0 || 0);
var m;
if(d > 0){
m = (d < n - d) ? -d : n - d;
}else{
m = (-d < n + d) ? -d : -(n + d);
}
var to = this.getPos();
to.y += m * this._itemHeight;
this.slideTo(to, 1);
},
getSpeed: function(){
// summary:
// Overrides dojox.mobile.scrollable.getSpeed().
var y = 0, n = this._time.length;
var delta = (new Date()).getTime() - this.startTime - this._time[n - 1];
if(n >= 2 && delta < 200){
var dy = this._posY[n - 1] - this._posY[(n - 6) >= 0 ? n - 6 : 0];
var dt = this._time[n - 1] - this._time[(n - 6) >= 0 ? n - 6 : 0];
y = this.calcSpeed(dy, dt);
}
return {x:0, y:y};
},
calcSpeed: function(/*Number*/d, /*Number*/t){
// summary:
// Overrides dojox.mobile.scrollable.calcSpeed().
var speed = this.inherited(arguments);
if(!speed){ return 0; }
var v = Math.abs(speed);
var ret = speed;
if(v > this.maxSpeed){
ret = this.maxSpeed*(speed/v);
}
return ret;
},
adjustDestination: function(to, pos){
// summary:
// Overrides dojox.mobile.scrollable.adjustDestination().
var h = this._itemHeight;
var j = to.y + Math.round(h/2);
var a = Math.abs(j);
var r = j >= 0 ? j % h : j % h + h;
to.y = j - r;
},
resize: function(e){
if(this._penddingValue){
this.setValue(this._penddingValue);
}
},
slideTo: function(/*Object*/to, /*Number*/duration, /*String*/easing){
// summary:
// Overrides dojox.mobile.scrollable.slideTo().
var pos = this.getPos();
var top = pos.y + this.panelNodes[1].offsetTop;
var bottom = top + this.panelNodes[1].offsetHeight;
var vh = this.domNode.parentNode.offsetHeight;
var t;
if(pos.y < to.y){ // going down
if(bottom > vh){
// move up the bottom panel
t = this.panelNodes[2];
t.style.top = this.panelNodes[0].offsetTop - this.panelNodes[0].offsetHeight + "px";
this.panelNodes[2] = this.panelNodes[1];
this.panelNodes[1] = this.panelNodes[0];
this.panelNodes[0] = t;
}
}else if(pos.y > to.y){ // going up
if(top < 0){
// move down the top panel
t = this.panelNodes[0];
t.style.top = this.panelNodes[2].offsetTop + this.panelNodes[2].offsetHeight + "px";
this.panelNodes[0] = this.panelNodes[1];
this.panelNodes[1] = this.panelNodes[2];
this.panelNodes[2] = t;
}
}
if(!this._initialized){
duration = 0; // to reduce flickers at start-up especially on android
this._initialized = true;
}else if(Math.abs(this._speed.y) < 40){
duration = 0.2;
}
this.inherited(arguments, [to, duration, easing]); // 2nd arg is to avoid excessive optimization by closure compiler
}
});
});
|