5 var Selectpicker = function(element, options, e) {
12 this.$element = $(element);
13 this.$newElement = null;
16 //Merge defaults, options and data-attributes to make our options
17 this.options = $.extend({}, $.fn.selectpicker.defaults, this.$element.data(), typeof options == 'object' && options);
19 //If we have no title yet, check the attribute 'title' (this is missed by jq as its not a data-attribute
20 if(this.options.title==null)
21 this.options.title = this.$element.attr('title');
23 //Expose public methods
24 this.val = Selectpicker.prototype.val;
25 this.render = Selectpicker.prototype.render;
26 this.refresh = Selectpicker.prototype.refresh;
27 this.selectAll = Selectpicker.prototype.selectAll;
28 this.deselectAll = Selectpicker.prototype.deselectAll;
32 Selectpicker.prototype = {
34 constructor: Selectpicker,
37 if (!this.options.container) {
40 this.$element.css('visibility','hidden');
42 this.multiple = this.$element.prop('multiple');
43 var classList = this.$element.attr('class') !== undefined ? this.$element.attr('class').split(/\s+/) : '';
44 var id = this.$element.attr('id');
45 this.$element.after( this.createView() );
46 this.$newElement = this.$element.next('.bootstrap-select');
47 if (this.options.container) {
48 this.selectPosition();
50 this.button = this.$newElement.find('> button');
51 if (id !== undefined) {
53 this.button.attr('data-id', id);
54 $('label[for="' + id + '"]').click(function(){
55 _this.$newElement.find('button[data-id='+id+']').focus();
58 for (var i = 0; i < classList.length; i++) {
59 if(classList[i] != 'selectpicker') {
60 this.$newElement.addClass(classList[i]);
63 //If we are multiple, then add the show-tick class by default
65 this.$newElement.addClass('show-tick');
67 this.button.addClass(this.options.style);
76 createDropdown: function() {
78 "<div class='btn-group bootstrap-select'>" +
79 "<button type='button' class='btn dropdown-toggle' data-toggle='dropdown'>" +
80 "<div class='filter-option pull-left'></div> " +
81 "<div class='caret'></div>" +
83 "<ul class='dropdown-menu' role='menu'>" +
91 createView: function() {
92 var $drop = this.createDropdown();
93 var $li = this.createLi();
94 $drop.find('ul').append($li);
98 reloadLi: function() {
99 //Remove all children.
102 var $li = this.createLi();
103 this.$newElement.find('ul').append( $li );
106 destroyLi:function() {
107 this.$newElement.find('li').remove();
110 createLi: function() {
117 this.$element.find('option').each(function(){
118 _li.push($(this).text());
121 this.$element.find('option').each(function(index) {
124 //Get the class and text for the option
125 var optionClass = $this.attr("class") !== undefined ? $this.attr("class") : '';
126 var text = $this.text();
127 var subtext = $this.data('subtext') !== undefined ? '<small class="muted">'+$this.data('subtext')+'</small>' : '';
128 var icon = $this.data('icon') !== undefined ? '<i class="'+$this.data('icon')+'"></i> ' : '';
129 if ($this.is(':disabled') || $this.parent().is(':disabled')) {
130 icon = '<span>'+icon+'</span>';
133 //Prepend any icon and append any subtext to the main text.
134 text = icon + '<span class="text">' + text + subtext + '</span>';
136 if (_this.options.hideDisabled && ($this.is(':disabled') || $this.parent().is(':disabled'))) {
137 _liA.push('<a style="min-height: 0; padding: 0"></a>');
138 } else if ($this.parent().is('optgroup') && $this.data('divider') != true) {
139 if ($this.index() == 0) {
140 //Get the opt group label
141 var label = $this.parent().attr('label');
142 var labelSubtext = $this.parent().data('subtext') !== undefined ? '<small class="muted">'+$this.parent().data('subtext')+'</small>' : '';
143 var labelIcon = $this.parent().data('icon') ? '<i class="'+$this.parent().data('icon')+'"></i> ' : '';
144 label = labelIcon + '<span class="text">' + label + labelSubtext + '</span>';
146 if ($this[0].index != 0) {
148 '<div class="div-contain"><div class="divider"></div></div>'+
149 '<dt>'+label+'</dt>'+
150 _this.createA(text, "opt " + optionClass )
154 '<dt>'+label+'</dt>'+
155 _this.createA(text, "opt " + optionClass ));
158 _liA.push( _this.createA(text, "opt " + optionClass ) );
160 } else if ($this.data('divider') == true) {
161 _liA.push('<div class="div-contain"><div class="divider"></div></div>');
162 } else if ($(this).data('hidden') == true) {
165 _liA.push( _this.createA(text, optionClass ) );
169 if (_li.length > 0) {
170 for (var i = 0; i < _li.length; i++) {
171 var $option = this.$element.find('option').eq(i);
172 _liHtml += "<li rel=" + i + ">" + _liA[i] + "</li>";
176 //If we are not multiple, and we dont have a selected item, and we dont have a title, select the first element so something is set in the button
177 if(!this.multiple && this.$element.find('option:selected').length==0 && !_this.options.title) {
178 this.$element.find('option').eq(0).prop('selected', true).attr('selected', 'selected');
184 createA:function(text, classes) {
185 return '<a tabindex="0" class="'+classes+'">' +
187 '<i class="icon-ok check-mark"></i>' +
194 //Update the LI to match the SELECT
195 this.$element.find('option').each(function(index) {
196 _this.setDisabled(index, $(this).is(':disabled') || $(this).parent().is(':disabled') );
197 _this.setSelected(index, $(this).is(':selected') );
200 var selectedItems = this.$element.find('option:selected').map(function(index,value) {
202 if (_this.options.showSubtext && $(this).attr('data-subtext') && !_this.multiple) {
203 subtext = ' <small class="muted">'+$(this).data('subtext') +'</small>';
207 if($(this).attr('title')!=undefined) {
208 return $(this).attr('title');
210 return $(this).text() + subtext;
214 //Fixes issue in IE10 occurring when no default option is selected and at least one option is disabled
215 //Convert all the values into a comma delimited string
216 var title = !this.multiple ? selectedItems[0] : selectedItems.join(", "),
217 separator = _this.options.separatorText || _this.options.defaultSeparatorText,
218 selected = _this.options.selectedText || _this.options.defaultSelectedText;
220 //If this is multi select, and the selectText type is count, the show 1 of 2 selected etc..
221 if(_this.multiple && _this.options.selectedTextFormat.indexOf('count') > -1) {
222 var max = _this.options.selectedTextFormat.split(">");
223 var notDisabled = this.options.hideDisabled ? ':not([disabled])' : '';
224 if( (max.length>1 && selectedItems.length > max[1]) || (max.length==1 && selectedItems.length>=2)) {
225 title = selectedItems.length +' ' + separator +' ' + this.$element.find('option:not([data-divider="true"]):not([data-hidden="true"])'+notDisabled).length + ' ' + selected;
229 //If we dont have a title, then use the default, or if nothing is set at all, use the not selected text
231 title = _this.options.title != undefined ? _this.options.title : _this.options.noneSelectedText;
235 if (this.options.showSubtext && this.$element.find('option:selected').attr('data-subtext')) {
236 subtext = ' <small class="muted">'+this.$element.find('option:selected').data('subtext') +'</small>';
241 var icon = this.$element.find('option:selected').data('icon') || '';
243 icon = '<i class="' + icon + '"></i> ';
246 _this.$newElement.find('.filter-option').html(icon + title + subtext);
250 if(this.options.container) {
251 // Show $newElement before perfoming size calculations
252 this.$newElement.toggle(this.$element.parent().is(':visible'));
255 var menu = this.$newElement.find('.dropdown-menu');
256 var menuA = menu.find('li > a');
257 var liHeight = this.$newElement.addClass('open').find('.dropdown-menu li > a').outerHeight();
258 this.$newElement.removeClass('open');
259 var divHeight = menu.find('li .divider').outerHeight(true);
260 var selectOffset_top = this.$newElement.offset().top;
261 var selectHeight = this.$newElement.outerHeight();
262 var menuPadding = parseInt(menu.css('padding-top')) + parseInt(menu.css('padding-bottom')) + parseInt(menu.css('border-top-width')) + parseInt(menu.css('border-bottom-width'));
263 var notDisabled = this.options.hideDisabled ? ':not(.disabled)' : '';
265 if (this.options.size == 'auto') {
266 var getSize = function() {
267 var selectOffset_top_scroll = selectOffset_top - $(window).scrollTop();
268 var windowHeight = window.innerHeight;
269 var menuExtras = menuPadding + parseInt(menu.css('margin-top')) + parseInt(menu.css('margin-bottom')) + 2;
270 var selectOffset_bot = windowHeight - selectOffset_top_scroll - selectHeight - menuExtras;
272 menuHeight = selectOffset_bot;
273 if (_this.$newElement.hasClass('dropup')) {
274 menuHeight = selectOffset_top_scroll - menuExtras;
276 if ((menu.find('li').length + menu.find('dt').length) > 3) {
277 minHeight = liHeight*3 + menuExtras - 2;
281 menu.css({'max-height' : menuHeight + 'px', 'overflow-y' : 'auto', 'min-height' : minHeight + 'px'});
284 $(window).resize(getSize);
285 $(window).scroll(getSize);
286 } else if (this.options.size && this.options.size != 'auto' && menu.find('li'+notDisabled).length > this.options.size) {
287 var optIndex = menu.find("li"+notDisabled+" > *").filter(':not(.div-contain)').slice(0,this.options.size).last().parent().index();
288 var divLength = menu.find("li").slice(0,optIndex + 1).find('.div-contain').length;
289 menuHeight = liHeight*this.options.size + divLength*divHeight + menuPadding;
290 menu.css({'max-height' : menuHeight + 'px', 'overflow-y' : 'auto'});
293 //Set width of select
294 if (this.options.width == 'auto') {
295 this.$newElement.find('.dropdown-menu').css('min-width','0');
296 var ulWidth = this.$newElement.find('.dropdown-menu').css('width');
297 this.$newElement.css('width',ulWidth);
298 if (this.options.container) {
299 this.$element.css('width',ulWidth);
301 } else if (this.options.width) {
302 if (this.options.container) {
303 // Note: options.width can be %
304 this.$element.css('width', this.options.width);
305 // Set pixel width of $newElement based on $element's pixel width
306 this.$newElement.width(this.$element.outerWidth());
308 this.$newElement.css('width',this.options.width);
310 } else if(this.options.container) {
311 // Set width of $newElement based on $element
312 this.$newElement.width(this.$element.outerWidth());
316 selectPosition:function() {
317 var containerOffset = $(this.options.container).offset();
318 var eltOffset = this.$element.offset();
319 if(containerOffset && eltOffset) {
320 var selectElementTop = eltOffset.top - containerOffset.top;
321 var selectElementLeft = eltOffset.left - containerOffset.left;
322 this.$newElement.appendTo(this.options.container);
323 this.$newElement.css({'position':'absolute', 'top':selectElementTop+'px', 'left':selectElementLeft+'px'});
331 this.checkDisabled();
332 if (this.options.container) {
333 this.selectPosition();
337 setSelected:function(index, selected) {
339 this.$newElement.find('li').eq(index).addClass('selected');
341 this.$newElement.find('li').eq(index).removeClass('selected');
345 setDisabled:function(index, disabled) {
347 this.$newElement.find('li').eq(index).addClass('disabled').find('a').attr('href','#').attr('tabindex',-1);
349 this.$newElement.find('li').eq(index).removeClass('disabled').find('a').removeAttr('href').attr('tabindex',0);
353 isDisabled: function() {
354 return this.$element.is(':disabled') || this.$element.attr('readonly');
357 checkDisabled: function() {
358 if (this.isDisabled()) {
359 this.button.addClass('disabled');
360 this.button.click(function(e) {
363 this.button.attr('tabindex','-1');
364 } else if (!this.isDisabled() && this.button.hasClass('disabled')) {
365 this.button.removeClass('disabled');
366 this.button.click(function() {
369 this.button.removeAttr('tabindex');
373 checkTabIndex: function() {
374 if (this.$element.is('[tabindex]')) {
375 var tabindex = this.$element.attr("tabindex");
376 this.button.attr('tabindex', tabindex);
380 clickListener: function() {
383 $('body').on('touchstart.dropdown', '.dropdown-menu', function (e) { e.stopPropagation(); });
385 this.$newElement.on('click', 'li a', function(e){
386 var clickedIndex = $(this).parent().index(),
387 $this = $(this).parent(),
388 $select = $this.parents('.bootstrap-select'),
389 prevValue = _this.$element.val();
391 //Dont close on multi choice menu
398 //Dont run if we have been disabled
399 if (_this.$element.not(':disabled') && !$(this).parent().hasClass('disabled')){
400 //Deselect all others if not multi select box
401 if (!_this.multiple) {
402 _this.$element.find('option').prop('selected', false);
403 _this.$element.find('option').eq(clickedIndex).prop('selected', true);
405 //Else toggle the one we have chosen if we are multi select.
407 var selected = _this.$element.find('option').eq(clickedIndex).prop('selected');
410 _this.$element.find('option').eq(clickedIndex).prop('selected', false);
412 _this.$element.find('option').eq(clickedIndex).prop('selected', true);
416 $select.find('button').focus();
418 // Trigger select 'change'
419 if (prevValue != _this.$element.val()) {
420 _this.$element.trigger('change');
428 this.$newElement.on('click', 'li.disabled a, li dt, li .div-contain', function(e) {
431 var $select = $(this).parent().parents('.bootstrap-select');
432 $select.find('button').focus();
435 this.$element.on('change', function(e) {
440 val:function(value) {
442 if(value!=undefined) {
443 this.$element.val( value );
445 this.$element.trigger('change');
446 return this.$element;
448 return this.$element.val();
452 selectAll:function() {
453 this.$element.find('option').prop('selected', true).attr('selected', 'selected');
457 deselectAll:function() {
458 this.$element.find('option').prop('selected', false).removeAttr('selected');
462 keydown: function (e) {
475 $parent = $this.parent();
477 $items = $('[role=menu] li:not(.divider):visible a', $parent);
479 if (!$items.length) return;
481 if (/(38|40)/.test(e.keyCode)) {
483 index = $items.index($items.filter(':focus'));
485 first = $items.parent(':not(.disabled)').first().index();
486 last = $items.parent(':not(.disabled)').last().index();
487 next = $items.eq(index).parent().nextAll(':not(.disabled)').eq(0).index();
488 prev = $items.eq(index).parent().prevAll(':not(.disabled)').eq(0).index();
489 nextPrev = $items.eq(next).parent().prevAll(':not(.disabled)').eq(0).index();
491 if (e.keyCode == 38) {
492 if (index != nextPrev && index > prev) index = prev;
493 if (index < first) index = first;
496 if (e.keyCode == 40) {
497 if (index != nextPrev && index < next) index = next;
498 if (index > last) index = last;
501 $items.eq(index).focus()
504 48:"0", 49:"1", 50:"2", 51:"3", 52:"4", 53:"5", 54:"6", 55:"7", 56:"8", 57:"9", 59:";",
505 65:"a", 66:"b", 67:"c", 68:"d", 69:"e", 70:"f", 71:"g", 72:"h", 73:"i", 74:"j", 75:"k", 76:"l",
506 77:"m", 78:"n", 79:"o", 80:"p", 81:"q", 82:"r", 83:"s", 84:"t", 85:"u", 86:"v", 87:"w", 88:"x", 89:"y", 90:"z",
507 96:"0", 97:"1", 98:"2", 99:"3", 100:"4", 101:"5", 102:"6", 103:"7", 104:"8", 105:"9"
512 $items.each(function() {
513 if ($(this).parent().is(':not(.disabled)')) {
514 if ($.trim($(this).text().toLowerCase()).substring(0,1) == keyCodeMap[e.keyCode]) {
515 keyIndex.push($(this).parent().index());
520 var count = $(document).data('keycount');
522 $(document).data('keycount',count);
524 var prevKey = $.trim($(':focus').text().toLowerCase()).substring(0,1);
526 if (prevKey != keyCodeMap[e.keyCode]) {
528 $(document).data('keycount',count);
529 } else if (count >= keyIndex.length) {
530 $(document).data('keycount',0);
533 $items.eq(keyIndex[count - 1]).focus();
536 if (/(13)/.test(e.keyCode)) {
538 $parent.addClass('open');
539 $(document).data('keycount',0);
544 $.fn.selectpicker = function(option, event) {
545 //get the args of the outer function..
546 var args = arguments;
548 var chain = this.each(function () {
549 if ($(this).is('select')) {
551 data = $this.data('selectpicker'),
552 options = typeof option == 'object' && option;
555 $this.data('selectpicker', (data = new Selectpicker(this, options, event)));
557 for(var i in options) {
558 data.options[i]=options[i];
562 if (typeof option == 'string') {
563 //Copy the value of option, as once we shift the arguments
564 //it also shifts the value of option.
565 var property = option;
566 if(data[property] instanceof Function) {
567 [].shift.apply(args);
568 value = data[property].apply(data, args);
570 value = data.options[property];
576 if(value!=undefined) {
583 $.fn.selectpicker.defaults = {
587 selectedTextFormat : 'values',
588 noneSelectedText : 'Nothing selected',
589 defaultSelectedText: 'selected',
590 defaultSeparatorText: 'of',
599 .on('keydown', '[data-toggle=dropdown], [role=menu]' , Selectpicker.prototype.keydown)