-
Notifications
You must be signed in to change notification settings - Fork 25
/
bootstrap-fullscreen-select.js
445 lines (405 loc) · 15.7 KB
/
bootstrap-fullscreen-select.js
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
'use strict';
/*!
* Bootstrap-fullscreen-select v1.5.1 (http://craftpip.github.io/bootstrap-fullscreen-select/)
* Author: boniface pereira
* Website: www.craftpip.com
* Contact: hey@craftpip.com
*
* Copyright 2013-2014 bootstrap-select
* Licensed under MIT (https://github.com/craftpip/bootstrap-fullscreen-select/blob/master/LICENSE)
*/
if (typeof jQuery === 'undefined') {
throw new Error('Mobileselect\' requires jQuery');
}
(function ($) {
$.fn.mobileSelect = function (options) {
var $this = $(this);
/*
* backout if no elements are selected.
*/
if (!$this.length)
return 'no elements to process';
/*
* set an empty object if options === undefined
*/
if (!options)
options = {};
if (typeof options === 'string') {
if (options === 'destroy') {
// destroy the mobile select initialization.
$this.each(function (i, a) {
var id = $(a).attr('data-msid');
$.mobileSelect.elements[id].destroy();
delete $.mobileSelect.elements[id];
});
}
if (options === 'sync' || options === 'refresh') {
//sync changes in the options.
$this.each(function (i, a) {
var id = $(a).attr('data-msid');
$.mobileSelect.elements[id].refresh();
});
}
if (options === 'hide') {
// programmically hide a shown mobileSelect
$this.each(function (i, a) {
var id = $(a).attr('data-msid');
$.mobileSelect.elements[id].hide();
});
}
if (options === 'show') {
// programmicallly show a mobileSelect
$this.each(function (i, a) {
var id = $(a).attr('data-msid');
$.mobileSelect.elements[id].show();
});
}
return;
}
// if user defaults provided overwrite with mobileSelect defaults.
if ($.mobileSelect.defaults) {
$.extend($.fn.mobileSelect.defaults, $.mobileSelect.defaults);
}
// options to be merged with defaults.
options = $.extend({}, $.fn.mobileSelect.defaults, options);
// start iterating over!
$this.each(function (i, a) {
var $elm = $(a);
//reject non SELECT elements
if ($elm[0].tagName !== 'SELECT') {
console.warn('Sorry, cannot initialized a ' + $elm[0].tagName + ' element');
return true;
// continue;
}
if ($elm.attr('data-msid') !== undefined) {
console.error('This element is already Initialized');
return true;
// continue
}
// track objects via generated IDs
var id = Math.floor(Math.random() * 999999);
$elm.attr('data-msid', id);
var mobileSelectObj = new Mobileselect(a, options);
$.mobileSelect.elements[id] = mobileSelectObj;
});
};
var Mobileselect = function (element, options) {
this.$e = $(element);
$.extend(this, options);
this.init();
};
Mobileselect.prototype = {
init: function () {
this._setUserOptions();
this._extractOptions();
this._buildHTML();
this._bindEvents();
},
_buildHTML: function () {
/*
* build and insert the trigger element
*/
if (this.$e.attr('data-triggerOn') === undefined) {
// no custom trigger element.
if (this.$e.attr('data-style') !== undefined) {
this.style = this.$e.attr('data-style');
}
var b = this.$e.attr('disabled') || '';
this.$e.before('<button type="button" class="btn ' + this.style + ' btn-mobileSelect-gen" '+b+'><span class="text"></span> <span class="caret"></span></button>');
this.$triggerElement = this.$e.prev();
this.$e.hide();
} else {
this.$triggerElement = $(this.$e.attr('data-triggerOn'));
}
/*
* Build mobile select container HTML.
*/
//seting up the container.
this.$c = $('<div class="mobileSelect-container"></div>').addClass(this.theme).appendTo('body');
//appending the container template
this.$c.html($.fn.mobileSelect.defaults.template);
//settings container animations.
this.$c.children('div').css({
'-webkit-transition': 'all ' + this.animationSpeed / 1000 + 's',
'transition': 'all ' + this.animationSpeed / 1000 + 's'
}).css(this.padding).addClass('anim-' + this.animation);
/*
* set title buttons text.
*/
this.$c.find('.mobileSelect-title').html(this.title).end().find('.mobileSelect-savebtn').html(this.buttonSave).end().find('.mobileSelect-clearbtn').html(this.buttonClear).end().find('.mobileSelect-cancelbtn').html(this.buttonCancel).end();
this.$listcontainer = this.$c.find('.list-container');
if (!this.isMultiple) {
this.$c.find('.mobileSelect-clearbtn').remove();
} else {
this.$listcontainer.data('multiple', 'true');
}
this._appendOptionsList();
},
_appendOptionsList: function () {
/*
* append options list.
*/
this.$listcontainer.html('');
var that = this;
var prevGroup = '';
$.each(this.options, function (i, a) {
if(a.group && a.group !== prevGroup){
if(a.groupDisabled){
var b = 'disabled';
}
that.$listcontainer.append('<span class="mobileSelect-group" '+b+'>'+a.group+'</span>');
prevGroup = a.group;
}
if(a.groupDisabled || a.disabled){
var b = 'disabled';
}
that.$listcontainer.append('<a href="#" class="mobileSelect-control" '+ b +' data-value="' + a.value + '">' + a.text + '</a>');
});
this.sync();
this._updateBtnCount();
},
_updateBtnCount: function () {
/*
* Update generated button count.
*/
if (this.$triggerElement.is('button') && this.$triggerElement.hasClass('btn-mobileSelect-gen')) {
var a = this.$triggerElement.find('.text'),
b = this.$triggerElement.next().find('option:selected').text() || this.$e.val(),
c = this.$e.attr('data-btntitle'),
d = this.$e.attr('data-selected');
if (b === null && c === undefined) {
a.html("Nothing selected");
return false;
}
if (b === null) {
a.html(c);
return false;
}
if (this.isMultiple) {
if (b.length === 1) {
a.html(b);
} else {
if(d === undefined){
a.html(b.length + ' items selected');
}else{
a.html(b.length + ' ' + d);
}
}
} else {
if (c === undefined) {
a.html(b);
}else{
a.html(c);
}
}
}
},
_bindEvents: function () {
/*
* binding events on trigger element & buttons.
*/
var that = this;
this.$triggerElement.on('click', function (e) {
e.preventDefault();
that.show();
});
this.$c.find('.mobileSelect-savebtn').on('click', function (e) {
e.preventDefault();
that.syncR();
that.hide();
});
this.$c.find('.mobileSelect-clearbtn').on('click', function (e) {
e.preventDefault();
that.$listcontainer.find('.selected').removeClass('selected');
that.syncR();
that.hide();
});
this.$c.find('.mobileSelect-cancelbtn').on('click', function (e) {
e.preventDefault();
that.hide();
});
this.$c.find('.mobileSelect-control').on('click', function (e) {
e.preventDefault();
var $this = $(this);
if($this.attr('disabled') == 'disabled')
return false;
if (that.isMultiple) {
$this.toggleClass('selected');
} else {
$this.siblings().removeClass('selected').end().addClass('selected');
}
});
},
_unbindEvents: function () {
/*
* to unbind events while destroy.
*/
this.$triggerElement.unbind('click');
this.$c.find('.mobileSelect-clearbtn').unbind('click');
this.$c.find('.mobileSelect-cancelbtn').unbind('click');
this.$c.find('.mobileSelect-control').unbind('click');
},
sync: function () {
/*
* sync from select element to mobile select container
*/
var selectedOptions = this.$e.val();
if (!this.isMultiple)
selectedOptions = [selectedOptions];
this.$c.find('a').removeClass('selected');
for (var i in selectedOptions) {
this.$c.find('a[data-value="' + selectedOptions[i] + '"]').addClass('selected');
}
},
syncR: function () {
/*
* sync from mobile select container to select element
*/
var selectedOptions = [];
this.$c.find('.selected').each(function () {
selectedOptions.push($(this).data('value'));
});
this.$e.val(selectedOptions);
},
hide: function () {
/*
* hide animation with onClose callback
*/
this.$c.children('div').addClass('anim-' + this.animation);
var that = this;
this._updateBtnCount();
setTimeout(function () {
that.$c.hide();
$('body').removeClass('mobileSelect-noscroll');
that.onClose.apply(that.$e);
}, this.animationSpeed);
},
show: function () {
/*
* show animation with onOpen callback
*/
this.$c.show();
this.sync();
$('body').addClass('mobileSelect-noscroll');
var that = this;
setTimeout(function () {
that.$c.children('div').removeClass($.mobileSelect.animations.join(' '));
}, 10);
this.onOpen.apply(this.$e);
},
_setUserOptions: function () {
/*
* overwrite options with data-attributes if provided.
*/
this.isMultiple = this.$e.attr('multiple') ? true : false;
if (this.$e.data('title') !== undefined) {
this.title = this.$e.data('title');
}
if (this.$e.data('animation') !== undefined) {
this.animation = this.$e.data('animation');
}
if (this.$e.data('animation-speed') !== undefined) {
this.animationSpeed = this.$e.data('animation-speed');
}
if (this.$e.data('padding') !== undefined) {
this.padding = this.$e.data('padding');
}
if (this.$e.data('btntext-save') !== undefined) {
this.buttonSave = this.$e.data('btntext-save');
}
if (this.$e.data('btntext-clear') !== undefined) {
this.buttonClear = this.$e.data('btntext-clear');
}
if (this.$e.data('btntext-cancel') !== undefined) {
this.buttonCancel = this.$e.data('btntext-cancel');
}
if (this.$e.data('theme') !== undefined) {
this.theme = this.$e.data('theme');
}
if (this.animation === 'none') {
this.animationSpeed = 0;
}
},
_extractOptions: function () {
/*
* Get options from the select element and store them in an array.
*/
var options = [];
$.each(this.$e.find('option'), function (i, a) {
var $t = $(a);
if ($t.text()) {
// var label = $t.parent().is('optgroup') ? $t.parent().attr('label') : false;
if($t.parent().is('optgroup')){
var label = $t.parent().attr('label');
var labelDisabled = $t.parent().prop('disabled');
}else{
var label = false;
var labelDisabled = false;
}
options.push({
value: $t.val(),
text: $.trim($t.text()),
disabled: $t.prop('disabled'),
group: label,
groupDisabled: labelDisabled
});
}
});
this.options = options;
},
destroy: function () {
/*
* destroy the select
* unbind events
* remove triggerElement, container
* show the Native select
*/
this.$e.removeAttr('data-msid');
this._unbindEvents();
this.$triggerElement.remove();
this.$c.remove();
this.$e.show();
console.log('done ');
},
refresh: function () {
/*
* refresh/sync the native select with the mobileSelect.
*/
this._extractOptions();
this._appendOptionsList();
this._unbindEvents();
this._bindEvents();
}
};
/*
* for user defaults.
*/
$.mobileSelect = {
elements: {}, //to store records
animations: ['anim-top', 'anim-bottom', 'anim-left', 'anim-right', 'anim-opacity', 'anim-scale', 'anim-zoom', 'anim-none'] //supported animations
};
/*
* plugin defaults
*/
$.fn.mobileSelect.defaults = {
template: '<div><div class="mobileSelect-title"></div><div class="list-container"></div><div class="mobileSelect-buttons"><a href="#" class="mobileSelect-savebtn"></a><a href="#" class="mobileSelect-clearbtn"></a><a href="#" class="mobileSelect-cancelbtn"></a></div></div>',
title: 'Select an option',
buttonSave: 'Save',
buttonClear: 'Clear',
buttonCancel: 'Cancel',
padding: {
'top': '20px',
'left': '20px',
'right': '20px',
'bottom': '20px'
},
animation: 'scale',
animationSpeed: 400,
theme: 'white',
onOpen: function () {
},
onClose: function () {
},
style: 'btn-default'
};
})(jQuery);