bootstrap-select.js 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061
  1. /*!
  2. * Bootstrap-select v1.13.9 (https://developer.snapappointments.com/bootstrap-select)
  3. *
  4. * Copyright 2012-2019 SnapAppointments, LLC
  5. * Licensed under MIT (https://github.com/snapappointments/bootstrap-select/blob/master/LICENSE)
  6. */
  7. (function (root, factory) {
  8. if (root === undefined && window !== undefined) root = window;
  9. if (typeof define === 'function' && define.amd) {
  10. // AMD. Register as an anonymous module unless amdModuleId is set
  11. define(["jquery"], function (a0) {
  12. return (factory(a0));
  13. });
  14. } else if (typeof module === 'object' && module.exports) {
  15. // Node. Does not work with strict CommonJS, but
  16. // only CommonJS-like environments that support module.exports,
  17. // like Node.
  18. module.exports = factory(require("jquery"));
  19. } else {
  20. factory(root["jQuery"]);
  21. }
  22. }(this, function (jQuery) {
  23. (function ($) {
  24. 'use strict';
  25. var DISALLOWED_ATTRIBUTES = ['sanitize', 'whiteList', 'sanitizeFn'];
  26. var uriAttrs = [
  27. 'background',
  28. 'cite',
  29. 'href',
  30. 'itemtype',
  31. 'longdesc',
  32. 'poster',
  33. 'src',
  34. 'xlink:href'
  35. ];
  36. var ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i;
  37. var DefaultWhitelist = {
  38. // Global attributes allowed on any supplied element below.
  39. '*': ['class', 'dir', 'id', 'lang', 'role', 'tabindex', 'style', ARIA_ATTRIBUTE_PATTERN],
  40. a: ['target', 'href', 'title', 'rel'],
  41. area: [],
  42. b: [],
  43. br: [],
  44. col: [],
  45. code: [],
  46. div: [],
  47. em: [],
  48. hr: [],
  49. h1: [],
  50. h2: [],
  51. h3: [],
  52. h4: [],
  53. h5: [],
  54. h6: [],
  55. i: [],
  56. img: ['src', 'alt', 'title', 'width', 'height'],
  57. li: [],
  58. ol: [],
  59. p: [],
  60. pre: [],
  61. s: [],
  62. small: [],
  63. span: [],
  64. sub: [],
  65. sup: [],
  66. strong: [],
  67. u: [],
  68. ul: []
  69. }
  70. /**
  71. * A pattern that recognizes a commonly useful subset of URLs that are safe.
  72. *
  73. * Shoutout to Angular 7 https://github.com/angular/angular/blob/7.2.4/packages/core/src/sanitization/url_sanitizer.ts
  74. */
  75. var SAFE_URL_PATTERN = /^(?:(?:https?|mailto|ftp|tel|file):|[^&:/?#]*(?:[/?#]|$))/gi;
  76. /**
  77. * A pattern that matches safe data URLs. Only matches image, video and audio types.
  78. *
  79. * Shoutout to Angular 7 https://github.com/angular/angular/blob/7.2.4/packages/core/src/sanitization/url_sanitizer.ts
  80. */
  81. var DATA_URL_PATTERN = /^data:(?:image\/(?:bmp|gif|jpeg|jpg|png|tiff|webp)|video\/(?:mpeg|mp4|ogg|webm)|audio\/(?:mp3|oga|ogg|opus));base64,[a-z0-9+/]+=*$/i;
  82. function allowedAttribute (attr, allowedAttributeList) {
  83. var attrName = attr.nodeName.toLowerCase()
  84. if ($.inArray(attrName, allowedAttributeList) !== -1) {
  85. if ($.inArray(attrName, uriAttrs) !== -1) {
  86. return Boolean(attr.nodeValue.match(SAFE_URL_PATTERN) || attr.nodeValue.match(DATA_URL_PATTERN))
  87. }
  88. return true
  89. }
  90. var regExp = $(allowedAttributeList).filter(function (index, value) {
  91. return value instanceof RegExp
  92. })
  93. // Check if a regular expression validates the attribute.
  94. for (var i = 0, l = regExp.length; i < l; i++) {
  95. if (attrName.match(regExp[i])) {
  96. return true
  97. }
  98. }
  99. return false
  100. }
  101. function sanitizeHtml (unsafeElements, whiteList, sanitizeFn) {
  102. if (sanitizeFn && typeof sanitizeFn === 'function') {
  103. return sanitizeFn(unsafeElements);
  104. }
  105. var whitelistKeys = Object.keys(whiteList);
  106. for (var i = 0, len = unsafeElements.length; i < len; i++) {
  107. var elements = unsafeElements[i].querySelectorAll('*');
  108. for (var j = 0, len2 = elements.length; j < len2; j++) {
  109. var el = elements[j];
  110. var elName = el.nodeName.toLowerCase();
  111. if (whitelistKeys.indexOf(elName) === -1) {
  112. el.parentNode.removeChild(el);
  113. continue;
  114. }
  115. var attributeList = [].slice.call(el.attributes);
  116. var whitelistedAttributes = [].concat(whiteList['*'] || [], whiteList[elName] || []);
  117. for (var k = 0, len3 = attributeList.length; k < len3; k++) {
  118. var attr = attributeList[k];
  119. if (!allowedAttribute(attr, whitelistedAttributes)) {
  120. el.removeAttribute(attr.nodeName);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. // Polyfill for browsers with no classList support
  127. // Remove in v2
  128. if (!('classList' in document.createElement('_'))) {
  129. (function (view) {
  130. if (!('Element' in view)) return;
  131. var classListProp = 'classList',
  132. protoProp = 'prototype',
  133. elemCtrProto = view.Element[protoProp],
  134. objCtr = Object,
  135. classListGetter = function () {
  136. var $elem = $(this);
  137. return {
  138. add: function (classes) {
  139. classes = Array.prototype.slice.call(arguments).join(' ');
  140. return $elem.addClass(classes);
  141. },
  142. remove: function (classes) {
  143. classes = Array.prototype.slice.call(arguments).join(' ');
  144. return $elem.removeClass(classes);
  145. },
  146. toggle: function (classes, force) {
  147. return $elem.toggleClass(classes, force);
  148. },
  149. contains: function (classes) {
  150. return $elem.hasClass(classes);
  151. }
  152. }
  153. };
  154. if (objCtr.defineProperty) {
  155. var classListPropDesc = {
  156. get: classListGetter,
  157. enumerable: true,
  158. configurable: true
  159. };
  160. try {
  161. objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
  162. } catch (ex) { // IE 8 doesn't support enumerable:true
  163. // adding undefined to fight this issue https://github.com/eligrey/classList.js/issues/36
  164. // modernie IE8-MSW7 machine has IE8 8.0.6001.18702 and is affected
  165. if (ex.number === undefined || ex.number === -0x7FF5EC54) {
  166. classListPropDesc.enumerable = false;
  167. objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
  168. }
  169. }
  170. } else if (objCtr[protoProp].__defineGetter__) {
  171. elemCtrProto.__defineGetter__(classListProp, classListGetter);
  172. }
  173. }(window));
  174. }
  175. var testElement = document.createElement('_');
  176. testElement.classList.add('c1', 'c2');
  177. if (!testElement.classList.contains('c2')) {
  178. var _add = DOMTokenList.prototype.add,
  179. _remove = DOMTokenList.prototype.remove;
  180. DOMTokenList.prototype.add = function () {
  181. Array.prototype.forEach.call(arguments, _add.bind(this));
  182. }
  183. DOMTokenList.prototype.remove = function () {
  184. Array.prototype.forEach.call(arguments, _remove.bind(this));
  185. }
  186. }
  187. testElement.classList.toggle('c3', false);
  188. // Polyfill for IE 10 and Firefox <24, where classList.toggle does not
  189. // support the second argument.
  190. if (testElement.classList.contains('c3')) {
  191. var _toggle = DOMTokenList.prototype.toggle;
  192. DOMTokenList.prototype.toggle = function (token, force) {
  193. if (1 in arguments && !this.contains(token) === !force) {
  194. return force;
  195. } else {
  196. return _toggle.call(this, token);
  197. }
  198. };
  199. }
  200. testElement = null;
  201. // shallow array comparison
  202. function isEqual (array1, array2) {
  203. return array1.length === array2.length && array1.every(function (element, index) {
  204. return element === array2[index];
  205. });
  206. };
  207. // <editor-fold desc="Shims">
  208. if (!String.prototype.startsWith) {
  209. (function () {
  210. 'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
  211. var defineProperty = (function () {
  212. // IE 8 only supports `Object.defineProperty` on DOM elements
  213. try {
  214. var object = {};
  215. var $defineProperty = Object.defineProperty;
  216. var result = $defineProperty(object, object, object) && $defineProperty;
  217. } catch (error) {
  218. }
  219. return result;
  220. }());
  221. var toString = {}.toString;
  222. var startsWith = function (search) {
  223. if (this == null) {
  224. throw new TypeError();
  225. }
  226. var string = String(this);
  227. if (search && toString.call(search) == '[object RegExp]') {
  228. throw new TypeError();
  229. }
  230. var stringLength = string.length;
  231. var searchString = String(search);
  232. var searchLength = searchString.length;
  233. var position = arguments.length > 1 ? arguments[1] : undefined;
  234. // `ToInteger`
  235. var pos = position ? Number(position) : 0;
  236. if (pos != pos) { // better `isNaN`
  237. pos = 0;
  238. }
  239. var start = Math.min(Math.max(pos, 0), stringLength);
  240. // Avoid the `indexOf` call if no match is possible
  241. if (searchLength + start > stringLength) {
  242. return false;
  243. }
  244. var index = -1;
  245. while (++index < searchLength) {
  246. if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
  247. return false;
  248. }
  249. }
  250. return true;
  251. };
  252. if (defineProperty) {
  253. defineProperty(String.prototype, 'startsWith', {
  254. 'value': startsWith,
  255. 'configurable': true,
  256. 'writable': true
  257. });
  258. } else {
  259. String.prototype.startsWith = startsWith;
  260. }
  261. }());
  262. }
  263. if (!Object.keys) {
  264. Object.keys = function (
  265. o, // object
  266. k, // key
  267. r // result array
  268. ) {
  269. // initialize object and result
  270. r = [];
  271. // iterate over object keys
  272. for (k in o) {
  273. // fill result array with non-prototypical keys
  274. r.hasOwnProperty.call(o, k) && r.push(k);
  275. }
  276. // return result
  277. return r;
  278. };
  279. }
  280. if (HTMLSelectElement && !HTMLSelectElement.prototype.hasOwnProperty('selectedOptions')) {
  281. Object.defineProperty(HTMLSelectElement.prototype, 'selectedOptions', {
  282. get: function () {
  283. return this.querySelectorAll(':checked');
  284. }
  285. });
  286. }
  287. // much faster than $.val()
  288. function getSelectValues (select) {
  289. var result = [];
  290. var options = select.selectedOptions;
  291. var opt;
  292. if (select.multiple) {
  293. for (var i = 0, len = options.length; i < len; i++) {
  294. opt = options[i];
  295. result.push(opt.value || opt.text);
  296. }
  297. } else {
  298. result = select.value;
  299. }
  300. return result;
  301. }
  302. // set data-selected on select element if the value has been programmatically selected
  303. // prior to initialization of bootstrap-select
  304. // * consider removing or replacing an alternative method *
  305. var valHooks = {
  306. useDefault: false,
  307. _set: $.valHooks.select.set
  308. };
  309. $.valHooks.select.set = function (elem, value) {
  310. if (value && !valHooks.useDefault) $(elem).data('selected', true);
  311. return valHooks._set.apply(this, arguments);
  312. };
  313. var changedArguments = null;
  314. var EventIsSupported = (function () {
  315. try {
  316. new Event('change');
  317. return true;
  318. } catch (e) {
  319. return false;
  320. }
  321. })();
  322. $.fn.triggerNative = function (eventName) {
  323. var el = this[0],
  324. event;
  325. if (el.dispatchEvent) { // for modern browsers & IE9+
  326. if (EventIsSupported) {
  327. // For modern browsers
  328. event = new Event(eventName, {
  329. bubbles: true
  330. });
  331. } else {
  332. // For IE since it doesn't support Event constructor
  333. event = document.createEvent('Event');
  334. event.initEvent(eventName, true, false);
  335. }
  336. el.dispatchEvent(event);
  337. } else if (el.fireEvent) { // for IE8
  338. event = document.createEventObject();
  339. event.eventType = eventName;
  340. el.fireEvent('on' + eventName, event);
  341. } else {
  342. // fall back to jQuery.trigger
  343. this.trigger(eventName);
  344. }
  345. };
  346. // </editor-fold>
  347. function stringSearch (li, searchString, method, normalize) {
  348. var stringTypes = [
  349. 'display',
  350. 'subtext',
  351. 'tokens'
  352. ],
  353. searchSuccess = false;
  354. for (var i = 0; i < stringTypes.length; i++) {
  355. var stringType = stringTypes[i],
  356. string = li[stringType];
  357. if (string) {
  358. string = string.toString();
  359. // Strip HTML tags. This isn't perfect, but it's much faster than any other method
  360. if (stringType === 'display') {
  361. string = string.replace(/<[^>]+>/g, '');
  362. }
  363. if (normalize) string = normalizeToBase(string);
  364. string = string.toUpperCase();
  365. if (method === 'contains') {
  366. searchSuccess = string.indexOf(searchString) >= 0;
  367. } else {
  368. searchSuccess = string.startsWith(searchString);
  369. }
  370. if (searchSuccess) break;
  371. }
  372. }
  373. return searchSuccess;
  374. }
  375. function toInteger (value) {
  376. return parseInt(value, 10) || 0;
  377. }
  378. // Borrowed from Lodash (_.deburr)
  379. /** Used to map Latin Unicode letters to basic Latin letters. */
  380. var deburredLetters = {
  381. // Latin-1 Supplement block.
  382. '\xc0': 'A', '\xc1': 'A', '\xc2': 'A', '\xc3': 'A', '\xc4': 'A', '\xc5': 'A',
  383. '\xe0': 'a', '\xe1': 'a', '\xe2': 'a', '\xe3': 'a', '\xe4': 'a', '\xe5': 'a',
  384. '\xc7': 'C', '\xe7': 'c',
  385. '\xd0': 'D', '\xf0': 'd',
  386. '\xc8': 'E', '\xc9': 'E', '\xca': 'E', '\xcb': 'E',
  387. '\xe8': 'e', '\xe9': 'e', '\xea': 'e', '\xeb': 'e',
  388. '\xcc': 'I', '\xcd': 'I', '\xce': 'I', '\xcf': 'I',
  389. '\xec': 'i', '\xed': 'i', '\xee': 'i', '\xef': 'i',
  390. '\xd1': 'N', '\xf1': 'n',
  391. '\xd2': 'O', '\xd3': 'O', '\xd4': 'O', '\xd5': 'O', '\xd6': 'O', '\xd8': 'O',
  392. '\xf2': 'o', '\xf3': 'o', '\xf4': 'o', '\xf5': 'o', '\xf6': 'o', '\xf8': 'o',
  393. '\xd9': 'U', '\xda': 'U', '\xdb': 'U', '\xdc': 'U',
  394. '\xf9': 'u', '\xfa': 'u', '\xfb': 'u', '\xfc': 'u',
  395. '\xdd': 'Y', '\xfd': 'y', '\xff': 'y',
  396. '\xc6': 'Ae', '\xe6': 'ae',
  397. '\xde': 'Th', '\xfe': 'th',
  398. '\xdf': 'ss',
  399. // Latin Extended-A block.
  400. '\u0100': 'A', '\u0102': 'A', '\u0104': 'A',
  401. '\u0101': 'a', '\u0103': 'a', '\u0105': 'a',
  402. '\u0106': 'C', '\u0108': 'C', '\u010a': 'C', '\u010c': 'C',
  403. '\u0107': 'c', '\u0109': 'c', '\u010b': 'c', '\u010d': 'c',
  404. '\u010e': 'D', '\u0110': 'D', '\u010f': 'd', '\u0111': 'd',
  405. '\u0112': 'E', '\u0114': 'E', '\u0116': 'E', '\u0118': 'E', '\u011a': 'E',
  406. '\u0113': 'e', '\u0115': 'e', '\u0117': 'e', '\u0119': 'e', '\u011b': 'e',
  407. '\u011c': 'G', '\u011e': 'G', '\u0120': 'G', '\u0122': 'G',
  408. '\u011d': 'g', '\u011f': 'g', '\u0121': 'g', '\u0123': 'g',
  409. '\u0124': 'H', '\u0126': 'H', '\u0125': 'h', '\u0127': 'h',
  410. '\u0128': 'I', '\u012a': 'I', '\u012c': 'I', '\u012e': 'I', '\u0130': 'I',
  411. '\u0129': 'i', '\u012b': 'i', '\u012d': 'i', '\u012f': 'i', '\u0131': 'i',
  412. '\u0134': 'J', '\u0135': 'j',
  413. '\u0136': 'K', '\u0137': 'k', '\u0138': 'k',
  414. '\u0139': 'L', '\u013b': 'L', '\u013d': 'L', '\u013f': 'L', '\u0141': 'L',
  415. '\u013a': 'l', '\u013c': 'l', '\u013e': 'l', '\u0140': 'l', '\u0142': 'l',
  416. '\u0143': 'N', '\u0145': 'N', '\u0147': 'N', '\u014a': 'N',
  417. '\u0144': 'n', '\u0146': 'n', '\u0148': 'n', '\u014b': 'n',
  418. '\u014c': 'O', '\u014e': 'O', '\u0150': 'O',
  419. '\u014d': 'o', '\u014f': 'o', '\u0151': 'o',
  420. '\u0154': 'R', '\u0156': 'R', '\u0158': 'R',
  421. '\u0155': 'r', '\u0157': 'r', '\u0159': 'r',
  422. '\u015a': 'S', '\u015c': 'S', '\u015e': 'S', '\u0160': 'S',
  423. '\u015b': 's', '\u015d': 's', '\u015f': 's', '\u0161': 's',
  424. '\u0162': 'T', '\u0164': 'T', '\u0166': 'T',
  425. '\u0163': 't', '\u0165': 't', '\u0167': 't',
  426. '\u0168': 'U', '\u016a': 'U', '\u016c': 'U', '\u016e': 'U', '\u0170': 'U', '\u0172': 'U',
  427. '\u0169': 'u', '\u016b': 'u', '\u016d': 'u', '\u016f': 'u', '\u0171': 'u', '\u0173': 'u',
  428. '\u0174': 'W', '\u0175': 'w',
  429. '\u0176': 'Y', '\u0177': 'y', '\u0178': 'Y',
  430. '\u0179': 'Z', '\u017b': 'Z', '\u017d': 'Z',
  431. '\u017a': 'z', '\u017c': 'z', '\u017e': 'z',
  432. '\u0132': 'IJ', '\u0133': 'ij',
  433. '\u0152': 'Oe', '\u0153': 'oe',
  434. '\u0149': "'n", '\u017f': 's'
  435. };
  436. /** Used to match Latin Unicode letters (excluding mathematical operators). */
  437. var reLatin = /[\xc0-\xd6\xd8-\xf6\xf8-\xff\u0100-\u017f]/g;
  438. /** Used to compose unicode character classes. */
  439. var rsComboMarksRange = '\\u0300-\\u036f',
  440. reComboHalfMarksRange = '\\ufe20-\\ufe2f',
  441. rsComboSymbolsRange = '\\u20d0-\\u20ff',
  442. rsComboMarksExtendedRange = '\\u1ab0-\\u1aff',
  443. rsComboMarksSupplementRange = '\\u1dc0-\\u1dff',
  444. rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange + rsComboMarksExtendedRange + rsComboMarksSupplementRange;
  445. /** Used to compose unicode capture groups. */
  446. var rsCombo = '[' + rsComboRange + ']';
  447. /**
  448. * Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks) and
  449. * [combining diacritical marks for symbols](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks_for_Symbols).
  450. */
  451. var reComboMark = RegExp(rsCombo, 'g');
  452. function deburrLetter (key) {
  453. return deburredLetters[key];
  454. };
  455. function normalizeToBase (string) {
  456. string = string.toString();
  457. return string && string.replace(reLatin, deburrLetter).replace(reComboMark, '');
  458. }
  459. // List of HTML entities for escaping.
  460. var escapeMap = {
  461. '&': '&amp;',
  462. '<': '&lt;',
  463. '>': '&gt;',
  464. '"': '&quot;',
  465. "'": '&#x27;',
  466. '`': '&#x60;'
  467. };
  468. // Functions for escaping and unescaping strings to/from HTML interpolation.
  469. var createEscaper = function (map) {
  470. var escaper = function (match) {
  471. return map[match];
  472. };
  473. // Regexes for identifying a key that needs to be escaped.
  474. var source = '(?:' + Object.keys(map).join('|') + ')';
  475. var testRegexp = RegExp(source);
  476. var replaceRegexp = RegExp(source, 'g');
  477. return function (string) {
  478. string = string == null ? '' : '' + string;
  479. return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
  480. };
  481. };
  482. var htmlEscape = createEscaper(escapeMap);
  483. /**
  484. * ------------------------------------------------------------------------
  485. * Constants
  486. * ------------------------------------------------------------------------
  487. */
  488. var keyCodeMap = {
  489. 32: ' ',
  490. 48: '0',
  491. 49: '1',
  492. 50: '2',
  493. 51: '3',
  494. 52: '4',
  495. 53: '5',
  496. 54: '6',
  497. 55: '7',
  498. 56: '8',
  499. 57: '9',
  500. 59: ';',
  501. 65: 'A',
  502. 66: 'B',
  503. 67: 'C',
  504. 68: 'D',
  505. 69: 'E',
  506. 70: 'F',
  507. 71: 'G',
  508. 72: 'H',
  509. 73: 'I',
  510. 74: 'J',
  511. 75: 'K',
  512. 76: 'L',
  513. 77: 'M',
  514. 78: 'N',
  515. 79: 'O',
  516. 80: 'P',
  517. 81: 'Q',
  518. 82: 'R',
  519. 83: 'S',
  520. 84: 'T',
  521. 85: 'U',
  522. 86: 'V',
  523. 87: 'W',
  524. 88: 'X',
  525. 89: 'Y',
  526. 90: 'Z',
  527. 96: '0',
  528. 97: '1',
  529. 98: '2',
  530. 99: '3',
  531. 100: '4',
  532. 101: '5',
  533. 102: '6',
  534. 103: '7',
  535. 104: '8',
  536. 105: '9'
  537. };
  538. var keyCodes = {
  539. ESCAPE: 27, // KeyboardEvent.which value for Escape (Esc) key
  540. ENTER: 13, // KeyboardEvent.which value for Enter key
  541. SPACE: 32, // KeyboardEvent.which value for space key
  542. TAB: 9, // KeyboardEvent.which value for tab key
  543. ARROW_UP: 38, // KeyboardEvent.which value for up arrow key
  544. ARROW_DOWN: 40 // KeyboardEvent.which value for down arrow key
  545. }
  546. var version = {
  547. success: false,
  548. major: '3'
  549. };
  550. try {
  551. version.full = ($.fn.dropdown.Constructor.VERSION || '').split(' ')[0].split('.');
  552. version.major = version.full[0];
  553. version.success = true;
  554. } catch (err) {
  555. // do nothing
  556. }
  557. var selectId = 0;
  558. var EVENT_KEY = '.bs.select';
  559. var classNames = {
  560. DISABLED: 'disabled',
  561. DIVIDER: 'divider',
  562. SHOW: 'open',
  563. DROPUP: 'dropup',
  564. MENU: 'dropdown-menu',
  565. MENURIGHT: 'dropdown-menu-right',
  566. MENULEFT: 'dropdown-menu-left',
  567. // to-do: replace with more advanced template/customization options
  568. BUTTONCLASS: 'btn-default',
  569. POPOVERHEADER: 'popover-title',
  570. ICONBASE: 'glyphicon',
  571. TICKICON: 'glyphicon-ok'
  572. }
  573. var Selector = {
  574. MENU: '.' + classNames.MENU
  575. }
  576. var elementTemplates = {
  577. span: document.createElement('span'),
  578. i: document.createElement('i'),
  579. subtext: document.createElement('small'),
  580. a: document.createElement('a'),
  581. li: document.createElement('li'),
  582. whitespace: document.createTextNode('\u00A0'),
  583. fragment: document.createDocumentFragment()
  584. }
  585. elementTemplates.a.setAttribute('role', 'option');
  586. elementTemplates.subtext.className = 'text-muted';
  587. elementTemplates.text = elementTemplates.span.cloneNode(false);
  588. elementTemplates.text.className = 'text';
  589. elementTemplates.checkMark = elementTemplates.span.cloneNode(false);
  590. var REGEXP_ARROW = new RegExp(keyCodes.ARROW_UP + '|' + keyCodes.ARROW_DOWN);
  591. var REGEXP_TAB_OR_ESCAPE = new RegExp('^' + keyCodes.TAB + '$|' + keyCodes.ESCAPE);
  592. var generateOption = {
  593. li: function (content, classes, optgroup) {
  594. var li = elementTemplates.li.cloneNode(false);
  595. if (content) {
  596. if (content.nodeType === 1 || content.nodeType === 11) {
  597. li.appendChild(content);
  598. } else {
  599. li.innerHTML = content;
  600. }
  601. }
  602. if (typeof classes !== 'undefined' && classes !== '') li.className = classes;
  603. if (typeof optgroup !== 'undefined' && optgroup !== null) li.classList.add('optgroup-' + optgroup);
  604. return li;
  605. },
  606. a: function (text, classes, inline) {
  607. var a = elementTemplates.a.cloneNode(true);
  608. if (text) {
  609. if (text.nodeType === 11) {
  610. a.appendChild(text);
  611. } else {
  612. a.insertAdjacentHTML('beforeend', text);
  613. }
  614. }
  615. if (typeof classes !== 'undefined' && classes !== '') a.className = classes;
  616. if (version.major === '4') a.classList.add('dropdown-item');
  617. if (inline) a.setAttribute('style', inline);
  618. return a;
  619. },
  620. text: function (options, useFragment) {
  621. var textElement = elementTemplates.text.cloneNode(false),
  622. subtextElement,
  623. iconElement;
  624. if (options.content) {
  625. textElement.innerHTML = options.content;
  626. } else {
  627. textElement.textContent = options.text;
  628. if (options.icon) {
  629. var whitespace = elementTemplates.whitespace.cloneNode(false);
  630. // need to use <i> for icons in the button to prevent a breaking change
  631. // note: switch to span in next major release
  632. iconElement = (useFragment === true ? elementTemplates.i : elementTemplates.span).cloneNode(false);
  633. iconElement.className = options.iconBase + ' ' + options.icon;
  634. elementTemplates.fragment.appendChild(iconElement);
  635. elementTemplates.fragment.appendChild(whitespace);
  636. }
  637. if (options.subtext) {
  638. subtextElement = elementTemplates.subtext.cloneNode(false);
  639. subtextElement.textContent = options.subtext;
  640. textElement.appendChild(subtextElement);
  641. }
  642. }
  643. if (useFragment === true) {
  644. while (textElement.childNodes.length > 0) {
  645. elementTemplates.fragment.appendChild(textElement.childNodes[0]);
  646. }
  647. } else {
  648. elementTemplates.fragment.appendChild(textElement);
  649. }
  650. return elementTemplates.fragment;
  651. },
  652. label: function (options) {
  653. var textElement = elementTemplates.text.cloneNode(false),
  654. subtextElement,
  655. iconElement;
  656. textElement.innerHTML = options.label;
  657. if (options.icon) {
  658. var whitespace = elementTemplates.whitespace.cloneNode(false);
  659. iconElement = elementTemplates.span.cloneNode(false);
  660. iconElement.className = options.iconBase + ' ' + options.icon;
  661. elementTemplates.fragment.appendChild(iconElement);
  662. elementTemplates.fragment.appendChild(whitespace);
  663. }
  664. if (options.subtext) {
  665. subtextElement = elementTemplates.subtext.cloneNode(false);
  666. subtextElement.textContent = options.subtext;
  667. textElement.appendChild(subtextElement);
  668. }
  669. elementTemplates.fragment.appendChild(textElement);
  670. return elementTemplates.fragment;
  671. }
  672. }
  673. var Selectpicker = function (element, options) {
  674. var that = this;
  675. // bootstrap-select has been initialized - revert valHooks.select.set back to its original function
  676. if (!valHooks.useDefault) {
  677. $.valHooks.select.set = valHooks._set;
  678. valHooks.useDefault = true;
  679. }
  680. this.$element = $(element);
  681. this.$newElement = null;
  682. this.$button = null;
  683. this.$menu = null;
  684. this.options = options;
  685. this.selectpicker = {
  686. main: {},
  687. current: {}, // current changes if a search is in progress
  688. search: {},
  689. view: {},
  690. keydown: {
  691. keyHistory: '',
  692. resetKeyHistory: {
  693. start: function () {
  694. return setTimeout(function () {
  695. that.selectpicker.keydown.keyHistory = '';
  696. }, 800);
  697. }
  698. }
  699. }
  700. };
  701. // 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
  702. // data-attribute)
  703. if (this.options.title === null) {
  704. this.options.title = this.$element.attr('title');
  705. }
  706. // Format window padding
  707. var winPad = this.options.windowPadding;
  708. if (typeof winPad === 'number') {
  709. this.options.windowPadding = [winPad, winPad, winPad, winPad];
  710. }
  711. // Expose public methods
  712. this.val = Selectpicker.prototype.val;
  713. this.render = Selectpicker.prototype.render;
  714. this.refresh = Selectpicker.prototype.refresh;
  715. this.setStyle = Selectpicker.prototype.setStyle;
  716. this.selectAll = Selectpicker.prototype.selectAll;
  717. this.deselectAll = Selectpicker.prototype.deselectAll;
  718. this.destroy = Selectpicker.prototype.destroy;
  719. this.remove = Selectpicker.prototype.remove;
  720. this.show = Selectpicker.prototype.show;
  721. this.hide = Selectpicker.prototype.hide;
  722. this.init();
  723. };
  724. Selectpicker.VERSION = '1.13.9';
  725. // part of this is duplicated in i18n/defaults-en_US.js. Make sure to update both.
  726. Selectpicker.DEFAULTS = {
  727. noneSelectedText: '--δѡÖÐ--',
  728. noneResultsText: 'δËÑË÷µ½ {0}',
  729. countSelectedText: function (numSelected, numTotal) {
  730. return (numSelected == 1) ? '{0} item selected' : '{0} items selected';
  731. },
  732. maxOptionsText: function (numAll, numGroup) {
  733. return [
  734. (numAll == 1) ? 'Limit reached ({n} item max)' : 'Limit reached ({n} items max)',
  735. (numGroup == 1) ? 'Group limit reached ({n} item max)' : 'Group limit reached ({n} items max)'
  736. ];
  737. },
  738. selectAllText: 'Select All',
  739. deselectAllText: 'Deselect All',
  740. doneButton: false,
  741. doneButtonText: 'Close',
  742. multipleSeparator: ', ',
  743. styleBase: 'btn',
  744. style: classNames.BUTTONCLASS,
  745. size: 'auto',
  746. title: null,
  747. selectedTextFormat: 'values',
  748. width: false,
  749. container: false,
  750. hideDisabled: false,
  751. showSubtext: false,
  752. showIcon: true,
  753. showContent: true,
  754. dropupAuto: true,
  755. header: false,
  756. liveSearch: false,
  757. liveSearchPlaceholder: null,
  758. liveSearchNormalize: false,
  759. liveSearchStyle: 'contains',
  760. actionsBox: false,
  761. iconBase: classNames.ICONBASE,
  762. tickIcon: classNames.TICKICON,
  763. showTick: false,
  764. template: {
  765. caret: '<span class="caret"></span>'
  766. },
  767. maxOptions: false,
  768. mobile: false,
  769. selectOnTab: false,
  770. dropdownAlignRight: false,
  771. windowPadding: 0,
  772. virtualScroll: 600,
  773. display: false,
  774. sanitize: true,
  775. sanitizeFn: null,
  776. whiteList: DefaultWhitelist
  777. };
  778. Selectpicker.prototype = {
  779. constructor: Selectpicker,
  780. init: function () {
  781. var that = this,
  782. id = this.$element.attr('id');
  783. this.selectId = selectId++;
  784. this.$element[0].classList.add('bs-select-hidden');
  785. this.multiple = this.$element.prop('multiple');
  786. this.autofocus = this.$element.prop('autofocus');
  787. this.options.showTick = this.$element[0].classList.contains('show-tick');
  788. this.$newElement = this.createDropdown();
  789. this.$element
  790. .after(this.$newElement)
  791. .prependTo(this.$newElement);
  792. this.$button = this.$newElement.children('button');
  793. this.$menu = this.$newElement.children(Selector.MENU);
  794. this.$menuInner = this.$menu.children('.inner');
  795. this.$searchbox = this.$menu.find('input');
  796. this.$element[0].classList.remove('bs-select-hidden');
  797. if (this.options.dropdownAlignRight === true) this.$menu[0].classList.add(classNames.MENURIGHT);
  798. if (typeof id !== 'undefined') {
  799. this.$button.attr('data-id', id);
  800. }
  801. this.checkDisabled();
  802. this.clickListener();
  803. if (this.options.liveSearch) this.liveSearchListener();
  804. this.setStyle();
  805. this.render();
  806. this.setWidth();
  807. if (this.options.container) {
  808. this.selectPosition();
  809. } else {
  810. this.$element.on('hide' + EVENT_KEY, function () {
  811. if (that.isVirtual()) {
  812. // empty menu on close
  813. var menuInner = that.$menuInner[0],
  814. emptyMenu = menuInner.firstChild.cloneNode(false);
  815. // replace the existing UL with an empty one - this is faster than $.empty() or innerHTML = ''
  816. menuInner.replaceChild(emptyMenu, menuInner.firstChild);
  817. menuInner.scrollTop = 0;
  818. }
  819. });
  820. }
  821. this.$menu.data('this', this);
  822. this.$newElement.data('this', this);
  823. if (this.options.mobile) this.mobile();
  824. this.$newElement.on({
  825. 'hide.bs.dropdown': function (e) {
  826. that.$menuInner.attr('aria-expanded', false);
  827. that.$element.trigger('hide' + EVENT_KEY, e);
  828. },
  829. 'hidden.bs.dropdown': function (e) {
  830. that.$element.trigger('hidden' + EVENT_KEY, e);
  831. },
  832. 'show.bs.dropdown': function (e) {
  833. that.$menuInner.attr('aria-expanded', true);
  834. that.$element.trigger('show' + EVENT_KEY, e);
  835. },
  836. 'shown.bs.dropdown': function (e) {
  837. that.$element.trigger('shown' + EVENT_KEY, e);
  838. }
  839. });
  840. if (that.$element[0].hasAttribute('required')) {
  841. this.$element.on('invalid' + EVENT_KEY, function () {
  842. that.$button[0].classList.add('bs-invalid');
  843. that.$element
  844. .on('shown' + EVENT_KEY + '.invalid', function () {
  845. that.$element
  846. .val(that.$element.val()) // set the value to hide the validation message in Chrome when menu is opened
  847. .off('shown' + EVENT_KEY + '.invalid');
  848. })
  849. .on('rendered' + EVENT_KEY, function () {
  850. // if select is no longer invalid, remove the bs-invalid class
  851. if (this.validity.valid) that.$button[0].classList.remove('bs-invalid');
  852. that.$element.off('rendered' + EVENT_KEY);
  853. });
  854. that.$button.on('blur' + EVENT_KEY, function () {
  855. that.$element.trigger('focus').trigger('blur');
  856. that.$button.off('blur' + EVENT_KEY);
  857. });
  858. });
  859. }
  860. setTimeout(function () {
  861. that.createLi();
  862. that.$element.trigger('loaded' + EVENT_KEY);
  863. });
  864. },
  865. createDropdown: function () {
  866. // Options
  867. // If we are multiple or showTick option is set, then add the show-tick class
  868. var showTick = (this.multiple || this.options.showTick) ? ' show-tick' : '',
  869. inputGroup = '',
  870. autofocus = this.autofocus ? ' autofocus' : '';
  871. if (version.major < 4 && this.$element.parent().hasClass('input-group')) {
  872. inputGroup = ' input-group-btn';
  873. }
  874. // Elements
  875. var drop,
  876. header = '',
  877. searchbox = '',
  878. actionsbox = '',
  879. donebutton = '';
  880. if (this.options.header) {
  881. header =
  882. '<div class="' + classNames.POPOVERHEADER + '">' +
  883. '<button type="button" class="close" aria-hidden="true">&times;</button>' +
  884. this.options.header +
  885. '</div>';
  886. }
  887. if (this.options.liveSearch) {
  888. searchbox =
  889. '<div class="bs-searchbox">' +
  890. '<input type="text" class="form-control" autocomplete="off"' +
  891. (
  892. this.options.liveSearchPlaceholder === null ? ''
  893. :
  894. ' placeholder="' + htmlEscape(this.options.liveSearchPlaceholder) + '"'
  895. ) +
  896. ' role="textbox" aria-label="Search">' +
  897. '</div>';
  898. }
  899. if (this.multiple && this.options.actionsBox) {
  900. actionsbox =
  901. '<div class="bs-actionsbox">' +
  902. '<div class="btn-group btn-group-sm btn-block">' +
  903. '<button type="button" class="actions-btn bs-select-all btn ' + classNames.BUTTONCLASS + '">' +
  904. this.options.selectAllText +
  905. '</button>' +
  906. '<button type="button" class="actions-btn bs-deselect-all btn ' + classNames.BUTTONCLASS + '">' +
  907. this.options.deselectAllText +
  908. '</button>' +
  909. '</div>' +
  910. '</div>';
  911. }
  912. if (this.multiple && this.options.doneButton) {
  913. donebutton =
  914. '<div class="bs-donebutton">' +
  915. '<div class="btn-group btn-block">' +
  916. '<button type="button" class="btn btn-sm ' + classNames.BUTTONCLASS + '">' +
  917. this.options.doneButtonText +
  918. '</button>' +
  919. '</div>' +
  920. '</div>';
  921. }
  922. drop =
  923. '<div class="dropdown bootstrap-select' + showTick + inputGroup + '">' +
  924. '<button type="button" class="' + this.options.styleBase + ' dropdown-toggle" ' + (this.options.display === 'static' ? 'data-display="static"' : '') + 'data-toggle="dropdown"' + autofocus + ' role="button">' +
  925. '<div class="filter-option">' +
  926. '<div class="filter-option-inner">' +
  927. '<div class="filter-option-inner-inner"></div>' +
  928. '</div> ' +
  929. '</div>' +
  930. (
  931. version.major === '4' ? ''
  932. :
  933. '<span class="bs-caret">' +
  934. this.options.template.caret +
  935. '</span>'
  936. ) +
  937. '</button>' +
  938. '<div class="' + classNames.MENU + ' ' + (version.major === '4' ? '' : classNames.SHOW) + '" role="combobox">' +
  939. header +
  940. searchbox +
  941. actionsbox +
  942. '<div class="inner ' + classNames.SHOW + '" role="listbox" aria-expanded="false" tabindex="-1">' +
  943. '<ul class="' + classNames.MENU + ' inner ' + (version.major === '4' ? classNames.SHOW : '') + '">' +
  944. '</ul>' +
  945. '</div>' +
  946. donebutton +
  947. '</div>' +
  948. '</div>';
  949. return $(drop);
  950. },
  951. setPositionData: function () {
  952. this.selectpicker.view.canHighlight = [];
  953. for (var i = 0; i < this.selectpicker.current.data.length; i++) {
  954. var li = this.selectpicker.current.data[i],
  955. canHighlight = true;
  956. if (li.type === 'divider') {
  957. canHighlight = false;
  958. li.height = this.sizeInfo.dividerHeight;
  959. } else if (li.type === 'optgroup-label') {
  960. canHighlight = false;
  961. li.height = this.sizeInfo.dropdownHeaderHeight;
  962. } else {
  963. li.height = this.sizeInfo.liHeight;
  964. }
  965. if (li.disabled) canHighlight = false;
  966. this.selectpicker.view.canHighlight.push(canHighlight);
  967. li.position = (i === 0 ? 0 : this.selectpicker.current.data[i - 1].position) + li.height;
  968. }
  969. },
  970. isVirtual: function () {
  971. return (this.options.virtualScroll !== false) && (this.selectpicker.main.elements.length >= this.options.virtualScroll) || this.options.virtualScroll === true;
  972. },
  973. createView: function (isSearching, scrollTop) {
  974. scrollTop = scrollTop || 0;
  975. var that = this;
  976. this.selectpicker.current = isSearching ? this.selectpicker.search : this.selectpicker.main;
  977. var active = [];
  978. var selected;
  979. var prevActive;
  980. this.setPositionData();
  981. scroll(scrollTop, true);
  982. this.$menuInner.off('scroll.createView').on('scroll.createView', function (e, updateValue) {
  983. if (!that.noScroll) scroll(this.scrollTop, updateValue);
  984. that.noScroll = false;
  985. });
  986. function scroll (scrollTop, init) {
  987. var size = that.selectpicker.current.elements.length,
  988. chunks = [],
  989. chunkSize,
  990. chunkCount,
  991. firstChunk,
  992. lastChunk,
  993. currentChunk,
  994. prevPositions,
  995. positionIsDifferent,
  996. previousElements,
  997. menuIsDifferent = true,
  998. isVirtual = that.isVirtual();
  999. that.selectpicker.view.scrollTop = scrollTop;
  1000. if (isVirtual === true) {
  1001. // if an option that is encountered that is wider than the current menu width, update the menu width accordingly
  1002. if (that.sizeInfo.hasScrollBar && that.$menu[0].offsetWidth > that.sizeInfo.totalMenuWidth) {
  1003. that.sizeInfo.menuWidth = that.$menu[0].offsetWidth;
  1004. that.sizeInfo.totalMenuWidth = that.sizeInfo.menuWidth + that.sizeInfo.scrollBarWidth;
  1005. that.$menu.css('min-width', that.sizeInfo.menuWidth);
  1006. }
  1007. }
  1008. chunkSize = Math.ceil(that.sizeInfo.menuInnerHeight / that.sizeInfo.liHeight * 1.5); // number of options in a chunk
  1009. chunkCount = Math.round(size / chunkSize) || 1; // number of chunks
  1010. for (var i = 0; i < chunkCount; i++) {
  1011. var endOfChunk = (i + 1) * chunkSize;
  1012. if (i === chunkCount - 1) {
  1013. endOfChunk = size;
  1014. }
  1015. chunks[i] = [
  1016. (i) * chunkSize + (!i ? 0 : 1),
  1017. endOfChunk
  1018. ];
  1019. if (!size) break;
  1020. if (currentChunk === undefined && scrollTop <= that.selectpicker.current.data[endOfChunk - 1].position - that.sizeInfo.menuInnerHeight) {
  1021. currentChunk = i;
  1022. }
  1023. }
  1024. if (currentChunk === undefined) currentChunk = 0;
  1025. prevPositions = [that.selectpicker.view.position0, that.selectpicker.view.position1];
  1026. // always display previous, current, and next chunks
  1027. firstChunk = Math.max(0, currentChunk - 1);
  1028. lastChunk = Math.min(chunkCount - 1, currentChunk + 1);
  1029. that.selectpicker.view.position0 = isVirtual === false ? 0 : (Math.max(0, chunks[firstChunk][0]) || 0);
  1030. that.selectpicker.view.position1 = isVirtual === false ? size : (Math.min(size, chunks[lastChunk][1]) || 0);
  1031. positionIsDifferent = prevPositions[0] !== that.selectpicker.view.position0 || prevPositions[1] !== that.selectpicker.view.position1;
  1032. if (that.activeIndex !== undefined) {
  1033. prevActive = that.selectpicker.main.elements[that.prevActiveIndex];
  1034. active = that.selectpicker.main.elements[that.activeIndex];
  1035. selected = that.selectpicker.main.elements[that.selectedIndex];
  1036. if (init) {
  1037. if (that.activeIndex !== that.selectedIndex && active && active.length) {
  1038. active.classList.remove('active');
  1039. if (active.firstChild) active.firstChild.classList.remove('active');
  1040. }
  1041. that.activeIndex = undefined;
  1042. }
  1043. if (that.activeIndex && that.activeIndex !== that.selectedIndex && selected && selected.length) {
  1044. selected.classList.remove('active');
  1045. if (selected.firstChild) selected.firstChild.classList.remove('active');
  1046. }
  1047. }
  1048. if (that.prevActiveIndex !== undefined && that.prevActiveIndex !== that.activeIndex && that.prevActiveIndex !== that.selectedIndex && prevActive && prevActive.length) {
  1049. prevActive.classList.remove('active');
  1050. if (prevActive.firstChild) prevActive.firstChild.classList.remove('active');
  1051. }
  1052. if (init || positionIsDifferent) {
  1053. previousElements = that.selectpicker.view.visibleElements ? that.selectpicker.view.visibleElements.slice() : [];
  1054. if (isVirtual === false) {
  1055. that.selectpicker.view.visibleElements = that.selectpicker.current.elements;
  1056. } else {
  1057. that.selectpicker.view.visibleElements = that.selectpicker.current.elements.slice(that.selectpicker.view.position0, that.selectpicker.view.position1);
  1058. }
  1059. that.setOptionStatus();
  1060. // if searching, check to make sure the list has actually been updated before updating DOM
  1061. // this prevents unnecessary repaints
  1062. if (isSearching || (isVirtual === false && init)) menuIsDifferent = !isEqual(previousElements, that.selectpicker.view.visibleElements);
  1063. // if virtual scroll is disabled and not searching,
  1064. // menu should never need to be updated more than once
  1065. if ((init || isVirtual === true) && menuIsDifferent) {
  1066. var menuInner = that.$menuInner[0],
  1067. menuFragment = document.createDocumentFragment(),
  1068. emptyMenu = menuInner.firstChild.cloneNode(false),
  1069. marginTop,
  1070. marginBottom,
  1071. elements = that.selectpicker.view.visibleElements,
  1072. toSanitize = [];
  1073. // replace the existing UL with an empty one - this is faster than $.empty()
  1074. menuInner.replaceChild(emptyMenu, menuInner.firstChild);
  1075. for (var i = 0, visibleElementsLen = elements.length; i < visibleElementsLen; i++) {
  1076. var element = elements[i],
  1077. elText,
  1078. elementData;
  1079. if (that.options.sanitize) {
  1080. elText = element.lastChild;
  1081. if (elText) {
  1082. elementData = that.selectpicker.current.data[i + that.selectpicker.view.position0];
  1083. if (elementData && elementData.content && !elementData.sanitized) {
  1084. toSanitize.push(elText);
  1085. elementData.sanitized = true;
  1086. }
  1087. }
  1088. }
  1089. menuFragment.appendChild(element);
  1090. }
  1091. if (that.options.sanitize && toSanitize.length) {
  1092. sanitizeHtml(toSanitize, that.options.whiteList, that.options.sanitizeFn);
  1093. }
  1094. if (isVirtual === true) {
  1095. marginTop = (that.selectpicker.view.position0 === 0 ? 0 : that.selectpicker.current.data[that.selectpicker.view.position0 - 1].position);
  1096. marginBottom = (that.selectpicker.view.position1 > size - 1 ? 0 : that.selectpicker.current.data[size - 1].position - that.selectpicker.current.data[that.selectpicker.view.position1 - 1].position);
  1097. menuInner.firstChild.style.marginTop = marginTop + 'px';
  1098. menuInner.firstChild.style.marginBottom = marginBottom + 'px';
  1099. }
  1100. menuInner.firstChild.appendChild(menuFragment);
  1101. }
  1102. }
  1103. that.prevActiveIndex = that.activeIndex;
  1104. if (!that.options.liveSearch) {
  1105. that.$menuInner.trigger('focus');
  1106. } else if (isSearching && init) {
  1107. var index = 0,
  1108. newActive;
  1109. if (!that.selectpicker.view.canHighlight[index]) {
  1110. index = 1 + that.selectpicker.view.canHighlight.slice(1).indexOf(true);
  1111. }
  1112. newActive = that.selectpicker.view.visibleElements[index];
  1113. if (that.selectpicker.view.currentActive) {
  1114. that.selectpicker.view.currentActive.classList.remove('active');
  1115. if (that.selectpicker.view.currentActive.firstChild) that.selectpicker.view.currentActive.firstChild.classList.remove('active');
  1116. }
  1117. if (newActive) {
  1118. newActive.classList.add('active');
  1119. if (newActive.firstChild) newActive.firstChild.classList.add('active');
  1120. }
  1121. that.activeIndex = (that.selectpicker.current.data[index] || {}).index;
  1122. }
  1123. }
  1124. $(window)
  1125. .off('resize' + EVENT_KEY + '.' + this.selectId + '.createView')
  1126. .on('resize' + EVENT_KEY + '.' + this.selectId + '.createView', function () {
  1127. var isActive = that.$newElement.hasClass(classNames.SHOW);
  1128. if (isActive) scroll(that.$menuInner[0].scrollTop);
  1129. });
  1130. },
  1131. setPlaceholder: function () {
  1132. var updateIndex = false;
  1133. if (this.options.title && !this.multiple) {
  1134. if (!this.selectpicker.view.titleOption) this.selectpicker.view.titleOption = document.createElement('option');
  1135. // this option doesn't create a new <li> element, but does add a new option at the start,
  1136. // so startIndex should increase to prevent having to check every option for the bs-title-option class
  1137. updateIndex = true;
  1138. var element = this.$element[0],
  1139. isSelected = false,
  1140. titleNotAppended = !this.selectpicker.view.titleOption.parentNode;
  1141. if (titleNotAppended) {
  1142. // Use native JS to prepend option (faster)
  1143. this.selectpicker.view.titleOption.className = 'bs-title-option';
  1144. this.selectpicker.view.titleOption.value = '';
  1145. // Check if selected or data-selected attribute is already set on an option. If not, select the titleOption option.
  1146. // the selected item may have been changed by user or programmatically before the bootstrap select plugin runs,
  1147. // if so, the select will have the data-selected attribute
  1148. var $opt = $(element.options[element.selectedIndex]);
  1149. isSelected = $opt.attr('selected') === undefined && this.$element.data('selected') === undefined;
  1150. }
  1151. if (titleNotAppended || this.selectpicker.view.titleOption.index !== 0) {
  1152. element.insertBefore(this.selectpicker.view.titleOption, element.firstChild);
  1153. }
  1154. // Set selected *after* appending to select,
  1155. // otherwise the option doesn't get selected in IE
  1156. // set using selectedIndex, as setting the selected attr to true here doesn't work in IE11
  1157. if (isSelected) element.selectedIndex = 0;
  1158. }
  1159. return updateIndex;
  1160. },
  1161. createLi: function () {
  1162. var that = this,
  1163. iconBase = this.options.iconBase,
  1164. optionSelector = ':not([hidden]):not([data-hidden="true"])',
  1165. mainElements = [],
  1166. mainData = [],
  1167. widestOptionLength = 0,
  1168. optID = 0,
  1169. startIndex = this.setPlaceholder() ? 1 : 0; // append the titleOption if necessary and skip the first option in the loop
  1170. if (this.options.hideDisabled) optionSelector += ':not(:disabled)';
  1171. if ((that.options.showTick || that.multiple) && !elementTemplates.checkMark.parentNode) {
  1172. elementTemplates.checkMark.className = iconBase + ' ' + that.options.tickIcon + ' check-mark';
  1173. elementTemplates.a.appendChild(elementTemplates.checkMark);
  1174. }
  1175. var selectOptions = this.$element[0].querySelectorAll('select > *' + optionSelector);
  1176. function addDivider (config) {
  1177. var previousData = mainData[mainData.length - 1];
  1178. // ensure optgroup doesn't create back-to-back dividers
  1179. if (
  1180. previousData &&
  1181. previousData.type === 'divider' &&
  1182. (previousData.optID || config.optID)
  1183. ) {
  1184. return;
  1185. }
  1186. config = config || {};
  1187. config.type = 'divider';
  1188. mainElements.push(
  1189. generateOption.li(
  1190. false,
  1191. classNames.DIVIDER,
  1192. (config.optID ? config.optID + 'div' : undefined)
  1193. )
  1194. );
  1195. mainData.push(config);
  1196. }
  1197. function addOption (option, config) {
  1198. config = config || {};
  1199. config.divider = option.getAttribute('data-divider') === 'true';
  1200. if (config.divider) {
  1201. addDivider({
  1202. optID: config.optID
  1203. });
  1204. } else {
  1205. var liIndex = mainData.length,
  1206. cssText = option.style.cssText,
  1207. inlineStyle = cssText ? htmlEscape(cssText) : '',
  1208. optionClass = (option.className || '') + (config.optgroupClass || '');
  1209. if (config.optID) optionClass = 'opt ' + optionClass;
  1210. config.text = option.textContent;
  1211. config.content = option.getAttribute('data-content');
  1212. config.tokens = option.getAttribute('data-tokens');
  1213. config.subtext = option.getAttribute('data-subtext');
  1214. config.icon = option.getAttribute('data-icon');
  1215. config.iconBase = iconBase;
  1216. var textElement = generateOption.text(config);
  1217. mainElements.push(
  1218. generateOption.li(
  1219. generateOption.a(
  1220. textElement,
  1221. optionClass,
  1222. inlineStyle
  1223. ),
  1224. '',
  1225. config.optID
  1226. )
  1227. );
  1228. option.liIndex = liIndex;
  1229. config.display = config.content || config.text;
  1230. config.type = 'option';
  1231. config.index = liIndex;
  1232. config.option = option;
  1233. config.disabled = config.disabled || option.disabled;
  1234. mainData.push(config);
  1235. var combinedLength = 0;
  1236. // count the number of characters in the option - not perfect, but should work in most cases
  1237. if (config.display) combinedLength += config.display.length;
  1238. if (config.subtext) combinedLength += config.subtext.length;
  1239. // if there is an icon, ensure this option's width is checked
  1240. if (config.icon) combinedLength += 1;
  1241. if (combinedLength > widestOptionLength) {
  1242. widestOptionLength = combinedLength;
  1243. // guess which option is the widest
  1244. // use this when calculating menu width
  1245. // not perfect, but it's fast, and the width will be updating accordingly when scrolling
  1246. that.selectpicker.view.widestOption = mainElements[mainElements.length - 1];
  1247. }
  1248. }
  1249. }
  1250. function addOptgroup (index, selectOptions) {
  1251. var optgroup = selectOptions[index],
  1252. previous = selectOptions[index - 1],
  1253. next = selectOptions[index + 1],
  1254. options = optgroup.querySelectorAll('option' + optionSelector);
  1255. if (!options.length) return;
  1256. var config = {
  1257. label: htmlEscape(optgroup.label),
  1258. subtext: optgroup.getAttribute('data-subtext'),
  1259. icon: optgroup.getAttribute('data-icon'),
  1260. iconBase: iconBase
  1261. },
  1262. optgroupClass = ' ' + (optgroup.className || ''),
  1263. headerIndex,
  1264. lastIndex;
  1265. optID++;
  1266. if (previous) {
  1267. addDivider({ optID: optID });
  1268. }
  1269. var labelElement = generateOption.label(config);
  1270. mainElements.push(
  1271. generateOption.li(labelElement, 'dropdown-header' + optgroupClass, optID)
  1272. );
  1273. mainData.push({
  1274. display: config.label,
  1275. subtext: config.subtext,
  1276. type: 'optgroup-label',
  1277. optID: optID
  1278. });
  1279. for (var j = 0, len = options.length; j < len; j++) {
  1280. var option = options[j];
  1281. if (j === 0) {
  1282. headerIndex = mainData.length - 1;
  1283. lastIndex = headerIndex + len;
  1284. }
  1285. addOption(option, {
  1286. headerIndex: headerIndex,
  1287. lastIndex: lastIndex,
  1288. optID: optID,
  1289. optgroupClass: optgroupClass,
  1290. disabled: optgroup.disabled
  1291. });
  1292. }
  1293. if (next) {
  1294. addDivider({ optID: optID });
  1295. }
  1296. }
  1297. for (var len = selectOptions.length; startIndex < len; startIndex++) {
  1298. var item = selectOptions[startIndex];
  1299. if (item.tagName !== 'OPTGROUP') {
  1300. addOption(item, {});
  1301. } else {
  1302. addOptgroup(startIndex, selectOptions);
  1303. }
  1304. }
  1305. this.selectpicker.main.elements = mainElements;
  1306. this.selectpicker.main.data = mainData;
  1307. this.selectpicker.current = this.selectpicker.main;
  1308. },
  1309. findLis: function () {
  1310. return this.$menuInner.find('.inner > li');
  1311. },
  1312. render: function () {
  1313. // ensure titleOption is appended and selected (if necessary) before getting selectedOptions
  1314. this.setPlaceholder();
  1315. var that = this,
  1316. selectedOptions = this.$element[0].selectedOptions,
  1317. selectedCount = selectedOptions.length,
  1318. button = this.$button[0],
  1319. buttonInner = button.querySelector('.filter-option-inner-inner'),
  1320. multipleSeparator = document.createTextNode(this.options.multipleSeparator),
  1321. titleFragment = elementTemplates.fragment.cloneNode(false),
  1322. showCount,
  1323. countMax,
  1324. hasContent = false;
  1325. this.togglePlaceholder();
  1326. this.tabIndex();
  1327. if (this.options.selectedTextFormat === 'static') {
  1328. titleFragment = generateOption.text({ text: this.options.title }, true);
  1329. } else {
  1330. showCount = this.multiple && this.options.selectedTextFormat.indexOf('count') !== -1 && selectedCount > 1;
  1331. // determine if the number of selected options will be shown (showCount === true)
  1332. if (showCount) {
  1333. countMax = this.options.selectedTextFormat.split('>');
  1334. showCount = (countMax.length > 1 && selectedCount > countMax[1]) || (countMax.length === 1 && selectedCount >= 2);
  1335. }
  1336. // only loop through all selected options if the count won't be shown
  1337. if (showCount === false) {
  1338. for (var selectedIndex = 0; selectedIndex < selectedCount; selectedIndex++) {
  1339. if (selectedIndex < 50) {
  1340. var option = selectedOptions[selectedIndex],
  1341. titleOptions = {},
  1342. thisData = {
  1343. content: option.getAttribute('data-content'),
  1344. subtext: option.getAttribute('data-subtext'),
  1345. icon: option.getAttribute('data-icon')
  1346. };
  1347. if (this.multiple && selectedIndex > 0) {
  1348. titleFragment.appendChild(multipleSeparator.cloneNode(false));
  1349. }
  1350. if (option.title) {
  1351. titleOptions.text = option.title;
  1352. } else if (thisData.content && that.options.showContent) {
  1353. titleOptions.content = thisData.content.toString();
  1354. hasContent = true;
  1355. } else {
  1356. if (that.options.showIcon) {
  1357. titleOptions.icon = thisData.icon;
  1358. titleOptions.iconBase = this.options.iconBase;
  1359. }
  1360. if (that.options.showSubtext && !that.multiple && thisData.subtext) titleOptions.subtext = ' ' + thisData.subtext;
  1361. titleOptions.text = option.textContent.trim();
  1362. }
  1363. titleFragment.appendChild(generateOption.text(titleOptions, true));
  1364. } else {
  1365. break;
  1366. }
  1367. }
  1368. // add ellipsis
  1369. if (selectedCount > 49) {
  1370. titleFragment.appendChild(document.createTextNode('...'));
  1371. }
  1372. } else {
  1373. var optionSelector = ':not([hidden]):not([data-hidden="true"]):not([data-divider="true"])';
  1374. if (this.options.hideDisabled) optionSelector += ':not(:disabled)';
  1375. // If this is a multiselect, and selectedTextFormat is count, then show 1 of 2 selected, etc.
  1376. var totalCount = this.$element[0].querySelectorAll('select > option' + optionSelector + ', optgroup' + optionSelector + ' option' + optionSelector).length,
  1377. tr8nText = (typeof this.options.countSelectedText === 'function') ? this.options.countSelectedText(selectedCount, totalCount) : this.options.countSelectedText;
  1378. titleFragment = generateOption.text({
  1379. text: tr8nText.replace('{0}', selectedCount.toString()).replace('{1}', totalCount.toString())
  1380. }, true);
  1381. }
  1382. }
  1383. if (this.options.title == undefined) {
  1384. // use .attr to ensure undefined is returned if title attribute is not set
  1385. this.options.title = this.$element.attr('title');
  1386. }
  1387. // If the select doesn't have a title, then use the default, or if nothing is set at all, use noneSelectedText
  1388. if (!titleFragment.childNodes.length) {
  1389. titleFragment = generateOption.text({
  1390. text: typeof this.options.title !== 'undefined' ? this.options.title : this.options.noneSelectedText
  1391. }, true);
  1392. }
  1393. // strip all HTML tags and trim the result, then unescape any escaped tags
  1394. button.title = titleFragment.textContent.replace(/<[^>]*>?/g, '').trim();
  1395. if (this.options.sanitize && hasContent) {
  1396. sanitizeHtml([titleFragment], that.options.whiteList, that.options.sanitizeFn);
  1397. }
  1398. buttonInner.innerHTML = '';
  1399. buttonInner.appendChild(titleFragment);
  1400. if (version.major < 4 && this.$newElement[0].classList.contains('bs3-has-addon')) {
  1401. var filterExpand = button.querySelector('.filter-expand'),
  1402. clone = buttonInner.cloneNode(true);
  1403. clone.className = 'filter-expand';
  1404. if (filterExpand) {
  1405. button.replaceChild(clone, filterExpand);
  1406. } else {
  1407. button.appendChild(clone);
  1408. }
  1409. }
  1410. this.$element.trigger('rendered' + EVENT_KEY);
  1411. },
  1412. /**
  1413. * @param [style]
  1414. * @param [status]
  1415. */
  1416. setStyle: function (newStyle, status) {
  1417. var button = this.$button[0],
  1418. newElement = this.$newElement[0],
  1419. style = this.options.style.trim(),
  1420. buttonClass;
  1421. if (this.$element.attr('class')) {
  1422. this.$newElement.addClass(this.$element.attr('class').replace(/selectpicker|mobile-device|bs-select-hidden|validate\[.*\]/gi, ''));
  1423. }
  1424. if (version.major < 4) {
  1425. newElement.classList.add('bs3');
  1426. if (newElement.parentNode.classList.contains('input-group') &&
  1427. (newElement.previousElementSibling || newElement.nextElementSibling) &&
  1428. (newElement.previousElementSibling || newElement.nextElementSibling).classList.contains('input-group-addon')
  1429. ) {
  1430. newElement.classList.add('bs3-has-addon');
  1431. }
  1432. }
  1433. if (newStyle) {
  1434. buttonClass = newStyle.trim();
  1435. } else {
  1436. buttonClass = style;
  1437. }
  1438. if (status == 'add') {
  1439. if (buttonClass) button.classList.add.apply(button.classList, buttonClass.split(' '));
  1440. } else if (status == 'remove') {
  1441. if (buttonClass) button.classList.remove.apply(button.classList, buttonClass.split(' '));
  1442. } else {
  1443. if (style) button.classList.remove.apply(button.classList, style.split(' '));
  1444. if (buttonClass) button.classList.add.apply(button.classList, buttonClass.split(' '));
  1445. }
  1446. },
  1447. liHeight: function (refresh) {
  1448. if (!refresh && (this.options.size === false || this.sizeInfo)) return;
  1449. if (!this.sizeInfo) this.sizeInfo = {};
  1450. var newElement = document.createElement('div'),
  1451. menu = document.createElement('div'),
  1452. menuInner = document.createElement('div'),
  1453. menuInnerInner = document.createElement('ul'),
  1454. divider = document.createElement('li'),
  1455. dropdownHeader = document.createElement('li'),
  1456. li = document.createElement('li'),
  1457. a = document.createElement('a'),
  1458. text = document.createElement('span'),
  1459. header = this.options.header && this.$menu.find('.' + classNames.POPOVERHEADER).length > 0 ? this.$menu.find('.' + classNames.POPOVERHEADER)[0].cloneNode(true) : null,
  1460. search = this.options.liveSearch ? document.createElement('div') : null,
  1461. actions = this.options.actionsBox && this.multiple && this.$menu.find('.bs-actionsbox').length > 0 ? this.$menu.find('.bs-actionsbox')[0].cloneNode(true) : null,
  1462. doneButton = this.options.doneButton && this.multiple && this.$menu.find('.bs-donebutton').length > 0 ? this.$menu.find('.bs-donebutton')[0].cloneNode(true) : null,
  1463. firstOption = this.$element.find('option')[0];
  1464. this.sizeInfo.selectWidth = this.$newElement[0].offsetWidth;
  1465. text.className = 'text';
  1466. a.className = 'dropdown-item ' + (firstOption ? firstOption.className : '');
  1467. newElement.className = this.$menu[0].parentNode.className + ' ' + classNames.SHOW;
  1468. newElement.style.width = this.sizeInfo.selectWidth + 'px';
  1469. if (this.options.width === 'auto') menu.style.minWidth = 0;
  1470. menu.className = classNames.MENU + ' ' + classNames.SHOW;
  1471. menuInner.className = 'inner ' + classNames.SHOW;
  1472. menuInnerInner.className = classNames.MENU + ' inner ' + (version.major === '4' ? classNames.SHOW : '');
  1473. divider.className = classNames.DIVIDER;
  1474. dropdownHeader.className = 'dropdown-header';
  1475. text.appendChild(document.createTextNode('\u200b'));
  1476. a.appendChild(text);
  1477. li.appendChild(a);
  1478. dropdownHeader.appendChild(text.cloneNode(true));
  1479. if (this.selectpicker.view.widestOption) {
  1480. menuInnerInner.appendChild(this.selectpicker.view.widestOption.cloneNode(true));
  1481. }
  1482. menuInnerInner.appendChild(li);
  1483. menuInnerInner.appendChild(divider);
  1484. menuInnerInner.appendChild(dropdownHeader);
  1485. if (header) menu.appendChild(header);
  1486. if (search) {
  1487. var input = document.createElement('input');
  1488. search.className = 'bs-searchbox';
  1489. input.className = 'form-control';
  1490. search.appendChild(input);
  1491. menu.appendChild(search);
  1492. }
  1493. if (actions) menu.appendChild(actions);
  1494. menuInner.appendChild(menuInnerInner);
  1495. menu.appendChild(menuInner);
  1496. if (doneButton) menu.appendChild(doneButton);
  1497. newElement.appendChild(menu);
  1498. document.body.appendChild(newElement);
  1499. var liHeight = li.offsetHeight,
  1500. dropdownHeaderHeight = dropdownHeader ? dropdownHeader.offsetHeight : 0,
  1501. headerHeight = header ? header.offsetHeight : 0,
  1502. searchHeight = search ? search.offsetHeight : 0,
  1503. actionsHeight = actions ? actions.offsetHeight : 0,
  1504. doneButtonHeight = doneButton ? doneButton.offsetHeight : 0,
  1505. dividerHeight = $(divider).outerHeight(true),
  1506. // fall back to jQuery if getComputedStyle is not supported
  1507. menuStyle = window.getComputedStyle ? window.getComputedStyle(menu) : false,
  1508. menuWidth = menu.offsetWidth,
  1509. $menu = menuStyle ? null : $(menu),
  1510. menuPadding = {
  1511. vert: toInteger(menuStyle ? menuStyle.paddingTop : $menu.css('paddingTop')) +
  1512. toInteger(menuStyle ? menuStyle.paddingBottom : $menu.css('paddingBottom')) +
  1513. toInteger(menuStyle ? menuStyle.borderTopWidth : $menu.css('borderTopWidth')) +
  1514. toInteger(menuStyle ? menuStyle.borderBottomWidth : $menu.css('borderBottomWidth')),
  1515. horiz: toInteger(menuStyle ? menuStyle.paddingLeft : $menu.css('paddingLeft')) +
  1516. toInteger(menuStyle ? menuStyle.paddingRight : $menu.css('paddingRight')) +
  1517. toInteger(menuStyle ? menuStyle.borderLeftWidth : $menu.css('borderLeftWidth')) +
  1518. toInteger(menuStyle ? menuStyle.borderRightWidth : $menu.css('borderRightWidth'))
  1519. },
  1520. menuExtras = {
  1521. vert: menuPadding.vert +
  1522. toInteger(menuStyle ? menuStyle.marginTop : $menu.css('marginTop')) +
  1523. toInteger(menuStyle ? menuStyle.marginBottom : $menu.css('marginBottom')) + 2,
  1524. horiz: menuPadding.horiz +
  1525. toInteger(menuStyle ? menuStyle.marginLeft : $menu.css('marginLeft')) +
  1526. toInteger(menuStyle ? menuStyle.marginRight : $menu.css('marginRight')) + 2
  1527. },
  1528. scrollBarWidth;
  1529. menuInner.style.overflowY = 'scroll';
  1530. scrollBarWidth = menu.offsetWidth - menuWidth;
  1531. document.body.removeChild(newElement);
  1532. this.sizeInfo.liHeight = liHeight;
  1533. this.sizeInfo.dropdownHeaderHeight = dropdownHeaderHeight;
  1534. this.sizeInfo.headerHeight = headerHeight;
  1535. this.sizeInfo.searchHeight = searchHeight;
  1536. this.sizeInfo.actionsHeight = actionsHeight;
  1537. this.sizeInfo.doneButtonHeight = doneButtonHeight;
  1538. this.sizeInfo.dividerHeight = dividerHeight;
  1539. this.sizeInfo.menuPadding = menuPadding;
  1540. this.sizeInfo.menuExtras = menuExtras;
  1541. this.sizeInfo.menuWidth = menuWidth;
  1542. this.sizeInfo.totalMenuWidth = this.sizeInfo.menuWidth;
  1543. this.sizeInfo.scrollBarWidth = scrollBarWidth;
  1544. this.sizeInfo.selectHeight = this.$newElement[0].offsetHeight;
  1545. this.setPositionData();
  1546. },
  1547. getSelectPosition: function () {
  1548. var that = this,
  1549. $window = $(window),
  1550. pos = that.$newElement.offset(),
  1551. $container = $(that.options.container),
  1552. containerPos;
  1553. if (that.options.container && $container.length && !$container.is('body')) {
  1554. containerPos = $container.offset();
  1555. containerPos.top += parseInt($container.css('borderTopWidth'));
  1556. containerPos.left += parseInt($container.css('borderLeftWidth'));
  1557. } else {
  1558. containerPos = { top: 0, left: 0 };
  1559. }
  1560. var winPad = that.options.windowPadding;
  1561. this.sizeInfo.selectOffsetTop = pos.top - containerPos.top - $window.scrollTop();
  1562. this.sizeInfo.selectOffsetBot = $window.height() - this.sizeInfo.selectOffsetTop - this.sizeInfo.selectHeight - containerPos.top - winPad[2];
  1563. this.sizeInfo.selectOffsetLeft = pos.left - containerPos.left - $window.scrollLeft();
  1564. this.sizeInfo.selectOffsetRight = $window.width() - this.sizeInfo.selectOffsetLeft - this.sizeInfo.selectWidth - containerPos.left - winPad[1];
  1565. this.sizeInfo.selectOffsetTop -= winPad[0];
  1566. this.sizeInfo.selectOffsetLeft -= winPad[3];
  1567. },
  1568. setMenuSize: function (isAuto) {
  1569. this.getSelectPosition();
  1570. var selectWidth = this.sizeInfo.selectWidth,
  1571. liHeight = this.sizeInfo.liHeight,
  1572. headerHeight = this.sizeInfo.headerHeight,
  1573. searchHeight = this.sizeInfo.searchHeight,
  1574. actionsHeight = this.sizeInfo.actionsHeight,
  1575. doneButtonHeight = this.sizeInfo.doneButtonHeight,
  1576. divHeight = this.sizeInfo.dividerHeight,
  1577. menuPadding = this.sizeInfo.menuPadding,
  1578. menuInnerHeight,
  1579. menuHeight,
  1580. divLength = 0,
  1581. minHeight,
  1582. _minHeight,
  1583. maxHeight,
  1584. menuInnerMinHeight,
  1585. estimate;
  1586. if (this.options.dropupAuto) {
  1587. // Get the estimated height of the menu without scrollbars.
  1588. // This is useful for smaller menus, where there might be plenty of room
  1589. // below the button without setting dropup, but we can't know
  1590. // the exact height of the menu until createView is called later
  1591. estimate = liHeight * this.selectpicker.current.elements.length + menuPadding.vert;
  1592. this.$newElement.toggleClass(classNames.DROPUP, this.sizeInfo.selectOffsetTop - this.sizeInfo.selectOffsetBot > this.sizeInfo.menuExtras.vert && estimate + this.sizeInfo.menuExtras.vert + 50 > this.sizeInfo.selectOffsetBot);
  1593. }
  1594. if (this.options.size === 'auto') {
  1595. _minHeight = this.selectpicker.current.elements.length > 3 ? this.sizeInfo.liHeight * 3 + this.sizeInfo.menuExtras.vert - 2 : 0;
  1596. menuHeight = this.sizeInfo.selectOffsetBot - this.sizeInfo.menuExtras.vert;
  1597. minHeight = _minHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight;
  1598. menuInnerMinHeight = Math.max(_minHeight - menuPadding.vert, 0);
  1599. if (this.$newElement.hasClass(classNames.DROPUP)) {
  1600. menuHeight = this.sizeInfo.selectOffsetTop - this.sizeInfo.menuExtras.vert;
  1601. }
  1602. maxHeight = menuHeight;
  1603. menuInnerHeight = menuHeight - headerHeight - searchHeight - actionsHeight - doneButtonHeight - menuPadding.vert;
  1604. } else if (this.options.size && this.options.size != 'auto' && this.selectpicker.current.elements.length > this.options.size) {
  1605. for (var i = 0; i < this.options.size; i++) {
  1606. if (this.selectpicker.current.data[i].type === 'divider') divLength++;
  1607. }
  1608. menuHeight = liHeight * this.options.size + divLength * divHeight + menuPadding.vert;
  1609. menuInnerHeight = menuHeight - menuPadding.vert;
  1610. maxHeight = menuHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight;
  1611. minHeight = menuInnerMinHeight = '';
  1612. }
  1613. if (this.options.dropdownAlignRight === 'auto') {
  1614. this.$menu.toggleClass(classNames.MENURIGHT, this.sizeInfo.selectOffsetLeft > this.sizeInfo.selectOffsetRight && this.sizeInfo.selectOffsetRight < (this.sizeInfo.totalMenuWidth - selectWidth));
  1615. }
  1616. this.$menu.css({
  1617. 'max-height': maxHeight - 120 + 'px',
  1618. 'overflow': 'hidden',
  1619. 'min-height': '80px'//minHeight + 'px'
  1620. });
  1621. this.$menuInner.css({
  1622. 'max-height': menuInnerHeight - 120 + 'px',
  1623. 'overflow-y': 'auto',
  1624. 'min-height': '80px'//menuInnerMinHeight + 'px'
  1625. });
  1626. // ensure menuInnerHeight is always a positive number to prevent issues calculating chunkSize in createView
  1627. this.sizeInfo.menuInnerHeight = Math.max(menuInnerHeight, 1);
  1628. if (this.selectpicker.current.data.length && this.selectpicker.current.data[this.selectpicker.current.data.length - 1].position > this.sizeInfo.menuInnerHeight) {
  1629. this.sizeInfo.hasScrollBar = true;
  1630. this.sizeInfo.totalMenuWidth = this.sizeInfo.menuWidth + this.sizeInfo.scrollBarWidth;
  1631. //this.$menu.css('min-width', this.sizeInfo.totalMenuWidth);
  1632. }
  1633. if (this.dropdown && this.dropdown._popper) this.dropdown._popper.update();
  1634. },
  1635. setSize: function (refresh) {
  1636. this.liHeight(refresh);
  1637. if (this.options.header) this.$menu.css('padding-top', 0);
  1638. if (this.options.size === false) return;
  1639. var that = this,
  1640. $window = $(window),
  1641. selectedIndex,
  1642. offset = 0;
  1643. this.setMenuSize();
  1644. if (this.options.liveSearch) {
  1645. this.$searchbox
  1646. .off('input.setMenuSize propertychange.setMenuSize')
  1647. .on('input.setMenuSize propertychange.setMenuSize', function () {
  1648. return that.setMenuSize();
  1649. });
  1650. }
  1651. if (this.options.size === 'auto') {
  1652. $window
  1653. .off('resize' + EVENT_KEY + '.' + this.selectId + '.setMenuSize' + ' scroll' + EVENT_KEY + '.' + this.selectId + '.setMenuSize')
  1654. .on('resize' + EVENT_KEY + '.' + this.selectId + '.setMenuSize' + ' scroll' + EVENT_KEY + '.' + this.selectId + '.setMenuSize', function () {
  1655. return that.setMenuSize();
  1656. });
  1657. } else if (this.options.size && this.options.size != 'auto' && this.selectpicker.current.elements.length > this.options.size) {
  1658. $window.off('resize' + EVENT_KEY + '.' + this.selectId + '.setMenuSize' + ' scroll' + EVENT_KEY + '.' + this.selectId + '.setMenuSize');
  1659. }
  1660. if (refresh) {
  1661. offset = this.$menuInner[0].scrollTop;
  1662. } else if (!that.multiple) {
  1663. var element = that.$element[0];
  1664. selectedIndex = (element.options[element.selectedIndex] || {}).liIndex;
  1665. if (typeof selectedIndex === 'number' && that.options.size !== false) {
  1666. offset = that.sizeInfo.liHeight * selectedIndex;
  1667. offset = offset - (that.sizeInfo.menuInnerHeight / 2) + (that.sizeInfo.liHeight / 2);
  1668. }
  1669. }
  1670. that.createView(false, offset);
  1671. },
  1672. setWidth: function () {
  1673. var that = this;
  1674. if (this.options.width === 'auto') {
  1675. requestAnimationFrame(function () {
  1676. that.$menu.css('min-width', '0');
  1677. that.$element.on('loaded' + EVENT_KEY, function () {
  1678. that.liHeight();
  1679. that.setMenuSize();
  1680. // Get correct width if element is hidden
  1681. var $selectClone = that.$newElement.clone().appendTo('body'),
  1682. btnWidth = $selectClone.css('width', 'auto').children('button').outerWidth();
  1683. $selectClone.remove();
  1684. // Set width to whatever's larger, button title or longest option
  1685. that.sizeInfo.selectWidth = Math.max(that.sizeInfo.totalMenuWidth, btnWidth);
  1686. that.$newElement.css('width', that.sizeInfo.selectWidth + 'px');
  1687. });
  1688. });
  1689. } else if (this.options.width === 'fit') {
  1690. // Remove inline min-width so width can be changed from 'auto'
  1691. this.$menu.css('min-width', '');
  1692. this.$newElement.css('width', '').addClass('fit-width');
  1693. } else if (this.options.width) {
  1694. // Remove inline min-width so width can be changed from 'auto'
  1695. this.$menu.css('min-width', '');
  1696. this.$newElement.css('width', this.options.width);
  1697. } else {
  1698. // Remove inline min-width/width so width can be changed
  1699. this.$menu.css('min-width', '');
  1700. this.$newElement.css('width', '');
  1701. }
  1702. // Remove fit-width class if width is changed programmatically
  1703. if (this.$newElement.hasClass('fit-width') && this.options.width !== 'fit') {
  1704. this.$newElement[0].classList.remove('fit-width');
  1705. }
  1706. },
  1707. selectPosition: function () {
  1708. this.$bsContainer = $('<div class="bs-container" />');
  1709. var that = this,
  1710. $container = $(this.options.container),
  1711. pos,
  1712. containerPos,
  1713. actualHeight,
  1714. getPlacement = function ($element) {
  1715. var containerPosition = {},
  1716. // fall back to dropdown's default display setting if display is not manually set
  1717. display = that.options.display || (
  1718. // Bootstrap 3 doesn't have $.fn.dropdown.Constructor.Default
  1719. $.fn.dropdown.Constructor.Default ? $.fn.dropdown.Constructor.Default.display
  1720. : false
  1721. );
  1722. that.$bsContainer.addClass($element.attr('class').replace(/form-control|fit-width/gi, '')).toggleClass(classNames.DROPUP, $element.hasClass(classNames.DROPUP));
  1723. pos = $element.offset();
  1724. if (!$container.is('body')) {
  1725. containerPos = $container.offset();
  1726. containerPos.top += parseInt($container.css('borderTopWidth')) - $container.scrollTop();
  1727. containerPos.left += parseInt($container.css('borderLeftWidth')) - $container.scrollLeft();
  1728. } else {
  1729. containerPos = { top: 0, left: 0 };
  1730. }
  1731. actualHeight = $element.hasClass(classNames.DROPUP) ? 0 : $element[0].offsetHeight;
  1732. // Bootstrap 4+ uses Popper for menu positioning
  1733. if (version.major < 4 || display === 'static') {
  1734. containerPosition.top = pos.top - containerPos.top + actualHeight;
  1735. containerPosition.left = pos.left - containerPos.left;
  1736. }
  1737. containerPosition.width = $element[0].offsetWidth;
  1738. that.$bsContainer.css(containerPosition);
  1739. };
  1740. this.$button.on('click.bs.dropdown.data-api', function () {
  1741. if (that.isDisabled()) {
  1742. return;
  1743. }
  1744. getPlacement(that.$newElement);
  1745. that.$bsContainer
  1746. .appendTo(that.options.container)
  1747. .toggleClass(classNames.SHOW, !that.$button.hasClass(classNames.SHOW))
  1748. .append(that.$menu);
  1749. });
  1750. $(window)
  1751. .off('resize' + EVENT_KEY + '.' + this.selectId + ' scroll' + EVENT_KEY + '.' + this.selectId)
  1752. .on('resize' + EVENT_KEY + '.' + this.selectId + ' scroll' + EVENT_KEY + '.' + this.selectId, function () {
  1753. var isActive = that.$newElement.hasClass(classNames.SHOW);
  1754. if (isActive) getPlacement(that.$newElement);
  1755. });
  1756. this.$element.on('hide' + EVENT_KEY, function () {
  1757. that.$menu.data('height', that.$menu.height());
  1758. that.$bsContainer.detach();
  1759. });
  1760. },
  1761. setOptionStatus: function () {
  1762. var that = this;
  1763. that.noScroll = false;
  1764. if (that.selectpicker.view.visibleElements && that.selectpicker.view.visibleElements.length) {
  1765. for (var i = 0; i < that.selectpicker.view.visibleElements.length; i++) {
  1766. var liData = that.selectpicker.current.data[i + that.selectpicker.view.position0],
  1767. option = liData.option;
  1768. if (option) {
  1769. that.setDisabled(
  1770. liData.index,
  1771. liData.disabled
  1772. );
  1773. that.setSelected(
  1774. liData.index,
  1775. option.selected
  1776. );
  1777. }
  1778. }
  1779. }
  1780. },
  1781. /**
  1782. * @param {number} index - the index of the option that is being changed
  1783. * @param {boolean} selected - true if the option is being selected, false if being deselected
  1784. */
  1785. setSelected: function (index, selected) {
  1786. var li = this.selectpicker.main.elements[index],
  1787. liData = this.selectpicker.main.data[index],
  1788. activeIndexIsSet = this.activeIndex !== undefined,
  1789. thisIsActive = this.activeIndex === index,
  1790. prevActive,
  1791. a,
  1792. // if current option is already active
  1793. // OR
  1794. // if the current option is being selected, it's NOT multiple, and
  1795. // activeIndex is undefined:
  1796. // - when the menu is first being opened, OR
  1797. // - after a search has been performed, OR
  1798. // - when retainActive is false when selecting a new option (i.e. index of the newly selected option is not the same as the current activeIndex)
  1799. keepActive = thisIsActive || (selected && !this.multiple && !activeIndexIsSet);
  1800. liData.selected = selected;
  1801. a = li.firstChild;
  1802. if (selected) {
  1803. this.selectedIndex = index;
  1804. }
  1805. li.classList.toggle('selected', selected);
  1806. li.classList.toggle('active', keepActive);
  1807. if (keepActive) {
  1808. this.selectpicker.view.currentActive = li;
  1809. this.activeIndex = index;
  1810. }
  1811. if (a) {
  1812. a.classList.toggle('selected', selected);
  1813. a.classList.toggle('active', keepActive);
  1814. a.setAttribute('aria-selected', selected);
  1815. }
  1816. if (!keepActive) {
  1817. if (!activeIndexIsSet && selected && this.prevActiveIndex !== undefined) {
  1818. prevActive = this.selectpicker.main.elements[this.prevActiveIndex];
  1819. prevActive.classList.remove('active');
  1820. if (prevActive.firstChild) {
  1821. prevActive.firstChild.classList.remove('active');
  1822. }
  1823. }
  1824. }
  1825. },
  1826. /**
  1827. * @param {number} index - the index of the option that is being disabled
  1828. * @param {boolean} disabled - true if the option is being disabled, false if being enabled
  1829. */
  1830. setDisabled: function (index, disabled) {
  1831. var li = this.selectpicker.main.elements[index],
  1832. a;
  1833. this.selectpicker.main.data[index].disabled = disabled;
  1834. a = li.firstChild;
  1835. li.classList.toggle(classNames.DISABLED, disabled);
  1836. if (a) {
  1837. if (version.major === '4') a.classList.toggle(classNames.DISABLED, disabled);
  1838. a.setAttribute('aria-disabled', disabled);
  1839. if (disabled) {
  1840. a.setAttribute('tabindex', -1);
  1841. } else {
  1842. a.setAttribute('tabindex', 0);
  1843. }
  1844. }
  1845. },
  1846. isDisabled: function () {
  1847. return this.$element[0].disabled;
  1848. },
  1849. checkDisabled: function () {
  1850. var that = this;
  1851. if (this.isDisabled()) {
  1852. this.$newElement[0].classList.add(classNames.DISABLED);
  1853. this.$button.addClass(classNames.DISABLED).attr('tabindex', -1).attr('aria-disabled', true);
  1854. } else {
  1855. if (this.$button[0].classList.contains(classNames.DISABLED)) {
  1856. this.$newElement[0].classList.remove(classNames.DISABLED);
  1857. this.$button.removeClass(classNames.DISABLED).attr('aria-disabled', false);
  1858. }
  1859. if (this.$button.attr('tabindex') == -1 && !this.$element.data('tabindex')) {
  1860. this.$button.removeAttr('tabindex');
  1861. }
  1862. }
  1863. this.$button.on('click', function () {
  1864. return !that.isDisabled();
  1865. });
  1866. },
  1867. togglePlaceholder: function () {
  1868. // much faster than calling $.val()
  1869. var element = this.$element[0],
  1870. selectedIndex = element.selectedIndex,
  1871. nothingSelected = selectedIndex === -1;
  1872. if (!nothingSelected && !element.options[selectedIndex].value) nothingSelected = true;
  1873. this.$button.toggleClass('bs-placeholder', nothingSelected);
  1874. },
  1875. tabIndex: function () {
  1876. if (this.$element.data('tabindex') !== this.$element.attr('tabindex') &&
  1877. (this.$element.attr('tabindex') !== -98 && this.$element.attr('tabindex') !== '-98')) {
  1878. this.$element.data('tabindex', this.$element.attr('tabindex'));
  1879. this.$button.attr('tabindex', this.$element.data('tabindex'));
  1880. }
  1881. this.$element.attr('tabindex', -98);
  1882. },
  1883. clickListener: function () {
  1884. var that = this,
  1885. $document = $(document);
  1886. $document.data('spaceSelect', false);
  1887. this.$button.on('keyup', function (e) {
  1888. if (/(32)/.test(e.keyCode.toString(10)) && $document.data('spaceSelect')) {
  1889. e.preventDefault();
  1890. $document.data('spaceSelect', false);
  1891. }
  1892. });
  1893. this.$newElement.on('show.bs.dropdown', function () {
  1894. if (version.major > 3 && !that.dropdown) {
  1895. that.dropdown = that.$button.data('bs.dropdown');
  1896. that.dropdown._menu = that.$menu[0];
  1897. }
  1898. });
  1899. this.$button.on('click.bs.dropdown.data-api', function () {
  1900. if (!that.$newElement.hasClass(classNames.SHOW)) {
  1901. that.setSize();
  1902. }
  1903. });
  1904. function setFocus () {
  1905. if (that.options.liveSearch) {
  1906. that.$searchbox.trigger('focus');
  1907. } else {
  1908. that.$menuInner.trigger('focus');
  1909. }
  1910. }
  1911. function checkPopperExists () {
  1912. if (that.dropdown && that.dropdown._popper && that.dropdown._popper.state.isCreated) {
  1913. setFocus();
  1914. } else {
  1915. requestAnimationFrame(checkPopperExists);
  1916. }
  1917. }
  1918. this.$element.on('shown' + EVENT_KEY, function () {
  1919. if (that.$menuInner[0].scrollTop !== that.selectpicker.view.scrollTop) {
  1920. that.$menuInner[0].scrollTop = that.selectpicker.view.scrollTop;
  1921. }
  1922. if (version.major > 3) {
  1923. requestAnimationFrame(checkPopperExists);
  1924. } else {
  1925. setFocus();
  1926. }
  1927. });
  1928. this.$menuInner.on('click', 'li a', function (e, retainActive) {
  1929. var $this = $(this),
  1930. position0 = that.isVirtual() ? that.selectpicker.view.position0 : 0,
  1931. clickedData = that.selectpicker.current.data[$this.parent().index() + position0],
  1932. clickedIndex = clickedData.index,
  1933. prevValue = getSelectValues(that.$element[0]),
  1934. prevIndex = that.$element.prop('selectedIndex'),
  1935. triggerChange = true;
  1936. // Don't close on multi choice menu
  1937. if (that.multiple && that.options.maxOptions !== 1) {
  1938. e.stopPropagation();
  1939. }
  1940. e.preventDefault();
  1941. // Don't run if the select is disabled
  1942. if (!that.isDisabled() && !$this.parent().hasClass(classNames.DISABLED)) {
  1943. var $options = that.$element.find('option'),
  1944. option = clickedData.option,
  1945. $option = $(option),
  1946. state = option.selected,
  1947. $optgroup = $option.parent('optgroup'),
  1948. $optgroupOptions = $optgroup.find('option'),
  1949. maxOptions = that.options.maxOptions,
  1950. maxOptionsGrp = $optgroup.data('maxOptions') || false;
  1951. if (clickedIndex === that.activeIndex) retainActive = true;
  1952. if (!retainActive) {
  1953. that.prevActiveIndex = that.activeIndex;
  1954. that.activeIndex = undefined;
  1955. }
  1956. if (!that.multiple) { // Deselect all others if not multi select box
  1957. $options.prop('selected', false);
  1958. option.selected = true;
  1959. that.setSelected(clickedIndex, true);
  1960. } else { // Toggle the one we have chosen if we are multi select.
  1961. option.selected = !state;
  1962. that.setSelected(clickedIndex, !state);
  1963. $this.trigger('blur');
  1964. if (maxOptions !== false || maxOptionsGrp !== false) {
  1965. var maxReached = maxOptions < $options.filter(':selected').length,
  1966. maxReachedGrp = maxOptionsGrp < $optgroup.find('option:selected').length;
  1967. if ((maxOptions && maxReached) || (maxOptionsGrp && maxReachedGrp)) {
  1968. if (maxOptions && maxOptions == 1) {
  1969. $options.prop('selected', false);
  1970. $option.prop('selected', true);
  1971. for (var i = 0; i < $options.length; i++) {
  1972. that.setSelected(i, false);
  1973. }
  1974. that.setSelected(clickedIndex, true);
  1975. } else if (maxOptionsGrp && maxOptionsGrp == 1) {
  1976. $optgroup.find('option:selected').prop('selected', false);
  1977. $option.prop('selected', true);
  1978. for (var i = 0; i < $optgroupOptions.length; i++) {
  1979. var option = $optgroupOptions[i];
  1980. that.setSelected($options.index(option), false);
  1981. }
  1982. that.setSelected(clickedIndex, true);
  1983. } else {
  1984. var maxOptionsText = typeof that.options.maxOptionsText === 'string' ? [that.options.maxOptionsText, that.options.maxOptionsText] : that.options.maxOptionsText,
  1985. maxOptionsArr = typeof maxOptionsText === 'function' ? maxOptionsText(maxOptions, maxOptionsGrp) : maxOptionsText,
  1986. maxTxt = maxOptionsArr[0].replace('{n}', maxOptions),
  1987. maxTxtGrp = maxOptionsArr[1].replace('{n}', maxOptionsGrp),
  1988. $notify = $('<div class="notify"></div>');
  1989. // If {var} is set in array, replace it
  1990. /** @deprecated */
  1991. if (maxOptionsArr[2]) {
  1992. maxTxt = maxTxt.replace('{var}', maxOptionsArr[2][maxOptions > 1 ? 0 : 1]);
  1993. maxTxtGrp = maxTxtGrp.replace('{var}', maxOptionsArr[2][maxOptionsGrp > 1 ? 0 : 1]);
  1994. }
  1995. $option.prop('selected', false);
  1996. that.$menu.append($notify);
  1997. if (maxOptions && maxReached) {
  1998. $notify.append($('<div>' + maxTxt + '</div>'));
  1999. triggerChange = false;
  2000. that.$element.trigger('maxReached' + EVENT_KEY);
  2001. }
  2002. if (maxOptionsGrp && maxReachedGrp) {
  2003. $notify.append($('<div>' + maxTxtGrp + '</div>'));
  2004. triggerChange = false;
  2005. that.$element.trigger('maxReachedGrp' + EVENT_KEY);
  2006. }
  2007. setTimeout(function () {
  2008. that.setSelected(clickedIndex, false);
  2009. }, 10);
  2010. $notify.delay(750).fadeOut(300, function () {
  2011. $(this).remove();
  2012. });
  2013. }
  2014. }
  2015. }
  2016. }
  2017. if (!that.multiple || (that.multiple && that.options.maxOptions === 1)) {
  2018. that.$button.trigger('focus');
  2019. } else if (that.options.liveSearch) {
  2020. that.$searchbox.trigger('focus');
  2021. }
  2022. // Trigger select 'change'
  2023. if (triggerChange) {
  2024. if ((prevValue != getSelectValues(that.$element[0]) && that.multiple) || (prevIndex != that.$element.prop('selectedIndex') && !that.multiple)) {
  2025. // $option.prop('selected') is current option state (selected/unselected). prevValue is the value of the select prior to being changed.
  2026. changedArguments = [option.index, $option.prop('selected'), prevValue];
  2027. that.$element
  2028. .triggerNative('change');
  2029. }
  2030. }
  2031. }
  2032. });
  2033. this.$menu.on('click', 'li.' + classNames.DISABLED + ' a, .' + classNames.POPOVERHEADER + ', .' + classNames.POPOVERHEADER + ' :not(.close)', function (e) {
  2034. if (e.currentTarget == this) {
  2035. e.preventDefault();
  2036. e.stopPropagation();
  2037. if (that.options.liveSearch && !$(e.target).hasClass('close')) {
  2038. that.$searchbox.trigger('focus');
  2039. } else {
  2040. that.$button.trigger('focus');
  2041. }
  2042. }
  2043. });
  2044. this.$menuInner.on('click', '.divider, .dropdown-header', function (e) {
  2045. e.preventDefault();
  2046. e.stopPropagation();
  2047. if (that.options.liveSearch) {
  2048. that.$searchbox.trigger('focus');
  2049. } else {
  2050. that.$button.trigger('focus');
  2051. }
  2052. });
  2053. this.$menu.on('click', '.' + classNames.POPOVERHEADER + ' .close', function () {
  2054. that.$button.trigger('click');
  2055. });
  2056. this.$searchbox.on('click', function (e) {
  2057. e.stopPropagation();
  2058. });
  2059. this.$menu.on('click', '.actions-btn', function (e) {
  2060. if (that.options.liveSearch) {
  2061. that.$searchbox.trigger('focus');
  2062. } else {
  2063. that.$button.trigger('focus');
  2064. }
  2065. e.preventDefault();
  2066. e.stopPropagation();
  2067. if ($(this).hasClass('bs-select-all')) {
  2068. that.selectAll();
  2069. } else {
  2070. that.deselectAll();
  2071. }
  2072. });
  2073. this.$element
  2074. .on('change' + EVENT_KEY, function () {
  2075. that.render();
  2076. that.$element.trigger('changed' + EVENT_KEY, changedArguments);
  2077. changedArguments = null;
  2078. })
  2079. .on('focus' + EVENT_KEY, function () {
  2080. if (!that.options.mobile) that.$button.trigger('focus');
  2081. });
  2082. },
  2083. liveSearchListener: function () {
  2084. var that = this,
  2085. noResults = document.createElement('li');
  2086. this.$button.on('click.bs.dropdown.data-api', function () {
  2087. if (!!that.$searchbox.val()) {
  2088. that.$searchbox.val('');
  2089. }
  2090. });
  2091. this.$searchbox.on('click.bs.dropdown.data-api focus.bs.dropdown.data-api touchend.bs.dropdown.data-api', function (e) {
  2092. e.stopPropagation();
  2093. });
  2094. this.$searchbox.on('input propertychange', function () {
  2095. var searchValue = that.$searchbox.val();
  2096. that.selectpicker.search.elements = [];
  2097. that.selectpicker.search.data = [];
  2098. if (searchValue) {
  2099. var i,
  2100. searchMatch = [],
  2101. q = searchValue.toUpperCase(),
  2102. cache = {},
  2103. cacheArr = [],
  2104. searchStyle = that._searchStyle(),
  2105. normalizeSearch = that.options.liveSearchNormalize;
  2106. if (normalizeSearch) q = normalizeToBase(q);
  2107. that._$lisSelected = that.$menuInner.find('.selected');
  2108. for (var i = 0; i < that.selectpicker.main.data.length; i++) {
  2109. var li = that.selectpicker.main.data[i];
  2110. if (!cache[i]) {
  2111. cache[i] = stringSearch(li, q, searchStyle, normalizeSearch);
  2112. }
  2113. if (cache[i] && li.headerIndex !== undefined && cacheArr.indexOf(li.headerIndex) === -1) {
  2114. if (li.headerIndex > 0) {
  2115. cache[li.headerIndex - 1] = true;
  2116. cacheArr.push(li.headerIndex - 1);
  2117. }
  2118. cache[li.headerIndex] = true;
  2119. cacheArr.push(li.headerIndex);
  2120. cache[li.lastIndex + 1] = true;
  2121. }
  2122. if (cache[i] && li.type !== 'optgroup-label') cacheArr.push(i);
  2123. }
  2124. for (var i = 0, cacheLen = cacheArr.length; i < cacheLen; i++) {
  2125. var index = cacheArr[i],
  2126. prevIndex = cacheArr[i - 1],
  2127. li = that.selectpicker.main.data[index],
  2128. liPrev = that.selectpicker.main.data[prevIndex];
  2129. if (li.type !== 'divider' || (li.type === 'divider' && liPrev && liPrev.type !== 'divider' && cacheLen - 1 !== i)) {
  2130. that.selectpicker.search.data.push(li);
  2131. searchMatch.push(that.selectpicker.main.elements[index]);
  2132. }
  2133. }
  2134. that.activeIndex = undefined;
  2135. that.noScroll = true;
  2136. that.$menuInner.scrollTop(0);
  2137. that.selectpicker.search.elements = searchMatch;
  2138. that.createView(true);
  2139. if (!searchMatch.length) {
  2140. noResults.className = 'no-results';
  2141. noResults.innerHTML = that.options.noneResultsText.replace('{0}', '"' + htmlEscape(searchValue) + '"');
  2142. that.$menuInner[0].firstChild.appendChild(noResults);
  2143. }
  2144. } else {
  2145. that.$menuInner.scrollTop(0);
  2146. that.createView(false);
  2147. }
  2148. });
  2149. },
  2150. _searchStyle: function () {
  2151. return this.options.liveSearchStyle || 'contains';
  2152. },
  2153. val: function (value) {
  2154. if (typeof value !== 'undefined') {
  2155. var prevValue = getSelectValues(this.$element[0]);
  2156. changedArguments = [null, null, prevValue];
  2157. this.$element
  2158. .val(value)
  2159. .trigger('changed' + EVENT_KEY, changedArguments);
  2160. this.render();
  2161. changedArguments = null;
  2162. return this.$element;
  2163. } else {
  2164. return this.$element.val();
  2165. }
  2166. },
  2167. changeAll: function (status) {
  2168. if (!this.multiple) return;
  2169. if (typeof status === 'undefined') status = true;
  2170. var element = this.$element[0],
  2171. previousSelected = 0,
  2172. currentSelected = 0,
  2173. prevValue = getSelectValues(element);
  2174. element.classList.add('bs-select-hidden');
  2175. for (var i = 0, len = this.selectpicker.current.elements.length; i < len; i++) {
  2176. var liData = this.selectpicker.current.data[i],
  2177. option = liData.option;
  2178. if (option && !liData.disabled && liData.type !== 'divider') {
  2179. if (liData.selected) previousSelected++;
  2180. option.selected = status;
  2181. if (status) currentSelected++;
  2182. }
  2183. }
  2184. element.classList.remove('bs-select-hidden');
  2185. if (previousSelected === currentSelected) return;
  2186. this.setOptionStatus();
  2187. this.togglePlaceholder();
  2188. changedArguments = [null, null, prevValue];
  2189. this.$element
  2190. .triggerNative('change');
  2191. },
  2192. selectAll: function () {
  2193. return this.changeAll(true);
  2194. },
  2195. deselectAll: function () {
  2196. return this.changeAll(false);
  2197. },
  2198. toggle: function (e) {
  2199. e = e || window.event;
  2200. if (e) e.stopPropagation();
  2201. this.$button.trigger('click.bs.dropdown.data-api');
  2202. },
  2203. keydown: function (e) {
  2204. var $this = $(this),
  2205. isToggle = $this.hasClass('dropdown-toggle'),
  2206. $parent = isToggle ? $this.closest('.dropdown') : $this.closest(Selector.MENU),
  2207. that = $parent.data('this'),
  2208. $items = that.findLis(),
  2209. index,
  2210. isActive,
  2211. liActive,
  2212. activeLi,
  2213. offset,
  2214. updateScroll = false,
  2215. downOnTab = e.which === keyCodes.TAB && !isToggle && !that.options.selectOnTab,
  2216. isArrowKey = REGEXP_ARROW.test(e.which) || downOnTab,
  2217. scrollTop = that.$menuInner[0].scrollTop,
  2218. isVirtual = that.isVirtual(),
  2219. position0 = isVirtual === true ? that.selectpicker.view.position0 : 0;
  2220. isActive = that.$newElement.hasClass(classNames.SHOW);
  2221. if (
  2222. !isActive &&
  2223. (
  2224. isArrowKey ||
  2225. (e.which >= 48 && e.which <= 57) ||
  2226. (e.which >= 96 && e.which <= 105) ||
  2227. (e.which >= 65 && e.which <= 90)
  2228. )
  2229. ) {
  2230. that.$button.trigger('click.bs.dropdown.data-api');
  2231. if (that.options.liveSearch) {
  2232. that.$searchbox.trigger('focus');
  2233. return;
  2234. }
  2235. }
  2236. if (e.which === keyCodes.ESCAPE && isActive) {
  2237. e.preventDefault();
  2238. that.$button.trigger('click.bs.dropdown.data-api').trigger('focus');
  2239. }
  2240. if (isArrowKey) { // if up or down
  2241. if (!$items.length) return;
  2242. // $items.index/.filter is too slow with a large list and no virtual scroll
  2243. index = isVirtual === true ? $items.index($items.filter('.active')) : that.activeIndex;
  2244. if (index === undefined) index = -1;
  2245. if (index !== -1) {
  2246. liActive = that.selectpicker.current.elements[index + position0];
  2247. liActive.classList.remove('active');
  2248. if (liActive.firstChild) liActive.firstChild.classList.remove('active');
  2249. }
  2250. if (e.which === keyCodes.ARROW_UP) { // up
  2251. if (index !== -1) index--;
  2252. if (index + position0 < 0) index += $items.length;
  2253. if (!that.selectpicker.view.canHighlight[index + position0]) {
  2254. index = that.selectpicker.view.canHighlight.slice(0, index + position0).lastIndexOf(true) - position0;
  2255. if (index === -1) index = $items.length - 1;
  2256. }
  2257. } else if (e.which === keyCodes.ARROW_DOWN || downOnTab) { // down
  2258. index++;
  2259. if (index + position0 >= that.selectpicker.view.canHighlight.length) index = 0;
  2260. if (!that.selectpicker.view.canHighlight[index + position0]) {
  2261. index = index + 1 + that.selectpicker.view.canHighlight.slice(index + position0 + 1).indexOf(true);
  2262. }
  2263. }
  2264. e.preventDefault();
  2265. var liActiveIndex = position0 + index;
  2266. if (e.which === keyCodes.ARROW_UP) { // up
  2267. // scroll to bottom and highlight last option
  2268. if (position0 === 0 && index === $items.length - 1) {
  2269. that.$menuInner[0].scrollTop = that.$menuInner[0].scrollHeight;
  2270. liActiveIndex = that.selectpicker.current.elements.length - 1;
  2271. } else {
  2272. activeLi = that.selectpicker.current.data[liActiveIndex];
  2273. offset = activeLi.position - activeLi.height;
  2274. updateScroll = offset < scrollTop;
  2275. }
  2276. } else if (e.which === keyCodes.ARROW_DOWN || downOnTab) { // down
  2277. // scroll to top and highlight first option
  2278. if (index === 0) {
  2279. that.$menuInner[0].scrollTop = 0;
  2280. liActiveIndex = 0;
  2281. } else {
  2282. activeLi = that.selectpicker.current.data[liActiveIndex];
  2283. offset = activeLi.position - that.sizeInfo.menuInnerHeight;
  2284. updateScroll = offset > scrollTop;
  2285. }
  2286. }
  2287. liActive = that.selectpicker.current.elements[liActiveIndex];
  2288. if (liActive) {
  2289. liActive.classList.add('active');
  2290. if (liActive.firstChild) liActive.firstChild.classList.add('active');
  2291. }
  2292. that.activeIndex = that.selectpicker.current.data[liActiveIndex].index;
  2293. that.selectpicker.view.currentActive = liActive;
  2294. if (updateScroll) that.$menuInner[0].scrollTop = offset;
  2295. if (that.options.liveSearch) {
  2296. that.$searchbox.trigger('focus');
  2297. } else {
  2298. $this.trigger('focus');
  2299. }
  2300. } else if (
  2301. (!$this.is('input') && !REGEXP_TAB_OR_ESCAPE.test(e.which)) ||
  2302. (e.which === keyCodes.SPACE && that.selectpicker.keydown.keyHistory)
  2303. ) {
  2304. var searchMatch,
  2305. matches = [],
  2306. keyHistory;
  2307. e.preventDefault();
  2308. that.selectpicker.keydown.keyHistory += keyCodeMap[e.which];
  2309. if (that.selectpicker.keydown.resetKeyHistory.cancel) clearTimeout(that.selectpicker.keydown.resetKeyHistory.cancel);
  2310. that.selectpicker.keydown.resetKeyHistory.cancel = that.selectpicker.keydown.resetKeyHistory.start();
  2311. keyHistory = that.selectpicker.keydown.keyHistory;
  2312. // if all letters are the same, set keyHistory to just the first character when searching
  2313. if (/^(.)\1+$/.test(keyHistory)) {
  2314. keyHistory = keyHistory.charAt(0);
  2315. }
  2316. // find matches
  2317. for (var i = 0; i < that.selectpicker.current.data.length; i++) {
  2318. var li = that.selectpicker.current.data[i],
  2319. hasMatch;
  2320. hasMatch = stringSearch(li, keyHistory, 'startsWith', true);
  2321. if (hasMatch && that.selectpicker.view.canHighlight[i]) {
  2322. matches.push(li.index);
  2323. }
  2324. }
  2325. if (matches.length) {
  2326. var matchIndex = 0;
  2327. $items.removeClass('active').find('a').removeClass('active');
  2328. // either only one key has been pressed or they are all the same key
  2329. if (keyHistory.length === 1) {
  2330. matchIndex = matches.indexOf(that.activeIndex);
  2331. if (matchIndex === -1 || matchIndex === matches.length - 1) {
  2332. matchIndex = 0;
  2333. } else {
  2334. matchIndex++;
  2335. }
  2336. }
  2337. searchMatch = matches[matchIndex];
  2338. activeLi = that.selectpicker.main.data[searchMatch];
  2339. if (scrollTop - activeLi.position > 0) {
  2340. offset = activeLi.position - activeLi.height;
  2341. updateScroll = true;
  2342. } else {
  2343. offset = activeLi.position - that.sizeInfo.menuInnerHeight;
  2344. // if the option is already visible at the current scroll position, just keep it the same
  2345. updateScroll = activeLi.position > scrollTop + that.sizeInfo.menuInnerHeight;
  2346. }
  2347. liActive = that.selectpicker.main.elements[searchMatch];
  2348. liActive.classList.add('active');
  2349. if (liActive.firstChild) liActive.firstChild.classList.add('active');
  2350. that.activeIndex = matches[matchIndex];
  2351. liActive.firstChild.focus();
  2352. if (updateScroll) that.$menuInner[0].scrollTop = offset;
  2353. $this.trigger('focus');
  2354. }
  2355. }
  2356. // Select focused option if "Enter", "Spacebar" or "Tab" (when selectOnTab is true) are pressed inside the menu.
  2357. if (
  2358. isActive &&
  2359. (
  2360. (e.which === keyCodes.SPACE && !that.selectpicker.keydown.keyHistory) ||
  2361. e.which === keyCodes.ENTER ||
  2362. (e.which === keyCodes.TAB && that.options.selectOnTab)
  2363. )
  2364. ) {
  2365. if (e.which !== keyCodes.SPACE) e.preventDefault();
  2366. if (!that.options.liveSearch || e.which !== keyCodes.SPACE) {
  2367. that.$menuInner.find('.active a').trigger('click', true); // retain active class
  2368. $this.trigger('focus');
  2369. if (!that.options.liveSearch) {
  2370. // Prevent screen from scrolling if the user hits the spacebar
  2371. e.preventDefault();
  2372. // Fixes spacebar selection of dropdown items in FF & IE
  2373. $(document).data('spaceSelect', true);
  2374. }
  2375. }
  2376. }
  2377. },
  2378. mobile: function () {
  2379. this.$element[0].classList.add('mobile-device');
  2380. },
  2381. refresh: function () {
  2382. // update options if data attributes have been changed
  2383. var config = $.extend({}, this.options, this.$element.data());
  2384. this.options = config;
  2385. this.checkDisabled();
  2386. this.setStyle();
  2387. this.render();
  2388. this.createLi();
  2389. this.setWidth();
  2390. this.setSize(true);
  2391. this.$element.trigger('refreshed' + EVENT_KEY);
  2392. },
  2393. hide: function () {
  2394. this.$newElement.hide();
  2395. },
  2396. show: function () {
  2397. this.$newElement.show();
  2398. },
  2399. remove: function () {
  2400. this.$newElement.remove();
  2401. this.$element.remove();
  2402. },
  2403. destroy: function () {
  2404. this.$newElement.before(this.$element).remove();
  2405. if (this.$bsContainer) {
  2406. this.$bsContainer.remove();
  2407. } else {
  2408. this.$menu.remove();
  2409. }
  2410. this.$element
  2411. .off(EVENT_KEY)
  2412. .removeData('selectpicker')
  2413. .removeClass('bs-select-hidden selectpicker');
  2414. $(window).off(EVENT_KEY + '.' + this.selectId);
  2415. }
  2416. };
  2417. // SELECTPICKER PLUGIN DEFINITION
  2418. // ==============================
  2419. function Plugin (option) {
  2420. // get the args of the outer function..
  2421. var args = arguments;
  2422. // The arguments of the function are explicitly re-defined from the argument list, because the shift causes them
  2423. // to get lost/corrupted in android 2.3 and IE9 #715 #775
  2424. var _option = option;
  2425. [].shift.apply(args);
  2426. // if the version was not set successfully
  2427. if (!version.success) {
  2428. // try to retreive it again
  2429. try {
  2430. version.full = ($.fn.dropdown.Constructor.VERSION || '').split(' ')[0].split('.');
  2431. } catch (err) {
  2432. // fall back to use BootstrapVersion if set
  2433. if (Selectpicker.BootstrapVersion) {
  2434. version.full = Selectpicker.BootstrapVersion.split(' ')[0].split('.');
  2435. } else {
  2436. version.full = [version.major, '0', '0'];
  2437. console.warn(
  2438. 'There was an issue retrieving Bootstrap\'s version. ' +
  2439. 'Ensure Bootstrap is being loaded before bootstrap-select and there is no namespace collision. ' +
  2440. 'If loading Bootstrap asynchronously, the version may need to be manually specified via $.fn.selectpicker.Constructor.BootstrapVersion.',
  2441. err
  2442. );
  2443. }
  2444. }
  2445. version.major = version.full[0];
  2446. version.success = true;
  2447. }
  2448. if (version.major === '4') {
  2449. // some defaults need to be changed if using Bootstrap 4
  2450. // check to see if they have already been manually changed before forcing them to update
  2451. var toUpdate = [];
  2452. if (Selectpicker.DEFAULTS.style === classNames.BUTTONCLASS) toUpdate.push({ name: 'style', className: 'BUTTONCLASS' });
  2453. if (Selectpicker.DEFAULTS.iconBase === classNames.ICONBASE) toUpdate.push({ name: 'iconBase', className: 'ICONBASE' });
  2454. if (Selectpicker.DEFAULTS.tickIcon === classNames.TICKICON) toUpdate.push({ name: 'tickIcon', className: 'TICKICON' });
  2455. classNames.DIVIDER = 'dropdown-divider';
  2456. classNames.SHOW = 'show';
  2457. classNames.BUTTONCLASS = 'btn-light';
  2458. classNames.POPOVERHEADER = 'popover-header';
  2459. classNames.ICONBASE = '';
  2460. classNames.TICKICON = 'bs-ok-default';
  2461. for (var i = 0; i < toUpdate.length; i++) {
  2462. var option = toUpdate[i];
  2463. Selectpicker.DEFAULTS[option.name] = classNames[option.className];
  2464. }
  2465. }
  2466. var value;
  2467. var chain = this.each(function () {
  2468. var $this = $(this);
  2469. if ($this.is('select')) {
  2470. var data = $this.data('selectpicker'),
  2471. options = typeof _option == 'object' && _option;
  2472. if (!data) {
  2473. var dataAttributes = $this.data();
  2474. for (var dataAttr in dataAttributes) {
  2475. if (dataAttributes.hasOwnProperty(dataAttr) && $.inArray(dataAttr, DISALLOWED_ATTRIBUTES) !== -1) {
  2476. delete dataAttributes[dataAttr];
  2477. }
  2478. }
  2479. var config = $.extend({}, Selectpicker.DEFAULTS, $.fn.selectpicker.defaults || {}, dataAttributes, options);
  2480. config.template = $.extend({}, Selectpicker.DEFAULTS.template, ($.fn.selectpicker.defaults ? $.fn.selectpicker.defaults.template : {}), dataAttributes.template, options.template);
  2481. $this.data('selectpicker', (data = new Selectpicker(this, config)));
  2482. } else if (options) {
  2483. for (var i in options) {
  2484. if (options.hasOwnProperty(i)) {
  2485. data.options[i] = options[i];
  2486. }
  2487. }
  2488. }
  2489. if (typeof _option == 'string') {
  2490. if (data[_option] instanceof Function) {
  2491. value = data[_option].apply(data, args);
  2492. } else {
  2493. value = data.options[_option];
  2494. }
  2495. }
  2496. }
  2497. });
  2498. if (typeof value !== 'undefined') {
  2499. // noinspection JSUnusedAssignment
  2500. return value;
  2501. } else {
  2502. return chain;
  2503. }
  2504. }
  2505. var old = $.fn.selectpicker;
  2506. $.fn.selectpicker = Plugin;
  2507. $.fn.selectpicker.Constructor = Selectpicker;
  2508. // SELECTPICKER NO CONFLICT
  2509. // ========================
  2510. $.fn.selectpicker.noConflict = function () {
  2511. $.fn.selectpicker = old;
  2512. return this;
  2513. };
  2514. $(document)
  2515. .off('keydown.bs.dropdown.data-api')
  2516. .on('keydown' + EVENT_KEY, '.bootstrap-select [data-toggle="dropdown"], .bootstrap-select [role="listbox"], .bootstrap-select .bs-searchbox input', Selectpicker.prototype.keydown)
  2517. .on('focusin.modal', '.bootstrap-select [data-toggle="dropdown"], .bootstrap-select [role="listbox"], .bootstrap-select .bs-searchbox input', function (e) {
  2518. e.stopPropagation();
  2519. });
  2520. // SELECTPICKER DATA-API
  2521. // =====================
  2522. $(window).on('load' + EVENT_KEY + '.data-api', function () {
  2523. $('.selectpicker').each(function () {
  2524. var $selectpicker = $(this);
  2525. Plugin.call($selectpicker, $selectpicker.data());
  2526. })
  2527. });
  2528. })(jQuery);
  2529. }));
  2530. //# sourceMappingURL=bootstrap-select.js.map