mobiscroll.list-2.5.1.js
14.5 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*jslint eqeq: true, plusplus: true, undef: true, sloppy: true, vars: true, forin: true */
(function ($) {
var ms = $.mobiscroll,
defaults = {
invalid: [],
showInput: true,
inputClass: ''
},
preset = function (inst) {
var s = $.extend({}, defaults, inst.settings),
elm = $(this),
input,
prevent,
id = this.id + '_dummy',
lvl = 0,
ilvl = 0,
timer = {},
wa = s.wheelArray || createWheelArray(elm),
labels = generateLabels(lvl),
currWheelVector = [],
fwv = firstWheelVector(wa),
w = generateWheelsFromVector(fwv, lvl);
/**
* Disables the invalid items on the wheels
* @param {Object} dw - the jQuery mobiscroll object
* @param {Number} nrWheels - the number of the current wheels
* @param {Array} whArray - The wheel array objects containing the wheel tree
* @param {Array} whVector - the wheel vector containing the current keys
*/
function setDisabled(dw, nrWheels, whArray, whVector) {
var i = 0;
while (i < nrWheels) {
var currWh = $('.dwwl' + i, dw),
inv = getInvalidKeys(whVector, i, whArray);
$.each(inv, function (i, v) {
$('.dw-li[data-val="' + v + '"]', currWh).removeClass('dw-v');
});
i++;
}
}
/**
* Returns the invalid keys of one wheel as an array
* @param {Array} whVector - the wheel vector used to search for the wheel in the wheel array
* @param {Number} index - index of the wheel in the wheel vector, that we are interested in
* @param {Array} whArray - the wheel array we are searching in
* @return {Array} - list of invalid keys
*/
function getInvalidKeys(whVector, index, whArray) {
var i = 0,
n,
whObjA = whArray,
invalids = [];
while (i < index) {
var ii = whVector[i];
//whObjA = whObjA[ii].children;
for (n in whObjA) {
if (whObjA[n].key == ii) {
whObjA = whObjA[n].children;
break;
}
}
i++;
}
i = 0;
while (i < whObjA.length) {
if (whObjA[i].invalid) {
invalids.push(whObjA[i].key);
}
i++;
}
return invalids;
}
/**
* Creates a Boolean vector with true values (except one) that can be used as the readonly vector
* n - the length of the vector
* i - the index of the value that's going to be false
*/
function createROVector(n, i) {
var a = [];
while (n) {
a[--n] = true;
}
a[i] = false;
return a;
}
/**
* Creates a labels vector, from values if they are defined, otherwise from numbers
* l - the length of the vector
*/
function generateLabels(l) {
var a = [],
i;
for (i = 0; i < l; i++) {
a[i] = s.labels && s.labels[i] ? s.labels[i] : i;
}
return a;
}
/**
* Creates the wheel array from the vector provided
* wv - wheel vector containing the values that should be selected on the wheels
* l - the length of the wheel array
*/
function generateWheelsFromVector(wv, l, index) {
var i = 0, j, obj, chInd,
w = [{}],
wtObjA = wa;
if (l) { // if length is defined we need to generate that many wheels (even if they are empty)
for (j = 0; j < l; j++) {
w[j] = {};
w[j][labels[j]] = {}; // each wheel will have a label generated by the generateLabels method
}
}
while (i < wv.length) { // we generate the wheels until the length of the wheel vector
w[i] = {};
w[i][labels[i]] = getWheelFromObjA(wtObjA);
j = 0;
chInd = undefined;
while (j < wtObjA.length && chInd === undefined) {
if (wtObjA[j].key == wv[i] && ((index !== undefined && i <= index) || index === undefined)) {
chInd = j;
}
j++;
}
if (chInd !== undefined && wtObjA[chInd].children) {
i++;
wtObjA = wtObjA[chInd].children;
} else if ((obj = getFirstValidItemObjOrInd(wtObjA)) && obj.children) {
i++;
wtObjA = obj.children;
} else {
return w;
}
}
return w;
}
/**
* Returns the first valid Wheel Node Object or its index from a Wheel Node Object Array
* getInd - if it is true then the return value is going to be the index, otherwise the object itself
*/
function getFirstValidItemObjOrInd(wtObjA, getInd) {
if (!wtObjA) {
return false;
}
var i = 0,
obj;
while (i < wtObjA.length) {
if (!(obj = wtObjA[i++]).invalid) {
return getInd ? i - 1 : obj;
}
}
return false;
}
function getWheelFromObjA(objA) {
var wheel = {},
j = 0;
while (j < objA.length) {
wheel[objA[j].key] = objA[j++].value;
}
return wheel;
}
/**
* Hides the last i number of wheels
* i - the last number of wheels that has to be hidden
*/
function hideWheels(dw, i) {
$('.dwc', dw).css('display', '').slice(i).hide();
}
/**
* Generates the first wheel vector from the wheeltree
* wt - the wheel tree object
* uses the lvl global variable to determine the length of the vector
*/
function firstWheelVector(wa) {
var t = [],
ndObjA = wa,
obj,
ok = true,
i = 0;
while (ok) {
obj = getFirstValidItemObjOrInd(ndObjA);
t[i++] = obj.key;
if (ok = obj.children) {
ndObjA = obj.children;
}
}
return t;
}
/**
* Calculates the level of a wheel vector and the new wheel vector, depending on current wheel vector and the index of the changed wheel
* wv - current wheel vector
* index - index of the changed wheel
*/
function calcLevelOfVector2(wv, index) {
var t = [],
ndObjA = wa,
lvl = 0,
next = false,
i,
childName,
chInd;
if (wv[lvl] !== undefined && lvl <= index) {
i = 0;
childName = wv[lvl];
chInd = undefined;
while (i < ndObjA.length && chInd === undefined) {
if (ndObjA[i].key == wv[lvl] && !ndObjA[i].invalid) {
chInd = i;
}
i++;
}
} else {
chInd = getFirstValidItemObjOrInd(ndObjA, true);
childName = ndObjA[chInd].key;
}
next = chInd !== undefined ? ndObjA[chInd].children : false;
t[lvl] = childName;
while (next) {
ndObjA = ndObjA[chInd].children;
lvl++;
next = false;
chInd = undefined;
if (wv[lvl] !== undefined && lvl <= index) {
i = 0;
childName = wv[lvl];
chInd = undefined;
while (i < ndObjA.length && chInd === undefined) {
if (ndObjA[i].key == wv[lvl] && !ndObjA[i].invalid) {
chInd = i;
}
i++;
}
} else {
chInd = getFirstValidItemObjOrInd(ndObjA, true);
chInd = chInd === false ? undefined : chInd;
childName = ndObjA[chInd].key;
}
next = chInd !== undefined && getFirstValidItemObjOrInd(ndObjA[chInd].children) ? ndObjA[chInd].children : false;
t[lvl] = childName;
}
return {
lvl: lvl + 1,
nVector: t
}; // return the calculated level and the wheel vector as an object
}
function createWheelArray(ul) {
var wheelArray = [];
lvl = lvl > ilvl++ ? lvl : ilvl;
ul.children('li').each(function (index) {
var that = $(this),
c = that.clone();
c.children('ul,ol').remove();
var v = c.html().replace(/^\s\s*/, '').replace(/\s\s*$/, ''),
inv = that.data('invalid') ? true : false,
wheelObj = {
key: that.data('val') || index,
value: v,
invalid: inv,
children: null
},
nest = that.children('ul,ol');
if (nest.length) {
wheelObj.children = createWheelArray(nest);
}
wheelArray.push(wheelObj);
});
ilvl--;
return wheelArray;
}
$('#' + id).remove(); // Remove input if exists
if (s.showInput) {
input = $('<input type="text" id="' + id + '" value="" class="' + s.inputClass + '" readonly />').insertBefore(elm);
inst.settings.anchor = input; // give the core the input element for the bubble positioning
if (s.showOnFocus) {
input.focus(function () {
inst.show();
});
}
}
if (!s.wheelArray) {
elm.hide().closest('.ui-field-contain').trigger('create');
}
return {
width: 50,
wheels: w,
headerText: false,
onBeforeShow: function (dw) {
var t = inst.temp;
currWheelVector = t.slice(0);
inst.settings.wheels = generateWheelsFromVector(t, lvl, lvl);
prevent = true;
},
onSelect: function (v, inst) {
if (input) {
input.val(v);
}
},
onChange: function (v, inst) {
if (input && s.display == 'inline') {
input.val(v);
}
},
onClose: function () {
if (input) {
input.blur();
}
},
onShow: function (dw) {
$('.dwwl', dw).bind('mousedown touchstart', function () {
clearTimeout(timer[$('.dwwl', dw).index(this)]);
});
},
validate: function (dw, index, time) {
var t = inst.temp;
if ((index !== undefined && currWheelVector[index] != t[index]) || (index === undefined && !prevent)) {
inst.settings.wheels = generateWheelsFromVector(t, null, index);
var args = [],
i = (index || 0) + 1,
o = calcLevelOfVector2(t, index);
if (index !== undefined) {
inst.temp = o.nVector.slice(0);
}
while (i < o.lvl) {
args.push(i++);
}
hideWheels(dw, o.lvl);
currWheelVector = inst.temp.slice(0);
if (args.length) {
prevent = true;
inst.settings.readonly = createROVector(lvl, index);
clearTimeout(timer[index]);
timer[index] = setTimeout(function () {
inst.changeWheel(args);
inst.settings.readonly = false;
}, time * 1000);
return false;
}
setDisabled(dw, o.lvl, wa, inst.temp);
} else {
var o = calcLevelOfVector2(t, t.length);
setDisabled(dw, o.lvl, wa, t);
hideWheels(dw, o.lvl);
}
prevent = false;
}
};
};
$.each(['list', 'image', 'treelist'], function (i, v) {
ms.presets[v] = preset;
ms.presetShort(v);
});
})(jQuery);