bootstrap-select.js 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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. '</span>' +
  483. '</button>' +
  484. '<div class="dropdown-menu open" role="combobox">' +
  485. header +
  486. searchbox +
  487. actionsbox +
  488. '<ul class="dropdown-menu inner" role="listbox" aria-expanded="false">' +
  489. '</ul>' +
  490. donebutton +
  491. '</div>' +
  492. '</div>';
  493. return $(drop);
  494. },
  495. createView: function () {
  496. var $drop = this.createDropdown(),
  497. li = this.createLi();
  498. $drop.find('ul')[0].innerHTML = li;
  499. return $drop;
  500. },
  501. reloadLi: function () {
  502. // rebuild
  503. var li = this.createLi();
  504. this.$menuInner[0].innerHTML = li;
  505. },
  506. createLi: function () {
  507. var that = this,
  508. _li = [],
  509. optID = 0,
  510. titleOption = document.createElement('option'),
  511. liIndex = -1; // increment liIndex whenever a new <li> element is created to ensure liObj is correct
  512. // Helper functions
  513. /**
  514. * @param content
  515. * @param [index]
  516. * @param [classes]
  517. * @param [optgroup]
  518. * @returns {string}
  519. */
  520. var generateLI = function (content, index, classes, optgroup) {
  521. return '<li' +
  522. ((typeof classes !== 'undefined' && '' !== classes) ? ' class="' + classes + '"' : '') +
  523. ((typeof index !== 'undefined' && null !== index) ? ' data-original-index="' + index + '"' : '') +
  524. ((typeof optgroup !== 'undefined' && null !== optgroup) ? 'data-optgroup="' + optgroup + '"' : '') +
  525. '>' + content + '</li>';
  526. };
  527. /**
  528. * @param text
  529. * @param [classes]
  530. * @param [inline]
  531. * @param [tokens]
  532. * @returns {string}
  533. */
  534. var generateA = function (text, classes, inline, tokens) {
  535. return '<a tabindex="0"' +
  536. (typeof classes !== 'undefined' ? ' class="' + classes + '"' : '') +
  537. (inline ? ' style="' + inline + '"' : '') +
  538. (that.options.liveSearchNormalize ? ' data-normalized-text="' + normalizeToBase(htmlEscape($(text).html())) + '"' : '') +
  539. (typeof tokens !== 'undefined' || tokens !== null ? ' data-tokens="' + tokens + '"' : '') +
  540. ' role="option">' + text +
  541. '<span class="' + that.options.iconBase + ' ' + that.options.tickIcon + ' check-mark"></span>' +
  542. '</a>';
  543. };
  544. if (this.options.title && !this.multiple) {
  545. // this option doesn't create a new <li> element, but does add a new option, so liIndex is decreased
  546. // since liObj is recalculated on every refresh, liIndex needs to be decreased even if the titleOption is already appended
  547. liIndex--;
  548. if (!this.$element.find('.bs-title-option').length) {
  549. // Use native JS to prepend option (faster)
  550. var element = this.$element[0];
  551. titleOption.className = 'bs-title-option';
  552. titleOption.innerHTML = this.options.title;
  553. titleOption.value = '';
  554. element.insertBefore(titleOption, element.firstChild);
  555. // Check if selected or data-selected attribute is already set on an option. If not, select the titleOption option.
  556. // the selected item may have been changed by user or programmatically before the bootstrap select plugin runs,
  557. // if so, the select will have the data-selected attribute
  558. var $opt = $(element.options[element.selectedIndex]);
  559. if ($opt.attr('selected') === undefined && this.$element.data('selected') === undefined) {
  560. titleOption.selected = true;
  561. }
  562. }
  563. }
  564. var $selectOptions = this.$element.find('option');
  565. $selectOptions.each(function (index) {
  566. var $this = $(this);
  567. liIndex++;
  568. if ($this.hasClass('bs-title-option')) return;
  569. // Get the class and text for the option
  570. var optionClass = this.className || '',
  571. inline = htmlEscape(this.style.cssText),
  572. text = $this.data('content') ? $this.data('content') : $this.html(),
  573. tokens = $this.data('tokens') ? $this.data('tokens') : null,
  574. subtext = typeof $this.data('subtext') !== 'undefined' ? '<small class="text-muted">' + $this.data('subtext') + '</small>' : '',
  575. icon = typeof $this.data('icon') !== 'undefined' ? '<span class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></span> ' : '',
  576. $parent = $this.parent(),
  577. isOptgroup = $parent[0].tagName === 'OPTGROUP',
  578. isOptgroupDisabled = isOptgroup && $parent[0].disabled,
  579. isDisabled = this.disabled || isOptgroupDisabled,
  580. prevHiddenIndex;
  581. if (icon !== '' && isDisabled) {
  582. icon = '<span>' + icon + '</span>';
  583. }
  584. if (that.options.hideDisabled && (isDisabled && !isOptgroup || isOptgroupDisabled)) {
  585. // set prevHiddenIndex - the index of the first hidden option in a group of hidden options
  586. // used to determine whether or not a divider should be placed after an optgroup if there are
  587. // hidden options between the optgroup and the first visible option
  588. prevHiddenIndex = $this.data('prevHiddenIndex');
  589. $this.next().data('prevHiddenIndex', (prevHiddenIndex !== undefined ? prevHiddenIndex : index));
  590. liIndex--;
  591. return;
  592. }
  593. if (!$this.data('content')) {
  594. // Prepend any icon and append any subtext to the main text.
  595. text = icon + '<span class="text">' + text + subtext + '</span>';
  596. }
  597. if (isOptgroup && $this.data('divider') !== true) {
  598. if (that.options.hideDisabled && isDisabled) {
  599. if ($parent.data('allOptionsDisabled') === undefined) {
  600. var $options = $parent.children();
  601. $parent.data('allOptionsDisabled', $options.filter(':disabled').length === $options.length);
  602. }
  603. if ($parent.data('allOptionsDisabled')) {
  604. liIndex--;
  605. return;
  606. }
  607. }
  608. var optGroupClass = ' ' + $parent[0].className || '';
  609. if ($this.index() === 0) { // Is it the first option of the optgroup?
  610. optID += 1;
  611. // Get the opt group label
  612. var label = $parent[0].label,
  613. labelSubtext = typeof $parent.data('subtext') !== 'undefined' ? '<small class="text-muted">' + $parent.data('subtext') + '</small>' : '',
  614. labelIcon = $parent.data('icon') ? '<span class="' + that.options.iconBase + ' ' + $parent.data('icon') + '"></span> ' : '';
  615. label = labelIcon + '<span class="text">' + htmlEscape(label) + labelSubtext + '</span>';
  616. if (index !== 0 && _li.length > 0) { // Is it NOT the first option of the select && are there elements in the dropdown?
  617. liIndex++;
  618. _li.push(generateLI('', null, 'divider', optID + 'div'));
  619. }
  620. liIndex++;
  621. _li.push(generateLI(label, null, 'dropdown-header' + optGroupClass, optID));
  622. }
  623. if (that.options.hideDisabled && isDisabled) {
  624. liIndex--;
  625. return;
  626. }
  627. _li.push(generateLI(generateA(text, 'opt ' + optionClass + optGroupClass, inline, tokens), index, '', optID));
  628. } else if ($this.data('divider') === true) {
  629. _li.push(generateLI('', index, 'divider'));
  630. } else if ($this.data('hidden') === true) {
  631. // set prevHiddenIndex - the index of the first hidden option in a group of hidden options
  632. // used to determine whether or not a divider should be placed after an optgroup if there are
  633. // hidden options between the optgroup and the first visible option
  634. prevHiddenIndex = $this.data('prevHiddenIndex');
  635. $this.next().data('prevHiddenIndex', (prevHiddenIndex !== undefined ? prevHiddenIndex : index));
  636. _li.push(generateLI(generateA(text, optionClass, inline, tokens), index, 'hidden is-hidden'));
  637. } else {
  638. var showDivider = this.previousElementSibling && this.previousElementSibling.tagName === 'OPTGROUP';
  639. // if previous element is not an optgroup and hideDisabled is true
  640. if (!showDivider && that.options.hideDisabled) {
  641. prevHiddenIndex = $this.data('prevHiddenIndex');
  642. if (prevHiddenIndex !== undefined) {
  643. // select the element **before** the first hidden element in the group
  644. var prevHidden = $selectOptions.eq(prevHiddenIndex)[0].previousElementSibling;
  645. if (prevHidden && prevHidden.tagName === 'OPTGROUP' && !prevHidden.disabled) {
  646. showDivider = true;
  647. }
  648. }
  649. }
  650. if (showDivider) {
  651. liIndex++;
  652. _li.push(generateLI('', null, 'divider', optID + 'div'));
  653. }
  654. _li.push(generateLI(generateA(text, optionClass, inline, tokens), index));
  655. }
  656. that.liObj[index] = liIndex;
  657. });
  658. //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
  659. if (!this.multiple && this.$element.find('option:selected').length === 0 && !this.options.title) {
  660. this.$element.find('option').eq(0).prop('selected', true).attr('selected', 'selected');
  661. }
  662. return _li.join('');
  663. },
  664. findLis: function () {
  665. if (this.$lis == null) this.$lis = this.$menu.find('li');
  666. return this.$lis;
  667. },
  668. /**
  669. * @param [updateLi] defaults to true
  670. */
  671. render: function (updateLi) {
  672. var that = this,
  673. notDisabled,
  674. $selectOptions = this.$element.find('option');
  675. //Update the LI to match the SELECT
  676. if (updateLi !== false) {
  677. $selectOptions.each(function (index) {
  678. var $lis = that.findLis().eq(that.liObj[index]);
  679. that.setDisabled(index, this.disabled || this.parentNode.tagName === 'OPTGROUP' && this.parentNode.disabled, $lis);
  680. that.setSelected(index, this.selected, $lis);
  681. });
  682. }
  683. this.togglePlaceholder();
  684. this.tabIndex();
  685. var selectedItems = $selectOptions.map(function () {
  686. if (this.selected) {
  687. if (that.options.hideDisabled && (this.disabled || this.parentNode.tagName === 'OPTGROUP' && this.parentNode.disabled)) return;
  688. var $this = $(this),
  689. icon = $this.data('icon') && that.options.showIcon ? '<i class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></i> ' : '',
  690. subtext;
  691. if (that.options.showSubtext && $this.data('subtext') && !that.multiple) {
  692. subtext = ' <small class="text-muted">' + $this.data('subtext') + '</small>';
  693. } else {
  694. subtext = '';
  695. }
  696. if (typeof $this.attr('title') !== 'undefined') {
  697. return $this.attr('title');
  698. } else if ($this.data('content') && that.options.showContent) {
  699. return $this.data('content').toString();
  700. } else {
  701. return icon + $this.html() + subtext;
  702. }
  703. }
  704. }).toArray();
  705. //Fixes issue in IE10 occurring when no default option is selected and at least one option is disabled
  706. //Convert all the values into a comma delimited string
  707. var title = !this.multiple ? selectedItems[0] : selectedItems.join(this.options.multipleSeparator);
  708. //If this is multi select, and the selectText type is count, the show 1 of 2 selected etc..
  709. if (this.multiple && this.options.selectedTextFormat.indexOf('count') > -1) {
  710. var max = this.options.selectedTextFormat.split('>');
  711. if ((max.length > 1 && selectedItems.length > max[1]) || (max.length == 1 && selectedItems.length >= 2)) {
  712. notDisabled = this.options.hideDisabled ? ', [disabled]' : '';
  713. var totalCount = $selectOptions.not('[data-divider="true"], [data-hidden="true"]' + notDisabled).length,
  714. tr8nText = (typeof this.options.countSelectedText === 'function') ? this.options.countSelectedText(selectedItems.length, totalCount) : this.options.countSelectedText;
  715. title = tr8nText.replace('{0}', selectedItems.length.toString()).replace('{1}', totalCount.toString());
  716. }
  717. }
  718. if (this.options.title == undefined) {
  719. this.options.title = this.$element.attr('title');
  720. }
  721. if (this.options.selectedTextFormat == 'static') {
  722. title = this.options.title;
  723. }
  724. //If we dont have a title, then use the default, or if nothing is set at all, use the not selected text
  725. if (!title) {
  726. title = typeof this.options.title !== 'undefined' ? this.options.title : this.options.noneSelectedText;
  727. }
  728. //strip all HTML tags and trim the result, then unescape any escaped tags
  729. this.$button.attr('title', htmlUnescape($.trim(title.replace(/<[^>]*>?/g, ''))));
  730. this.$button.children('.filter-option').html(title);
  731. this.$element.trigger('rendered.bs.select');
  732. },
  733. /**
  734. * @param [style]
  735. * @param [status]
  736. */
  737. setStyle: function (style, status) {
  738. if (this.$element.attr('class')) {
  739. this.$newElement.addClass(this.$element.attr('class').replace(/selectpicker|mobile-device|bs-select-hidden|validate\[.*\]/gi, ''));
  740. }
  741. var buttonClass = style ? style : this.options.style;
  742. if (status == 'add') {
  743. this.$button.addClass(buttonClass);
  744. } else if (status == 'remove') {
  745. this.$button.removeClass(buttonClass);
  746. } else {
  747. this.$button.removeClass(this.options.style);
  748. this.$button.addClass(buttonClass);
  749. }
  750. },
  751. liHeight: function (refresh) {
  752. if (!refresh && (this.options.size === false || this.sizeInfo)) return;
  753. var newElement = document.createElement('div'),
  754. menu = document.createElement('div'),
  755. menuInner = document.createElement('ul'),
  756. divider = document.createElement('li'),
  757. li = document.createElement('li'),
  758. a = document.createElement('a'),
  759. text = document.createElement('span'),
  760. header = this.options.header && this.$menu.find('.popover-title').length > 0 ? this.$menu.find('.popover-title')[0].cloneNode(true) : null,
  761. search = this.options.liveSearch ? document.createElement('div') : null,
  762. actions = this.options.actionsBox && this.multiple && this.$menu.find('.bs-actionsbox').length > 0 ? this.$menu.find('.bs-actionsbox')[0].cloneNode(true) : null,
  763. doneButton = this.options.doneButton && this.multiple && this.$menu.find('.bs-donebutton').length > 0 ? this.$menu.find('.bs-donebutton')[0].cloneNode(true) : null;
  764. text.className = 'text';
  765. newElement.className = this.$menu[0].parentNode.className + ' open';
  766. menu.className = 'dropdown-menu open';
  767. menuInner.className = 'dropdown-menu inner';
  768. divider.className = 'divider';
  769. text.appendChild(document.createTextNode('Inner text'));
  770. a.appendChild(text);
  771. li.appendChild(a);
  772. menuInner.appendChild(li);
  773. menuInner.appendChild(divider);
  774. if (header) menu.appendChild(header);
  775. if (search) {
  776. var input = document.createElement('input');
  777. search.className = 'bs-searchbox';
  778. input.className = 'form-control';
  779. search.appendChild(input);
  780. menu.appendChild(search);
  781. }
  782. if (actions) menu.appendChild(actions);
  783. menu.appendChild(menuInner);
  784. if (doneButton) menu.appendChild(doneButton);
  785. newElement.appendChild(menu);
  786. document.body.appendChild(newElement);
  787. var liHeight = a.offsetHeight,
  788. headerHeight = header ? header.offsetHeight : 0,
  789. searchHeight = search ? search.offsetHeight : 0,
  790. actionsHeight = actions ? actions.offsetHeight : 0,
  791. doneButtonHeight = doneButton ? doneButton.offsetHeight : 0,
  792. dividerHeight = $(divider).outerHeight(true),
  793. // fall back to jQuery if getComputedStyle is not supported
  794. menuStyle = typeof getComputedStyle === 'function' ? getComputedStyle(menu) : false,
  795. $menu = menuStyle ? null : $(menu),
  796. menuPadding = {
  797. vert: parseInt(menuStyle ? menuStyle.paddingTop : $menu.css('paddingTop')) +
  798. parseInt(menuStyle ? menuStyle.paddingBottom : $menu.css('paddingBottom')) +
  799. parseInt(menuStyle ? menuStyle.borderTopWidth : $menu.css('borderTopWidth')) +
  800. parseInt(menuStyle ? menuStyle.borderBottomWidth : $menu.css('borderBottomWidth')),
  801. horiz: parseInt(menuStyle ? menuStyle.paddingLeft : $menu.css('paddingLeft')) +
  802. parseInt(menuStyle ? menuStyle.paddingRight : $menu.css('paddingRight')) +
  803. parseInt(menuStyle ? menuStyle.borderLeftWidth : $menu.css('borderLeftWidth')) +
  804. parseInt(menuStyle ? menuStyle.borderRightWidth : $menu.css('borderRightWidth'))
  805. },
  806. menuExtras = {
  807. vert: menuPadding.vert +
  808. parseInt(menuStyle ? menuStyle.marginTop : $menu.css('marginTop')) +
  809. parseInt(menuStyle ? menuStyle.marginBottom : $menu.css('marginBottom')) + 2,
  810. horiz: menuPadding.horiz +
  811. parseInt(menuStyle ? menuStyle.marginLeft : $menu.css('marginLeft')) +
  812. parseInt(menuStyle ? menuStyle.marginRight : $menu.css('marginRight')) + 2
  813. }
  814. document.body.removeChild(newElement);
  815. this.sizeInfo = {
  816. liHeight: liHeight,
  817. headerHeight: headerHeight,
  818. searchHeight: searchHeight,
  819. actionsHeight: actionsHeight,
  820. doneButtonHeight: doneButtonHeight,
  821. dividerHeight: dividerHeight,
  822. menuPadding: menuPadding,
  823. menuExtras: menuExtras
  824. };
  825. },
  826. setSize: function () {
  827. this.findLis();
  828. this.liHeight();
  829. if (this.options.header) this.$menu.css('padding-top', 0);
  830. if (this.options.size === false) return;
  831. var that = this,
  832. $menu = this.$menu,
  833. $menuInner = this.$menuInner,
  834. $window = $(window),
  835. selectHeight = this.$newElement[0].offsetHeight,
  836. selectWidth = this.$newElement[0].offsetWidth,
  837. liHeight = this.sizeInfo['liHeight'],
  838. headerHeight = this.sizeInfo['headerHeight'],
  839. searchHeight = this.sizeInfo['searchHeight'],
  840. actionsHeight = this.sizeInfo['actionsHeight'],
  841. doneButtonHeight = this.sizeInfo['doneButtonHeight'],
  842. divHeight = this.sizeInfo['dividerHeight'],
  843. menuPadding = this.sizeInfo['menuPadding'],
  844. menuExtras = this.sizeInfo['menuExtras'],
  845. notDisabled = this.options.hideDisabled ? '.disabled' : '',
  846. menuHeight,
  847. menuWidth,
  848. getHeight,
  849. getWidth,
  850. selectOffsetTop,
  851. selectOffsetBot,
  852. selectOffsetLeft,
  853. selectOffsetRight,
  854. getPos = function() {
  855. var pos = that.$newElement.offset(),
  856. $container = $(that.options.container),
  857. containerPos;
  858. if (that.options.container && !$container.is('body')) {
  859. containerPos = $container.offset();
  860. containerPos.top += parseInt($container.css('borderTopWidth'));
  861. containerPos.left += parseInt($container.css('borderLeftWidth'));
  862. } else {
  863. containerPos = { top: 0, left: 0 };
  864. }
  865. var winPad = that.options.windowPadding;
  866. selectOffsetTop = pos.top - containerPos.top - $window.scrollTop();
  867. selectOffsetBot = $window.height() - selectOffsetTop - selectHeight - containerPos.top - winPad[2];
  868. selectOffsetLeft = pos.left - containerPos.left - $window.scrollLeft();
  869. selectOffsetRight = $window.width() - selectOffsetLeft - selectWidth - containerPos.left - winPad[1];
  870. selectOffsetTop -= winPad[0];
  871. selectOffsetLeft -= winPad[3];
  872. };
  873. getPos();
  874. if (this.options.size === 'auto') {
  875. var getSize = function () {
  876. var minHeight,
  877. hasClass = function (className, include) {
  878. return function (element) {
  879. if (include) {
  880. return (element.classList ? element.classList.contains(className) : $(element).hasClass(className));
  881. } else {
  882. return !(element.classList ? element.classList.contains(className) : $(element).hasClass(className));
  883. }
  884. };
  885. },
  886. lis = that.$menuInner[0].getElementsByTagName('li'),
  887. lisVisible = Array.prototype.filter ? Array.prototype.filter.call(lis, hasClass('hidden', false)) : that.$lis.not('.hidden'),
  888. optGroup = Array.prototype.filter ? Array.prototype.filter.call(lisVisible, hasClass('dropdown-header', true)) : lisVisible.filter('.dropdown-header');
  889. getPos();
  890. menuHeight = selectOffsetBot - menuExtras.vert;
  891. menuWidth = selectOffsetRight - menuExtras.horiz;
  892. if (that.options.container) {
  893. if (!$menu.data('height')) $menu.data('height', $menu.height());
  894. getHeight = $menu.data('height');
  895. if (!$menu.data('width')) $menu.data('width', $menu.width());
  896. getWidth = $menu.data('width');
  897. } else {
  898. getHeight = $menu.height();
  899. getWidth = $menu.width();
  900. }
  901. if (that.options.dropupAuto) {
  902. that.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && (menuHeight - menuExtras.vert) < getHeight);
  903. }
  904. if (that.$newElement.hasClass('dropup')) {
  905. menuHeight = selectOffsetTop - menuExtras.vert;
  906. }
  907. if (that.options.dropdownAlignRight === 'auto') {
  908. $menu.toggleClass('dropdown-menu-right', selectOffsetLeft > selectOffsetRight && (menuWidth - menuExtras.horiz) < (getWidth - selectWidth));
  909. }
  910. if ((lisVisible.length + optGroup.length) > 3) {
  911. minHeight = liHeight * 3 + menuExtras.vert - 2;
  912. } else {
  913. minHeight = 0;
  914. }
  915. $menu.css({
  916. 'max-height': menuHeight + 'px',
  917. 'overflow': 'hidden',
  918. 'min-height': minHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px'
  919. });
  920. $menuInner.css({
  921. 'max-height': menuHeight - headerHeight - searchHeight - actionsHeight - doneButtonHeight - menuPadding.vert + 'px',
  922. 'overflow-y': 'auto',
  923. 'min-height': Math.max(minHeight - menuPadding.vert, 0) + 'px'
  924. });
  925. };
  926. getSize();
  927. this.$searchbox.off('input.getSize propertychange.getSize').on('input.getSize propertychange.getSize', getSize);
  928. $window.off('resize.getSize scroll.getSize').on('resize.getSize scroll.getSize', getSize);
  929. } else if (this.options.size && this.options.size != 'auto' && this.$lis.not(notDisabled).length > this.options.size) {
  930. var optIndex = this.$lis.not('.divider').not(notDisabled).children().slice(0, this.options.size).last().parent().index(),
  931. divLength = this.$lis.slice(0, optIndex + 1).filter('.divider').length;
  932. menuHeight = liHeight * this.options.size + divLength * divHeight + menuPadding.vert;
  933. if (that.options.container) {
  934. if (!$menu.data('height')) $menu.data('height', $menu.height());
  935. getHeight = $menu.data('height');
  936. } else {
  937. getHeight = $menu.height();
  938. }
  939. if (that.options.dropupAuto) {
  940. //noinspection JSUnusedAssignment
  941. this.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && (menuHeight - menuExtras.vert) < getHeight);
  942. }
  943. $menu.css({
  944. 'max-height': menuHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px',
  945. 'overflow': 'hidden',
  946. 'min-height': ''
  947. });
  948. $menuInner.css({
  949. 'max-height': menuHeight - menuPadding.vert + 'px',
  950. 'overflow-y': 'auto',
  951. 'min-height': ''
  952. });
  953. }
  954. },
  955. setWidth: function () {
  956. if (this.options.width === 'auto') {
  957. this.$menu.css('min-width', '0');
  958. // Get correct width if element is hidden
  959. var $selectClone = this.$menu.parent().clone().appendTo('body'),
  960. $selectClone2 = this.options.container ? this.$newElement.clone().appendTo('body') : $selectClone,
  961. ulWidth = $selectClone.children('.dropdown-menu').outerWidth(),
  962. btnWidth = $selectClone2.css('width', 'auto').children('button').outerWidth();
  963. $selectClone.remove();
  964. $selectClone2.remove();
  965. // Set width to whatever's larger, button title or longest option
  966. this.$newElement.css('width', Math.max(ulWidth, btnWidth) + 'px');
  967. } else if (this.options.width === 'fit') {
  968. // Remove inline min-width so width can be changed from 'auto'
  969. this.$menu.css('min-width', '');
  970. this.$newElement.css('width', '').addClass('fit-width');
  971. } else if (this.options.width) {
  972. // Remove inline min-width so width can be changed from 'auto'
  973. this.$menu.css('min-width', '');
  974. this.$newElement.css('width', this.options.width);
  975. } else {
  976. // Remove inline min-width/width so width can be changed
  977. this.$menu.css('min-width', '');
  978. this.$newElement.css('width', '');
  979. }
  980. // Remove fit-width class if width is changed programmatically
  981. if (this.$newElement.hasClass('fit-width') && this.options.width !== 'fit') {
  982. this.$newElement.removeClass('fit-width');
  983. }
  984. },
  985. selectPosition: function () {
  986. this.$bsContainer = $('<div class="bs-container" />');
  987. var that = this,
  988. $container = $(this.options.container),
  989. pos,
  990. containerPos,
  991. actualHeight,
  992. getPlacement = function ($element) {
  993. that.$bsContainer.addClass($element.attr('class').replace(/form-control|fit-width/gi, '')).toggleClass('dropup', $element.hasClass('dropup'));
  994. pos = $element.offset();
  995. if (!$container.is('body')) {
  996. containerPos = $container.offset();
  997. containerPos.top += parseInt($container.css('borderTopWidth')) - $container.scrollTop();
  998. containerPos.left += parseInt($container.css('borderLeftWidth')) - $container.scrollLeft();
  999. } else {
  1000. containerPos = { top: 0, left: 0 };
  1001. }
  1002. actualHeight = $element.hasClass('dropup') ? 0 : $element[0].offsetHeight;
  1003. that.$bsContainer.css({
  1004. 'top': pos.top - containerPos.top + actualHeight,
  1005. 'left': pos.left - containerPos.left,
  1006. 'width': $element[0].offsetWidth
  1007. });
  1008. };
  1009. this.$button.on('click', function () {
  1010. var $this = $(this);
  1011. if (that.isDisabled()) {
  1012. return;
  1013. }
  1014. getPlacement(that.$newElement);
  1015. that.$bsContainer
  1016. .appendTo(that.options.container)
  1017. .toggleClass('open', !$this.hasClass('open'))
  1018. .append(that.$menu);
  1019. });
  1020. $(window).on('resize scroll', function () {
  1021. getPlacement(that.$newElement);
  1022. });
  1023. this.$element.on('hide.bs.select', function () {
  1024. that.$menu.data('height', that.$menu.height());
  1025. that.$bsContainer.detach();
  1026. });
  1027. },
  1028. /**
  1029. * @param {number} index - the index of the option that is being changed
  1030. * @param {boolean} selected - true if the option is being selected, false if being deselected
  1031. * @param {JQuery} $lis - the 'li' element that is being modified
  1032. */
  1033. setSelected: function (index, selected, $lis) {
  1034. if (!$lis) {
  1035. this.togglePlaceholder(); // check if setSelected is being called by changing the value of the select
  1036. $lis = this.findLis().eq(this.liObj[index]);
  1037. }
  1038. $lis.toggleClass('selected', selected).find('a').attr('aria-selected', selected);
  1039. },
  1040. /**
  1041. * @param {number} index - the index of the option that is being disabled
  1042. * @param {boolean} disabled - true if the option is being disabled, false if being enabled
  1043. * @param {JQuery} $lis - the 'li' element that is being modified
  1044. */
  1045. setDisabled: function (index, disabled, $lis) {
  1046. if (!$lis) {
  1047. $lis = this.findLis().eq(this.liObj[index]);
  1048. }
  1049. if (disabled) {
  1050. $lis.addClass('disabled').children('a').attr('href', '#').attr('tabindex', -1).attr('aria-disabled', true);
  1051. } else {
  1052. $lis.removeClass('disabled').children('a').removeAttr('href').attr('tabindex', 0).attr('aria-disabled', false);
  1053. }
  1054. },
  1055. isDisabled: function () {
  1056. return this.$element[0].disabled;
  1057. },
  1058. checkDisabled: function () {
  1059. var that = this;
  1060. if (this.isDisabled()) {
  1061. this.$newElement.addClass('disabled');
  1062. this.$button.addClass('disabled').attr('tabindex', -1).attr('aria-disabled', true);
  1063. } else {
  1064. if (this.$button.hasClass('disabled')) {
  1065. this.$newElement.removeClass('disabled');
  1066. this.$button.removeClass('disabled').attr('aria-disabled', false);
  1067. }
  1068. if (this.$button.attr('tabindex') == -1 && !this.$element.data('tabindex')) {
  1069. this.$button.removeAttr('tabindex');
  1070. }
  1071. }
  1072. this.$button.click(function () {
  1073. return !that.isDisabled();
  1074. });
  1075. },
  1076. togglePlaceholder: function () {
  1077. var value = this.$element.val();
  1078. this.$button.toggleClass('bs-placeholder', value === null || value === '' || (value.constructor === Array && value.length === 0));
  1079. },
  1080. tabIndex: function () {
  1081. if (this.$element.data('tabindex') !== this.$element.attr('tabindex') &&
  1082. (this.$element.attr('tabindex') !== -98 && this.$element.attr('tabindex') !== '-98')) {
  1083. this.$element.data('tabindex', this.$element.attr('tabindex'));
  1084. this.$button.attr('tabindex', this.$element.data('tabindex'));
  1085. }
  1086. this.$element.attr('tabindex', -98);
  1087. },
  1088. clickListener: function () {
  1089. var that = this,
  1090. $document = $(document);
  1091. $document.data('spaceSelect', false);
  1092. this.$button.on('keyup', function (e) {
  1093. if (/(32)/.test(e.keyCode.toString(10)) && $document.data('spaceSelect')) {
  1094. e.preventDefault();
  1095. $document.data('spaceSelect', false);
  1096. }
  1097. });
  1098. this.$button.on('click', function () {
  1099. that.setSize();
  1100. });
  1101. this.$element.on('shown.bs.select', function () {
  1102. if (!that.options.liveSearch && !that.multiple) {
  1103. that.$menuInner.find('.selected a').focus();
  1104. } else if (!that.multiple) {
  1105. var selectedIndex = that.liObj[that.$element[0].selectedIndex];
  1106. if (typeof selectedIndex !== 'number' || that.options.size === false) return;
  1107. // scroll to selected option
  1108. var offset = that.$lis.eq(selectedIndex)[0].offsetTop - that.$menuInner[0].offsetTop;
  1109. offset = offset - that.$menuInner[0].offsetHeight/2 + that.sizeInfo.liHeight/2;
  1110. that.$menuInner[0].scrollTop = offset;
  1111. }
  1112. });
  1113. this.$menuInner.on('click', 'li a', function (e) {
  1114. var $this = $(this),
  1115. clickedIndex = $this.parent().data('originalIndex'),
  1116. prevValue = that.$element.val(),
  1117. prevIndex = that.$element.prop('selectedIndex'),
  1118. triggerChange = true;
  1119. // Don't close on multi choice menu
  1120. if (that.multiple && that.options.maxOptions !== 1) {
  1121. e.stopPropagation();
  1122. }
  1123. e.preventDefault();
  1124. //Don't run if we have been disabled
  1125. if (!that.isDisabled() && !$this.parent().hasClass('disabled')) {
  1126. var $options = that.$element.find('option'),
  1127. $option = $options.eq(clickedIndex),
  1128. state = $option.prop('selected'),
  1129. $optgroup = $option.parent('optgroup'),
  1130. maxOptions = that.options.maxOptions,
  1131. maxOptionsGrp = $optgroup.data('maxOptions') || false;
  1132. if (!that.multiple) { // Deselect all others if not multi select box
  1133. $options.prop('selected', false);
  1134. $option.prop('selected', true);
  1135. that.$menuInner.find('.selected').removeClass('selected').find('a').attr('aria-selected', false);
  1136. that.setSelected(clickedIndex, true);
  1137. } else { // Toggle the one we have chosen if we are multi select.
  1138. $option.prop('selected', !state);
  1139. that.setSelected(clickedIndex, !state);
  1140. $this.blur();
  1141. if (maxOptions !== false || maxOptionsGrp !== false) {
  1142. var maxReached = maxOptions < $options.filter(':selected').length,
  1143. maxReachedGrp = maxOptionsGrp < $optgroup.find('option:selected').length;
  1144. if ((maxOptions && maxReached) || (maxOptionsGrp && maxReachedGrp)) {
  1145. if (maxOptions && maxOptions == 1) {
  1146. $options.prop('selected', false);
  1147. $option.prop('selected', true);
  1148. that.$menuInner.find('.selected').removeClass('selected');
  1149. that.setSelected(clickedIndex, true);
  1150. } else if (maxOptionsGrp && maxOptionsGrp == 1) {
  1151. $optgroup.find('option:selected').prop('selected', false);
  1152. $option.prop('selected', true);
  1153. var optgroupID = $this.parent().data('optgroup');
  1154. that.$menuInner.find('[data-optgroup="' + optgroupID + '"]').removeClass('selected');
  1155. that.setSelected(clickedIndex, true);
  1156. } else {
  1157. var maxOptionsText = typeof that.options.maxOptionsText === 'string' ? [that.options.maxOptionsText, that.options.maxOptionsText] : that.options.maxOptionsText,
  1158. maxOptionsArr = typeof maxOptionsText === 'function' ? maxOptionsText(maxOptions, maxOptionsGrp) : maxOptionsText,
  1159. maxTxt = maxOptionsArr[0].replace('{n}', maxOptions),
  1160. maxTxtGrp = maxOptionsArr[1].replace('{n}', maxOptionsGrp),
  1161. $notify = $('<div class="notify"></div>');
  1162. // If {var} is set in array, replace it
  1163. /** @deprecated */
  1164. if (maxOptionsArr[2]) {
  1165. maxTxt = maxTxt.replace('{var}', maxOptionsArr[2][maxOptions > 1 ? 0 : 1]);
  1166. maxTxtGrp = maxTxtGrp.replace('{var}', maxOptionsArr[2][maxOptionsGrp > 1 ? 0 : 1]);
  1167. }
  1168. $option.prop('selected', false);
  1169. that.$menu.append($notify);
  1170. if (maxOptions && maxReached) {
  1171. $notify.append($('<div>' + maxTxt + '</div>'));
  1172. triggerChange = false;
  1173. that.$element.trigger('maxReached.bs.select');
  1174. }
  1175. if (maxOptionsGrp && maxReachedGrp) {
  1176. $notify.append($('<div>' + maxTxtGrp + '</div>'));
  1177. triggerChange = false;
  1178. that.$element.trigger('maxReachedGrp.bs.select');
  1179. }
  1180. setTimeout(function () {
  1181. that.setSelected(clickedIndex, false);
  1182. }, 10);
  1183. $notify.delay(750).fadeOut(300, function () {
  1184. $(this).remove();
  1185. });
  1186. }
  1187. }
  1188. }
  1189. }
  1190. if (!that.multiple || (that.multiple && that.options.maxOptions === 1)) {
  1191. that.$button.focus();
  1192. } else if (that.options.liveSearch) {
  1193. that.$searchbox.focus();
  1194. }
  1195. // Trigger select 'change'
  1196. if (triggerChange) {
  1197. if ((prevValue != that.$element.val() && that.multiple) || (prevIndex != that.$element.prop('selectedIndex') && !that.multiple)) {
  1198. // $option.prop('selected') is current option state (selected/unselected). state is previous option state.
  1199. changed_arguments = [clickedIndex, $option.prop('selected'), state];
  1200. that.$element
  1201. .triggerNative('change');
  1202. }
  1203. }
  1204. }
  1205. });
  1206. this.$menu.on('click', 'li.disabled a, .popover-title, .popover-title :not(.close)', function (e) {
  1207. if (e.currentTarget == this) {
  1208. e.preventDefault();
  1209. e.stopPropagation();
  1210. if (that.options.liveSearch && !$(e.target).hasClass('close')) {
  1211. that.$searchbox.focus();
  1212. } else {
  1213. that.$button.focus();
  1214. }
  1215. }
  1216. });
  1217. this.$menuInner.on('click', '.divider, .dropdown-header', function (e) {
  1218. e.preventDefault();
  1219. e.stopPropagation();
  1220. if (that.options.liveSearch) {
  1221. that.$searchbox.focus();
  1222. } else {
  1223. that.$button.focus();
  1224. }
  1225. });
  1226. this.$menu.on('click', '.popover-title .close', function () {
  1227. that.$button.click();
  1228. });
  1229. this.$searchbox.on('click', function (e) {
  1230. e.stopPropagation();
  1231. });
  1232. this.$menu.on('click', '.actions-btn', function (e) {
  1233. if (that.options.liveSearch) {
  1234. that.$searchbox.focus();
  1235. } else {
  1236. that.$button.focus();
  1237. }
  1238. e.preventDefault();
  1239. e.stopPropagation();
  1240. if ($(this).hasClass('bs-select-all')) {
  1241. that.selectAll();
  1242. } else {
  1243. that.deselectAll();
  1244. }
  1245. });
  1246. this.$element.change(function () {
  1247. that.render(false);
  1248. that.$element.trigger('changed.bs.select', changed_arguments);
  1249. changed_arguments = null;
  1250. });
  1251. },
  1252. liveSearchListener: function () {
  1253. var that = this,
  1254. $no_results = $('<li class="no-results"></li>');
  1255. this.$button.on('click.dropdown.data-api', function () {
  1256. that.$menuInner.find('.active').removeClass('active');
  1257. if (!!that.$searchbox.val()) {
  1258. that.$searchbox.val('');
  1259. that.$lis.not('.is-hidden').removeClass('hidden');
  1260. if (!!$no_results.parent().length) $no_results.remove();
  1261. }
  1262. if (!that.multiple) that.$menuInner.find('.selected').addClass('active');
  1263. setTimeout(function () {
  1264. that.$searchbox.focus();
  1265. }, 10);
  1266. });
  1267. this.$searchbox.on('click.dropdown.data-api focus.dropdown.data-api touchend.dropdown.data-api', function (e) {
  1268. e.stopPropagation();
  1269. });
  1270. this.$searchbox.on('input propertychange', function () {
  1271. that.$lis.not('.is-hidden').removeClass('hidden');
  1272. that.$lis.filter('.active').removeClass('active');
  1273. $no_results.remove();
  1274. if (that.$searchbox.val()) {
  1275. var $searchBase = that.$lis.not('.is-hidden, .divider, .dropdown-header'),
  1276. $hideItems;
  1277. if (that.options.liveSearchNormalize) {
  1278. $hideItems = $searchBase.not(':a' + that._searchStyle() + '("' + normalizeToBase(that.$searchbox.val()) + '")');
  1279. } else {
  1280. $hideItems = $searchBase.not(':' + that._searchStyle() + '("' + that.$searchbox.val() + '")');
  1281. }
  1282. if ($hideItems.length === $searchBase.length) {
  1283. $no_results.html(that.options.noneResultsText.replace('{0}', '"' + htmlEscape(that.$searchbox.val()) + '"'));
  1284. that.$menuInner.append($no_results);
  1285. that.$lis.addClass('hidden');
  1286. } else {
  1287. $hideItems.addClass('hidden');
  1288. var $lisVisible = that.$lis.not('.hidden'),
  1289. $foundDiv;
  1290. // hide divider if first or last visible, or if followed by another divider
  1291. $lisVisible.each(function (index) {
  1292. var $this = $(this);
  1293. if ($this.hasClass('divider')) {
  1294. if ($foundDiv === undefined) {
  1295. $this.addClass('hidden');
  1296. } else {
  1297. if ($foundDiv) $foundDiv.addClass('hidden');
  1298. $foundDiv = $this;
  1299. }
  1300. } else if ($this.hasClass('dropdown-header') && $lisVisible.eq(index + 1).data('optgroup') !== $this.data('optgroup')) {
  1301. $this.addClass('hidden');
  1302. } else {
  1303. $foundDiv = null;
  1304. }
  1305. });
  1306. if ($foundDiv) $foundDiv.addClass('hidden');
  1307. $searchBase.not('.hidden').first().addClass('active');
  1308. that.$menuInner.scrollTop(0);
  1309. }
  1310. }
  1311. });
  1312. },
  1313. _searchStyle: function () {
  1314. var styles = {
  1315. begins: 'ibegins',
  1316. startsWith: 'ibegins'
  1317. };
  1318. return styles[this.options.liveSearchStyle] || 'icontains';
  1319. },
  1320. val: function (value) {
  1321. if (typeof value !== 'undefined') {
  1322. this.$element.val(value);
  1323. this.render();
  1324. return this.$element;
  1325. } else {
  1326. return this.$element.val();
  1327. }
  1328. },
  1329. changeAll: function (status) {
  1330. if (!this.multiple) return;
  1331. if (typeof status === 'undefined') status = true;
  1332. this.findLis();
  1333. var $options = this.$element.find('option'),
  1334. $lisVisible = this.$lis.not('.divider, .dropdown-header, .disabled, .hidden'),
  1335. lisVisLen = $lisVisible.length,
  1336. selectedOptions = [];
  1337. if (status) {
  1338. if ($lisVisible.filter('.selected').length === $lisVisible.length) return;
  1339. } else {
  1340. if ($lisVisible.filter('.selected').length === 0) return;
  1341. }
  1342. $lisVisible.toggleClass('selected', status);
  1343. for (var i = 0; i < lisVisLen; i++) {
  1344. var origIndex = $lisVisible[i].getAttribute('data-original-index');
  1345. selectedOptions[selectedOptions.length] = $options.eq(origIndex)[0];
  1346. }
  1347. $(selectedOptions).prop('selected', status);
  1348. this.render(false);
  1349. this.togglePlaceholder();
  1350. this.$element
  1351. .triggerNative('change');
  1352. },
  1353. selectAll: function () {
  1354. return this.changeAll(true);
  1355. },
  1356. deselectAll: function () {
  1357. return this.changeAll(false);
  1358. },
  1359. toggle: function (e) {
  1360. e = e || window.event;
  1361. if (e) e.stopPropagation();
  1362. this.$button.trigger('click');
  1363. },
  1364. keydown: function (e) {
  1365. var $this = $(this),
  1366. $parent = $this.is('input') ? $this.parent().parent() : $this.parent(),
  1367. $items,
  1368. that = $parent.data('this'),
  1369. index,
  1370. prevIndex,
  1371. isActive,
  1372. selector = ':not(.disabled, .hidden, .dropdown-header, .divider)',
  1373. keyCodeMap = {
  1374. 32: ' ',
  1375. 48: '0',
  1376. 49: '1',
  1377. 50: '2',
  1378. 51: '3',
  1379. 52: '4',
  1380. 53: '5',
  1381. 54: '6',
  1382. 55: '7',
  1383. 56: '8',
  1384. 57: '9',
  1385. 59: ';',
  1386. 65: 'a',
  1387. 66: 'b',
  1388. 67: 'c',
  1389. 68: 'd',
  1390. 69: 'e',
  1391. 70: 'f',
  1392. 71: 'g',
  1393. 72: 'h',
  1394. 73: 'i',
  1395. 74: 'j',
  1396. 75: 'k',
  1397. 76: 'l',
  1398. 77: 'm',
  1399. 78: 'n',
  1400. 79: 'o',
  1401. 80: 'p',
  1402. 81: 'q',
  1403. 82: 'r',
  1404. 83: 's',
  1405. 84: 't',
  1406. 85: 'u',
  1407. 86: 'v',
  1408. 87: 'w',
  1409. 88: 'x',
  1410. 89: 'y',
  1411. 90: 'z',
  1412. 96: '0',
  1413. 97: '1',
  1414. 98: '2',
  1415. 99: '3',
  1416. 100: '4',
  1417. 101: '5',
  1418. 102: '6',
  1419. 103: '7',
  1420. 104: '8',
  1421. 105: '9'
  1422. };
  1423. isActive = that.$newElement.hasClass('open');
  1424. if (!isActive && (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105 || e.keyCode >= 65 && e.keyCode <= 90)) {
  1425. if (!that.options.container) {
  1426. that.setSize();
  1427. that.$menu.parent().addClass('open');
  1428. isActive = true;
  1429. } else {
  1430. that.$button.trigger('click');
  1431. }
  1432. that.$searchbox.focus();
  1433. return;
  1434. }
  1435. if (that.options.liveSearch) {
  1436. if (/(^9$|27)/.test(e.keyCode.toString(10)) && isActive) {
  1437. e.preventDefault();
  1438. e.stopPropagation();
  1439. that.$menuInner.click();
  1440. that.$button.focus();
  1441. }
  1442. }
  1443. if (/(38|40)/.test(e.keyCode.toString(10))) {
  1444. $items = that.$lis.filter(selector);
  1445. if (!$items.length) return;
  1446. if (!that.options.liveSearch) {
  1447. index = $items.index($items.find('a').filter(':focus').parent());
  1448. } else {
  1449. index = $items.index($items.filter('.active'));
  1450. }
  1451. prevIndex = that.$menuInner.data('prevIndex');
  1452. if (e.keyCode == 38) {
  1453. if ((that.options.liveSearch || index == prevIndex) && index != -1) index--;
  1454. if (index < 0) index += $items.length;
  1455. } else if (e.keyCode == 40) {
  1456. if (that.options.liveSearch || index == prevIndex) index++;
  1457. index = index % $items.length;
  1458. }
  1459. that.$menuInner.data('prevIndex', index);
  1460. if (!that.options.liveSearch) {
  1461. $items.eq(index).children('a').focus();
  1462. } else {
  1463. e.preventDefault();
  1464. if (!$this.hasClass('dropdown-toggle')) {
  1465. $items.removeClass('active').eq(index).addClass('active').children('a').focus();
  1466. $this.focus();
  1467. }
  1468. }
  1469. } else if (!$this.is('input')) {
  1470. var keyIndex = [],
  1471. count,
  1472. prevKey;
  1473. $items = that.$lis.filter(selector);
  1474. $items.each(function (i) {
  1475. if ($.trim($(this).children('a').text().toLowerCase()).substring(0, 1) == keyCodeMap[e.keyCode]) {
  1476. keyIndex.push(i);
  1477. }
  1478. });
  1479. count = $(document).data('keycount');
  1480. count++;
  1481. $(document).data('keycount', count);
  1482. prevKey = $.trim($(':focus').text().toLowerCase()).substring(0, 1);
  1483. if (prevKey != keyCodeMap[e.keyCode]) {
  1484. count = 1;
  1485. $(document).data('keycount', count);
  1486. } else if (count >= keyIndex.length) {
  1487. $(document).data('keycount', 0);
  1488. if (count > keyIndex.length) count = 1;
  1489. }
  1490. $items.eq(keyIndex[count - 1]).children('a').focus();
  1491. }
  1492. // Select focused option if "Enter", "Spacebar" or "Tab" (when selectOnTab is true) are pressed inside the menu.
  1493. if ((/(13|32)/.test(e.keyCode.toString(10)) || (/(^9$)/.test(e.keyCode.toString(10)) && that.options.selectOnTab)) && isActive) {
  1494. if (!/(32)/.test(e.keyCode.toString(10))) e.preventDefault();
  1495. if (!that.options.liveSearch) {
  1496. var elem = $(':focus');
  1497. elem.click();
  1498. // Bring back focus for multiselects
  1499. elem.focus();
  1500. // Prevent screen from scrolling if the user hit the spacebar
  1501. e.preventDefault();
  1502. // Fixes spacebar selection of dropdown items in FF & IE
  1503. $(document).data('spaceSelect', true);
  1504. } else if (!/(32)/.test(e.keyCode.toString(10))) {
  1505. that.$menuInner.find('.active a').click();
  1506. $this.focus();
  1507. }
  1508. $(document).data('keycount', 0);
  1509. }
  1510. if ((/(^9$|27)/.test(e.keyCode.toString(10)) && isActive && (that.multiple || that.options.liveSearch)) || (/(27)/.test(e.keyCode.toString(10)) && !isActive)) {
  1511. that.$menu.parent().removeClass('open');
  1512. if (that.options.container) that.$newElement.removeClass('open');
  1513. that.$button.focus();
  1514. }
  1515. },
  1516. mobile: function () {
  1517. this.$element.addClass('mobile-device');
  1518. },
  1519. refresh: function () {
  1520. this.$lis = null;
  1521. this.liObj = {};
  1522. this.reloadLi();
  1523. this.render();
  1524. this.checkDisabled();
  1525. this.liHeight(true);
  1526. this.setStyle();
  1527. this.setWidth();
  1528. if (this.$lis) this.$searchbox.trigger('propertychange');
  1529. this.$element.trigger('refreshed.bs.select');
  1530. },
  1531. hide: function () {
  1532. this.$newElement.hide();
  1533. },
  1534. show: function () {
  1535. this.$newElement.show();
  1536. },
  1537. remove: function () {
  1538. this.$newElement.remove();
  1539. this.$element.remove();
  1540. },
  1541. destroy: function () {
  1542. this.$newElement.before(this.$element).remove();
  1543. if (this.$bsContainer) {
  1544. this.$bsContainer.remove();
  1545. } else {
  1546. this.$menu.remove();
  1547. }
  1548. this.$element
  1549. .off('.bs.select')
  1550. .removeData('selectpicker')
  1551. .removeClass('bs-select-hidden selectpicker');
  1552. }
  1553. };
  1554. // SELECTPICKER PLUGIN DEFINITION
  1555. // ==============================
  1556. function Plugin(option) {
  1557. // get the args of the outer function..
  1558. var args = arguments;
  1559. // The arguments of the function are explicitly re-defined from the argument list, because the shift causes them
  1560. // to get lost/corrupted in android 2.3 and IE9 #715 #775
  1561. var _option = option;
  1562. [].shift.apply(args);
  1563. var value;
  1564. var chain = this.each(function () {
  1565. var $this = $(this);
  1566. if ($this.is('select')) {
  1567. var data = $this.data('selectpicker'),
  1568. options = typeof _option == 'object' && _option;
  1569. if (!data) {
  1570. var config = $.extend({}, Selectpicker.DEFAULTS, $.fn.selectpicker.defaults || {}, $this.data(), options);
  1571. config.template = $.extend({}, Selectpicker.DEFAULTS.template, ($.fn.selectpicker.defaults ? $.fn.selectpicker.defaults.template : {}), $this.data().template, options.template);
  1572. $this.data('selectpicker', (data = new Selectpicker(this, config)));
  1573. } else if (options) {
  1574. for (var i in options) {
  1575. if (options.hasOwnProperty(i)) {
  1576. data.options[i] = options[i];
  1577. }
  1578. }
  1579. }
  1580. if (typeof _option == 'string') {
  1581. if (data[_option] instanceof Function) {
  1582. value = data[_option].apply(data, args);
  1583. } else {
  1584. value = data.options[_option];
  1585. }
  1586. }
  1587. }
  1588. });
  1589. if (typeof value !== 'undefined') {
  1590. //noinspection JSUnusedAssignment
  1591. return value;
  1592. } else {
  1593. return chain;
  1594. }
  1595. }
  1596. var old = $.fn.selectpicker;
  1597. $.fn.selectpicker = Plugin;
  1598. $.fn.selectpicker.Constructor = Selectpicker;
  1599. // SELECTPICKER NO CONFLICT
  1600. // ========================
  1601. $.fn.selectpicker.noConflict = function () {
  1602. $.fn.selectpicker = old;
  1603. return this;
  1604. };
  1605. $(document)
  1606. .data('keycount', 0)
  1607. .on('keydown.bs.select', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role="listbox"], .bs-searchbox input', Selectpicker.prototype.keydown)
  1608. .on('focusin.modal', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role="listbox"], .bs-searchbox input', function (e) {
  1609. e.stopPropagation();
  1610. });
  1611. // SELECTPICKER DATA-API
  1612. // =====================
  1613. $(window).on('load.bs.select.data-api', function () {
  1614. $('.selectpicker').each(function () {
  1615. var $selectpicker = $(this);
  1616. Plugin.call($selectpicker, $selectpicker.data());
  1617. })
  1618. });
  1619. })(jQuery);
  1620. }));