1
|
!function($) {
|
2
|
var Selectpicker = function(element, options, e) {
|
3
|
if (e ) {
|
4
|
e.stopPropagation();
|
5
|
e.preventDefault();
|
6
|
}
|
7
|
this.$element = $(element);
|
8
|
this.$newElement = null;
|
9
|
this.button = null;
|
10
|
|
11
|
//Merge defaults, options and data-attributes to make our options
|
12
|
this.options = $.extend({}, $.fn.selectpicker.defaults, this.$element.data(), typeof options == 'object' && options);
|
13
|
|
14
|
//If we have no title yet, check the attribute 'title' (this is missed by jq as its not a data-attribute
|
15
|
if(this.options.title==null)
|
16
|
this.options.title = this.$element.attr('title');
|
17
|
|
18
|
//Expose public methods
|
19
|
this.val = Selectpicker.prototype.val;
|
20
|
this.render = Selectpicker.prototype.render;
|
21
|
this.init();
|
22
|
};
|
23
|
|
24
|
Selectpicker.prototype = {
|
25
|
|
26
|
constructor: Selectpicker,
|
27
|
|
28
|
init: function (e) {
|
29
|
var _this = this;
|
30
|
this.$element.hide();
|
31
|
this.multiple = this.$element.prop('multiple');
|
32
|
|
33
|
|
34
|
var classList = this.$element.attr('class') !== undefined ? this.$element.attr('class').split(/\s+/) : '';
|
35
|
var id = this.$element.attr('id');
|
36
|
this.$element.after( this.createView() );
|
37
|
this.$newElement = this.$element.next('.select');
|
38
|
var select = this.$newElement;
|
39
|
var menu = this.$newElement.find('.dropdown-menu');
|
40
|
var menuArrow = this.$newElement.find('.dropdown-arrow');
|
41
|
var menuA = menu.find('li > a');
|
42
|
var liHeight = select.addClass('open').find('.dropdown-menu li > a').outerHeight();
|
43
|
select.removeClass('open');
|
44
|
var divHeight = menu.find('li .divider').outerHeight(true);
|
45
|
var selectOffset_top = this.$newElement.offset().top;
|
46
|
var size = 0;
|
47
|
var menuHeight = 0;
|
48
|
var selectHeight = this.$newElement.outerHeight();
|
49
|
this.button = this.$newElement.find('> button');
|
50
|
if (id !== undefined) {
|
51
|
this.button.attr('id', id);
|
52
|
$('label[for="' + id + '"]').click(function(){ select.find('button#'+id).focus(); })
|
53
|
}
|
54
|
for (var i = 0; i < classList.length; i++) {
|
55
|
if(classList[i] != 'selectpicker') {
|
56
|
this.$newElement.addClass(classList[i]);
|
57
|
}
|
58
|
}
|
59
|
//If we are multiple, then add the show-tick class by default
|
60
|
if(this.multiple) {
|
61
|
this.$newElement.addClass('select-multiple');
|
62
|
}
|
63
|
this.button.addClass(this.options.style);
|
64
|
menu.addClass(this.options.menuStyle);
|
65
|
menuArrow.addClass(function() {
|
66
|
if (_this.options.menuStyle) {
|
67
|
return _this.options.menuStyle.replace('dropdown-', 'dropdown-arrow-');
|
68
|
}
|
69
|
});
|
70
|
this.checkDisabled();
|
71
|
this.checkTabIndex();
|
72
|
this.clickListener();
|
73
|
var menuPadding = parseInt(menu.css('padding-top')) + parseInt(menu.css('padding-bottom')) + parseInt(menu.css('border-top-width')) + parseInt(menu.css('border-bottom-width'));
|
74
|
if (this.options.size == 'auto') {
|
75
|
function getSize() {
|
76
|
var selectOffset_top_scroll = selectOffset_top - $(window).scrollTop();
|
77
|
var windowHeight = $(window).innerHeight();
|
78
|
var menuExtras = menuPadding + parseInt(menu.css('margin-top')) + parseInt(menu.css('margin-bottom')) + 2;
|
79
|
var selectOffset_bot = windowHeight - selectOffset_top_scroll - selectHeight - menuExtras;
|
80
|
menuHeight = selectOffset_bot;
|
81
|
if (select.hasClass('dropup')) {
|
82
|
menuHeight = selectOffset_top_scroll - menuExtras;
|
83
|
}
|
84
|
menu.css({'max-height' : menuHeight + 'px', 'overflow-y' : 'auto', 'min-height' : liHeight*3 + 'px'});
|
85
|
}
|
86
|
getSize();
|
87
|
$(window).resize(getSize);
|
88
|
$(window).scroll(getSize);
|
89
|
if (window.MutationObserver) {
|
90
|
new MutationObserver(getSize).observe(this.$element.get(0), {
|
91
|
childList: true
|
92
|
});
|
93
|
} else {
|
94
|
this.$element.bind('DOMNodeInserted', getSize);
|
95
|
}
|
96
|
} else if (this.options.size && this.options.size != 'auto' && menu.find('li').length > this.options.size) {
|
97
|
var optIndex = menu.find("li > *").filter(':not(.divider)').slice(0,this.options.size).last().parent().index();
|
98
|
var divLength = menu.find("li").slice(0,optIndex + 1).find('.divider').length;
|
99
|
menuHeight = liHeight*this.options.size + divLength*divHeight + menuPadding;
|
100
|
menu.css({'max-height' : menuHeight + 'px', 'overflow-y' : 'scroll'});
|
101
|
}
|
102
|
|
103
|
// Listen for updates to the DOM and re render... (Use Mutation Observer when availiable)
|
104
|
if (window.MutationObserver) {
|
105
|
new MutationObserver($.proxy(this.reloadLi, this)).observe(this.$element.get(0), {
|
106
|
childList: true
|
107
|
});
|
108
|
} else {
|
109
|
this.$element.bind('DOMNodeInserted', $.proxy(this.reloadLi, this));
|
110
|
}
|
111
|
|
112
|
this.render();
|
113
|
},
|
114
|
|
115
|
createDropdown: function() {
|
116
|
var drop =
|
117
|
"<div class='btn-group select'>" +
|
118
|
"<button class='btn dropdown-toggle clearfix' data-toggle='dropdown'>" +
|
119
|
"<span class='filter-option pull-left'></span> " +
|
120
|
"<span class='caret'></span>" +
|
121
|
"</button>" +
|
122
|
"<span class='dropdown-arrow'></span>" +
|
123
|
"<ul class='dropdown-menu' role='menu'>" +
|
124
|
"</ul>" +
|
125
|
"</div>";
|
126
|
|
127
|
return $(drop);
|
128
|
},
|
129
|
|
130
|
|
131
|
createView: function() {
|
132
|
var $drop = this.createDropdown();
|
133
|
var $li = this.createLi();
|
134
|
$drop.find('ul').append($li);
|
135
|
return $drop;
|
136
|
},
|
137
|
|
138
|
reloadLi: function() {
|
139
|
//Remove all children.
|
140
|
this.destroyLi();
|
141
|
//Re build
|
142
|
$li = this.createLi();
|
143
|
this.$newElement.find('ul').append( $li );
|
144
|
//render view
|
145
|
this.render();
|
146
|
},
|
147
|
|
148
|
destroyLi:function() {
|
149
|
this.$newElement.find('li').remove();
|
150
|
},
|
151
|
|
152
|
createLi: function() {
|
153
|
|
154
|
var _this = this;
|
155
|
var _li = [];
|
156
|
var _liA = [];
|
157
|
var _liHtml = '';
|
158
|
|
159
|
this.$element.find('option').each(function(){
|
160
|
_li.push($(this).text());
|
161
|
});
|
162
|
|
163
|
this.$element.find('option').each(function(index) {
|
164
|
//Get the class and text for the option
|
165
|
var optionClass = $(this).attr("class") !== undefined ? $(this).attr("class") : '';
|
166
|
var text = $(this).text();
|
167
|
var subtext = $(this).data('subtext') !== undefined ? '<small class="muted">'+$(this).data('subtext')+'</small>' : '';
|
168
|
|
169
|
//Append any subtext to the main text.
|
170
|
text+=subtext;
|
171
|
|
172
|
if ($(this).parent().is('optgroup') && $(this).data('divider') != true) {
|
173
|
if ($(this).index() == 0) {
|
174
|
//Get the opt group label
|
175
|
var label = $(this).parent().attr('label');
|
176
|
var labelSubtext = $(this).parent().data('subtext') !== undefined ? '<small class="muted">'+$(this).parent().data('subtext')+'</small>' : '';
|
177
|
label += labelSubtext;
|
178
|
|
179
|
if ($(this)[0].index != 0) {
|
180
|
_liA.push(
|
181
|
'<div class="divider"></div>'+
|
182
|
'<dt>'+label+'</dt>'+
|
183
|
_this.createA(text, "opt " + optionClass )
|
184
|
);
|
185
|
} else {
|
186
|
_liA.push(
|
187
|
'<dt>'+label+'</dt>'+
|
188
|
_this.createA(text, "opt " + optionClass ));
|
189
|
}
|
190
|
} else {
|
191
|
_liA.push( _this.createA(text, "opt " + optionClass ) );
|
192
|
}
|
193
|
} else if ($(this).data('divider') == true) {
|
194
|
_liA.push('<div class="divider"></div>');
|
195
|
} else if ($(this).data('hidden') == true) {
|
196
|
_liA.push('');
|
197
|
} else {
|
198
|
_liA.push( _this.createA(text, optionClass ) );
|
199
|
}
|
200
|
});
|
201
|
|
202
|
if (_li.length > 0) {
|
203
|
for (var i = 0; i < _li.length; i++) {
|
204
|
var $option = this.$element.find('option').eq(i);
|
205
|
_liHtml += "<li rel=" + i + ">" + _liA[i] + "</li>";
|
206
|
}
|
207
|
}
|
208
|
|
209
|
//If we dont have a selected item, and we dont have a title, select the first element so something is set in the button
|
210
|
if(this.$element.find('option:selected').length==0 && !_this.options.title) {
|
211
|
this.$element.find('option').eq(0).prop('selected', true).attr('selected', 'selected');
|
212
|
}
|
213
|
|
214
|
return $(_liHtml);
|
215
|
},
|
216
|
|
217
|
createA:function(test, classes) {
|
218
|
return '<a tabindex="-1" href="#" class="'+classes+'">' +
|
219
|
'<span class="pull-left">' + test + '</span>' +
|
220
|
'</a>';
|
221
|
|
222
|
},
|
223
|
|
224
|
render:function() {
|
225
|
var _this = this;
|
226
|
|
227
|
//Set width of select
|
228
|
if (this.options.width == 'auto') {
|
229
|
var ulWidth = this.$newElement.find('.dropdown-menu').css('width');
|
230
|
this.$newElement.css('width',ulWidth);
|
231
|
} else if (this.options.width && this.options.width != 'auto') {
|
232
|
this.$newElement.css('width',this.options.width);
|
233
|
}
|
234
|
|
235
|
//Update the LI to match the SELECT
|
236
|
this.$element.find('option').each(function(index) {
|
237
|
_this.setDisabled(index, $(this).is(':disabled') || $(this).parent().is(':disabled') );
|
238
|
_this.setSelected(index, $(this).is(':selected') );
|
239
|
});
|
240
|
|
241
|
|
242
|
|
243
|
var selectedItems = this.$element.find('option:selected').map(function(index,value) {
|
244
|
if($(this).attr('title')!=undefined) {
|
245
|
return $(this).attr('title');
|
246
|
} else {
|
247
|
return $(this).text();
|
248
|
}
|
249
|
}).toArray();
|
250
|
|
251
|
//Convert all the values into a comma delimited string
|
252
|
var title = selectedItems.join(", ");
|
253
|
|
254
|
//If this is multi select, and the selectText type is count, the show 1 of 2 selected etc..
|
255
|
if(_this.multiple && _this.options.selectedTextFormat.indexOf('count') > -1) {
|
256
|
var max = _this.options.selectedTextFormat.split(">");
|
257
|
if( (max.length>1 && selectedItems.length > max[1]) || (max.length==1 && selectedItems.length>=2)) {
|
258
|
title = selectedItems.length +' of ' + this.$element.find('option').length + ' selected';
|
259
|
}
|
260
|
}
|
261
|
|
262
|
//If we dont have a title, then use the default, or if nothing is set at all, use the not selected text
|
263
|
if(!title) {
|
264
|
title = _this.options.title != undefined ? _this.options.title : _this.options.noneSelectedText;
|
265
|
}
|
266
|
|
267
|
this.$element.next('.select').find('.filter-option').html( title );
|
268
|
},
|
269
|
|
270
|
|
271
|
|
272
|
setSelected:function(index, selected) {
|
273
|
if(selected) {
|
274
|
this.$newElement.find('li').eq(index).addClass('selected');
|
275
|
} else {
|
276
|
this.$newElement.find('li').eq(index).removeClass('selected');
|
277
|
}
|
278
|
},
|
279
|
|
280
|
setDisabled:function(index, disabled) {
|
281
|
if(disabled) {
|
282
|
this.$newElement.find('li').eq(index).addClass('disabled');
|
283
|
} else {
|
284
|
this.$newElement.find('li').eq(index).removeClass('disabled');
|
285
|
}
|
286
|
},
|
287
|
|
288
|
checkDisabled: function() {
|
289
|
if (this.$element.is(':disabled')) {
|
290
|
this.button.addClass('disabled');
|
291
|
this.button.click(function(e) {
|
292
|
e.preventDefault();
|
293
|
});
|
294
|
}
|
295
|
},
|
296
|
|
297
|
checkTabIndex: function() {
|
298
|
if (this.$element.is('[tabindex]')) {
|
299
|
var tabindex = this.$element.attr("tabindex");
|
300
|
this.button.attr('tabindex', tabindex);
|
301
|
}
|
302
|
},
|
303
|
|
304
|
clickListener: function() {
|
305
|
var _this = this;
|
306
|
|
307
|
$('body').on('touchstart.dropdown', '.dropdown-menu', function (e) { e.stopPropagation(); });
|
308
|
|
309
|
|
310
|
|
311
|
this.$newElement.on('click', 'li a', function(e){
|
312
|
var clickedIndex = $(this).parent().index(),
|
313
|
$this = $(this).parent(),
|
314
|
$select = $this.parents('.select');
|
315
|
|
316
|
|
317
|
//Dont close on multi choice menu
|
318
|
if(_this.multiple) {
|
319
|
e.stopPropagation();
|
320
|
}
|
321
|
|
322
|
e.preventDefault();
|
323
|
|
324
|
//Dont run if we have been disabled
|
325
|
if ($select.prev('select').not(':disabled') && !$(this).parent().hasClass('disabled')){
|
326
|
//Deselect all others if not multi select box
|
327
|
if (!_this.multiple) {
|
328
|
$select.prev('select').find('option').removeAttr('selected');
|
329
|
$select.prev('select').find('option').eq(clickedIndex).prop('selected', true).attr('selected', 'selected');
|
330
|
}
|
331
|
//Else toggle the one we have chosen if we are multi selet.
|
332
|
else {
|
333
|
var selected = $select.prev('select').find('option').eq(clickedIndex).prop('selected');
|
334
|
|
335
|
if(selected) {
|
336
|
$select.prev('select').find('option').eq(clickedIndex).removeAttr('selected');
|
337
|
} else {
|
338
|
$select.prev('select').find('option').eq(clickedIndex).prop('selected', true).attr('selected', 'selected');
|
339
|
}
|
340
|
}
|
341
|
|
342
|
|
343
|
$select.find('.filter-option').html($this.text());
|
344
|
$select.find('button').focus();
|
345
|
|
346
|
// Trigger select 'change'
|
347
|
$select.prev('select').trigger('change');
|
348
|
}
|
349
|
|
350
|
});
|
351
|
|
352
|
this.$newElement.on('click', 'li.disabled a, li dt, li .divider', function(e) {
|
353
|
e.preventDefault();
|
354
|
e.stopPropagation();
|
355
|
$select = $(this).parent().parents('.select');
|
356
|
$select.find('button').focus();
|
357
|
});
|
358
|
|
359
|
this.$element.on('change', function(e) {
|
360
|
_this.render();
|
361
|
});
|
362
|
},
|
363
|
|
364
|
val:function(value) {
|
365
|
|
366
|
if(value!=undefined) {
|
367
|
this.$element.val( value );
|
368
|
|
369
|
this.$element.trigger('change');
|
370
|
return this.$element;
|
371
|
} else {
|
372
|
return this.$element.val();
|
373
|
}
|
374
|
}
|
375
|
|
376
|
};
|
377
|
|
378
|
$.fn.selectpicker = function(option, event) {
|
379
|
//get the args of the outer function..
|
380
|
var args = arguments;
|
381
|
var value;
|
382
|
var chain = this.each(function () {
|
383
|
var $this = $(this),
|
384
|
data = $this.data('selectpicker'),
|
385
|
options = typeof option == 'object' && option;
|
386
|
|
387
|
if (!data) {
|
388
|
$this.data('selectpicker', (data = new Selectpicker(this, options, event)));
|
389
|
} else {
|
390
|
for(var i in option) {
|
391
|
data[i]=option[i];
|
392
|
}
|
393
|
}
|
394
|
|
395
|
if (typeof option == 'string') {
|
396
|
//Copy the value of option, as once we shift the arguments
|
397
|
//it also shifts the value of option.
|
398
|
property = option;
|
399
|
if(data[property] instanceof Function) {
|
400
|
[].shift.apply(args);
|
401
|
value = data[property].apply(data, args);
|
402
|
} else {
|
403
|
value = data[property];
|
404
|
}
|
405
|
}
|
406
|
});
|
407
|
|
408
|
if(value!=undefined) {
|
409
|
return value;
|
410
|
} else {
|
411
|
return chain;
|
412
|
}
|
413
|
};
|
414
|
|
415
|
$.fn.selectpicker.defaults = {
|
416
|
style: null,
|
417
|
size: 'auto',
|
418
|
title: null,
|
419
|
selectedTextFormat : 'values',
|
420
|
noneSelectedText : 'Nothing selected',
|
421
|
width: null,
|
422
|
menuStyle: null,
|
423
|
toggleSize: null
|
424
|
}
|
425
|
|
426
|
}(window.jQuery);
|