Brak opisu

bootstrap-select.js 67KB

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