No Description

bootstrap-select.js 67KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. /*!
  2. * Bootstrap-select v1.12.4 (http://silviomoreto.github.io/bootstrap-select)
  3. *
  4. * Copyright 2013-2017 bootstrap-select
  5. * Licensed under MIT (https://github.com/silviomoreto/bootstrap-select/blob/master/LICENSE)
  6. */
  7. (function (root, factory) {
  8. if (typeof define === 'function' && define.amd) {
  9. // AMD. Register as an anonymous module unless amdModuleId is set
  10. define(["jquery"], function (a0) {
  11. return (factory(a0));
  12. });
  13. } else if (typeof module === 'object' && module.exports) {
  14. // Node. Does not work with strict CommonJS, but
  15. // only CommonJS-like environments that support module.exports,
  16. // like Node.
  17. module.exports = factory(require("jquery"));
  18. } else {
  19. factory(root["jQuery"]);
  20. }
  21. }(this, function (jQuery) {
  22. (function ($) {
  23. 'use strict';
  24. //<editor-fold desc="Shims">
  25. if (!String.prototype.includes) {
  26. (function () {
  27. 'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
  28. var toString = {}.toString;
  29. var defineProperty = (function () {
  30. // IE 8 only supports `Object.defineProperty` on DOM elements
  31. try {
  32. var object = {};
  33. var $defineProperty = Object.defineProperty;
  34. var result = $defineProperty(object, object, object) && $defineProperty;
  35. } catch (error) {
  36. }
  37. return result;
  38. }());
  39. var indexOf = ''.indexOf;
  40. var includes = function (search) {
  41. if (this == null) {
  42. throw new TypeError();
  43. }
  44. var string = String(this);
  45. if (search && toString.call(search) == '[object RegExp]') {
  46. throw new TypeError();
  47. }
  48. var stringLength = string.length;
  49. var searchString = String(search);
  50. var searchLength = searchString.length;
  51. var position = arguments.length > 1 ? arguments[1] : undefined;
  52. // `ToInteger`
  53. var pos = position ? Number(position) : 0;
  54. if (pos != pos) { // better `isNaN`
  55. pos = 0;
  56. }
  57. var start = Math.min(Math.max(pos, 0), stringLength);
  58. // Avoid the `indexOf` call if no match is possible
  59. if (searchLength + start > stringLength) {
  60. return false;
  61. }
  62. return indexOf.call(string, searchString, pos) != -1;
  63. };
  64. if (defineProperty) {
  65. defineProperty(String.prototype, 'includes', {
  66. 'value': includes,
  67. 'configurable': true,
  68. 'writable': true
  69. });
  70. } else {
  71. String.prototype.includes = includes;
  72. }
  73. }());
  74. }
  75. if (!String.prototype.startsWith) {
  76. (function () {
  77. 'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
  78. var defineProperty = (function () {
  79. // IE 8 only supports `Object.defineProperty` on DOM elements
  80. try {
  81. var object = {};
  82. var $defineProperty = Object.defineProperty;
  83. var result = $defineProperty(object, object, object) && $defineProperty;
  84. } catch (error) {
  85. }
  86. return result;
  87. }());
  88. var toString = {}.toString;
  89. var startsWith = function (search) {
  90. if (this == null) {
  91. throw new TypeError();
  92. }
  93. var string = String(this);
  94. if (search && toString.call(search) == '[object RegExp]') {
  95. throw new TypeError();
  96. }
  97. var stringLength = string.length;
  98. var searchString = String(search);
  99. var searchLength = searchString.length;
  100. var position = arguments.length > 1 ? arguments[1] : undefined;
  101. // `ToInteger`
  102. var pos = position ? Number(position) : 0;
  103. if (pos != pos) { // better `isNaN`
  104. pos = 0;
  105. }
  106. var start = Math.min(Math.max(pos, 0), stringLength);
  107. // Avoid the `indexOf` call if no match is possible
  108. if (searchLength + start > stringLength) {
  109. return false;
  110. }
  111. var index = -1;
  112. while (++index < searchLength) {
  113. if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
  114. return false;
  115. }
  116. }
  117. return true;
  118. };
  119. if (defineProperty) {
  120. defineProperty(String.prototype, 'startsWith', {
  121. 'value': startsWith,
  122. 'configurable': true,
  123. 'writable': true
  124. });
  125. } else {
  126. String.prototype.startsWith = startsWith;
  127. }
  128. }());
  129. }
  130. if (!Object.keys) {
  131. Object.keys = function (
  132. o, // object
  133. k, // key
  134. r // result array
  135. ){
  136. // initialize object and result
  137. r=[];
  138. // iterate over object keys
  139. for (k in o)
  140. // fill result array with non-prototypical keys
  141. r.hasOwnProperty.call(o, k) && r.push(k);
  142. // return result
  143. return r;
  144. };
  145. }
  146. // set data-selected on select element if the value has been programmatically selected
  147. // prior to initialization of bootstrap-select
  148. // * consider removing or replacing an alternative method *
  149. var valHooks = {
  150. useDefault: false,
  151. _set: $.valHooks.select.set
  152. };
  153. $.valHooks.select.set = function(elem, value) {
  154. if (value && !valHooks.useDefault) $(elem).data('selected', true);
  155. return valHooks._set.apply(this, arguments);
  156. };
  157. var changed_arguments = null;
  158. var EventIsSupported = (function() {
  159. try {
  160. new Event('change');
  161. return true;
  162. } catch (e) {
  163. return false;
  164. }
  165. })();
  166. $.fn.triggerNative = function (eventName) {
  167. var el = this[0],
  168. event;
  169. if (el.dispatchEvent) { // for modern browsers & IE9+
  170. if (EventIsSupported) {
  171. // For modern browsers
  172. event = new Event(eventName, {
  173. bubbles: true
  174. });
  175. } else {
  176. // For IE since it doesn't support Event constructor
  177. event = document.createEvent('Event');
  178. event.initEvent(eventName, true, false);
  179. }
  180. el.dispatchEvent(event);
  181. } else if (el.fireEvent) { // for IE8
  182. event = document.createEventObject();
  183. event.eventType = eventName;
  184. el.fireEvent('on' + eventName, event);
  185. } else {
  186. // fall back to jQuery.trigger
  187. this.trigger(eventName);
  188. }
  189. };
  190. //</editor-fold>
  191. // Case insensitive contains search
  192. $.expr.pseudos.icontains = function (obj, index, meta) {
  193. var $obj = $(obj).find('a');
  194. var haystack = ($obj.data('tokens') || $obj.text()).toString().toUpperCase();
  195. return haystack.includes(meta[3].toUpperCase());
  196. };
  197. // Case insensitive begins search
  198. $.expr.pseudos.ibegins = function (obj, index, meta) {
  199. var $obj = $(obj).find('a');
  200. var haystack = ($obj.data('tokens') || $obj.text()).toString().toUpperCase();
  201. return haystack.startsWith(meta[3].toUpperCase());
  202. };
  203. // Case and accent insensitive contains search
  204. $.expr.pseudos.aicontains = function (obj, index, meta) {
  205. var $obj = $(obj).find('a');
  206. var haystack = ($obj.data('tokens') || $obj.data('normalizedText') || $obj.text()).toString().toUpperCase();
  207. return haystack.includes(meta[3].toUpperCase());
  208. };
  209. // Case and accent insensitive begins search
  210. $.expr.pseudos.aibegins = function (obj, index, meta) {
  211. var $obj = $(obj).find('a');
  212. var haystack = ($obj.data('tokens') || $obj.data('normalizedText') || $obj.text()).toString().toUpperCase();
  213. return haystack.startsWith(meta[3].toUpperCase());
  214. };
  215. /**
  216. * Remove all diatrics from the given text.
  217. * @access private
  218. * @param {String} text
  219. * @returns {String}
  220. */
  221. function normalizeToBase(text) {
  222. var rExps = [
  223. {re: /[\xC0-\xC6]/g, ch: "A"},
  224. {re: /[\xE0-\xE6]/g, ch: "a"},
  225. {re: /[\xC8-\xCB]/g, ch: "E"},
  226. {re: /[\xE8-\xEB]/g, ch: "e"},
  227. {re: /[\xCC-\xCF]/g, ch: "I"},
  228. {re: /[\xEC-\xEF]/g, ch: "i"},
  229. {re: /[\xD2-\xD6]/g, ch: "O"},
  230. {re: /[\xF2-\xF6]/g, ch: "o"},
  231. {re: /[\xD9-\xDC]/g, ch: "U"},
  232. {re: /[\xF9-\xFC]/g, ch: "u"},
  233. {re: /[\xC7-\xE7]/g, ch: "c"},
  234. {re: /[\xD1]/g, ch: "N"},
  235. {re: /[\xF1]/g, ch: "n"}
  236. ];
  237. $.each(rExps, function () {
  238. text = text ? text.replace(this.re, this.ch) : '';
  239. });
  240. return text;
  241. }
  242. // List of HTML entities for escaping.
  243. var escapeMap = {
  244. '&': '&amp;',
  245. '<': '&lt;',
  246. '>': '&gt;',
  247. '"': '&quot;',
  248. "'": '&#x27;',
  249. '`': '&#x60;'
  250. };
  251. var unescapeMap = {
  252. '&amp;': '&',
  253. '&lt;': '<',
  254. '&gt;': '>',
  255. '&quot;': '"',
  256. '&#x27;': "'",
  257. '&#x60;': '`'
  258. };
  259. // Functions for escaping and unescaping strings to/from HTML interpolation.
  260. var createEscaper = function(map) {
  261. var escaper = function(match) {
  262. return map[match];
  263. };
  264. // Regexes for identifying a key that needs to be escaped.
  265. var source = '(?:' + Object.keys(map).join('|') + ')';
  266. var testRegexp = RegExp(source);
  267. var replaceRegexp = RegExp(source, 'g');
  268. return function(string) {
  269. string = string == null ? '' : '' + string;
  270. return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
  271. };
  272. };
  273. var htmlEscape = createEscaper(escapeMap);
  274. var htmlUnescape = createEscaper(unescapeMap);
  275. var Selectpicker = function (element, options) {
  276. // bootstrap-select has been initialized - revert valHooks.select.set back to its original function
  277. if (!valHooks.useDefault) {
  278. $.valHooks.select.set = valHooks._set;
  279. valHooks.useDefault = true;
  280. }
  281. this.$element = $(element);
  282. this.$newElement = null;
  283. this.$button = null;
  284. this.$menu = null;
  285. this.$lis = null;
  286. this.options = options;
  287. // 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
  288. // data-attribute)
  289. if (this.options.title === null) {
  290. this.options.title = this.$element.attr('title');
  291. }
  292. // Format window padding
  293. var winPad = this.options.windowPadding;
  294. if (typeof winPad === 'number') {
  295. this.options.windowPadding = [winPad, winPad, winPad, winPad];
  296. }
  297. //Expose public methods
  298. this.val = Selectpicker.prototype.val;
  299. this.render = Selectpicker.prototype.render;
  300. this.refresh = Selectpicker.prototype.refresh;
  301. this.setStyle = Selectpicker.prototype.setStyle;
  302. this.selectAll = Selectpicker.prototype.selectAll;
  303. this.deselectAll = Selectpicker.prototype.deselectAll;
  304. this.destroy = Selectpicker.prototype.destroy;
  305. this.remove = Selectpicker.prototype.remove;
  306. this.show = Selectpicker.prototype.show;
  307. this.hide = Selectpicker.prototype.hide;
  308. this.init();
  309. };
  310. Selectpicker.VERSION = '1.12.4';
  311. // part of this is duplicated in i18n/defaults-en_US.js. Make sure to update both.
  312. Selectpicker.DEFAULTS = {
  313. noneSelectedText: 'Nothing selected',
  314. noneResultsText: 'No results matched {0}',
  315. countSelectedText: function (numSelected, numTotal) {
  316. return (numSelected == 1) ? "{0} item selected" : "{0} items selected";
  317. },
  318. maxOptionsText: function (numAll, numGroup) {
  319. return [
  320. (numAll == 1) ? 'Limit reached ({n} item max)' : 'Limit reached ({n} items max)',
  321. (numGroup == 1) ? 'Group limit reached ({n} item max)' : 'Group limit reached ({n} items max)'
  322. ];
  323. },
  324. selectAllText: 'Select All',
  325. deselectAllText: 'Deselect All',
  326. doneButton: false,
  327. doneButtonText: 'Close',
  328. multipleSeparator: ', ',
  329. styleBase: 'btn',
  330. style: 'btn-default',
  331. size: 'auto',
  332. title: null,
  333. selectedTextFormat: 'values',
  334. width: false,
  335. container: false,
  336. hideDisabled: false,
  337. showSubtext: false,
  338. showIcon: true,
  339. showContent: true,
  340. dropupAuto: true,
  341. header: false,
  342. liveSearch: false,
  343. liveSearchPlaceholder: null,
  344. liveSearchNormalize: false,
  345. liveSearchStyle: 'contains',
  346. actionsBox: false,
  347. iconBase: 'glyphicon',
  348. tickIcon: 'glyphicon-ok',
  349. showTick: false,
  350. template: {
  351. caret: '<span class="caret"></span>'
  352. },
  353. maxOptions: false,
  354. mobile: false,
  355. selectOnTab: false,
  356. dropdownAlignRight: false,
  357. windowPadding: 0
  358. };
  359. Selectpicker.prototype = {
  360. constructor: Selectpicker,
  361. init: function () {
  362. var that = this,
  363. id = this.$element.attr('id');
  364. this.$element.addClass('bs-select-hidden');
  365. // store originalIndex (key) and newIndex (value) in this.liObj for fast accessibility
  366. // allows us to do this.$lis.eq(that.liObj[index]) instead of this.$lis.filter('[data-original-index="' + index + '"]')
  367. this.liObj = {};
  368. this.multiple = this.$element.prop('multiple');
  369. this.autofocus = this.$element.prop('autofocus');
  370. this.$newElement = this.createView();
  371. this.$element
  372. .after(this.$newElement)
  373. .appendTo(this.$newElement);
  374. this.$button = this.$newElement.children('button');
  375. this.$menu = this.$newElement.children('.dropdown-menu');
  376. this.$menuInner = this.$menu.children('.inner');
  377. this.$searchbox = this.$menu.find('input');
  378. this.$element.removeClass('bs-select-hidden');
  379. if (this.options.dropdownAlignRight === true) this.$menu.addClass('dropdown-menu-right');
  380. if (typeof id !== 'undefined') {
  381. this.$button.attr('data-id', id);
  382. $('label[for="' + id + '"]').click(function (e) {
  383. e.preventDefault();
  384. that.$button.focus();
  385. });
  386. }
  387. this.checkDisabled();
  388. this.clickListener();
  389. if (this.options.liveSearch) this.liveSearchListener();
  390. this.render();
  391. this.setStyle();
  392. this.setWidth();
  393. if (this.options.container) this.selectPosition();
  394. this.$menu.data('this', this);
  395. this.$newElement.data('this', this);
  396. if (this.options.mobile) this.mobile();
  397. this.$newElement.on({
  398. 'hide.bs.dropdown': function (e) {
  399. that.$menuInner.attr('aria-expanded', false);
  400. that.$element.trigger('hide.bs.select', e);
  401. },
  402. 'hidden.bs.dropdown': function (e) {
  403. that.$element.trigger('hidden.bs.select', e);
  404. },
  405. 'show.bs.dropdown': function (e) {
  406. that.$menuInner.attr('aria-expanded', true);
  407. that.$element.trigger('show.bs.select', e);
  408. },
  409. 'shown.bs.dropdown': function (e) {
  410. that.$element.trigger('shown.bs.select', e);
  411. }
  412. });
  413. if (that.$element[0].hasAttribute('required')) {
  414. this.$element.on('invalid', function () {
  415. that.$button.addClass('bs-invalid');
  416. that.$element.on({
  417. 'focus.bs.select': function () {
  418. that.$button.focus();
  419. that.$element.off('focus.bs.select');
  420. },
  421. 'shown.bs.select': function () {
  422. that.$element
  423. .val(that.$element.val()) // set the value to hide the validation message in Chrome when menu is opened
  424. .off('shown.bs.select');
  425. },
  426. 'rendered.bs.select': function () {
  427. // if select is no longer invalid, remove the bs-invalid class
  428. if (this.validity.valid) that.$button.removeClass('bs-invalid');
  429. that.$element.off('rendered.bs.select');
  430. }
  431. });
  432. that.$button.on('blur.bs.select', function() {
  433. that.$element.focus().blur();
  434. that.$button.off('blur.bs.select');
  435. });
  436. });
  437. }
  438. setTimeout(function () {
  439. that.$element.trigger('loaded.bs.select');
  440. });
  441. },
  442. createDropdown: function () {
  443. // Options
  444. // If we are multiple or showTick option is set, then add the show-tick class
  445. var showTick = (this.multiple || this.options.showTick) ? ' show-tick' : '',
  446. inputGroup = this.$element.parent().hasClass('input-group') ? ' input-group-btn' : '',
  447. autofocus = this.autofocus ? ' autofocus' : '';
  448. // Elements
  449. var header = this.options.header ? '<div class="popover-title"><button type="button" class="close" aria-hidden="true">&times;</button>' + this.options.header + '</div>' : '';
  450. var searchbox = this.options.liveSearch ?
  451. '<div class="bs-searchbox">' +
  452. '<input type="text" class="form-control" autocomplete="off"' +
  453. (null === this.options.liveSearchPlaceholder ? '' : ' placeholder="' + htmlEscape(this.options.liveSearchPlaceholder) + '"') + ' role="textbox" aria-label="Search">' +
  454. '</div>'
  455. : '';
  456. var actionsbox = this.multiple && this.options.actionsBox ?
  457. '<div class="bs-actionsbox">' +
  458. '<div class="btn-group btn-group-sm btn-block">' +
  459. '<button type="button" class="actions-btn bs-select-all btn btn-default">' +
  460. this.options.selectAllText +
  461. '</button>' +
  462. '<button type="button" class="actions-btn bs-deselect-all btn btn-default">' +
  463. this.options.deselectAllText +
  464. '</button>' +
  465. '</div>' +
  466. '</div>'
  467. : '';
  468. var donebutton = this.multiple && this.options.doneButton ?
  469. '<div class="bs-donebutton">' +
  470. '<div class="btn-group btn-block">' +
  471. '<button type="button" class="btn btn-sm btn-default">' +
  472. this.options.doneButtonText +
  473. '</button>' +
  474. '</div>' +
  475. '</div>'
  476. : '';
  477. var drop =
  478. '<div class="btn-group bootstrap-select' + showTick + inputGroup + '">' +
  479. '<button type="button" class="' + this.options.styleBase + ' dropdown-toggle" data-toggle="dropdown"' + autofocus + ' role="button">' +
  480. '<span class="filter-option pull-left"></span>&nbsp;' +
  481. '<span class="bs-caret">' +
  482. this.options.template.caret +
  483. '</span>' +
  484. '</button>' +
  485. '<div class="dropdown-menu open" role="combobox">' +
  486. header +
  487. searchbox +
  488. actionsbox +
  489. '<ul class="dropdown-menu inner" role="listbox" aria-expanded="false">' +
  490. '</ul>' +
  491. donebutton +
  492. '</div>' +
  493. '</div>';
  494. return $(drop);
  495. },
  496. createView: function () {
  497. var $drop = this.createDropdown(),
  498. li = this.createLi();
  499. $drop.find('ul')[0].innerHTML = li;
  500. return $drop;
  501. },
  502. reloadLi: function () {
  503. // rebuild
  504. var li = this.createLi();
  505. this.$menuInner[0].innerHTML = li;
  506. },
  507. createLi: function () {
  508. var that = this,
  509. _li = [],
  510. optID = 0,
  511. titleOption = document.createElement('option'),
  512. liIndex = -1; // increment liIndex whenever a new <li> element is created to ensure liObj is correct
  513. // Helper functions
  514. /**
  515. * @param content
  516. * @param [index]
  517. * @param [classes]
  518. * @param [optgroup]
  519. * @returns {string}
  520. */
  521. var generateLI = function (content, index, classes, optgroup) {
  522. return '<li' +
  523. ((typeof classes !== 'undefined' && '' !== classes) ? ' class="' + classes + '"' : '') +
  524. ((typeof index !== 'undefined' && null !== index) ? ' data-original-index="' + index + '"' : '') +
  525. ((typeof optgroup !== 'undefined' && null !== optgroup) ? 'data-optgroup="' + optgroup + '"' : '') +
  526. '>' + content + '</li>';
  527. };
  528. /**
  529. * @param text
  530. * @param [classes]
  531. * @param [inline]
  532. * @param [tokens]
  533. * @returns {string}
  534. */
  535. var generateA = function (text, classes, inline, tokens) {
  536. return '<a tabindex="0"' +
  537. (typeof classes !== 'undefined' ? ' class="' + classes + '"' : '') +
  538. (inline ? ' style="' + inline + '"' : '') +
  539. (that.options.liveSearchNormalize ? ' data-normalized-text="' + normalizeToBase(htmlEscape($(text).html())) + '"' : '') +
  540. (typeof tokens !== 'undefined' || tokens !== null ? ' data-tokens="' + tokens + '"' : '') +
  541. ' role="option">' + text +
  542. '<span class="' + that.options.iconBase + ' ' + that.options.tickIcon + ' check-mark"></span>' +
  543. '</a>';
  544. };
  545. if (this.options.title && !this.multiple) {
  546. // this option doesn't create a new <li> element, but does add a new option, so liIndex is decreased
  547. // since liObj is recalculated on every refresh, liIndex needs to be decreased even if the titleOption is already appended
  548. liIndex--;
  549. if (!this.$element.find('.bs-title-option').length) {
  550. // Use native JS to prepend option (faster)
  551. var element = this.$element[0];
  552. titleOption.className = 'bs-title-option';
  553. titleOption.innerHTML = this.options.title;
  554. titleOption.value = '';
  555. element.insertBefore(titleOption, element.firstChild);
  556. // Check if selected or data-selected attribute is already set on an option. If not, select the titleOption option.
  557. // the selected item may have been changed by user or programmatically before the bootstrap select plugin runs,
  558. // if so, the select will have the data-selected attribute
  559. var $opt = $(element.options[element.selectedIndex]);
  560. if ($opt.attr('selected') === undefined && this.$element.data('selected') === undefined) {
  561. titleOption.selected = true;
  562. }
  563. }
  564. }
  565. var $selectOptions = this.$element.find('option');
  566. $selectOptions.each(function (index) {
  567. var $this = $(this);
  568. liIndex++;
  569. if ($this.hasClass('bs-title-option')) return;
  570. // Get the class and text for the option
  571. var optionClass = this.className || '',
  572. inline = htmlEscape(this.style.cssText),
  573. text = $this.data('content') ? $this.data('content') : $this.html(),
  574. tokens = $this.data('tokens') ? $this.data('tokens') : null,
  575. subtext = typeof $this.data('subtext') !== 'undefined' ? '<small class="text-muted">' + $this.data('subtext') + '</small>' : '',
  576. icon = typeof $this.data('icon') !== 'undefined' ? '<span class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></span> ' : '',
  577. $parent = $this.parent(),
  578. isOptgroup = $parent[0].tagName === 'OPTGROUP',
  579. isOptgroupDisabled = isOptgroup && $parent[0].disabled,
  580. isDisabled = this.disabled || isOptgroupDisabled,
  581. prevHiddenIndex;
  582. if (icon !== '' && isDisabled) {
  583. icon = '<span>' + icon + '</span>';
  584. }
  585. if (that.options.hideDisabled && (isDisabled && !isOptgroup || isOptgroupDisabled)) {
  586. // set prevHiddenIndex - the index of the first hidden option in a group of hidden options
  587. // used to determine whether or not a divider should be placed after an optgroup if there are
  588. // hidden options between the optgroup and the first visible option
  589. prevHiddenIndex = $this.data('prevHiddenIndex');
  590. $this.next().data('prevHiddenIndex', (prevHiddenIndex !== undefined ? prevHiddenIndex : index));
  591. liIndex--;
  592. return;
  593. }
  594. if (!$this.data('content')) {
  595. // Prepend any icon and append any subtext to the main text.
  596. text = icon + '<span class="text">' + text + subtext + '</span>';
  597. }
  598. if (isOptgroup && $this.data('divider') !== true) {
  599. if (that.options.hideDisabled && isDisabled) {
  600. if ($parent.data('allOptionsDisabled') === undefined) {
  601. var $options = $parent.children();
  602. $parent.data('allOptionsDisabled', $options.filter(':disabled').length === $options.length);
  603. }
  604. if ($parent.data('allOptionsDisabled')) {
  605. liIndex--;
  606. return;
  607. }
  608. }
  609. var optGroupClass = ' ' + $parent[0].className || '';
  610. if ($this.index() === 0) { // Is it the first option of the optgroup?
  611. optID += 1;
  612. // Get the opt group label
  613. var label = $parent[0].label,
  614. labelSubtext = typeof $parent.data('subtext') !== 'undefined' ? '<small class="text-muted">' + $parent.data('subtext') + '</small>' : '',
  615. labelIcon = $parent.data('icon') ? '<span class="' + that.options.iconBase + ' ' + $parent.data('icon') + '"></span> ' : '';
  616. label = labelIcon + '<span class="text">' + htmlEscape(label) + labelSubtext + '</span>';
  617. if (index !== 0 && _li.length > 0) { // Is it NOT the first option of the select && are there elements in the dropdown?
  618. liIndex++;
  619. _li.push(generateLI('', null, 'divider', optID + 'div'));
  620. }
  621. liIndex++;
  622. _li.push(generateLI(label, null, 'dropdown-header' + optGroupClass, optID));
  623. }
  624. if (that.options.hideDisabled && isDisabled) {
  625. liIndex--;
  626. return;
  627. }
  628. _li.push(generateLI(generateA(text, 'opt ' + optionClass + optGroupClass, inline, tokens), index, '', optID));
  629. } else if ($this.data('divider') === true) {
  630. _li.push(generateLI('', index, 'divider'));
  631. } else if ($this.data('hidden') === true) {
  632. // set prevHiddenIndex - the index of the first hidden option in a group of hidden options
  633. // used to determine whether or not a divider should be placed after an optgroup if there are
  634. // hidden options between the optgroup and the first visible option
  635. prevHiddenIndex = $this.data('prevHiddenIndex');
  636. $this.next().data('prevHiddenIndex', (prevHiddenIndex !== undefined ? prevHiddenIndex : index));
  637. _li.push(generateLI(generateA(text, optionClass, inline, tokens), index, 'hidden is-hidden'));
  638. } else {
  639. var showDivider = this.previousElementSibling && this.previousElementSibling.tagName === 'OPTGROUP';
  640. // if previous element is not an optgroup and hideDisabled is true
  641. if (!showDivider && that.options.hideDisabled) {
  642. prevHiddenIndex = $this.data('prevHiddenIndex');
  643. if (prevHiddenIndex !== undefined) {
  644. // select the element **before** the first hidden element in the group
  645. var prevHidden = $selectOptions.eq(prevHiddenIndex)[0].previousElementSibling;
  646. if (prevHidden && prevHidden.tagName === 'OPTGROUP' && !prevHidden.disabled) {
  647. showDivider = true;
  648. }
  649. }
  650. }
  651. if (showDivider) {
  652. liIndex++;
  653. _li.push(generateLI('', null, 'divider', optID + 'div'));
  654. }
  655. _li.push(generateLI(generateA(text, optionClass, inline, tokens), index));
  656. }
  657. that.liObj[index] = liIndex;
  658. });
  659. //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
  660. if (!this.multiple && this.$element.find('option:selected').length === 0 && !this.options.title) {
  661. this.$element.find('option').eq(0).prop('selected', true).attr('selected', 'selected');
  662. }
  663. return _li.join('');
  664. },
  665. findLis: function () {
  666. if (this.$lis == null) this.$lis = this.$menu.find('li');
  667. return this.$lis;
  668. },
  669. /**
  670. * @param [updateLi] defaults to true
  671. */
  672. render: function (updateLi) {
  673. var that = this,
  674. notDisabled,
  675. $selectOptions = this.$element.find('option');
  676. //Update the LI to match the SELECT
  677. if (updateLi !== false) {
  678. $selectOptions.each(function (index) {
  679. var $lis = that.findLis().eq(that.liObj[index]);
  680. that.setDisabled(index, this.disabled || this.parentNode.tagName === 'OPTGROUP' && this.parentNode.disabled, $lis);
  681. that.setSelected(index, this.selected, $lis);
  682. });
  683. }
  684. this.togglePlaceholder();
  685. this.tabIndex();
  686. var selectedItems = $selectOptions.map(function () {
  687. if (this.selected) {
  688. if (that.options.hideDisabled && (this.disabled || this.parentNode.tagName === 'OPTGROUP' && this.parentNode.disabled)) return;
  689. var $this = $(this),
  690. icon = $this.data('icon') && that.options.showIcon ? '<i class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></i> ' : '',
  691. subtext;
  692. if (that.options.showSubtext && $this.data('subtext') && !that.multiple) {
  693. subtext = ' <small class="text-muted">' + $this.data('subtext') + '</small>';
  694. } else {
  695. subtext = '';
  696. }
  697. if (typeof $this.attr('title') !== 'undefined') {
  698. return $this.attr('title');
  699. } else if ($this.data('content') && that.options.showContent) {
  700. return $this.data('content').toString();
  701. } else {
  702. return icon + $this.html() + subtext;
  703. }
  704. }
  705. }).toArray();
  706. //Fixes issue in IE10 occurring when no default option is selected and at least one option is disabled
  707. //Convert all the values into a comma delimited string
  708. var title = !this.multiple ? selectedItems[0] : selectedItems.join(this.options.multipleSeparator);
  709. //If this is multi select, and the selectText type is count, the show 1 of 2 selected etc..
  710. if (this.multiple && this.options.selectedTextFormat.indexOf('count') > -1) {
  711. var max = this.options.selectedTextFormat.split('>');
  712. if ((max.length > 1 && selectedItems.length > max[1]) || (max.length == 1 && selectedItems.length >= 2)) {
  713. notDisabled = this.options.hideDisabled ? ', [disabled]' : '';
  714. var totalCount = $selectOptions.not('[data-divider="true"], [data-hidden="true"]' + notDisabled).length,
  715. tr8nText = (typeof this.options.countSelectedText === 'function') ? this.options.countSelectedText(selectedItems.length, totalCount) : this.options.countSelectedText;
  716. title = tr8nText.replace('{0}', selectedItems.length.toString()).replace('{1}', totalCount.toString());
  717. }
  718. }
  719. if (this.options.title == undefined) {
  720. this.options.title = this.$element.attr('title');
  721. }
  722. if (this.options.selectedTextFormat == 'static') {
  723. title = this.options.title;
  724. }
  725. //If we dont have a title, then use the default, or if nothing is set at all, use the not selected text
  726. if (!title) {
  727. title = typeof this.options.title !== 'undefined' ? this.options.title : this.options.noneSelectedText;
  728. }
  729. //strip all HTML tags and trim the result, then unescape any escaped tags
  730. this.$button.attr('title', htmlUnescape($.trim(title.replace(/<[^>]*>?/g, ''))));
  731. this.$button.children('.filter-option').html(title);
  732. this.$element.trigger('rendered.bs.select');
  733. },
  734. /**
  735. * @param [style]
  736. * @param [status]
  737. */
  738. setStyle: function (style, status) {
  739. if (this.$element.attr('class')) {
  740. this.$newElement.addClass(this.$element.attr('class').replace(/selectpicker|mobile-device|bs-select-hidden|validate\[.*\]/gi, ''));
  741. }
  742. var buttonClass = style ? style : this.options.style;
  743. if (status == 'add') {
  744. this.$button.addClass(buttonClass);
  745. } else if (status == 'remove') {
  746. this.$button.removeClass(buttonClass);
  747. } else {
  748. this.$button.removeClass(this.options.style);
  749. this.$button.addClass(buttonClass);
  750. }
  751. },
  752. liHeight: function (refresh) {
  753. if (!refresh && (this.options.size === false || this.sizeInfo)) return;
  754. var newElement = document.createElement('div'),
  755. menu = document.createElement('div'),
  756. menuInner = document.createElement('ul'),
  757. divider = document.createElement('li'),
  758. li = document.createElement('li'),
  759. a = document.createElement('a'),
  760. text = document.createElement('span'),
  761. header = this.options.header && this.$menu.find('.popover-title').length > 0 ? this.$menu.find('.popover-title')[0].cloneNode(true) : null,
  762. search = this.options.liveSearch ? document.createElement('div') : null,
  763. actions = this.options.actionsBox && this.multiple && this.$menu.find('.bs-actionsbox').length > 0 ? this.$menu.find('.bs-actionsbox')[0].cloneNode(true) : null,
  764. doneButton = this.options.doneButton && this.multiple && this.$menu.find('.bs-donebutton').length > 0 ? this.$menu.find('.bs-donebutton')[0].cloneNode(true) : null;
  765. text.className = 'text';
  766. newElement.className = this.$menu[0].parentNode.className + ' open';
  767. menu.className = 'dropdown-menu open';
  768. menuInner.className = 'dropdown-menu inner';
  769. divider.className = 'divider';
  770. text.appendChild(document.createTextNode('Inner text'));
  771. a.appendChild(text);
  772. li.appendChild(a);
  773. menuInner.appendChild(li);
  774. menuInner.appendChild(divider);
  775. if (header) menu.appendChild(header);
  776. if (search) {
  777. var input = document.createElement('input');
  778. search.className = 'bs-searchbox';
  779. input.className = 'form-control';
  780. search.appendChild(input);
  781. menu.appendChild(search);
  782. }
  783. if (actions) menu.appendChild(actions);
  784. menu.appendChild(menuInner);
  785. if (doneButton) menu.appendChild(doneButton);
  786. newElement.appendChild(menu);
  787. document.body.appendChild(newElement);
  788. var liHeight = a.offsetHeight,
  789. headerHeight = header ? header.offsetHeight : 0,
  790. searchHeight = search ? search.offsetHeight : 0,
  791. actionsHeight = actions ? actions.offsetHeight : 0,
  792. doneButtonHeight = doneButton ? doneButton.offsetHeight : 0,
  793. dividerHeight = $(divider).outerHeight(true),
  794. // fall back to jQuery if getComputedStyle is not supported
  795. menuStyle = typeof getComputedStyle === 'function' ? getComputedStyle(menu) : false,
  796. $menu = menuStyle ? null : $(menu),
  797. menuPadding = {
  798. vert: parseInt(menuStyle ? menuStyle.paddingTop : $menu.css('paddingTop')) +
  799. parseInt(menuStyle ? menuStyle.paddingBottom : $menu.css('paddingBottom')) +
  800. parseInt(menuStyle ? menuStyle.borderTopWidth : $menu.css('borderTopWidth')) +
  801. parseInt(menuStyle ? menuStyle.borderBottomWidth : $menu.css('borderBottomWidth')),
  802. horiz: parseInt(menuStyle ? menuStyle.paddingLeft : $menu.css('paddingLeft')) +
  803. parseInt(menuStyle ? menuStyle.paddingRight : $menu.css('paddingRight')) +
  804. parseInt(menuStyle ? menuStyle.borderLeftWidth : $menu.css('borderLeftWidth')) +
  805. parseInt(menuStyle ? menuStyle.borderRightWidth : $menu.css('borderRightWidth'))
  806. },
  807. menuExtras = {
  808. vert: menuPadding.vert +
  809. parseInt(menuStyle ? menuStyle.marginTop : $menu.css('marginTop')) +
  810. parseInt(menuStyle ? menuStyle.marginBottom : $menu.css('marginBottom')) + 2,
  811. horiz: menuPadding.horiz +
  812. parseInt(menuStyle ? menuStyle.marginLeft : $menu.css('marginLeft')) +
  813. parseInt(menuStyle ? menuStyle.marginRight : $menu.css('marginRight')) + 2
  814. }
  815. document.body.removeChild(newElement);
  816. this.sizeInfo = {
  817. liHeight: liHeight,
  818. headerHeight: headerHeight,
  819. searchHeight: searchHeight,
  820. actionsHeight: actionsHeight,
  821. doneButtonHeight: doneButtonHeight,
  822. dividerHeight: dividerHeight,
  823. menuPadding: menuPadding,
  824. menuExtras: menuExtras
  825. };
  826. },
  827. setSize: function () {
  828. this.findLis();
  829. this.liHeight();
  830. if (this.options.header) this.$menu.css('padding-top', 0);
  831. if (this.options.size === false) return;
  832. var that = this,
  833. $menu = this.$menu,
  834. $menuInner = this.$menuInner,
  835. $window = $(window),
  836. selectHeight = this.$newElement[0].offsetHeight,
  837. selectWidth = this.$newElement[0].offsetWidth,
  838. liHeight = this.sizeInfo['liHeight'],
  839. headerHeight = this.sizeInfo['headerHeight'],
  840. searchHeight = this.sizeInfo['searchHeight'],
  841. actionsHeight = this.sizeInfo['actionsHeight'],
  842. doneButtonHeight = this.sizeInfo['doneButtonHeight'],
  843. divHeight = this.sizeInfo['dividerHeight'],
  844. menuPadding = this.sizeInfo['menuPadding'],
  845. menuExtras = this.sizeInfo['menuExtras'],
  846. notDisabled = this.options.hideDisabled ? '.disabled' : '',
  847. menuHeight,
  848. menuWidth,
  849. getHeight,
  850. getWidth,
  851. selectOffsetTop,
  852. selectOffsetBot,
  853. selectOffsetLeft,
  854. selectOffsetRight,
  855. getPos = function() {
  856. var pos = that.$newElement.offset(),
  857. $container = $(that.options.container),
  858. containerPos;
  859. if (that.options.container && !$container.is('body')) {
  860. containerPos = $container.offset();
  861. containerPos.top += parseInt($container.css('borderTopWidth'));
  862. containerPos.left += parseInt($container.css('borderLeftWidth'));
  863. } else {
  864. containerPos = { top: 0, left: 0 };
  865. }
  866. var winPad = that.options.windowPadding;
  867. selectOffsetTop = pos.top - containerPos.top - $window.scrollTop();
  868. selectOffsetBot = $window.height() - selectOffsetTop - selectHeight - containerPos.top - winPad[2];
  869. selectOffsetLeft = pos.left - containerPos.left - $window.scrollLeft();
  870. selectOffsetRight = $window.width() - selectOffsetLeft - selectWidth - containerPos.left - winPad[1];
  871. selectOffsetTop -= winPad[0];
  872. selectOffsetLeft -= winPad[3];
  873. };
  874. getPos();
  875. if (this.options.size === 'auto') {
  876. var getSize = function () {
  877. var minHeight,
  878. hasClass = function (className, include) {
  879. return function (element) {
  880. if (include) {
  881. return (element.classList ? element.classList.contains(className) : $(element).hasClass(className));
  882. } else {
  883. return !(element.classList ? element.classList.contains(className) : $(element).hasClass(className));
  884. }
  885. };
  886. },
  887. lis = that.$menuInner[0].getElementsByTagName('li'),
  888. lisVisible = Array.prototype.filter ? Array.prototype.filter.call(lis, hasClass('hidden', false)) : that.$lis.not('.hidden'),
  889. optGroup = Array.prototype.filter ? Array.prototype.filter.call(lisVisible, hasClass('dropdown-header', true)) : lisVisible.filter('.dropdown-header');
  890. getPos();
  891. menuHeight = selectOffsetBot - menuExtras.vert;
  892. menuWidth = selectOffsetRight - menuExtras.horiz;
  893. if (that.options.container) {
  894. if (!$menu.data('height')) $menu.data('height', $menu.height());
  895. getHeight = $menu.data('height');
  896. if (!$menu.data('width')) $menu.data('width', $menu.width());
  897. getWidth = $menu.data('width');
  898. } else {
  899. getHeight = $menu.height();
  900. getWidth = $menu.width();
  901. }
  902. if (that.options.dropupAuto) {
  903. that.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && (menuHeight - menuExtras.vert) < getHeight);
  904. }
  905. if (that.$newElement.hasClass('dropup')) {
  906. menuHeight = selectOffsetTop - menuExtras.vert;
  907. }
  908. if (that.options.dropdownAlignRight === 'auto') {
  909. $menu.toggleClass('dropdown-menu-right', selectOffsetLeft > selectOffsetRight && (menuWidth - menuExtras.horiz) < (getWidth - selectWidth));
  910. }
  911. if ((lisVisible.length + optGroup.length) > 3) {
  912. minHeight = liHeight * 3 + menuExtras.vert - 2;
  913. } else {
  914. minHeight = 0;
  915. }
  916. $menu.css({
  917. 'max-height': menuHeight + 'px',
  918. 'overflow': 'hidden',
  919. 'min-height': minHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px'
  920. });
  921. $menuInner.css({
  922. 'max-height': menuHeight - headerHeight - searchHeight - actionsHeight - doneButtonHeight - menuPadding.vert + 'px',
  923. 'overflow-y': 'auto',
  924. 'min-height': Math.max(minHeight - menuPadding.vert, 0) + 'px'
  925. });
  926. };
  927. getSize();
  928. this.$searchbox.off('input.getSize propertychange.getSize').on('input.getSize propertychange.getSize', getSize);
  929. $window.off('resize.getSize scroll.getSize').on('resize.getSize scroll.getSize', getSize);
  930. } else if (this.options.size && this.options.size != 'auto' && this.$lis.not(notDisabled).length > this.options.size) {
  931. var optIndex = this.$lis.not('.divider').not(notDisabled).children().slice(0, this.options.size).last().parent().index(),
  932. divLength = this.$lis.slice(0, optIndex + 1).filter('.divider').length;
  933. menuHeight = liHeight * this.options.size + divLength * divHeight + menuPadding.vert;
  934. if (that.options.container) {
  935. if (!$menu.data('height')) $menu.data('height', $menu.height());
  936. getHeight = $menu.data('height');
  937. } else {
  938. getHeight = $menu.height();
  939. }
  940. if (that.options.dropupAuto) {
  941. //noinspection JSUnusedAssignment
  942. this.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && (menuHeight - menuExtras.vert) < getHeight);
  943. }
  944. $menu.css({
  945. 'max-height': menuHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px',
  946. 'overflow': 'hidden',
  947. 'min-height': ''
  948. });
  949. $menuInner.css({
  950. 'max-height': menuHeight - menuPadding.vert + 'px',
  951. 'overflow-y': 'auto',
  952. 'min-height': ''
  953. });
  954. }
  955. },
  956. setWidth: function () {
  957. if (this.options.width === 'auto') {
  958. this.$menu.css('min-width', '0');
  959. // Get correct width if element is hidden
  960. var $selectClone = this.$menu.parent().clone().appendTo('body'),
  961. $selectClone2 = this.options.container ? this.$newElement.clone().appendTo('body') : $selectClone,
  962. ulWidth = $selectClone.children('.dropdown-menu').outerWidth(),
  963. btnWidth = $selectClone2.css('width', 'auto').children('button').outerWidth();
  964. $selectClone.remove();
  965. $selectClone2.remove();
  966. // Set width to whatever's larger, button title or longest option
  967. this.$newElement.css('width', Math.max(ulWidth, btnWidth) + 'px');
  968. } else if (this.options.width === 'fit') {
  969. // Remove inline min-width so width can be changed from 'auto'
  970. this.$menu.css('min-width', '');
  971. this.$newElement.css('width', '').addClass('fit-width');
  972. } else if (this.options.width) {
  973. // Remove inline min-width so width can be changed from 'auto'
  974. this.$menu.css('min-width', '');
  975. this.$newElement.css('width', this.options.width);
  976. } else {
  977. // Remove inline min-width/width so width can be changed
  978. this.$menu.css('min-width', '');
  979. this.$newElement.css('width', '');
  980. }
  981. // Remove fit-width class if width is changed programmatically
  982. if (this.$newElement.hasClass('fit-width') && this.options.width !== 'fit') {
  983. this.$newElement.removeClass('fit-width');
  984. }
  985. },
  986. selectPosition: function () {
  987. this.$bsContainer = $('<div class="bs-container" />');
  988. var that = this,
  989. $container = $(this.options.container),
  990. pos,
  991. containerPos,
  992. actualHeight,
  993. getPlacement = function ($element) {
  994. that.$bsContainer.addClass($element.attr('class').replace(/form-control|fit-width/gi, '')).toggleClass('dropup', $element.hasClass('dropup'));
  995. pos = $element.offset();
  996. if (!$container.is('body')) {
  997. containerPos = $container.offset();
  998. containerPos.top += parseInt($container.css('borderTopWidth')) - $container.scrollTop();
  999. containerPos.left += parseInt($container.css('borderLeftWidth')) - $container.scrollLeft();
  1000. } else {
  1001. containerPos = { top: 0, left: 0 };
  1002. }
  1003. actualHeight = $element.hasClass('dropup') ? 0 : $element[0].offsetHeight;
  1004. that.$bsContainer.css({
  1005. 'top': pos.top - containerPos.top + actualHeight,
  1006. 'left': pos.left - containerPos.left,
  1007. 'width': $element[0].offsetWidth
  1008. });
  1009. };
  1010. this.$button.on('click', function () {
  1011. var $this = $(this);
  1012. if (that.isDisabled()) {
  1013. return;
  1014. }
  1015. getPlacement(that.$newElement);
  1016. that.$bsContainer
  1017. .appendTo(that.options.container)
  1018. .toggleClass('open', !$this.hasClass('open'))
  1019. .append(that.$menu);
  1020. });
  1021. $(window).on('resize scroll', function () {
  1022. getPlacement(that.$newElement);
  1023. });
  1024. this.$element.on('hide.bs.select', function () {
  1025. that.$menu.data('height', that.$menu.height());
  1026. that.$bsContainer.detach();
  1027. });
  1028. },
  1029. /**
  1030. * @param {number} index - the index of the option that is being changed
  1031. * @param {boolean} selected - true if the option is being selected, false if being deselected
  1032. * @param {JQuery} $lis - the 'li' element that is being modified
  1033. */
  1034. setSelected: function (index, selected, $lis) {
  1035. if (!$lis) {
  1036. this.togglePlaceholder(); // check if setSelected is being called by changing the value of the select
  1037. $lis = this.findLis().eq(this.liObj[index]);
  1038. }
  1039. $lis.toggleClass('selected', selected).find('a').attr('aria-selected', selected);
  1040. },
  1041. /**
  1042. * @param {number} index - the index of the option that is being disabled
  1043. * @param {boolean} disabled - true if the option is being disabled, false if being enabled
  1044. * @param {JQuery} $lis - the 'li' element that is being modified
  1045. */
  1046. setDisabled: function (index, disabled, $lis) {
  1047. if (!$lis) {
  1048. $lis = this.findLis().eq(this.liObj[index]);
  1049. }
  1050. if (disabled) {
  1051. $lis.addClass('disabled').children('a').attr('href', '#').attr('tabindex', -1).attr('aria-disabled', true);
  1052. } else {
  1053. $lis.removeClass('disabled').children('a').removeAttr('href').attr('tabindex', 0).attr('aria-disabled', false);
  1054. }
  1055. },
  1056. isDisabled: function () {
  1057. return this.$element[0].disabled;
  1058. },
  1059. checkDisabled: function () {
  1060. var that = this;
  1061. if (this.isDisabled()) {
  1062. this.$newElement.addClass('disabled');
  1063. this.$button.addClass('disabled').attr('tabindex', -1).attr('aria-disabled', true);
  1064. } else {
  1065. if (this.$button.hasClass('disabled')) {
  1066. this.$newElement.removeClass('disabled');
  1067. this.$button.removeClass('disabled').attr('aria-disabled', false);
  1068. }
  1069. if (this.$button.attr('tabindex') == -1 && !this.$element.data('tabindex')) {
  1070. this.$button.removeAttr('tabindex');
  1071. }
  1072. }
  1073. this.$button.click(function () {
  1074. return !that.isDisabled();
  1075. });
  1076. },
  1077. togglePlaceholder: function () {
  1078. var value = this.$element.val();
  1079. this.$button.toggleClass('bs-placeholder', value === null || value === '' || (value.constructor === Array && value.length === 0));
  1080. },
  1081. tabIndex: function () {
  1082. if (this.$element.data('tabindex') !== this.$element.attr('tabindex') &&
  1083. (this.$element.attr('tabindex') !== -98 && this.$element.attr('tabindex') !== '-98')) {
  1084. this.$element.data('tabindex', this.$element.attr('tabindex'));
  1085. this.$button.attr('tabindex', this.$element.data('tabindex'));
  1086. }
  1087. this.$element.attr('tabindex', -98);
  1088. },
  1089. clickListener: function () {
  1090. var that = this,
  1091. $document = $(document);
  1092. $document.data('spaceSelect', false);
  1093. this.$button.on('keyup', function (e) {
  1094. if (/(32)/.test(e.keyCode.toString(10)) && $document.data('spaceSelect')) {
  1095. e.preventDefault();
  1096. $document.data('spaceSelect', false);
  1097. }
  1098. });
  1099. this.$button.on('click', function () {
  1100. that.setSize();
  1101. });
  1102. this.$element.on('shown.bs.select', function () {
  1103. if (!that.options.liveSearch && !that.multiple) {
  1104. that.$menuInner.find('.selected a').focus();
  1105. } else if (!that.multiple) {
  1106. var selectedIndex = that.liObj[that.$element[0].selectedIndex];
  1107. if (typeof selectedIndex !== 'number' || that.options.size === false) return;
  1108. // scroll to selected option
  1109. var offset = that.$lis.eq(selectedIndex)[0].offsetTop - that.$menuInner[0].offsetTop;
  1110. offset = offset - that.$menuInner[0].offsetHeight/2 + that.sizeInfo.liHeight/2;
  1111. that.$menuInner[0].scrollTop = offset;
  1112. }
  1113. });
  1114. this.$menuInner.on('click', 'li a', function (e) {
  1115. var $this = $(this),
  1116. clickedIndex = $this.parent().data('originalIndex'),
  1117. prevValue = that.$element.val(),
  1118. prevIndex = that.$element.prop('selectedIndex'),
  1119. triggerChange = true;
  1120. // Don't close on multi choice menu
  1121. if (that.multiple && that.options.maxOptions !== 1) {
  1122. e.stopPropagation();
  1123. }
  1124. e.preventDefault();
  1125. //Don't run if we have been disabled
  1126. if (!that.isDisabled() && !$this.parent().hasClass('disabled')) {
  1127. var $options = that.$element.find('option'),
  1128. $option = $options.eq(clickedIndex),
  1129. state = $option.prop('selected'),
  1130. $optgroup = $option.parent('optgroup'),
  1131. maxOptions = that.options.maxOptions,
  1132. maxOptionsGrp = $optgroup.data('maxOptions') || false;
  1133. if (!that.multiple) { // Deselect all others if not multi select box
  1134. $options.prop('selected', false);
  1135. $option.prop('selected', true);
  1136. that.$menuInner.find('.selected').removeClass('selected').find('a').attr('aria-selected', false);
  1137. that.setSelected(clickedIndex, true);
  1138. } else { // Toggle the one we have chosen if we are multi select.
  1139. $option.prop('selected', !state);
  1140. that.setSelected(clickedIndex, !state);
  1141. $this.blur();
  1142. if (maxOptions !== false || maxOptionsGrp !== false) {
  1143. var maxReached = maxOptions < $options.filter(':selected').length,
  1144. maxReachedGrp = maxOptionsGrp < $optgroup.find('option:selected').length;
  1145. if ((maxOptions && maxReached) || (maxOptionsGrp && maxReachedGrp)) {
  1146. if (maxOptions && maxOptions == 1) {
  1147. $options.prop('selected', false);
  1148. $option.prop('selected', true);
  1149. that.$menuInner.find('.selected').removeClass('selected');
  1150. that.setSelected(clickedIndex, true);
  1151. } else if (maxOptionsGrp && maxOptionsGrp == 1) {
  1152. $optgroup.find('option:selected').prop('selected', false);
  1153. $option.prop('selected', true);
  1154. var optgroupID = $this.parent().data('optgroup');
  1155. that.$menuInner.find('[data-optgroup="' + optgroupID + '"]').removeClass('selected');
  1156. that.setSelected(clickedIndex, true);
  1157. } else {
  1158. var maxOptionsText = typeof that.options.maxOptionsText === 'string' ? [that.options.maxOptionsText, that.options.maxOptionsText] : that.options.maxOptionsText,
  1159. maxOptionsArr = typeof maxOptionsText === 'function' ? maxOptionsText(maxOptions, maxOptionsGrp) : maxOptionsText,
  1160. maxTxt = maxOptionsArr[0].replace('{n}', maxOptions),
  1161. maxTxtGrp = maxOptionsArr[1].replace('{n}', maxOptionsGrp),
  1162. $notify = $('<div class="notify"></div>');
  1163. // If {var} is set in array, replace it
  1164. /** @deprecated */
  1165. if (maxOptionsArr[2]) {
  1166. maxTxt = maxTxt.replace('{var}', maxOptionsArr[2][maxOptions > 1 ? 0 : 1]);
  1167. maxTxtGrp = maxTxtGrp.replace('{var}', maxOptionsArr[2][maxOptionsGrp > 1 ? 0 : 1]);
  1168. }
  1169. $option.prop('selected', false);
  1170. that.$menu.append($notify);
  1171. if (maxOptions && maxReached) {
  1172. $notify.append($('<div>' + maxTxt + '</div>'));
  1173. triggerChange = false;
  1174. that.$element.trigger('maxReached.bs.select');
  1175. }
  1176. if (maxOptionsGrp && maxReachedGrp) {
  1177. $notify.append($('<div>' + maxTxtGrp + '</div>'));
  1178. triggerChange = false;
  1179. that.$element.trigger('maxReachedGrp.bs.select');
  1180. }
  1181. setTimeout(function () {
  1182. that.setSelected(clickedIndex, false);
  1183. }, 10);
  1184. $notify.delay(750).fadeOut(300, function () {
  1185. $(this).remove();
  1186. });
  1187. }
  1188. }
  1189. }
  1190. }
  1191. if (!that.multiple || (that.multiple && that.options.maxOptions === 1)) {
  1192. that.$button.focus();
  1193. } else if (that.options.liveSearch) {
  1194. that.$searchbox.focus();
  1195. }
  1196. // Trigger select 'change'
  1197. if (triggerChange) {
  1198. if ((prevValue != that.$element.val() && that.multiple) || (prevIndex != that.$element.prop('selectedIndex') && !that.multiple)) {
  1199. // $option.prop('selected') is current option state (selected/unselected). state is previous option state.
  1200. changed_arguments = [clickedIndex, $option.prop('selected'), state];
  1201. that.$element
  1202. .triggerNative('change');
  1203. }
  1204. }
  1205. }
  1206. });
  1207. this.$menu.on('click', 'li.disabled a, .popover-title, .popover-title :not(.close)', function (e) {
  1208. if (e.currentTarget == this) {
  1209. e.preventDefault();
  1210. e.stopPropagation();
  1211. if (that.options.liveSearch && !$(e.target).hasClass('close')) {
  1212. that.$searchbox.focus();
  1213. } else {
  1214. that.$button.focus();
  1215. }
  1216. }
  1217. });
  1218. this.$menuInner.on('click', '.divider, .dropdown-header', function (e) {
  1219. e.preventDefault();
  1220. e.stopPropagation();
  1221. if (that.options.liveSearch) {
  1222. that.$searchbox.focus();
  1223. } else {
  1224. that.$button.focus();
  1225. }
  1226. });
  1227. this.$menu.on('click', '.popover-title .close', function () {
  1228. that.$button.click();
  1229. });
  1230. this.$searchbox.on('click', function (e) {
  1231. e.stopPropagation();
  1232. });
  1233. this.$menu.on('click', '.actions-btn', function (e) {
  1234. if (that.options.liveSearch) {
  1235. that.$searchbox.focus();
  1236. } else {
  1237. that.$button.focus();
  1238. }
  1239. e.preventDefault();
  1240. e.stopPropagation();
  1241. if ($(this).hasClass('bs-select-all')) {
  1242. that.selectAll();
  1243. } else {
  1244. that.deselectAll();
  1245. }
  1246. });
  1247. this.$element.change(function () {
  1248. that.render(false);
  1249. that.$element.trigger('changed.bs.select', changed_arguments);
  1250. changed_arguments = null;
  1251. });
  1252. },
  1253. liveSearchListener: function () {
  1254. var that = this,
  1255. $no_results = $('<li class="no-results"></li>');
  1256. this.$button.on('click.dropdown.data-api', function () {
  1257. that.$menuInner.find('.active').removeClass('active');
  1258. if (!!that.$searchbox.val()) {
  1259. that.$searchbox.val('');
  1260. that.$lis.not('.is-hidden').removeClass('hidden');
  1261. if (!!$no_results.parent().length) $no_results.remove();
  1262. }
  1263. if (!that.multiple) that.$menuInner.find('.selected').addClass('active');
  1264. setTimeout(function () {
  1265. that.$searchbox.focus();
  1266. }, 10);
  1267. });
  1268. this.$searchbox.on('click.dropdown.data-api focus.dropdown.data-api touchend.dropdown.data-api', function (e) {
  1269. e.stopPropagation();
  1270. });
  1271. this.$searchbox.on('input propertychange', function () {
  1272. that.$lis.not('.is-hidden').removeClass('hidden');
  1273. that.$lis.filter('.active').removeClass('active');
  1274. $no_results.remove();
  1275. if (that.$searchbox.val()) {
  1276. var $searchBase = that.$lis.not('.is-hidden, .divider, .dropdown-header'),
  1277. $hideItems;
  1278. if (that.options.liveSearchNormalize) {
  1279. $hideItems = $searchBase.not(':a' + that._searchStyle() + '("' + normalizeToBase(that.$searchbox.val()) + '")');
  1280. } else {
  1281. $hideItems = $searchBase.not(':' + that._searchStyle() + '("' + that.$searchbox.val() + '")');
  1282. }
  1283. if ($hideItems.length === $searchBase.length) {
  1284. $no_results.html(that.options.noneResultsText.replace('{0}', '"' + htmlEscape(that.$searchbox.val()) + '"'));
  1285. that.$menuInner.append($no_results);
  1286. that.$lis.addClass('hidden');
  1287. } else {
  1288. $hideItems.addClass('hidden');
  1289. var $lisVisible = that.$lis.not('.hidden'),
  1290. $foundDiv;
  1291. // hide divider if first or last visible, or if followed by another divider
  1292. $lisVisible.each(function (index) {
  1293. var $this = $(this);
  1294. if ($this.hasClass('divider')) {
  1295. if ($foundDiv === undefined) {
  1296. $this.addClass('hidden');
  1297. } else {
  1298. if ($foundDiv) $foundDiv.addClass('hidden');
  1299. $foundDiv = $this;
  1300. }
  1301. } else if ($this.hasClass('dropdown-header') && $lisVisible.eq(index + 1).data('optgroup') !== $this.data('optgroup')) {
  1302. $this.addClass('hidden');
  1303. } else {
  1304. $foundDiv = null;
  1305. }
  1306. });
  1307. if ($foundDiv) $foundDiv.addClass('hidden');
  1308. $searchBase.not('.hidden').first().addClass('active');
  1309. that.$menuInner.scrollTop(0);
  1310. }
  1311. }
  1312. });
  1313. },
  1314. _searchStyle: function () {
  1315. var styles = {
  1316. begins: 'ibegins',
  1317. startsWith: 'ibegins'
  1318. };
  1319. return styles[this.options.liveSearchStyle] || 'icontains';
  1320. },
  1321. val: function (value) {
  1322. if (typeof value !== 'undefined') {
  1323. this.$element.val(value);
  1324. this.render();
  1325. return this.$element;
  1326. } else {
  1327. return this.$element.val();
  1328. }
  1329. },
  1330. changeAll: function (status) {
  1331. if (!this.multiple) return;
  1332. if (typeof status === 'undefined') status = true;
  1333. this.findLis();
  1334. var $options = this.$element.find('option'),
  1335. $lisVisible = this.$lis.not('.divider, .dropdown-header, .disabled, .hidden'),
  1336. lisVisLen = $lisVisible.length,
  1337. selectedOptions = [];
  1338. if (status) {
  1339. if ($lisVisible.filter('.selected').length === $lisVisible.length) return;
  1340. } else {
  1341. if ($lisVisible.filter('.selected').length === 0) return;
  1342. }
  1343. $lisVisible.toggleClass('selected', status);
  1344. for (var i = 0; i < lisVisLen; i++) {
  1345. var origIndex = $lisVisible[i].getAttribute('data-original-index');
  1346. selectedOptions[selectedOptions.length] = $options.eq(origIndex)[0];
  1347. }
  1348. $(selectedOptions).prop('selected', status);
  1349. this.render(false);
  1350. this.togglePlaceholder();
  1351. this.$element
  1352. .triggerNative('change');
  1353. },
  1354. selectAll: function () {
  1355. return this.changeAll(true);
  1356. },
  1357. deselectAll: function () {
  1358. return this.changeAll(false);
  1359. },
  1360. toggle: function (e) {
  1361. e = e || window.event;
  1362. if (e) e.stopPropagation();
  1363. this.$button.trigger('click');
  1364. },
  1365. keydown: function (e) {
  1366. var $this = $(this),
  1367. $parent = $this.is('input') ? $this.parent().parent() : $this.parent(),
  1368. $items,
  1369. that = $parent.data('this'),
  1370. index,
  1371. prevIndex,
  1372. isActive,
  1373. selector = ':not(.disabled, .hidden, .dropdown-header, .divider)',
  1374. keyCodeMap = {
  1375. 32: ' ',
  1376. 48: '0',
  1377. 49: '1',
  1378. 50: '2',
  1379. 51: '3',
  1380. 52: '4',
  1381. 53: '5',
  1382. 54: '6',
  1383. 55: '7',
  1384. 56: '8',
  1385. 57: '9',
  1386. 59: ';',
  1387. 65: 'a',
  1388. 66: 'b',
  1389. 67: 'c',
  1390. 68: 'd',
  1391. 69: 'e',
  1392. 70: 'f',
  1393. 71: 'g',
  1394. 72: 'h',
  1395. 73: 'i',
  1396. 74: 'j',
  1397. 75: 'k',
  1398. 76: 'l',
  1399. 77: 'm',
  1400. 78: 'n',
  1401. 79: 'o',
  1402. 80: 'p',
  1403. 81: 'q',
  1404. 82: 'r',
  1405. 83: 's',
  1406. 84: 't',
  1407. 85: 'u',
  1408. 86: 'v',
  1409. 87: 'w',
  1410. 88: 'x',
  1411. 89: 'y',
  1412. 90: 'z',
  1413. 96: '0',
  1414. 97: '1',
  1415. 98: '2',
  1416. 99: '3',
  1417. 100: '4',
  1418. 101: '5',
  1419. 102: '6',
  1420. 103: '7',
  1421. 104: '8',
  1422. 105: '9'
  1423. };
  1424. isActive = that.$newElement.hasClass('open');
  1425. if (!isActive && (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105 || e.keyCode >= 65 && e.keyCode <= 90)) {
  1426. if (!that.options.container) {
  1427. that.setSize();
  1428. that.$menu.parent().addClass('open');
  1429. isActive = true;
  1430. } else {
  1431. that.$button.trigger('click');
  1432. }
  1433. that.$searchbox.focus();
  1434. return;
  1435. }
  1436. if (that.options.liveSearch) {
  1437. if (/(^9$|27)/.test(e.keyCode.toString(10)) && isActive) {
  1438. e.preventDefault();
  1439. e.stopPropagation();
  1440. that.$menuInner.click();
  1441. that.$button.focus();
  1442. }
  1443. }
  1444. if (/(38|40)/.test(e.keyCode.toString(10))) {
  1445. $items = that.$lis.filter(selector);
  1446. if (!$items.length) return;
  1447. if (!that.options.liveSearch) {
  1448. index = $items.index($items.find('a').filter(':focus').parent());
  1449. } else {
  1450. index = $items.index($items.filter('.active'));
  1451. }
  1452. prevIndex = that.$menuInner.data('prevIndex');
  1453. if (e.keyCode == 38) {
  1454. if ((that.options.liveSearch || index == prevIndex) && index != -1) index--;
  1455. if (index < 0) index += $items.length;
  1456. } else if (e.keyCode == 40) {
  1457. if (that.options.liveSearch || index == prevIndex) index++;
  1458. index = index % $items.length;
  1459. }
  1460. that.$menuInner.data('prevIndex', index);
  1461. if (!that.options.liveSearch) {
  1462. $items.eq(index).children('a').focus();
  1463. } else {
  1464. e.preventDefault();
  1465. if (!$this.hasClass('dropdown-toggle')) {
  1466. $items.removeClass('active').eq(index).addClass('active').children('a').focus();
  1467. $this.focus();
  1468. }
  1469. }
  1470. } else if (!$this.is('input')) {
  1471. var keyIndex = [],
  1472. count,
  1473. prevKey;
  1474. $items = that.$lis.filter(selector);
  1475. $items.each(function (i) {
  1476. if ($.trim($(this).children('a').text().toLowerCase()).substring(0, 1) == keyCodeMap[e.keyCode]) {
  1477. keyIndex.push(i);
  1478. }
  1479. });
  1480. count = $(document).data('keycount');
  1481. count++;
  1482. $(document).data('keycount', count);
  1483. prevKey = $.trim($(':focus').text().toLowerCase()).substring(0, 1);
  1484. if (prevKey != keyCodeMap[e.keyCode]) {
  1485. count = 1;
  1486. $(document).data('keycount', count);
  1487. } else if (count >= keyIndex.length) {
  1488. $(document).data('keycount', 0);
  1489. if (count > keyIndex.length) count = 1;
  1490. }
  1491. $items.eq(keyIndex[count - 1]).children('a').focus();
  1492. }
  1493. // Select focused option if "Enter", "Spacebar" or "Tab" (when selectOnTab is true) are pressed inside the menu.
  1494. if ((/(13|32)/.test(e.keyCode.toString(10)) || (/(^9$)/.test(e.keyCode.toString(10)) && that.options.selectOnTab)) && isActive) {
  1495. if (!/(32)/.test(e.keyCode.toString(10))) e.preventDefault();
  1496. if (!that.options.liveSearch) {
  1497. var elem = $(':focus');
  1498. elem.click();
  1499. // Bring back focus for multiselects
  1500. elem.focus();
  1501. // Prevent screen from scrolling if the user hit the spacebar
  1502. e.preventDefault();
  1503. // Fixes spacebar selection of dropdown items in FF & IE
  1504. $(document).data('spaceSelect', true);
  1505. } else if (!/(32)/.test(e.keyCode.toString(10))) {
  1506. that.$menuInner.find('.active a').click();
  1507. $this.focus();
  1508. }
  1509. $(document).data('keycount', 0);
  1510. }
  1511. if ((/(^9$|27)/.test(e.keyCode.toString(10)) && isActive && (that.multiple || that.options.liveSearch)) || (/(27)/.test(e.keyCode.toString(10)) && !isActive)) {
  1512. that.$menu.parent().removeClass('open');
  1513. if (that.options.container) that.$newElement.removeClass('open');
  1514. that.$button.focus();
  1515. }
  1516. },
  1517. mobile: function () {
  1518. this.$element.addClass('mobile-device');
  1519. },
  1520. refresh: function () {
  1521. this.$lis = null;
  1522. this.liObj = {};
  1523. this.reloadLi();
  1524. this.render();
  1525. this.checkDisabled();
  1526. this.liHeight(true);
  1527. this.setStyle();
  1528. this.setWidth();
  1529. if (this.$lis) this.$searchbox.trigger('propertychange');
  1530. this.$element.trigger('refreshed.bs.select');
  1531. },
  1532. hide: function () {
  1533. this.$newElement.hide();
  1534. },
  1535. show: function () {
  1536. this.$newElement.show();
  1537. },
  1538. remove: function () {
  1539. this.$newElement.remove();
  1540. this.$element.remove();
  1541. },
  1542. destroy: function () {
  1543. this.$newElement.before(this.$element).remove();
  1544. if (this.$bsContainer) {
  1545. this.$bsContainer.remove();
  1546. } else {
  1547. this.$menu.remove();
  1548. }
  1549. this.$element
  1550. .off('.bs.select')
  1551. .removeData('selectpicker')
  1552. .removeClass('bs-select-hidden selectpicker');
  1553. }
  1554. };
  1555. // SELECTPICKER PLUGIN DEFINITION
  1556. // ==============================
  1557. function Plugin(option) {
  1558. // get the args of the outer function..
  1559. var args = arguments;
  1560. // The arguments of the function are explicitly re-defined from the argument list, because the shift causes them
  1561. // to get lost/corrupted in android 2.3 and IE9 #715 #775
  1562. var _option = option;
  1563. [].shift.apply(args);
  1564. var value;
  1565. var chain = this.each(function () {
  1566. var $this = $(this);
  1567. if ($this.is('select')) {
  1568. var data = $this.data('selectpicker'),
  1569. options = typeof _option == 'object' && _option;
  1570. if (!data) {
  1571. var config = $.extend({}, Selectpicker.DEFAULTS, $.fn.selectpicker.defaults || {}, $this.data(), options);
  1572. config.template = $.extend({}, Selectpicker.DEFAULTS.template, ($.fn.selectpicker.defaults ? $.fn.selectpicker.defaults.template : {}), $this.data().template, options.template);
  1573. $this.data('selectpicker', (data = new Selectpicker(this, config)));
  1574. } else if (options) {
  1575. for (var i in options) {
  1576. if (options.hasOwnProperty(i)) {
  1577. data.options[i] = options[i];
  1578. }
  1579. }
  1580. }
  1581. if (typeof _option == 'string') {
  1582. if (data[_option] instanceof Function) {
  1583. value = data[_option].apply(data, args);
  1584. } else {
  1585. value = data.options[_option];
  1586. }
  1587. }
  1588. }
  1589. });
  1590. if (typeof value !== 'undefined') {
  1591. //noinspection JSUnusedAssignment
  1592. return value;
  1593. } else {
  1594. return chain;
  1595. }
  1596. }
  1597. var old = $.fn.selectpicker;
  1598. $.fn.selectpicker = Plugin;
  1599. $.fn.selectpicker.Constructor = Selectpicker;
  1600. // SELECTPICKER NO CONFLICT
  1601. // ========================
  1602. $.fn.selectpicker.noConflict = function () {
  1603. $.fn.selectpicker = old;
  1604. return this;
  1605. };
  1606. $(document)
  1607. .data('keycount', 0)
  1608. .on('keydown.bs.select', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role="listbox"], .bs-searchbox input', Selectpicker.prototype.keydown)
  1609. .on('focusin.modal', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role="listbox"], .bs-searchbox input', function (e) {
  1610. e.stopPropagation();
  1611. });
  1612. // SELECTPICKER DATA-API
  1613. // =====================
  1614. $(window).on('load.bs.select.data-api', function () {
  1615. $('.selectpicker').each(function () {
  1616. var $selectpicker = $(this);
  1617. Plugin.call($selectpicker, $selectpicker.data());
  1618. })
  1619. });
  1620. })(jQuery);
  1621. }));