2 * Bootstrap-select v1.7.7 (http://silviomoreto.github.io/bootstrap-select)
\r
4 * Copyright 2013-2015 bootstrap-select
\r
5 * Licensed under MIT (https://github.com/silviomoreto/bootstrap-select/blob/master/LICENSE)
\r
8 (function (root, factory) {
9 if (typeof define === 'function' && define.amd) {
10 // AMD. Register as an anonymous module unless amdModuleId is set
11 define(["jquery"], function (a0) {
14 } else if (typeof exports === 'object') {
15 // Node. Does not work with strict CommonJS, but
16 // only CommonJS-like environments that support module.exports,
18 module.exports = factory(require("jquery"));
22 }(this, function (jQuery) {
27 //<editor-fold desc="Shims">
\r
28 if (!String.prototype.includes) {
\r
30 'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
\r
31 var toString = {}.toString;
\r
32 var defineProperty = (function () {
\r
33 // IE 8 only supports `Object.defineProperty` on DOM elements
\r
36 var $defineProperty = Object.defineProperty;
\r
37 var result = $defineProperty(object, object, object) && $defineProperty;
\r
42 var indexOf = ''.indexOf;
\r
43 var includes = function (search) {
\r
45 throw new TypeError();
\r
47 var string = String(this);
\r
48 if (search && toString.call(search) == '[object RegExp]') {
\r
49 throw new TypeError();
\r
51 var stringLength = string.length;
\r
52 var searchString = String(search);
\r
53 var searchLength = searchString.length;
\r
54 var position = arguments.length > 1 ? arguments[1] : undefined;
\r
56 var pos = position ? Number(position) : 0;
\r
57 if (pos != pos) { // better `isNaN`
\r
60 var start = Math.min(Math.max(pos, 0), stringLength);
\r
61 // Avoid the `indexOf` call if no match is possible
\r
62 if (searchLength + start > stringLength) {
\r
65 return indexOf.call(string, searchString, pos) != -1;
\r
67 if (defineProperty) {
\r
68 defineProperty(String.prototype, 'includes', {
\r
70 'configurable': true,
\r
74 String.prototype.includes = includes;
\r
79 if (!String.prototype.startsWith) {
\r
81 'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
\r
82 var defineProperty = (function () {
\r
83 // IE 8 only supports `Object.defineProperty` on DOM elements
\r
86 var $defineProperty = Object.defineProperty;
\r
87 var result = $defineProperty(object, object, object) && $defineProperty;
\r
92 var toString = {}.toString;
\r
93 var startsWith = function (search) {
\r
95 throw new TypeError();
\r
97 var string = String(this);
\r
98 if (search && toString.call(search) == '[object RegExp]') {
\r
99 throw new TypeError();
\r
101 var stringLength = string.length;
\r
102 var searchString = String(search);
\r
103 var searchLength = searchString.length;
\r
104 var position = arguments.length > 1 ? arguments[1] : undefined;
\r
106 var pos = position ? Number(position) : 0;
\r
107 if (pos != pos) { // better `isNaN`
\r
110 var start = Math.min(Math.max(pos, 0), stringLength);
\r
111 // Avoid the `indexOf` call if no match is possible
\r
112 if (searchLength + start > stringLength) {
\r
116 while (++index < searchLength) {
\r
117 if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
\r
123 if (defineProperty) {
\r
124 defineProperty(String.prototype, 'startsWith', {
\r
125 'value': startsWith,
\r
126 'configurable': true,
\r
130 String.prototype.startsWith = startsWith;
\r
135 if (!Object.keys) {
\r
136 Object.keys = function (
\r
141 // initialize object and result
\r
143 // iterate over object keys
\r
145 // fill result array with non-prototypical keys
\r
146 r.hasOwnProperty.call(o, k) && r.push(k);
\r
152 $.fn.triggerNative = function (eventName) {
\r
156 if (el.dispatchEvent) {
\r
157 if (typeof Event === 'function') {
\r
158 // For modern browsers
\r
159 event = new Event(eventName, {
\r
163 // For IE since it doesn't support Event constructor
\r
164 event = document.createEvent('Event');
\r
165 event.initEvent(eventName, true, false);
\r
168 el.dispatchEvent(event);
\r
170 if (el.fireEvent) {
\r
171 event = document.createEventObject();
\r
172 event.eventType = eventName;
\r
173 el.fireEvent('on' + eventName, event);
\r
176 this.trigger(eventName);
\r
181 // Case insensitive contains search
\r
182 $.expr[':'].icontains = function (obj, index, meta) {
\r
184 var haystack = ($obj.data('tokens') || $obj.text()).toUpperCase();
\r
185 return haystack.includes(meta[3].toUpperCase());
\r
188 // Case insensitive begins search
\r
189 $.expr[':'].ibegins = function (obj, index, meta) {
\r
191 var haystack = ($obj.data('tokens') || $obj.text()).toUpperCase();
\r
192 return haystack.startsWith(meta[3].toUpperCase());
\r
195 // Case and accent insensitive contains search
\r
196 $.expr[':'].aicontains = function (obj, index, meta) {
\r
198 var haystack = ($obj.data('tokens') || $obj.data('normalizedText') || $obj.text()).toUpperCase();
\r
199 return haystack.includes(meta[3].toUpperCase());
\r
202 // Case and accent insensitive begins search
\r
203 $.expr[':'].aibegins = function (obj, index, meta) {
\r
205 var haystack = ($obj.data('tokens') || $obj.data('normalizedText') || $obj.text()).toUpperCase();
\r
206 return haystack.startsWith(meta[3].toUpperCase());
\r
210 * Remove all diatrics from the given text.
\r
212 * @param {String} text
\r
213 * @returns {String}
\r
215 function normalizeToBase(text) {
\r
217 {re: /[\xC0-\xC6]/g, ch: "A"},
\r
218 {re: /[\xE0-\xE6]/g, ch: "a"},
\r
219 {re: /[\xC8-\xCB]/g, ch: "E"},
\r
220 {re: /[\xE8-\xEB]/g, ch: "e"},
\r
221 {re: /[\xCC-\xCF]/g, ch: "I"},
\r
222 {re: /[\xEC-\xEF]/g, ch: "i"},
\r
223 {re: /[\xD2-\xD6]/g, ch: "O"},
\r
224 {re: /[\xF2-\xF6]/g, ch: "o"},
\r
225 {re: /[\xD9-\xDC]/g, ch: "U"},
\r
226 {re: /[\xF9-\xFC]/g, ch: "u"},
\r
227 {re: /[\xC7-\xE7]/g, ch: "c"},
\r
228 {re: /[\xD1]/g, ch: "N"},
\r
229 {re: /[\xF1]/g, ch: "n"}
\r
231 $.each(rExps, function () {
\r
232 text = text.replace(this.re, this.ch);
\r
238 function htmlEscape(html) {
\r
247 var source = '(?:' + Object.keys(escapeMap).join('|') + ')',
\r
248 testRegexp = new RegExp(source),
\r
249 replaceRegexp = new RegExp(source, 'g'),
\r
250 string = html == null ? '' : '' + html;
\r
251 return testRegexp.test(string) ? string.replace(replaceRegexp, function (match) {
\r
252 return escapeMap[match];
\r
256 var Selectpicker = function (element, options, e) {
\r
258 e.stopPropagation();
\r
259 e.preventDefault();
\r
262 this.$element = $(element);
\r
263 this.$newElement = null;
\r
264 this.$button = null;
\r
267 this.options = options;
\r
269 // If we have no title yet, try to pull it from the html title attribute (jQuery doesnt' pick it up as it's not a
\r
271 if (this.options.title === null) {
\r
272 this.options.title = this.$element.attr('title');
\r
275 //Expose public methods
\r
276 this.val = Selectpicker.prototype.val;
\r
277 this.render = Selectpicker.prototype.render;
\r
278 this.refresh = Selectpicker.prototype.refresh;
\r
279 this.setStyle = Selectpicker.prototype.setStyle;
\r
280 this.selectAll = Selectpicker.prototype.selectAll;
\r
281 this.deselectAll = Selectpicker.prototype.deselectAll;
\r
282 this.destroy = Selectpicker.prototype.destroy;
\r
283 this.remove = Selectpicker.prototype.remove;
\r
284 this.show = Selectpicker.prototype.show;
\r
285 this.hide = Selectpicker.prototype.hide;
\r
290 Selectpicker.VERSION = '1.7.7';
\r
292 // part of this is duplicated in i18n/defaults-en_US.js. Make sure to update both.
\r
293 Selectpicker.DEFAULTS = {
\r
294 noneSelectedText: 'Nothing selected',
\r
295 noneResultsText: 'No results matched {0}',
\r
296 countSelectedText: function (numSelected, numTotal) {
\r
297 return (numSelected == 1) ? "{0} item selected" : "{0} items selected";
\r
299 maxOptionsText: function (numAll, numGroup) {
\r
301 (numAll == 1) ? 'Limit reached ({n} item max)' : 'Limit reached ({n} items max)',
\r
302 (numGroup == 1) ? 'Group limit reached ({n} item max)' : 'Group limit reached ({n} items max)'
\r
305 selectAllText: 'Select All',
\r
306 deselectAllText: 'Deselect All',
\r
308 doneButtonText: 'Close',
\r
309 multipleSeparator: ', ',
\r
311 style: 'btn-default',
\r
314 selectedTextFormat: 'values',
\r
317 hideDisabled: false,
\r
318 showSubtext: false,
\r
324 liveSearchPlaceholder: null,
\r
325 liveSearchNormalize: false,
\r
326 liveSearchStyle: 'contains',
\r
328 iconBase: 'glyphicon',
\r
329 tickIcon: 'glyphicon-ok',
\r
331 caret: '<span class="caret"></span>'
\r
335 selectOnTab: false,
\r
336 dropdownAlignRight: false
\r
339 Selectpicker.prototype = {
\r
341 constructor: Selectpicker,
\r
343 init: function () {
\r
345 id = this.$element.attr('id');
\r
347 this.$element.addClass('bs-select-hidden');
\r
348 // store originalIndex (key) and newIndex (value) in this.liObj for fast accessibility
\r
349 // allows us to do this.$lis.eq(that.liObj[index]) instead of this.$lis.filter('[data-original-index="' + index + '"]')
\r
351 this.multiple = this.$element.prop('multiple');
\r
352 this.autofocus = this.$element.prop('autofocus');
\r
353 this.$newElement = this.createView();
\r
354 this.$element.after(this.$newElement);
\r
355 this.$button = this.$newElement.children('button');
\r
356 this.$menu = this.$newElement.children('.dropdown-menu');
\r
357 this.$menuInner = this.$menu.children('.inner');
\r
358 this.$searchbox = this.$menu.find('input');
\r
360 if (this.options.dropdownAlignRight)
\r
361 this.$menu.addClass('dropdown-menu-right');
\r
363 if (typeof id !== 'undefined') {
\r
364 this.$button.attr('data-id', id);
\r
365 $('label[for="' + id + '"]').click(function (e) {
\r
366 e.preventDefault();
\r
367 that.$button.focus();
\r
371 this.checkDisabled();
\r
372 this.clickListener();
\r
373 if (this.options.liveSearch) this.liveSearchListener();
\r
377 if (this.options.container) this.selectPosition();
\r
378 this.$menu.data('this', this);
\r
379 this.$newElement.data('this', this);
\r
380 if (this.options.mobile) this.mobile();
\r
382 this.$newElement.on({
\r
383 'hide.bs.dropdown': function (e) {
\r
384 that.$element.trigger('hide.bs.select', e);
\r
386 'hidden.bs.dropdown': function (e) {
\r
387 that.$element.trigger('hidden.bs.select', e);
\r
389 'show.bs.dropdown': function (e) {
\r
390 that.$element.trigger('show.bs.select', e);
\r
392 'shown.bs.dropdown': function (e) {
\r
393 that.$element.trigger('shown.bs.select', e);
\r
397 setTimeout(function () {
\r
398 that.$element.trigger('loaded.bs.select');
\r
402 createDropdown: function () {
\r
404 // If we are multiple, then add the show-tick class by default
\r
405 var multiple = this.multiple ? ' show-tick' : '',
\r
406 inputGroup = this.$element.parent().hasClass('input-group') ? ' input-group-btn' : '',
\r
407 autofocus = this.autofocus ? ' autofocus' : '';
\r
409 var header = this.options.header ? '<div class="popover-title"><button type="button" class="close" aria-hidden="true">×</button>' + this.options.header + '</div>' : '';
\r
410 var searchbox = this.options.liveSearch ?
\r
411 '<div class="bs-searchbox">' +
\r
412 '<input type="text" class="form-control" autocomplete="off"' +
\r
413 (null === this.options.liveSearchPlaceholder ? '' : ' placeholder="' + htmlEscape(this.options.liveSearchPlaceholder) + '"') + '>' +
\r
416 var actionsbox = this.multiple && this.options.actionsBox ?
\r
417 '<div class="bs-actionsbox">' +
\r
418 '<div class="btn-group btn-group-sm btn-block">' +
\r
419 '<button type="button" class="actions-btn bs-select-all btn btn-default">' +
\r
420 this.options.selectAllText +
\r
422 '<button type="button" class="actions-btn bs-deselect-all btn btn-default">' +
\r
423 this.options.deselectAllText +
\r
428 var donebutton = this.multiple && this.options.doneButton ?
\r
429 '<div class="bs-donebutton">' +
\r
430 '<div class="btn-group btn-block">' +
\r
431 '<button type="button" class="btn btn-sm btn-default">' +
\r
432 this.options.doneButtonText +
\r
438 '<div class="btn-group bootstrap-select' + multiple + inputGroup + '">' +
\r
439 '<button type="button" class="' + this.options.styleBase + ' dropdown-toggle" data-toggle="dropdown"' + autofocus + '>' +
\r
440 '<span class="filter-option pull-left"></span> ' +
\r
441 '<span class="bs-caret">' +
\r
442 this.options.template.caret +
\r
445 '<div class="dropdown-menu open">' +
\r
449 '<ul class="dropdown-menu inner" role="menu">' +
\r
458 createView: function () {
\r
459 var $drop = this.createDropdown(),
\r
460 li = this.createLi();
\r
462 $drop.find('ul')[0].innerHTML = li;
\r
466 reloadLi: function () {
\r
467 //Remove all children.
\r
470 var li = this.createLi();
\r
471 this.$menuInner[0].innerHTML = li;
\r
474 destroyLi: function () {
\r
475 this.$menu.find('li').remove();
\r
478 createLi: function () {
\r
482 titleOption = document.createElement('option'),
\r
483 liIndex = -1; // increment liIndex whenever a new <li> element is created to ensure liObj is correct
\r
485 // Helper functions
\r
490 * @param [optgroup]
\r
491 * @returns {string}
\r
493 var generateLI = function (content, index, classes, optgroup) {
\r
495 ((typeof classes !== 'undefined' & '' !== classes) ? ' class="' + classes + '"' : '') +
\r
496 ((typeof index !== 'undefined' & null !== index) ? ' data-original-index="' + index + '"' : '') +
\r
497 ((typeof optgroup !== 'undefined' & null !== optgroup) ? 'data-optgroup="' + optgroup + '"' : '') +
\r
498 '>' + content + '</li>';
\r
506 * @returns {string}
\r
508 var generateA = function (text, classes, inline, tokens) {
\r
509 return '<a tabindex="0"' +
\r
510 (typeof classes !== 'undefined' ? ' class="' + classes + '"' : '') +
\r
511 (typeof inline !== 'undefined' ? ' style="' + inline + '"' : '') +
\r
512 (that.options.liveSearchNormalize ? ' data-normalized-text="' + normalizeToBase(htmlEscape(text)) + '"' : '') +
\r
513 (typeof tokens !== 'undefined' || tokens !== null ? ' data-tokens="' + tokens + '"' : '') +
\r
515 '<span class="' + that.options.iconBase + ' ' + that.options.tickIcon + ' check-mark"></span>' +
\r
519 if (this.options.title && !this.multiple) {
\r
520 // this option doesn't create a new <li> element, but does add a new option, so liIndex is decreased
\r
521 // since liObj is recalculated on every refresh, liIndex needs to be decreased even if the titleOption is already appended
\r
524 if (!this.$element.find('.bs-title-option').length) {
\r
525 // Use native JS to prepend option (faster)
\r
526 var element = this.$element[0];
\r
527 titleOption.className = 'bs-title-option';
\r
528 titleOption.appendChild(document.createTextNode(this.options.title));
\r
529 titleOption.value = '';
\r
530 element.insertBefore(titleOption, element.firstChild);
\r
531 // Check if selected attribute is already set on an option. If not, select the titleOption option.
\r
532 if ($(element.options[element.selectedIndex]).attr('selected') === undefined) titleOption.selected = true;
\r
536 this.$element.find('option').each(function (index) {
\r
537 var $this = $(this);
\r
541 if ($this.hasClass('bs-title-option')) return;
\r
543 // Get the class and text for the option
\r
544 var optionClass = this.className || '',
\r
545 inline = this.style.cssText,
\r
546 text = $this.data('content') ? $this.data('content') : $this.html(),
\r
547 tokens = $this.data('tokens') ? $this.data('tokens') : null,
\r
548 subtext = typeof $this.data('subtext') !== 'undefined' ? '<small class="text-muted">' + $this.data('subtext') + '</small>' : '',
\r
549 icon = typeof $this.data('icon') !== 'undefined' ? '<span class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></span> ' : '',
\r
550 isDisabled = this.disabled || (this.parentNode.tagName === 'OPTGROUP' && this.parentNode.disabled);
\r
552 if (icon !== '' && isDisabled) {
\r
553 icon = '<span>' + icon + '</span>';
\r
556 if (that.options.hideDisabled && isDisabled) {
\r
561 if (!$this.data('content')) {
\r
562 // Prepend any icon and append any subtext to the main text.
\r
563 text = icon + '<span class="text">' + text + subtext + '</span>';
\r
566 if (this.parentNode.tagName === 'OPTGROUP' && $this.data('divider') !== true) {
\r
567 var optGroupClass = ' ' + this.parentNode.className || '';
\r
569 if ($this.index() === 0) { // Is it the first option of the optgroup?
\r
572 // Get the opt group label
\r
573 var label = this.parentNode.label,
\r
574 labelSubtext = typeof $this.parent().data('subtext') !== 'undefined' ? '<small class="text-muted">' + $this.parent().data('subtext') + '</small>' : '',
\r
575 labelIcon = $this.parent().data('icon') ? '<span class="' + that.options.iconBase + ' ' + $this.parent().data('icon') + '"></span> ' : '';
\r
577 label = labelIcon + '<span class="text">' + label + labelSubtext + '</span>';
\r
579 if (index !== 0 && _li.length > 0) { // Is it NOT the first option of the select && are there elements in the dropdown?
\r
581 _li.push(generateLI('', null, 'divider', optID + 'div'));
\r
584 _li.push(generateLI(label, null, 'dropdown-header' + optGroupClass, optID));
\r
586 _li.push(generateLI(generateA(text, 'opt ' + optionClass + optGroupClass, inline, tokens), index, '', optID));
\r
587 } else if ($this.data('divider') === true) {
\r
588 _li.push(generateLI('', index, 'divider'));
\r
589 } else if ($this.data('hidden') === true) {
\r
590 _li.push(generateLI(generateA(text, optionClass, inline, tokens), index, 'hidden is-hidden'));
\r
592 if (this.previousElementSibling && this.previousElementSibling.tagName === 'OPTGROUP') {
\r
594 _li.push(generateLI('', null, 'divider', optID + 'div'));
\r
596 _li.push(generateLI(generateA(text, optionClass, inline, tokens), index));
\r
599 that.liObj[index] = liIndex;
\r
602 //If we are not multiple, we don't have a selected item, and we don't have a title, select the first element so something is set in the button
\r
603 if (!this.multiple && this.$element.find('option:selected').length === 0 && !this.options.title) {
\r
604 this.$element.find('option').eq(0).prop('selected', true).attr('selected', 'selected');
\r
607 return _li.join('');
\r
610 findLis: function () {
\r
611 if (this.$lis == null) this.$lis = this.$menu.find('li');
\r
616 * @param [updateLi] defaults to true
\r
618 render: function (updateLi) {
\r
622 //Update the LI to match the SELECT
\r
623 if (updateLi !== false) {
\r
624 this.$element.find('option').each(function (index) {
\r
625 var $lis = that.findLis().eq(that.liObj[index]);
\r
627 that.setDisabled(index, this.disabled || this.parentNode.tagName === 'OPTGROUP' && this.parentNode.disabled, $lis);
\r
628 that.setSelected(index, this.selected, $lis);
\r
634 var selectedItems = this.$element.find('option').map(function () {
\r
635 if (this.selected) {
\r
636 if (that.options.hideDisabled && (this.disabled || this.parentNode.tagName === 'OPTGROUP' && this.parentNode.disabled)) return;
\r
638 var $this = $(this),
\r
639 icon = $this.data('icon') && that.options.showIcon ? '<i class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></i> ' : '',
\r
642 if (that.options.showSubtext && $this.data('subtext') && !that.multiple) {
\r
643 subtext = ' <small class="text-muted">' + $this.data('subtext') + '</small>';
\r
647 if (typeof $this.attr('title') !== 'undefined') {
\r
648 return $this.attr('title');
\r
649 } else if ($this.data('content') && that.options.showContent) {
\r
650 return $this.data('content');
\r
652 return icon + $this.html() + subtext;
\r
657 //Fixes issue in IE10 occurring when no default option is selected and at least one option is disabled
\r
658 //Convert all the values into a comma delimited string
\r
659 var title = !this.multiple ? selectedItems[0] : selectedItems.join(this.options.multipleSeparator);
\r
661 //If this is multi select, and the selectText type is count, the show 1 of 2 selected etc..
\r
662 if (this.multiple && this.options.selectedTextFormat.indexOf('count') > -1) {
\r
663 var max = this.options.selectedTextFormat.split('>');
\r
664 if ((max.length > 1 && selectedItems.length > max[1]) || (max.length == 1 && selectedItems.length >= 2)) {
\r
665 notDisabled = this.options.hideDisabled ? ', [disabled]' : '';
\r
666 var totalCount = this.$element.find('option').not('[data-divider="true"], [data-hidden="true"]' + notDisabled).length,
\r
667 tr8nText = (typeof this.options.countSelectedText === 'function') ? this.options.countSelectedText(selectedItems.length, totalCount) : this.options.countSelectedText;
\r
668 title = tr8nText.replace('{0}', selectedItems.length.toString()).replace('{1}', totalCount.toString());
\r
672 if (this.options.title == undefined) {
\r
673 this.options.title = this.$element.attr('title');
\r
676 if (this.options.selectedTextFormat == 'static') {
\r
677 title = this.options.title;
\r
680 //If we dont have a title, then use the default, or if nothing is set at all, use the not selected text
\r
682 title = typeof this.options.title !== 'undefined' ? this.options.title : this.options.noneSelectedText;
\r
685 //strip all html-tags and trim the result
\r
686 this.$button.attr('title', $.trim(title.replace(/<[^>]*>?/g, '')));
\r
687 this.$button.children('.filter-option').html(title);
\r
689 this.$element.trigger('rendered.bs.select');
\r
696 setStyle: function (style, status) {
\r
697 if (this.$element.attr('class')) {
\r
698 this.$newElement.addClass(this.$element.attr('class').replace(/selectpicker|mobile-device|bs-select-hidden|validate\[.*\]/gi, ''));
\r
701 var buttonClass = style ? style : this.options.style;
\r
703 if (status == 'add') {
\r
704 this.$button.addClass(buttonClass);
\r
705 } else if (status == 'remove') {
\r
706 this.$button.removeClass(buttonClass);
\r
708 this.$button.removeClass(this.options.style);
\r
709 this.$button.addClass(buttonClass);
\r
713 liHeight: function (refresh) {
\r
714 if (!refresh && (this.options.size === false || this.sizeInfo)) return;
\r
716 var newElement = document.createElement('div'),
\r
717 menu = document.createElement('div'),
\r
718 menuInner = document.createElement('ul'),
\r
719 divider = document.createElement('li'),
\r
720 li = document.createElement('li'),
\r
721 a = document.createElement('a'),
\r
722 text = document.createElement('span'),
\r
723 header = this.options.header && this.$menu.find('.popover-title').length > 0 ? this.$menu.find('.popover-title')[0].cloneNode(true) : null,
\r
724 search = this.options.liveSearch ? document.createElement('div') : null,
\r
725 actions = this.options.actionsBox && this.multiple && this.$menu.find('.bs-actionsbox').length > 0 ? this.$menu.find('.bs-actionsbox')[0].cloneNode(true) : null,
\r
726 doneButton = this.options.doneButton && this.multiple && this.$menu.find('.bs-donebutton').length > 0 ? this.$menu.find('.bs-donebutton')[0].cloneNode(true) : null;
\r
728 text.className = 'text';
\r
729 newElement.className = this.$menu[0].parentNode.className + ' open';
\r
730 menu.className = 'dropdown-menu open';
\r
731 menuInner.className = 'dropdown-menu inner';
\r
732 divider.className = 'divider';
\r
734 text.appendChild(document.createTextNode('Inner text'));
\r
735 a.appendChild(text);
\r
737 menuInner.appendChild(li);
\r
738 menuInner.appendChild(divider);
\r
739 if (header) menu.appendChild(header);
\r
741 // create a span instead of input as creating an input element is slower
\r
742 var input = document.createElement('span');
\r
743 search.className = 'bs-searchbox';
\r
744 input.className = 'form-control';
\r
745 search.appendChild(input);
\r
746 menu.appendChild(search);
\r
748 if (actions) menu.appendChild(actions);
\r
749 menu.appendChild(menuInner);
\r
750 if (doneButton) menu.appendChild(doneButton);
\r
751 newElement.appendChild(menu);
\r
753 document.body.appendChild(newElement);
\r
755 var liHeight = a.offsetHeight,
\r
756 headerHeight = header ? header.offsetHeight : 0,
\r
757 searchHeight = search ? search.offsetHeight : 0,
\r
758 actionsHeight = actions ? actions.offsetHeight : 0,
\r
759 doneButtonHeight = doneButton ? doneButton.offsetHeight : 0,
\r
760 dividerHeight = $(divider).outerHeight(true),
\r
761 // fall back to jQuery if getComputedStyle is not supported
\r
762 menuStyle = typeof getComputedStyle === 'function' ? getComputedStyle(menu) : false,
\r
763 $menu = menuStyle ? null : $(menu),
\r
764 menuPadding = parseInt(menuStyle ? menuStyle.paddingTop : $menu.css('paddingTop')) +
\r
765 parseInt(menuStyle ? menuStyle.paddingBottom : $menu.css('paddingBottom')) +
\r
766 parseInt(menuStyle ? menuStyle.borderTopWidth : $menu.css('borderTopWidth')) +
\r
767 parseInt(menuStyle ? menuStyle.borderBottomWidth : $menu.css('borderBottomWidth')),
\r
768 menuExtras = menuPadding +
\r
769 parseInt(menuStyle ? menuStyle.marginTop : $menu.css('marginTop')) +
\r
770 parseInt(menuStyle ? menuStyle.marginBottom : $menu.css('marginBottom')) + 2;
\r
772 document.body.removeChild(newElement);
\r
775 liHeight: liHeight,
\r
776 headerHeight: headerHeight,
\r
777 searchHeight: searchHeight,
\r
778 actionsHeight: actionsHeight,
\r
779 doneButtonHeight: doneButtonHeight,
\r
780 dividerHeight: dividerHeight,
\r
781 menuPadding: menuPadding,
\r
782 menuExtras: menuExtras
\r
786 setSize: function () {
\r
790 if (this.options.header) this.$menu.css('padding-top', 0);
\r
791 if (this.options.size === false) return;
\r
794 $menu = this.$menu,
\r
795 $menuInner = this.$menuInner,
\r
796 $window = $(window),
\r
797 selectHeight = this.$newElement[0].offsetHeight,
\r
798 liHeight = this.sizeInfo['liHeight'],
\r
799 headerHeight = this.sizeInfo['headerHeight'],
\r
800 searchHeight = this.sizeInfo['searchHeight'],
\r
801 actionsHeight = this.sizeInfo['actionsHeight'],
\r
802 doneButtonHeight = this.sizeInfo['doneButtonHeight'],
\r
803 divHeight = this.sizeInfo['dividerHeight'],
\r
804 menuPadding = this.sizeInfo['menuPadding'],
\r
805 menuExtras = this.sizeInfo['menuExtras'],
\r
806 notDisabled = this.options.hideDisabled ? '.disabled' : '',
\r
811 posVert = function () {
\r
812 selectOffsetTop = that.$newElement.offset().top - $window.scrollTop();
\r
813 selectOffsetBot = $window.height() - selectOffsetTop - selectHeight;
\r
818 if (this.options.size === 'auto') {
\r
819 var getSize = function () {
\r
821 hasClass = function (className, include) {
\r
822 return function (element) {
\r
824 return (element.classList ? element.classList.contains(className) : $(element).hasClass(className));
\r
826 return !(element.classList ? element.classList.contains(className) : $(element).hasClass(className));
\r
830 lis = that.$menuInner[0].getElementsByTagName('li'),
\r
831 lisVisible = Array.prototype.filter ? Array.prototype.filter.call(lis, hasClass('hidden', false)) : that.$lis.not('.hidden'),
\r
832 optGroup = Array.prototype.filter ? Array.prototype.filter.call(lisVisible, hasClass('dropdown-header', true)) : lisVisible.filter('.dropdown-header');
\r
835 menuHeight = selectOffsetBot - menuExtras;
\r
837 if (that.options.container) {
\r
838 if (!$menu.data('height')) $menu.data('height', $menu.height());
\r
839 getHeight = $menu.data('height');
\r
841 getHeight = $menu.height();
\r
844 if (that.options.dropupAuto) {
\r
845 that.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && (menuHeight - menuExtras) < getHeight);
\r
847 if (that.$newElement.hasClass('dropup')) {
\r
848 menuHeight = selectOffsetTop - menuExtras;
\r
851 if ((lisVisible.length + optGroup.length) > 3) {
\r
852 minHeight = liHeight * 3 + menuExtras - 2;
\r
858 'max-height': menuHeight + 'px',
\r
859 'overflow': 'hidden',
\r
860 'min-height': minHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px'
\r
863 'max-height': menuHeight - headerHeight - searchHeight - actionsHeight - doneButtonHeight - menuPadding + 'px',
\r
864 'overflow-y': 'auto',
\r
865 'min-height': Math.max(minHeight - menuPadding, 0) + 'px'
\r
869 this.$searchbox.off('input.getSize propertychange.getSize').on('input.getSize propertychange.getSize', getSize);
\r
870 $window.off('resize.getSize scroll.getSize').on('resize.getSize scroll.getSize', getSize);
\r
871 } else if (this.options.size && this.options.size != 'auto' && this.$lis.not(notDisabled).length > this.options.size) {
\r
872 var optIndex = this.$lis.not('.divider').not(notDisabled).children().slice(0, this.options.size).last().parent().index(),
\r
873 divLength = this.$lis.slice(0, optIndex + 1).filter('.divider').length;
\r
874 menuHeight = liHeight * this.options.size + divLength * divHeight + menuPadding;
\r
876 if (that.options.container) {
\r
877 if (!$menu.data('height')) $menu.data('height', $menu.height());
\r
878 getHeight = $menu.data('height');
\r
880 getHeight = $menu.height();
\r
883 if (that.options.dropupAuto) {
\r
884 //noinspection JSUnusedAssignment
\r
885 this.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && (menuHeight - menuExtras) < getHeight);
\r
888 'max-height': menuHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px',
\r
889 'overflow': 'hidden',
\r
893 'max-height': menuHeight - menuPadding + 'px',
\r
894 'overflow-y': 'auto',
\r
900 setWidth: function () {
\r
901 if (this.options.width === 'auto') {
\r
902 this.$menu.css('min-width', '0');
\r
904 // Get correct width if element is hidden
\r
905 var $selectClone = this.$menu.parent().clone().appendTo('body'),
\r
906 $selectClone2 = this.options.container ? this.$newElement.clone().appendTo('body') : $selectClone,
\r
907 ulWidth = $selectClone.children('.dropdown-menu').outerWidth(),
\r
908 btnWidth = $selectClone2.css('width', 'auto').children('button').outerWidth();
\r
910 $selectClone.remove();
\r
911 $selectClone2.remove();
\r
913 // Set width to whatever's larger, button title or longest option
\r
914 this.$newElement.css('width', Math.max(ulWidth, btnWidth) + 'px');
\r
915 } else if (this.options.width === 'fit') {
\r
916 // Remove inline min-width so width can be changed from 'auto'
\r
917 this.$menu.css('min-width', '');
\r
918 this.$newElement.css('width', '').addClass('fit-width');
\r
919 } else if (this.options.width) {
\r
920 // Remove inline min-width so width can be changed from 'auto'
\r
921 this.$menu.css('min-width', '');
\r
922 this.$newElement.css('width', this.options.width);
\r
924 // Remove inline min-width/width so width can be changed
\r
925 this.$menu.css('min-width', '');
\r
926 this.$newElement.css('width', '');
\r
928 // Remove fit-width class if width is changed programmatically
\r
929 if (this.$newElement.hasClass('fit-width') && this.options.width !== 'fit') {
\r
930 this.$newElement.removeClass('fit-width');
\r
934 selectPosition: function () {
\r
935 this.$bsContainer = $('<div class="bs-container" />');
\r
940 getPlacement = function ($element) {
\r
941 that.$bsContainer.addClass($element.attr('class').replace(/form-control|fit-width/gi, '')).toggleClass('dropup', $element.hasClass('dropup'));
\r
942 pos = $element.offset();
\r
943 actualHeight = $element.hasClass('dropup') ? 0 : $element[0].offsetHeight;
\r
944 that.$bsContainer.css({
\r
945 'top': pos.top + actualHeight,
\r
947 'width': $element[0].offsetWidth
\r
951 this.$newElement.on('click', function () {
\r
952 var $this = $(this);
\r
954 if (that.isDisabled()) {
\r
958 getPlacement($this);
\r
961 .appendTo(that.options.container)
\r
962 .toggleClass('open', !$this.hasClass('open'))
\r
963 .append(that.$menu);
\r
966 $(window).on('resize scroll', function () {
\r
967 getPlacement(that.$newElement);
\r
970 this.$element.on('hide.bs.select', function () {
\r
971 that.$menu.data('height', that.$menu.height());
\r
972 that.$bsContainer.detach();
\r
976 setSelected: function (index, selected, $lis) {
\r
978 $lis = this.findLis().eq(this.liObj[index]);
\r
981 $lis.toggleClass('selected', selected);
\r
984 setDisabled: function (index, disabled, $lis) {
\r
986 $lis = this.findLis().eq(this.liObj[index]);
\r
990 $lis.addClass('disabled').children('a').attr('href', '#').attr('tabindex', -1);
\r
992 $lis.removeClass('disabled').children('a').removeAttr('href').attr('tabindex', 0);
\r
996 isDisabled: function () {
\r
997 return this.$element[0].disabled;
\r
1000 checkDisabled: function () {
\r
1003 if (this.isDisabled()) {
\r
1004 this.$newElement.addClass('disabled');
\r
1005 this.$button.addClass('disabled').attr('tabindex', -1);
\r
1007 if (this.$button.hasClass('disabled')) {
\r
1008 this.$newElement.removeClass('disabled');
\r
1009 this.$button.removeClass('disabled');
\r
1012 if (this.$button.attr('tabindex') == -1 && !this.$element.data('tabindex')) {
\r
1013 this.$button.removeAttr('tabindex');
\r
1017 this.$button.click(function () {
\r
1018 return !that.isDisabled();
\r
1022 tabIndex: function () {
\r
1023 if (this.$element.is('[tabindex]')) {
\r
1024 this.$element.data('tabindex', this.$element.attr('tabindex'));
\r
1025 this.$button.attr('tabindex', this.$element.data('tabindex'));
\r
1029 clickListener: function () {
\r
1031 $document = $(document);
\r
1033 this.$newElement.on('touchstart.dropdown', '.dropdown-menu', function (e) {
\r
1034 e.stopPropagation();
\r
1037 $document.data('spaceSelect', false);
\r
1039 this.$button.on('keyup', function (e) {
\r
1040 if (/(32)/.test(e.keyCode.toString(10)) && $document.data('spaceSelect')) {
\r
1041 e.preventDefault();
\r
1042 $document.data('spaceSelect', false);
\r
1046 this.$newElement.on('click', function () {
\r
1048 that.$element.on('shown.bs.select', function () {
\r
1049 if (!that.options.liveSearch && !that.multiple) {
\r
1050 that.$menuInner.find('.selected a').focus();
\r
1051 } else if (!that.multiple) {
\r
1052 var selectedIndex = that.liObj[that.$element[0].selectedIndex];
\r
1054 if (typeof selectedIndex !== 'number' || that.options.size === false) return;
\r
1056 // scroll to selected option
\r
1057 var offset = that.$lis.eq(selectedIndex)[0].offsetTop - that.$menuInner[0].offsetTop;
\r
1058 offset = offset - that.$menuInner[0].offsetHeight/2 + that.sizeInfo.liHeight/2;
\r
1059 that.$menuInner[0].scrollTop = offset;
\r
1064 this.$menuInner.on('click', 'li a', function (e) {
\r
1065 var $this = $(this),
\r
1066 clickedIndex = $this.parent().data('originalIndex'),
\r
1067 prevValue = that.$element.val(),
\r
1068 prevIndex = that.$element.prop('selectedIndex');
\r
1070 // Don't close on multi choice menu
\r
1071 if (that.multiple) {
\r
1072 e.stopPropagation();
\r
1075 e.preventDefault();
\r
1077 //Don't run if we have been disabled
\r
1078 if (!that.isDisabled() && !$this.parent().hasClass('disabled')) {
\r
1079 var $options = that.$element.find('option'),
\r
1080 $option = $options.eq(clickedIndex),
\r
1081 state = $option.prop('selected'),
\r
1082 $optgroup = $option.parent('optgroup'),
\r
1083 maxOptions = that.options.maxOptions,
\r
1084 maxOptionsGrp = $optgroup.data('maxOptions') || false;
\r
1086 if (!that.multiple) { // Deselect all others if not multi select box
\r
1087 $options.prop('selected', false);
\r
1088 $option.prop('selected', true);
\r
1089 that.$menuInner.find('.selected').removeClass('selected');
\r
1090 that.setSelected(clickedIndex, true);
\r
1091 } else { // Toggle the one we have chosen if we are multi select.
\r
1092 $option.prop('selected', !state);
\r
1093 that.setSelected(clickedIndex, !state);
\r
1096 if (maxOptions !== false || maxOptionsGrp !== false) {
\r
1097 var maxReached = maxOptions < $options.filter(':selected').length,
\r
1098 maxReachedGrp = maxOptionsGrp < $optgroup.find('option:selected').length;
\r
1100 if ((maxOptions && maxReached) || (maxOptionsGrp && maxReachedGrp)) {
\r
1101 if (maxOptions && maxOptions == 1) {
\r
1102 $options.prop('selected', false);
\r
1103 $option.prop('selected', true);
\r
1104 that.$menuInner.find('.selected').removeClass('selected');
\r
1105 that.setSelected(clickedIndex, true);
\r
1106 } else if (maxOptionsGrp && maxOptionsGrp == 1) {
\r
1107 $optgroup.find('option:selected').prop('selected', false);
\r
1108 $option.prop('selected', true);
\r
1109 var optgroupID = $this.parent().data('optgroup');
\r
1110 that.$menuInner.find('[data-optgroup="' + optgroupID + '"]').removeClass('selected');
\r
1111 that.setSelected(clickedIndex, true);
\r
1113 var maxOptionsArr = (typeof that.options.maxOptionsText === 'function') ?
\r
1114 that.options.maxOptionsText(maxOptions, maxOptionsGrp) : that.options.maxOptionsText,
\r
1115 maxTxt = maxOptionsArr[0].replace('{n}', maxOptions),
\r
1116 maxTxtGrp = maxOptionsArr[1].replace('{n}', maxOptionsGrp),
\r
1117 $notify = $('<div class="notify"></div>');
\r
1118 // If {var} is set in array, replace it
\r
1119 /** @deprecated */
\r
1120 if (maxOptionsArr[2]) {
\r
1121 maxTxt = maxTxt.replace('{var}', maxOptionsArr[2][maxOptions > 1 ? 0 : 1]);
\r
1122 maxTxtGrp = maxTxtGrp.replace('{var}', maxOptionsArr[2][maxOptionsGrp > 1 ? 0 : 1]);
\r
1125 $option.prop('selected', false);
\r
1127 that.$menu.append($notify);
\r
1129 if (maxOptions && maxReached) {
\r
1130 $notify.append($('<div>' + maxTxt + '</div>'));
\r
1131 that.$element.trigger('maxReached.bs.select');
\r
1134 if (maxOptionsGrp && maxReachedGrp) {
\r
1135 $notify.append($('<div>' + maxTxtGrp + '</div>'));
\r
1136 that.$element.trigger('maxReachedGrp.bs.select');
\r
1139 setTimeout(function () {
\r
1140 that.setSelected(clickedIndex, false);
\r
1143 $notify.delay(750).fadeOut(300, function () {
\r
1151 if (!that.multiple) {
\r
1152 that.$button.focus();
\r
1153 } else if (that.options.liveSearch) {
\r
1154 that.$searchbox.focus();
\r
1157 // Trigger select 'change'
\r
1158 if ((prevValue != that.$element.val() && that.multiple) || (prevIndex != that.$element.prop('selectedIndex') && !that.multiple)) {
\r
1159 that.$element.triggerNative('change');
\r
1160 // $option.prop('selected') is current option state (selected/unselected). state is previous option state.
\r
1161 that.$element.trigger('changed.bs.select', [clickedIndex, $option.prop('selected'), state]);
\r
1166 this.$menu.on('click', 'li.disabled a, .popover-title, .popover-title :not(.close)', function (e) {
\r
1167 if (e.currentTarget == this) {
\r
1168 e.preventDefault();
\r
1169 e.stopPropagation();
\r
1170 if (that.options.liveSearch && !$(e.target).hasClass('close')) {
\r
1171 that.$searchbox.focus();
\r
1173 that.$button.focus();
\r
1178 this.$menuInner.on('click', '.divider, .dropdown-header', function (e) {
\r
1179 e.preventDefault();
\r
1180 e.stopPropagation();
\r
1181 if (that.options.liveSearch) {
\r
1182 that.$searchbox.focus();
\r
1184 that.$button.focus();
\r
1188 this.$menu.on('click', '.popover-title .close', function () {
\r
1189 that.$button.click();
\r
1192 this.$searchbox.on('click', function (e) {
\r
1193 e.stopPropagation();
\r
1196 this.$menu.on('click', '.actions-btn', function (e) {
\r
1197 if (that.options.liveSearch) {
\r
1198 that.$searchbox.focus();
\r
1200 that.$button.focus();
\r
1203 e.preventDefault();
\r
1204 e.stopPropagation();
\r
1206 if ($(this).hasClass('bs-select-all')) {
\r
1209 that.deselectAll();
\r
1211 that.$element.triggerNative('change');
\r
1214 this.$element.change(function () {
\r
1215 that.render(false);
\r
1219 liveSearchListener: function () {
\r
1221 $no_results = $('<li class="no-results"></li>');
\r
1223 this.$newElement.on('click.dropdown.data-api touchstart.dropdown.data-api', function () {
\r
1224 that.$menuInner.find('.active').removeClass('active');
\r
1225 if (!!that.$searchbox.val()) {
\r
1226 that.$searchbox.val('');
\r
1227 that.$lis.not('.is-hidden').removeClass('hidden');
\r
1228 if (!!$no_results.parent().length) $no_results.remove();
\r
1230 if (!that.multiple) that.$menuInner.find('.selected').addClass('active');
\r
1231 setTimeout(function () {
\r
1232 that.$searchbox.focus();
\r
1236 this.$searchbox.on('click.dropdown.data-api focus.dropdown.data-api touchend.dropdown.data-api', function (e) {
\r
1237 e.stopPropagation();
\r
1240 this.$searchbox.on('input propertychange', function () {
\r
1241 if (that.$searchbox.val()) {
\r
1242 var $searchBase = that.$lis.not('.is-hidden').removeClass('hidden').children('a');
\r
1243 if (that.options.liveSearchNormalize) {
\r
1244 $searchBase = $searchBase.not(':a' + that._searchStyle() + '("' + normalizeToBase(that.$searchbox.val()) + '")');
\r
1246 $searchBase = $searchBase.not(':' + that._searchStyle() + '("' + that.$searchbox.val() + '")');
\r
1248 $searchBase.parent().addClass('hidden');
\r
1250 that.$lis.filter('.dropdown-header').each(function () {
\r
1251 var $this = $(this),
\r
1252 optgroup = $this.data('optgroup');
\r
1254 if (that.$lis.filter('[data-optgroup=' + optgroup + ']').not($this).not('.hidden').length === 0) {
\r
1255 $this.addClass('hidden');
\r
1256 that.$lis.filter('[data-optgroup=' + optgroup + 'div]').addClass('hidden');
\r
1260 var $lisVisible = that.$lis.not('.hidden');
\r
1262 // hide divider if first or last visible, or if followed by another divider
\r
1263 $lisVisible.each(function (index) {
\r
1264 var $this = $(this);
\r
1266 if ($this.hasClass('divider') && (
\r
1267 $this.index() === $lisVisible.first().index() ||
\r
1268 $this.index() === $lisVisible.last().index() ||
\r
1269 $lisVisible.eq(index + 1).hasClass('divider'))) {
\r
1270 $this.addClass('hidden');
\r
1274 if (!that.$lis.not('.hidden, .no-results').length) {
\r
1275 if (!!$no_results.parent().length) {
\r
1276 $no_results.remove();
\r
1278 $no_results.html(that.options.noneResultsText.replace('{0}', '"' + htmlEscape(that.$searchbox.val()) + '"')).show();
\r
1279 that.$menuInner.append($no_results);
\r
1280 } else if (!!$no_results.parent().length) {
\r
1281 $no_results.remove();
\r
1284 that.$lis.not('.is-hidden').removeClass('hidden');
\r
1285 if (!!$no_results.parent().length) {
\r
1286 $no_results.remove();
\r
1290 that.$lis.filter('.active').removeClass('active');
\r
1291 if (that.$searchbox.val()) that.$lis.not('.hidden, .divider, .dropdown-header').eq(0).addClass('active').children('a').focus();
\r
1296 _searchStyle: function () {
\r
1298 begins: 'ibegins',
\r
1299 startsWith: 'ibegins'
\r
1302 return styles[this.options.liveSearchStyle] || 'icontains';
\r
1305 val: function (value) {
\r
1306 if (typeof value !== 'undefined') {
\r
1307 this.$element.val(value);
\r
1310 return this.$element;
\r
1312 return this.$element.val();
\r
1316 changeAll: function (status) {
\r
1317 if (typeof status === 'undefined') status = true;
\r
1321 var $options = this.$element.find('option'),
\r
1322 $lisVisible = this.$lis.not('.divider, .dropdown-header, .disabled, .hidden').toggleClass('selected', status),
\r
1323 lisVisLen = $lisVisible.length,
\r
1324 selectedOptions = [];
\r
1326 for (var i = 0; i < lisVisLen; i++) {
\r
1327 var origIndex = $lisVisible[i].getAttribute('data-original-index');
\r
1328 selectedOptions[selectedOptions.length] = $options.eq(origIndex)[0];
\r
1331 $(selectedOptions).prop('selected', status);
\r
1333 this.render(false);
\r
1336 selectAll: function () {
\r
1337 return this.changeAll(true);
\r
1340 deselectAll: function () {
\r
1341 return this.changeAll(false);
\r
1344 keydown: function (e) {
\r
1345 var $this = $(this),
\r
1346 $parent = $this.is('input') ? $this.parent().parent() : $this.parent(),
\r
1348 that = $parent.data('this'),
\r
1357 selector = ':not(.disabled, .hidden, .dropdown-header, .divider)',
\r
1409 if (that.options.liveSearch) $parent = $this.parent().parent();
\r
1411 if (that.options.container) $parent = that.$menu;
\r
1413 $items = $('[role=menu] li', $parent);
\r
1415 isActive = that.$menu.parent().hasClass('open');
\r
1417 if (!isActive && (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105 || e.keyCode >= 65 && e.keyCode <= 90)) {
\r
1418 if (!that.options.container) {
\r
1420 that.$menu.parent().addClass('open');
\r
1423 that.$newElement.trigger('click');
\r
1425 that.$searchbox.focus();
\r
1428 if (that.options.liveSearch) {
\r
1429 if (/(^9$|27)/.test(e.keyCode.toString(10)) && isActive && that.$menu.find('.active').length === 0) {
\r
1430 e.preventDefault();
\r
1431 that.$menu.parent().removeClass('open');
\r
1432 if (that.options.container) that.$newElement.removeClass('open');
\r
1433 that.$button.focus();
\r
1435 // $items contains li elements when liveSearch is enabled
\r
1436 $items = $('[role=menu] li' + selector, $parent);
\r
1437 if (!$this.val() && !/(38|40)/.test(e.keyCode.toString(10))) {
\r
1438 if ($items.filter('.active').length === 0) {
\r
1439 $items = that.$menuInner.find('li');
\r
1440 if (that.options.liveSearchNormalize) {
\r
1441 $items = $items.filter(':a' + that._searchStyle() + '(' + normalizeToBase(keyCodeMap[e.keyCode]) + ')');
\r
1443 $items = $items.filter(':' + that._searchStyle() + '(' + keyCodeMap[e.keyCode] + ')');
\r
1449 if (!$items.length) return;
\r
1451 if (/(38|40)/.test(e.keyCode.toString(10))) {
\r
1452 index = $items.index($items.find('a').filter(':focus').parent());
\r
1453 first = $items.filter(selector).first().index();
\r
1454 last = $items.filter(selector).last().index();
\r
1455 next = $items.eq(index).nextAll(selector).eq(0).index();
\r
1456 prev = $items.eq(index).prevAll(selector).eq(0).index();
\r
1457 nextPrev = $items.eq(next).prevAll(selector).eq(0).index();
\r
1459 if (that.options.liveSearch) {
\r
1460 $items.each(function (i) {
\r
1461 if (!$(this).hasClass('disabled')) {
\r
1462 $(this).data('index', i);
\r
1465 index = $items.index($items.filter('.active'));
\r
1466 first = $items.first().data('index');
\r
1467 last = $items.last().data('index');
\r
1468 next = $items.eq(index).nextAll().eq(0).data('index');
\r
1469 prev = $items.eq(index).prevAll().eq(0).data('index');
\r
1470 nextPrev = $items.eq(next).prevAll().eq(0).data('index');
\r
1473 prevIndex = $this.data('prevIndex');
\r
1475 if (e.keyCode == 38) {
\r
1476 if (that.options.liveSearch) index--;
\r
1477 if (index != nextPrev && index > prev) index = prev;
\r
1478 if (index < first) index = first;
\r
1479 if (index == prevIndex) index = last;
\r
1480 } else if (e.keyCode == 40) {
\r
1481 if (that.options.liveSearch) index++;
\r
1482 if (index == -1) index = 0;
\r
1483 if (index != nextPrev && index < next) index = next;
\r
1484 if (index > last) index = last;
\r
1485 if (index == prevIndex) index = first;
\r
1488 $this.data('prevIndex', index);
\r
1490 if (!that.options.liveSearch) {
\r
1491 $items.eq(index).children('a').focus();
\r
1493 e.preventDefault();
\r
1494 if (!$this.hasClass('dropdown-toggle')) {
\r
1495 $items.removeClass('active').eq(index).addClass('active').children('a').focus();
\r
1500 } else if (!$this.is('input')) {
\r
1501 var keyIndex = [],
\r
1505 $items.each(function () {
\r
1506 if (!$(this).hasClass('disabled')) {
\r
1507 if ($.trim($(this).children('a').text().toLowerCase()).substring(0, 1) == keyCodeMap[e.keyCode]) {
\r
1508 keyIndex.push($(this).index());
\r
1513 count = $(document).data('keycount');
\r
1515 $(document).data('keycount', count);
\r
1517 prevKey = $.trim($(':focus').text().toLowerCase()).substring(0, 1);
\r
1519 if (prevKey != keyCodeMap[e.keyCode]) {
\r
1521 $(document).data('keycount', count);
\r
1522 } else if (count >= keyIndex.length) {
\r
1523 $(document).data('keycount', 0);
\r
1524 if (count > keyIndex.length) count = 1;
\r
1527 $items.eq(keyIndex[count - 1]).children('a').focus();
\r
1530 // Select focused option if "Enter", "Spacebar" or "Tab" (when selectOnTab is true) are pressed inside the menu.
\r
1531 if ((/(13|32)/.test(e.keyCode.toString(10)) || (/(^9$)/.test(e.keyCode.toString(10)) && that.options.selectOnTab)) && isActive) {
\r
1532 if (!/(32)/.test(e.keyCode.toString(10))) e.preventDefault();
\r
1533 if (!that.options.liveSearch) {
\r
1534 var elem = $(':focus');
\r
1536 // Bring back focus for multiselects
\r
1538 // Prevent screen from scrolling if the user hit the spacebar
\r
1539 e.preventDefault();
\r
1540 // Fixes spacebar selection of dropdown items in FF & IE
\r
1541 $(document).data('spaceSelect', true);
\r
1542 } else if (!/(32)/.test(e.keyCode.toString(10))) {
\r
1543 that.$menuInner.find('.active a').click();
\r
1546 $(document).data('keycount', 0);
\r
1549 if ((/(^9$|27)/.test(e.keyCode.toString(10)) && isActive && (that.multiple || that.options.liveSearch)) || (/(27)/.test(e.keyCode.toString(10)) && !isActive)) {
\r
1550 that.$menu.parent().removeClass('open');
\r
1551 if (that.options.container) that.$newElement.removeClass('open');
\r
1552 that.$button.focus();
\r
1556 mobile: function () {
\r
1557 this.$element.addClass('mobile-device').appendTo(this.$newElement);
\r
1558 if (this.options.container) this.$menu.hide();
\r
1561 refresh: function () {
\r
1566 this.checkDisabled();
\r
1567 this.liHeight(true);
\r
1570 if (this.$lis) this.$searchbox.trigger('propertychange');
\r
1572 this.$element.trigger('refreshed.bs.select');
\r
1575 hide: function () {
\r
1576 this.$newElement.hide();
\r
1579 show: function () {
\r
1580 this.$newElement.show();
\r
1583 remove: function () {
\r
1584 this.$newElement.remove();
\r
1585 this.$element.remove();
\r
1588 destroy: function() {
\r
1589 this.$newElement.remove();
\r
1591 if (this.$bsContainer) {
\r
1592 this.$bsContainer.remove();
\r
1594 this.$menu.remove();
\r
1598 .off('.bs.select')
\r
1599 .removeData('selectpicker')
\r
1600 .removeClass('bs-select-hidden selectpicker');
\r
1604 // SELECTPICKER PLUGIN DEFINITION
\r
1605 // ==============================
\r
1606 function Plugin(option, event) {
\r
1607 // get the args of the outer function..
\r
1608 var args = arguments;
\r
1609 // The arguments of the function are explicitly re-defined from the argument list, because the shift causes them
\r
1610 // to get lost/corrupted in android 2.3 and IE9 #715 #775
\r
1611 var _option = option,
\r
1613 [].shift.apply(args);
\r
1616 var chain = this.each(function () {
\r
1617 var $this = $(this);
\r
1618 if ($this.is('select')) {
\r
1619 var data = $this.data('selectpicker'),
\r
1620 options = typeof _option == 'object' && _option;
\r
1623 var config = $.extend({}, Selectpicker.DEFAULTS, $.fn.selectpicker.defaults || {}, $this.data(), options);
\r
1624 config.template = $.extend({}, Selectpicker.DEFAULTS.template, ($.fn.selectpicker.defaults ? $.fn.selectpicker.defaults.template : {}), $this.data().template, options.template);
\r
1625 $this.data('selectpicker', (data = new Selectpicker(this, config, _event)));
\r
1626 } else if (options) {
\r
1627 for (var i in options) {
\r
1628 if (options.hasOwnProperty(i)) {
\r
1629 data.options[i] = options[i];
\r
1634 if (typeof _option == 'string') {
\r
1635 if (data[_option] instanceof Function) {
\r
1636 value = data[_option].apply(data, args);
\r
1638 value = data.options[_option];
\r
1644 if (typeof value !== 'undefined') {
\r
1645 //noinspection JSUnusedAssignment
\r
1652 var old = $.fn.selectpicker;
\r
1653 $.fn.selectpicker = Plugin;
\r
1654 $.fn.selectpicker.Constructor = Selectpicker;
\r
1656 // SELECTPICKER NO CONFLICT
\r
1657 // ========================
\r
1658 $.fn.selectpicker.noConflict = function () {
\r
1659 $.fn.selectpicker = old;
\r
1664 .data('keycount', 0)
\r
1665 .on('keydown.bs.select', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role="menu"], .bs-searchbox input', Selectpicker.prototype.keydown)
\r
1666 .on('focusin.modal', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role="menu"], .bs-searchbox input', function (e) {
\r
1667 e.stopPropagation();
\r
1670 // SELECTPICKER DATA-API
\r
1671 // =====================
\r
1672 $(window).on('load.bs.select.data-api', function () {
\r
1673 $('.selectpicker').each(function () {
\r
1674 var $selectpicker = $(this);
\r
1675 Plugin.call($selectpicker, $selectpicker.data());
\r