Advertisement
andresSCL

CSS bootstrap-select

Apr 24th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 78.92 KB | None | 0 0
  1. Crea un archivo con nombre bootstrap-select.css
  2.  
  3. lo linkeas al sitio
  4. <link href="css/bootstrap-select.css" rel="stylesheet">
  5.  
  6. Creas el archivo y le aplicas todo este codigo.-
  7.  
  8.  
  9. /*!
  10. * Bootstrap-select v1.12.2 (http://silviomoreto.github.io/bootstrap-select)
  11. *
  12. * Copyright 2013-2017 bootstrap-select
  13. * Licensed under MIT (https://github.com/silviomoreto/bootstrap-select/blob/master/LICENSE)
  14. */
  15.  
  16. select.bs-select-hidden,
  17. select.selectpicker {
  18. display: none !important;
  19. }
  20. .bootstrap-select {
  21. width: 220px \0;
  22. /*IE9 and below*/
  23. }
  24. .bootstrap-select > .dropdown-toggle {
  25. width: 100%;
  26. padding-right: 25px;
  27. z-index: 1;
  28. }
  29. .bootstrap-select > .dropdown-toggle.bs-placeholder,
  30. .bootstrap-select > .dropdown-toggle.bs-placeholder:hover,
  31. .bootstrap-select > .dropdown-toggle.bs-placeholder:focus,
  32. .bootstrap-select > .dropdown-toggle.bs-placeholder:active {
  33. color: #999;
  34. }
  35. .bootstrap-select > select {
  36. position: absolute !important;
  37. bottom: 0;
  38. left: 50%;
  39. display: block !important;
  40. width: 0.5px !important;
  41. height: 100% !important;
  42. padding: 0 !important;
  43. opacity: 0 !important;
  44. border: none;
  45. }
  46. .bootstrap-select > select.mobile-device {
  47. top: 0;
  48. left: 0;
  49. display: block !important;
  50. width: 100% !important;
  51. z-index: 2;
  52. }
  53. .has-error .bootstrap-select .dropdown-toggle,
  54. .error .bootstrap-select .dropdown-toggle {
  55. border-color: #b94a48;
  56. }
  57. .bootstrap-select.fit-width {
  58. width: auto !important;
  59. }
  60. .bootstrap-select:not([class*="col-"]):not([class*="form-control"]):not(.input-group-btn) {
  61. width: 220px;
  62. }
  63. .bootstrap-select .dropdown-toggle:focus {
  64. outline: thin dotted #333333 !important;
  65. outline: 5px auto -webkit-focus-ring-color !important;
  66. outline-offset: -2px;
  67. }
  68. .bootstrap-select.form-control {
  69. margin-bottom: 0;
  70. padding: 0;
  71. border: none;
  72. }
  73. .bootstrap-select.form-control:not([class*="col-"]) {
  74. width: 100%;
  75. }
  76. .bootstrap-select.form-control.input-group-btn {
  77. z-index: auto;
  78. }
  79. .bootstrap-select.form-control.input-group-btn:not(:first-child):not(:last-child) > .btn {
  80. border-radius: 0;
  81. }
  82. .bootstrap-select.btn-group:not(.input-group-btn),
  83. .bootstrap-select.btn-group[class*="col-"] {
  84. float: none;
  85. display: inline-block;
  86. margin-left: 0;
  87. }
  88. .bootstrap-select.btn-group.dropdown-menu-right,
  89. .bootstrap-select.btn-group[class*="col-"].dropdown-menu-right,
  90. .row .bootstrap-select.btn-group[class*="col-"].dropdown-menu-right {
  91. float: right;
  92. }
  93. .form-inline .bootstrap-select.btn-group,
  94. .form-horizontal .bootstrap-select.btn-group,
  95. .form-group .bootstrap-select.btn-group {
  96. margin-bottom: 0;
  97. }
  98. .form-group-lg .bootstrap-select.btn-group.form-control,
  99. .form-group-sm .bootstrap-select.btn-group.form-control {
  100. padding: 0;
  101. }
  102. .form-group-lg .bootstrap-select.btn-group.form-control .dropdown-toggle,
  103. .form-group-sm .bootstrap-select.btn-group.form-control .dropdown-toggle {
  104. height: 100%;
  105. font-size: inherit;
  106. line-height: inherit;
  107. border-radius: inherit;
  108. }
  109. .form-inline .bootstrap-select.btn-group .form-control {
  110. width: 100%;
  111. }
  112. .bootstrap-select.btn-group.disabled,
  113. .bootstrap-select.btn-group > .disabled {
  114. cursor: not-allowed;
  115. }
  116. .bootstrap-select.btn-group.disabled:focus,
  117. .bootstrap-select.btn-group > .disabled:focus {
  118. outline: none !important;
  119. }
  120. .bootstrap-select.btn-group.bs-container {
  121. position: absolute;
  122. height: 0 !important;
  123. padding: 0 !important;
  124. }
  125. .bootstrap-select.btn-group.bs-container .dropdown-menu {
  126. z-index: 1060;
  127. }
  128. .bootstrap-select.btn-group .dropdown-toggle .filter-option {
  129. display: inline-block;
  130. overflow: hidden;
  131. width: 100%;
  132. text-align: left;
  133. }
  134. .bootstrap-select.btn-group .dropdown-toggle .caret {
  135. position: absolute;
  136. top: 50%;
  137. right: 12px;
  138. margin-top: -2px;
  139. vertical-align: middle;
  140. }
  141. .bootstrap-select.btn-group[class*="col-"] .dropdown-toggle {
  142. width: 100%;
  143. }
  144. .bootstrap-select.btn-group .dropdown-menu {
  145. min-width: 100%;
  146. -webkit-box-sizing: border-box;
  147. -moz-box-sizing: border-box;
  148. box-sizing: border-box;
  149. }
  150. .bootstrap-select.btn-group .dropdown-menu.inner {
  151. position: static;
  152. float: none;
  153. border: 0;
  154. padding: 0;
  155. margin: 0;
  156. border-radius: 0;
  157. -webkit-box-shadow: none;
  158. box-shadow: none;
  159. }
  160. .bootstrap-select.btn-group .dropdown-menu li {
  161. position: relative;
  162. }
  163. .bootstrap-select.btn-group .dropdown-menu li.active small {
  164. color: #fff;
  165. }
  166. .bootstrap-select.btn-group .dropdown-menu li.disabled a {
  167. cursor: not-allowed;
  168. }
  169. .bootstrap-select.btn-group .dropdown-menu li a {
  170. cursor: pointer;
  171. -webkit-user-select: none;
  172. -moz-user-select: none;
  173. -ms-user-select: none;
  174. user-select: none;
  175. }
  176. .bootstrap-select.btn-group .dropdown-menu li a.opt {
  177. position: relative;
  178. padding-left: 2.25em;
  179. }
  180. .bootstrap-select.btn-group .dropdown-menu li a span.check-mark {
  181. display: none;
  182. }
  183. .bootstrap-select.btn-group .dropdown-menu li a span.text {
  184. display: inline-block;
  185. }
  186. .bootstrap-select.btn-group .dropdown-menu li small {
  187. padding-left: 0.5em;
  188. }
  189. .bootstrap-select.btn-group .dropdown-menu .notify {
  190. position: absolute;
  191. bottom: 5px;
  192. width: 96%;
  193. margin: 0 2%;
  194. min-height: 26px;
  195. padding: 3px 5px;
  196. background: #f5f5f5;
  197. border: 1px solid #e3e3e3;
  198. -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
  199. box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
  200. pointer-events: none;
  201. opacity: 0.9;
  202. -webkit-box-sizing: border-box;
  203. -moz-box-sizing: border-box;
  204. box-sizing: border-box;
  205. }
  206. .bootstrap-select.btn-group .no-results {
  207. padding: 3px;
  208. background: #f5f5f5;
  209. margin: 0 5px;
  210. white-space: nowrap;
  211. }
  212. .bootstrap-select.btn-group.fit-width .dropdown-toggle .filter-option {
  213. position: static;
  214. }
  215. .bootstrap-select.btn-group.fit-width .dropdown-toggle .caret {
  216. position: static;
  217. top: auto;
  218. margin-top: -1px;
  219. }
  220. .bootstrap-select.btn-group.show-tick .dropdown-menu li.selected a span.check-mark {
  221. position: absolute;
  222. display: inline-block;
  223. right: 15px;
  224. margin-top: 5px;
  225. }
  226. .bootstrap-select.btn-group.show-tick .dropdown-menu li a span.text {
  227. margin-right: 34px;
  228. }
  229. .bootstrap-select.show-menu-arrow.open > .dropdown-toggle {
  230. z-index: 1061;
  231. }
  232. .bootstrap-select.show-menu-arrow .dropdown-toggle:before {
  233. content: '';
  234. border-left: 7px solid transparent;
  235. border-right: 7px solid transparent;
  236. border-bottom: 7px solid rgba(204, 204, 204, 0.2);
  237. position: absolute;
  238. bottom: -4px;
  239. left: 9px;
  240. display: none;
  241. }
  242. .bootstrap-select.show-menu-arrow .dropdown-toggle:after {
  243. content: '';
  244. border-left: 6px solid transparent;
  245. border-right: 6px solid transparent;
  246. border-bottom: 6px solid white;
  247. position: absolute;
  248. bottom: -4px;
  249. left: 10px;
  250. display: none;
  251. }
  252. .bootstrap-select.show-menu-arrow.dropup .dropdown-toggle:before {
  253. bottom: auto;
  254. top: -3px;
  255. border-top: 7px solid rgba(204, 204, 204, 0.2);
  256. border-bottom: 0;
  257. }
  258. .bootstrap-select.show-menu-arrow.dropup .dropdown-toggle:after {
  259. bottom: auto;
  260. top: -3px;
  261. border-top: 6px solid white;
  262. border-bottom: 0;
  263. }
  264. .bootstrap-select.show-menu-arrow.pull-right .dropdown-toggle:before {
  265. right: 12px;
  266. left: auto;
  267. }
  268. .bootstrap-select.show-menu-arrow.pull-right .dropdown-toggle:after {
  269. right: 13px;
  270. left: auto;
  271. }
  272. .bootstrap-select.show-menu-arrow.open > .dropdown-toggle:before,
  273. .bootstrap-select.show-menu-arrow.open > .dropdown-toggle:after {
  274. display: block;
  275. }
  276. .bs-searchbox,
  277. .bs-actionsbox,
  278. .bs-donebutton {
  279. padding: 4px 8px;
  280. }
  281. .bs-actionsbox {
  282. width: 100%;
  283. -webkit-box-sizing: border-box;
  284. -moz-box-sizing: border-box;
  285. box-sizing: border-box;
  286. }
  287. .bs-actionsbox .btn-group button {
  288. width: 50%;
  289. }
  290. .bs-donebutton {
  291. float: left;
  292. width: 100%;
  293. -webkit-box-sizing: border-box;
  294. -moz-box-sizing: border-box;
  295. box-sizing: border-box;
  296. }
  297. .bs-donebutton .btn-group button {
  298. width: 100%;
  299. }
  300. .bs-searchbox + .bs-actionsbox {
  301. padding: 0 8px 4px;
  302. }
  303. .bs-searchbox .form-control {
  304. margin-bottom: 0;
  305. width: 100%;
  306. float: none;
  307. }
  308. /*# sourceMappingURL=bootstrap-select.css.map */
  309.  
  310.  
  311. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  312. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  313. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  314.  
  315.  
  316. /*!
  317. * Bootstrap-select v1.12.2 (http://silviomoreto.github.io/bootstrap-select)
  318. *
  319. * Copyright 2013-2017 bootstrap-select
  320. * Licensed under MIT (https://github.com/silviomoreto/bootstrap-select/blob/master/LICENSE)
  321. */
  322.  
  323. (function (root, factory) {
  324. if (typeof define === 'function' && define.amd) {
  325. // AMD. Register as an anonymous module unless amdModuleId is set
  326. define(["jquery"], function (a0) {
  327. return (factory(a0));
  328. });
  329. } else if (typeof module === 'object' && module.exports) {
  330. // Node. Does not work with strict CommonJS, but
  331. // only CommonJS-like environments that support module.exports,
  332. // like Node.
  333. module.exports = factory(require("jquery"));
  334. } else {
  335. factory(root["jQuery"]);
  336. }
  337. }(this, function (jQuery) {
  338.  
  339. (function ($) {
  340. 'use strict';
  341.  
  342. //<editor-fold desc="Shims">
  343. if (!String.prototype.includes) {
  344. (function () {
  345. 'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
  346. var toString = {}.toString;
  347. var defineProperty = (function () {
  348. // IE 8 only supports `Object.defineProperty` on DOM elements
  349. try {
  350. var object = {};
  351. var $defineProperty = Object.defineProperty;
  352. var result = $defineProperty(object, object, object) && $defineProperty;
  353. } catch (error) {
  354. }
  355. return result;
  356. }());
  357. var indexOf = ''.indexOf;
  358. var includes = function (search) {
  359. if (this == null) {
  360. throw new TypeError();
  361. }
  362. var string = String(this);
  363. if (search && toString.call(search) == '[object RegExp]') {
  364. throw new TypeError();
  365. }
  366. var stringLength = string.length;
  367. var searchString = String(search);
  368. var searchLength = searchString.length;
  369. var position = arguments.length > 1 ? arguments[1] : undefined;
  370. // `ToInteger`
  371. var pos = position ? Number(position) : 0;
  372. if (pos != pos) { // better `isNaN`
  373. pos = 0;
  374. }
  375. var start = Math.min(Math.max(pos, 0), stringLength);
  376. // Avoid the `indexOf` call if no match is possible
  377. if (searchLength + start > stringLength) {
  378. return false;
  379. }
  380. return indexOf.call(string, searchString, pos) != -1;
  381. };
  382. if (defineProperty) {
  383. defineProperty(String.prototype, 'includes', {
  384. 'value': includes,
  385. 'configurable': true,
  386. 'writable': true
  387. });
  388. } else {
  389. String.prototype.includes = includes;
  390. }
  391. }());
  392. }
  393.  
  394. if (!String.prototype.startsWith) {
  395. (function () {
  396. 'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
  397. var defineProperty = (function () {
  398. // IE 8 only supports `Object.defineProperty` on DOM elements
  399. try {
  400. var object = {};
  401. var $defineProperty = Object.defineProperty;
  402. var result = $defineProperty(object, object, object) && $defineProperty;
  403. } catch (error) {
  404. }
  405. return result;
  406. }());
  407. var toString = {}.toString;
  408. var startsWith = function (search) {
  409. if (this == null) {
  410. throw new TypeError();
  411. }
  412. var string = String(this);
  413. if (search && toString.call(search) == '[object RegExp]') {
  414. throw new TypeError();
  415. }
  416. var stringLength = string.length;
  417. var searchString = String(search);
  418. var searchLength = searchString.length;
  419. var position = arguments.length > 1 ? arguments[1] : undefined;
  420. // `ToInteger`
  421. var pos = position ? Number(position) : 0;
  422. if (pos != pos) { // better `isNaN`
  423. pos = 0;
  424. }
  425. var start = Math.min(Math.max(pos, 0), stringLength);
  426. // Avoid the `indexOf` call if no match is possible
  427. if (searchLength + start > stringLength) {
  428. return false;
  429. }
  430. var index = -1;
  431. while (++index < searchLength) {
  432. if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
  433. return false;
  434. }
  435. }
  436. return true;
  437. };
  438. if (defineProperty) {
  439. defineProperty(String.prototype, 'startsWith', {
  440. 'value': startsWith,
  441. 'configurable': true,
  442. 'writable': true
  443. });
  444. } else {
  445. String.prototype.startsWith = startsWith;
  446. }
  447. }());
  448. }
  449.  
  450. if (!Object.keys) {
  451. Object.keys = function (
  452. o, // object
  453. k, // key
  454. r // result array
  455. ){
  456. // initialize object and result
  457. r=[];
  458. // iterate over object keys
  459. for (k in o)
  460. // fill result array with non-prototypical keys
  461. r.hasOwnProperty.call(o, k) && r.push(k);
  462. // return result
  463. return r;
  464. };
  465. }
  466.  
  467. // set data-selected on select element if the value has been programmatically selected
  468. // prior to initialization of bootstrap-select
  469. // * consider removing or replacing an alternative method *
  470. var valHooks = {
  471. useDefault: false,
  472. _set: $.valHooks.select.set
  473. };
  474.  
  475. $.valHooks.select.set = function(elem, value) {
  476. if (value && !valHooks.useDefault) $(elem).data('selected', true);
  477.  
  478. return valHooks._set.apply(this, arguments);
  479. };
  480.  
  481. var changed_arguments = null;
  482. $.fn.triggerNative = function (eventName) {
  483. var el = this[0],
  484. event;
  485.  
  486. if (el.dispatchEvent) { // for modern browsers & IE9+
  487. if (typeof Event === 'function') {
  488. // For modern browsers
  489. event = new Event(eventName, {
  490. bubbles: true
  491. });
  492. } else {
  493. // For IE since it doesn't support Event constructor
  494. event = document.createEvent('Event');
  495. event.initEvent(eventName, true, false);
  496. }
  497.  
  498. el.dispatchEvent(event);
  499. } else if (el.fireEvent) { // for IE8
  500. event = document.createEventObject();
  501. event.eventType = eventName;
  502. el.fireEvent('on' + eventName, event);
  503. } else {
  504. // fall back to jQuery.trigger
  505. this.trigger(eventName);
  506. }
  507. };
  508. //</editor-fold>
  509.  
  510. // Case insensitive contains search
  511. $.expr.pseudos.icontains = function (obj, index, meta) {
  512. var $obj = $(obj);
  513. var haystack = ($obj.data('tokens') || $obj.text()).toString().toUpperCase();
  514. return haystack.includes(meta[3].toUpperCase());
  515. };
  516.  
  517. // Case insensitive begins search
  518. $.expr.pseudos.ibegins = function (obj, index, meta) {
  519. var $obj = $(obj);
  520. var haystack = ($obj.data('tokens') || $obj.text()).toString().toUpperCase();
  521. return haystack.startsWith(meta[3].toUpperCase());
  522. };
  523.  
  524. // Case and accent insensitive contains search
  525. $.expr.pseudos.aicontains = function (obj, index, meta) {
  526. var $obj = $(obj);
  527. var haystack = ($obj.data('tokens') || $obj.data('normalizedText') || $obj.text()).toString().toUpperCase();
  528. return haystack.includes(meta[3].toUpperCase());
  529. };
  530.  
  531. // Case and accent insensitive begins search
  532. $.expr.pseudos.aibegins = function (obj, index, meta) {
  533. var $obj = $(obj);
  534. var haystack = ($obj.data('tokens') || $obj.data('normalizedText') || $obj.text()).toString().toUpperCase();
  535. return haystack.startsWith(meta[3].toUpperCase());
  536. };
  537.  
  538. /**
  539. * Remove all diatrics from the given text.
  540. * @access private
  541. * @param {String} text
  542. * @returns {String}
  543. */
  544. function normalizeToBase(text) {
  545. var rExps = [
  546. {re: /[\xC0-\xC6]/g, ch: "A"},
  547. {re: /[\xE0-\xE6]/g, ch: "a"},
  548. {re: /[\xC8-\xCB]/g, ch: "E"},
  549. {re: /[\xE8-\xEB]/g, ch: "e"},
  550. {re: /[\xCC-\xCF]/g, ch: "I"},
  551. {re: /[\xEC-\xEF]/g, ch: "i"},
  552. {re: /[\xD2-\xD6]/g, ch: "O"},
  553. {re: /[\xF2-\xF6]/g, ch: "o"},
  554. {re: /[\xD9-\xDC]/g, ch: "U"},
  555. {re: /[\xF9-\xFC]/g, ch: "u"},
  556. {re: /[\xC7-\xE7]/g, ch: "c"},
  557. {re: /[\xD1]/g, ch: "N"},
  558. {re: /[\xF1]/g, ch: "n"}
  559. ];
  560. $.each(rExps, function () {
  561. text = text ? text.replace(this.re, this.ch) : '';
  562. });
  563. return text;
  564. }
  565.  
  566.  
  567. // List of HTML entities for escaping.
  568. var escapeMap = {
  569. '&': '&amp;',
  570. '<': '&lt;',
  571. '>': '&gt;',
  572. '"': '&quot;',
  573. "'": '&#x27;',
  574. '`': '&#x60;'
  575. };
  576.  
  577. var unescapeMap = {
  578. '&amp;': '&',
  579. '&lt;': '<',
  580. '&gt;': '>',
  581. '&quot;': '"',
  582. '&#x27;': "'",
  583. '&#x60;': '`'
  584. };
  585.  
  586. // Functions for escaping and unescaping strings to/from HTML interpolation.
  587. var createEscaper = function(map) {
  588. var escaper = function(match) {
  589. return map[match];
  590. };
  591. // Regexes for identifying a key that needs to be escaped.
  592. var source = '(?:' + Object.keys(map).join('|') + ')';
  593. var testRegexp = RegExp(source);
  594. var replaceRegexp = RegExp(source, 'g');
  595. return function(string) {
  596. string = string == null ? '' : '' + string;
  597. return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
  598. };
  599. };
  600.  
  601. var htmlEscape = createEscaper(escapeMap);
  602. var htmlUnescape = createEscaper(unescapeMap);
  603.  
  604. var Selectpicker = function (element, options) {
  605. // bootstrap-select has been initialized - revert valHooks.select.set back to its original function
  606. if (!valHooks.useDefault) {
  607. $.valHooks.select.set = valHooks._set;
  608. valHooks.useDefault = true;
  609. }
  610.  
  611. this.$element = $(element);
  612. this.$newElement = null;
  613. this.$button = null;
  614. this.$menu = null;
  615. this.$lis = null;
  616. this.options = options;
  617.  
  618. // 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
  619. // data-attribute)
  620. if (this.options.title === null) {
  621. this.options.title = this.$element.attr('title');
  622. }
  623.  
  624. // Format window padding
  625. var winPad = this.options.windowPadding;
  626. if (typeof winPad === 'number') {
  627. this.options.windowPadding = [winPad, winPad, winPad, winPad];
  628. }
  629.  
  630. //Expose public methods
  631. this.val = Selectpicker.prototype.val;
  632. this.render = Selectpicker.prototype.render;
  633. this.refresh = Selectpicker.prototype.refresh;
  634. this.setStyle = Selectpicker.prototype.setStyle;
  635. this.selectAll = Selectpicker.prototype.selectAll;
  636. this.deselectAll = Selectpicker.prototype.deselectAll;
  637. this.destroy = Selectpicker.prototype.destroy;
  638. this.remove = Selectpicker.prototype.remove;
  639. this.show = Selectpicker.prototype.show;
  640. this.hide = Selectpicker.prototype.hide;
  641.  
  642. this.init();
  643. };
  644.  
  645. Selectpicker.VERSION = '1.12.2';
  646.  
  647. // part of this is duplicated in i18n/defaults-en_US.js. Make sure to update both.
  648. Selectpicker.DEFAULTS = {
  649. noneSelectedText: 'Nothing selected',
  650. noneResultsText: 'No results matched {0}',
  651. countSelectedText: function (numSelected, numTotal) {
  652. return (numSelected == 1) ? "{0} item selected" : "{0} items selected";
  653. },
  654. maxOptionsText: function (numAll, numGroup) {
  655. return [
  656. (numAll == 1) ? 'Limit reached ({n} item max)' : 'Limit reached ({n} items max)',
  657. (numGroup == 1) ? 'Group limit reached ({n} item max)' : 'Group limit reached ({n} items max)'
  658. ];
  659. },
  660. selectAllText: 'Select All',
  661. deselectAllText: 'Deselect All',
  662. doneButton: false,
  663. doneButtonText: 'Close',
  664. multipleSeparator: ', ',
  665. styleBase: 'btn',
  666. style: 'btn-default',
  667. size: 'auto',
  668. title: null,
  669. selectedTextFormat: 'values',
  670. width: false,
  671. container: false,
  672. hideDisabled: false,
  673. showSubtext: false,
  674. showIcon: true,
  675. showContent: true,
  676. dropupAuto: true,
  677. header: false,
  678. liveSearch: false,
  679. liveSearchPlaceholder: null,
  680. liveSearchNormalize: false,
  681. liveSearchStyle: 'contains',
  682. actionsBox: false,
  683. iconBase: 'glyphicon',
  684. tickIcon: 'glyphicon-ok',
  685. showTick: false,
  686. template: {
  687. caret: '<span class="caret"></span>'
  688. },
  689. maxOptions: false,
  690. mobile: false,
  691. selectOnTab: false,
  692. dropdownAlignRight: false,
  693. windowPadding: 0
  694. };
  695.  
  696. Selectpicker.prototype = {
  697.  
  698. constructor: Selectpicker,
  699.  
  700. init: function () {
  701. var that = this,
  702. id = this.$element.attr('id');
  703.  
  704. this.$element.addClass('bs-select-hidden');
  705.  
  706. // store originalIndex (key) and newIndex (value) in this.liObj for fast accessibility
  707. // allows us to do this.$lis.eq(that.liObj[index]) instead of this.$lis.filter('[data-original-index="' + index + '"]')
  708. this.liObj = {};
  709. this.multiple = this.$element.prop('multiple');
  710. this.autofocus = this.$element.prop('autofocus');
  711. this.$newElement = this.createView();
  712. this.$element
  713. .after(this.$newElement)
  714. .appendTo(this.$newElement);
  715. this.$button = this.$newElement.children('button');
  716. this.$menu = this.$newElement.children('.dropdown-menu');
  717. this.$menuInner = this.$menu.children('.inner');
  718. this.$searchbox = this.$menu.find('input');
  719.  
  720. this.$element.removeClass('bs-select-hidden');
  721.  
  722. if (this.options.dropdownAlignRight === true) this.$menu.addClass('dropdown-menu-right');
  723.  
  724. if (typeof id !== 'undefined') {
  725. this.$button.attr('data-id', id);
  726. $('label[for="' + id + '"]').click(function (e) {
  727. e.preventDefault();
  728. that.$button.focus();
  729. });
  730. }
  731.  
  732. this.checkDisabled();
  733. this.clickListener();
  734. if (this.options.liveSearch) this.liveSearchListener();
  735. this.render();
  736. this.setStyle();
  737. this.setWidth();
  738. if (this.options.container) this.selectPosition();
  739. this.$menu.data('this', this);
  740. this.$newElement.data('this', this);
  741. if (this.options.mobile) this.mobile();
  742.  
  743. this.$newElement.on({
  744. 'hide.bs.dropdown': function (e) {
  745. that.$menuInner.attr('aria-expanded', false);
  746. that.$element.trigger('hide.bs.select', e);
  747. },
  748. 'hidden.bs.dropdown': function (e) {
  749. that.$element.trigger('hidden.bs.select', e);
  750. },
  751. 'show.bs.dropdown': function (e) {
  752. that.$menuInner.attr('aria-expanded', true);
  753. that.$element.trigger('show.bs.select', e);
  754. },
  755. 'shown.bs.dropdown': function (e) {
  756. that.$element.trigger('shown.bs.select', e);
  757. }
  758. });
  759.  
  760. if (that.$element[0].hasAttribute('required')) {
  761. this.$element.on('invalid', function () {
  762. that.$button
  763. .addClass('bs-invalid')
  764. .focus();
  765.  
  766. that.$element.on({
  767. 'focus.bs.select': function () {
  768. that.$button.focus();
  769. that.$element.off('focus.bs.select');
  770. },
  771. 'shown.bs.select': function () {
  772. that.$element
  773. .val(that.$element.val()) // set the value to hide the validation message in Chrome when menu is opened
  774. .off('shown.bs.select');
  775. },
  776. 'rendered.bs.select': function () {
  777. // if select is no longer invalid, remove the bs-invalid class
  778. if (this.validity.valid) that.$button.removeClass('bs-invalid');
  779. that.$element.off('rendered.bs.select');
  780. }
  781. });
  782. });
  783. }
  784.  
  785. setTimeout(function () {
  786. that.$element.trigger('loaded.bs.select');
  787. });
  788. },
  789.  
  790. createDropdown: function () {
  791. // Options
  792. // If we are multiple or showTick option is set, then add the show-tick class
  793. var showTick = (this.multiple || this.options.showTick) ? ' show-tick' : '',
  794. inputGroup = this.$element.parent().hasClass('input-group') ? ' input-group-btn' : '',
  795. autofocus = this.autofocus ? ' autofocus' : '';
  796. // Elements
  797. var header = this.options.header ? '<div class="popover-title"><button type="button" class="close" aria-hidden="true">&times;</button>' + this.options.header + '</div>' : '';
  798. var searchbox = this.options.liveSearch ?
  799. '<div class="bs-searchbox">' +
  800. '<input type="text" class="form-control" autocomplete="off"' +
  801. (null === this.options.liveSearchPlaceholder ? '' : ' placeholder="' + htmlEscape(this.options.liveSearchPlaceholder) + '"') + ' role="textbox" aria-label="Search">' +
  802. '</div>'
  803. : '';
  804. var actionsbox = this.multiple && this.options.actionsBox ?
  805. '<div class="bs-actionsbox">' +
  806. '<div class="btn-group btn-group-sm btn-block">' +
  807. '<button type="button" class="actions-btn bs-select-all btn btn-default">' +
  808. this.options.selectAllText +
  809. '</button>' +
  810. '<button type="button" class="actions-btn bs-deselect-all btn btn-default">' +
  811. this.options.deselectAllText +
  812. '</button>' +
  813. '</div>' +
  814. '</div>'
  815. : '';
  816. var donebutton = this.multiple && this.options.doneButton ?
  817. '<div class="bs-donebutton">' +
  818. '<div class="btn-group btn-block">' +
  819. '<button type="button" class="btn btn-sm btn-default">' +
  820. this.options.doneButtonText +
  821. '</button>' +
  822. '</div>' +
  823. '</div>'
  824. : '';
  825. var drop =
  826. '<div class="btn-group bootstrap-select' + showTick + inputGroup + '">' +
  827. '<button type="button" class="' + this.options.styleBase + ' dropdown-toggle" data-toggle="dropdown"' + autofocus + ' role="button">' +
  828. '<span class="filter-option pull-left"></span>&nbsp;' +
  829. '<span class="bs-caret">' +
  830. this.options.template.caret +
  831. '</span>' +
  832. '</button>' +
  833. '<div class="dropdown-menu open" role="combobox">' +
  834. header +
  835. searchbox +
  836. actionsbox +
  837. '<ul class="dropdown-menu inner" role="listbox" aria-expanded="false">' +
  838. '</ul>' +
  839. donebutton +
  840. '</div>' +
  841. '</div>';
  842.  
  843. return $(drop);
  844. },
  845.  
  846. createView: function () {
  847. var $drop = this.createDropdown(),
  848. li = this.createLi();
  849.  
  850. $drop.find('ul')[0].innerHTML = li;
  851. return $drop;
  852. },
  853.  
  854. reloadLi: function () {
  855. // rebuild
  856. var li = this.createLi();
  857. this.$menuInner[0].innerHTML = li;
  858. },
  859.  
  860. createLi: function () {
  861. var that = this,
  862. _li = [],
  863. optID = 0,
  864. titleOption = document.createElement('option'),
  865. liIndex = -1; // increment liIndex whenever a new <li> element is created to ensure liObj is correct
  866.  
  867. // Helper functions
  868. /**
  869. * @param content
  870. * @param [index]
  871. * @param [classes]
  872. * @param [optgroup]
  873. * @returns {string}
  874. */
  875. var generateLI = function (content, index, classes, optgroup) {
  876. return '<li' +
  877. ((typeof classes !== 'undefined' & '' !== classes) ? ' class="' + classes + '"' : '') +
  878. ((typeof index !== 'undefined' & null !== index) ? ' data-original-index="' + index + '"' : '') +
  879. ((typeof optgroup !== 'undefined' & null !== optgroup) ? 'data-optgroup="' + optgroup + '"' : '') +
  880. '>' + content + '</li>';
  881. };
  882.  
  883. /**
  884. * @param text
  885. * @param [classes]
  886. * @param [inline]
  887. * @param [tokens]
  888. * @returns {string}
  889. */
  890. var generateA = function (text, classes, inline, tokens) {
  891. return '<a tabindex="0"' +
  892. (typeof classes !== 'undefined' ? ' class="' + classes + '"' : '') +
  893. (inline ? ' style="' + inline + '"' : '') +
  894. (that.options.liveSearchNormalize ? ' data-normalized-text="' + normalizeToBase(htmlEscape($(text).html())) + '"' : '') +
  895. (typeof tokens !== 'undefined' || tokens !== null ? ' data-tokens="' + tokens + '"' : '') +
  896. ' role="option">' + text +
  897. '<span class="' + that.options.iconBase + ' ' + that.options.tickIcon + ' check-mark"></span>' +
  898. '</a>';
  899. };
  900.  
  901. if (this.options.title && !this.multiple) {
  902. // this option doesn't create a new <li> element, but does add a new option, so liIndex is decreased
  903. // since liObj is recalculated on every refresh, liIndex needs to be decreased even if the titleOption is already appended
  904. liIndex--;
  905.  
  906. if (!this.$element.find('.bs-title-option').length) {
  907. // Use native JS to prepend option (faster)
  908. var element = this.$element[0];
  909. titleOption.className = 'bs-title-option';
  910. titleOption.innerHTML = this.options.title;
  911. titleOption.value = '';
  912. element.insertBefore(titleOption, element.firstChild);
  913. // Check if selected or data-selected attribute is already set on an option. If not, select the titleOption option.
  914. // the selected item may have been changed by user or programmatically before the bootstrap select plugin runs,
  915. // if so, the select will have the data-selected attribute
  916. var $opt = $(element.options[element.selectedIndex]);
  917. if ($opt.attr('selected') === undefined && this.$element.data('selected') === undefined) {
  918. titleOption.selected = true;
  919. }
  920. }
  921. }
  922.  
  923. this.$element.find('option').each(function (index) {
  924. var $this = $(this);
  925.  
  926. liIndex++;
  927.  
  928. if ($this.hasClass('bs-title-option')) return;
  929.  
  930. // Get the class and text for the option
  931. var optionClass = this.className || '',
  932. inline = this.style.cssText,
  933. text = $this.data('content') ? $this.data('content') : $this.html(),
  934. tokens = $this.data('tokens') ? $this.data('tokens') : null,
  935. subtext = typeof $this.data('subtext') !== 'undefined' ? '<small class="text-muted">' + $this.data('subtext') + '</small>' : '',
  936. icon = typeof $this.data('icon') !== 'undefined' ? '<span class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></span> ' : '',
  937. $parent = $this.parent(),
  938. isOptgroup = $parent[0].tagName === 'OPTGROUP',
  939. isOptgroupDisabled = isOptgroup && $parent[0].disabled,
  940. isDisabled = this.disabled || isOptgroupDisabled;
  941.  
  942. if (icon !== '' && isDisabled) {
  943. icon = '<span>' + icon + '</span>';
  944. }
  945.  
  946. if (that.options.hideDisabled && (isDisabled && !isOptgroup || isOptgroupDisabled)) {
  947. liIndex--;
  948. return;
  949. }
  950.  
  951. if (!$this.data('content')) {
  952. // Prepend any icon and append any subtext to the main text.
  953. text = icon + '<span class="text">' + text + subtext + '</span>';
  954. }
  955.  
  956. if (isOptgroup && $this.data('divider') !== true) {
  957. if (that.options.hideDisabled && isDisabled) {
  958. if ($parent.data('allOptionsDisabled') === undefined) {
  959. var $options = $parent.children();
  960. $parent.data('allOptionsDisabled', $options.filter(':disabled').length === $options.length);
  961. }
  962.  
  963. if ($parent.data('allOptionsDisabled')) {
  964. liIndex--;
  965. return;
  966. }
  967. }
  968.  
  969. var optGroupClass = ' ' + $parent[0].className || '';
  970.  
  971. if ($this.index() === 0) { // Is it the first option of the optgroup?
  972. optID += 1;
  973.  
  974. // Get the opt group label
  975. var label = $parent[0].label,
  976. labelSubtext = typeof $parent.data('subtext') !== 'undefined' ? '<small class="text-muted">' + $parent.data('subtext') + '</small>' : '',
  977. labelIcon = $parent.data('icon') ? '<span class="' + that.options.iconBase + ' ' + $parent.data('icon') + '"></span> ' : '';
  978.  
  979. label = labelIcon + '<span class="text">' + htmlEscape(label) + labelSubtext + '</span>';
  980.  
  981. if (index !== 0 && _li.length > 0) { // Is it NOT the first option of the select && are there elements in the dropdown?
  982. liIndex++;
  983. _li.push(generateLI('', null, 'divider', optID + 'div'));
  984. }
  985. liIndex++;
  986. _li.push(generateLI(label, null, 'dropdown-header' + optGroupClass, optID));
  987. }
  988.  
  989. if (that.options.hideDisabled && isDisabled) {
  990. liIndex--;
  991. return;
  992. }
  993.  
  994. _li.push(generateLI(generateA(text, 'opt ' + optionClass + optGroupClass, inline, tokens), index, '', optID));
  995. } else if ($this.data('divider') === true) {
  996. _li.push(generateLI('', index, 'divider'));
  997. } else if ($this.data('hidden') === true) {
  998. _li.push(generateLI(generateA(text, optionClass, inline, tokens), index, 'hidden is-hidden'));
  999. } else {
  1000. var showDivider = this.previousElementSibling && this.previousElementSibling.tagName === 'OPTGROUP';
  1001.  
  1002. // if previous element is not an optgroup and hideDisabled is true
  1003. if (!showDivider && that.options.hideDisabled) {
  1004. // get previous elements
  1005. var $prev = $(this).prevAll();
  1006.  
  1007. for (var i = 0; i < $prev.length; i++) {
  1008. // find the first element in the previous elements that is an optgroup
  1009. if ($prev[i].tagName === 'OPTGROUP') {
  1010. var optGroupDistance = 0;
  1011.  
  1012. // loop through the options in between the current option and the optgroup
  1013. // and check if they are hidden or disabled
  1014. for (var d = 0; d < i; d++) {
  1015. var prevOption = $prev[d];
  1016. if (prevOption.disabled || $(prevOption).data('hidden') === true) optGroupDistance++;
  1017. }
  1018.  
  1019. // if all of the options between the current option and the optgroup are hidden or disabled, show the divider
  1020. if (optGroupDistance === i) showDivider = true;
  1021.  
  1022. break;
  1023. }
  1024. }
  1025. }
  1026.  
  1027. if (showDivider) {
  1028. liIndex++;
  1029. _li.push(generateLI('', null, 'divider', optID + 'div'));
  1030. }
  1031. _li.push(generateLI(generateA(text, optionClass, inline, tokens), index));
  1032. }
  1033.  
  1034. that.liObj[index] = liIndex;
  1035. });
  1036.  
  1037. //If we are not multiple, we don't have a selected item, and we don't have a title, select the first element so something is set in the button
  1038. if (!this.multiple && this.$element.find('option:selected').length === 0 && !this.options.title) {
  1039. this.$element.find('option').eq(0).prop('selected', true).attr('selected', 'selected');
  1040. }
  1041.  
  1042. return _li.join('');
  1043. },
  1044.  
  1045. findLis: function () {
  1046. if (this.$lis == null) this.$lis = this.$menu.find('li');
  1047. return this.$lis;
  1048. },
  1049.  
  1050. /**
  1051. * @param [updateLi] defaults to true
  1052. */
  1053. render: function (updateLi) {
  1054. var that = this,
  1055. notDisabled;
  1056.  
  1057. //Update the LI to match the SELECT
  1058. if (updateLi !== false) {
  1059. this.$element.find('option').each(function (index) {
  1060. var $lis = that.findLis().eq(that.liObj[index]);
  1061.  
  1062. that.setDisabled(index, this.disabled || this.parentNode.tagName === 'OPTGROUP' && this.parentNode.disabled, $lis);
  1063. that.setSelected(index, this.selected, $lis);
  1064. });
  1065. }
  1066.  
  1067. this.togglePlaceholder();
  1068.  
  1069. this.tabIndex();
  1070.  
  1071. var selectedItems = this.$element.find('option').map(function () {
  1072. if (this.selected) {
  1073. if (that.options.hideDisabled && (this.disabled || this.parentNode.tagName === 'OPTGROUP' && this.parentNode.disabled)) return;
  1074.  
  1075. var $this = $(this),
  1076. icon = $this.data('icon') && that.options.showIcon ? '<i class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></i> ' : '',
  1077. subtext;
  1078.  
  1079. if (that.options.showSubtext && $this.data('subtext') && !that.multiple) {
  1080. subtext = ' <small class="text-muted">' + $this.data('subtext') + '</small>';
  1081. } else {
  1082. subtext = '';
  1083. }
  1084. if (typeof $this.attr('title') !== 'undefined') {
  1085. return $this.attr('title');
  1086. } else if ($this.data('content') && that.options.showContent) {
  1087. return $this.data('content').toString();
  1088. } else {
  1089. return icon + $this.html() + subtext;
  1090. }
  1091. }
  1092. }).toArray();
  1093.  
  1094. //Fixes issue in IE10 occurring when no default option is selected and at least one option is disabled
  1095. //Convert all the values into a comma delimited string
  1096. var title = !this.multiple ? selectedItems[0] : selectedItems.join(this.options.multipleSeparator);
  1097.  
  1098. //If this is multi select, and the selectText type is count, the show 1 of 2 selected etc..
  1099. if (this.multiple && this.options.selectedTextFormat.indexOf('count') > -1) {
  1100. var max = this.options.selectedTextFormat.split('>');
  1101. if ((max.length > 1 && selectedItems.length > max[1]) || (max.length == 1 && selectedItems.length >= 2)) {
  1102. notDisabled = this.options.hideDisabled ? ', [disabled]' : '';
  1103. var totalCount = this.$element.find('option').not('[data-divider="true"], [data-hidden="true"]' + notDisabled).length,
  1104. tr8nText = (typeof this.options.countSelectedText === 'function') ? this.options.countSelectedText(selectedItems.length, totalCount) : this.options.countSelectedText;
  1105. title = tr8nText.replace('{0}', selectedItems.length.toString()).replace('{1}', totalCount.toString());
  1106. }
  1107. }
  1108.  
  1109. if (this.options.title == undefined) {
  1110. this.options.title = this.$element.attr('title');
  1111. }
  1112.  
  1113. if (this.options.selectedTextFormat == 'static') {
  1114. title = this.options.title;
  1115. }
  1116.  
  1117. //If we dont have a title, then use the default, or if nothing is set at all, use the not selected text
  1118. if (!title) {
  1119. title = typeof this.options.title !== 'undefined' ? this.options.title : this.options.noneSelectedText;
  1120. }
  1121.  
  1122. //strip all HTML tags and trim the result, then unescape any escaped tags
  1123. this.$button.attr('title', htmlUnescape($.trim(title.replace(/<[^>]*>?/g, ''))));
  1124. this.$button.children('.filter-option').html(title);
  1125.  
  1126. this.$element.trigger('rendered.bs.select');
  1127. },
  1128.  
  1129. /**
  1130. * @param [style]
  1131. * @param [status]
  1132. */
  1133. setStyle: function (style, status) {
  1134. if (this.$element.attr('class')) {
  1135. this.$newElement.addClass(this.$element.attr('class').replace(/selectpicker|mobile-device|bs-select-hidden|validate\[.*\]/gi, ''));
  1136. }
  1137.  
  1138. var buttonClass = style ? style : this.options.style;
  1139.  
  1140. if (status == 'add') {
  1141. this.$button.addClass(buttonClass);
  1142. } else if (status == 'remove') {
  1143. this.$button.removeClass(buttonClass);
  1144. } else {
  1145. this.$button.removeClass(this.options.style);
  1146. this.$button.addClass(buttonClass);
  1147. }
  1148. },
  1149.  
  1150. liHeight: function (refresh) {
  1151. if (!refresh && (this.options.size === false || this.sizeInfo)) return;
  1152.  
  1153. var newElement = document.createElement('div'),
  1154. menu = document.createElement('div'),
  1155. menuInner = document.createElement('ul'),
  1156. divider = document.createElement('li'),
  1157. li = document.createElement('li'),
  1158. a = document.createElement('a'),
  1159. text = document.createElement('span'),
  1160. header = this.options.header && this.$menu.find('.popover-title').length > 0 ? this.$menu.find('.popover-title')[0].cloneNode(true) : null,
  1161. search = this.options.liveSearch ? document.createElement('div') : null,
  1162. actions = this.options.actionsBox && this.multiple && this.$menu.find('.bs-actionsbox').length > 0 ? this.$menu.find('.bs-actionsbox')[0].cloneNode(true) : null,
  1163. doneButton = this.options.doneButton && this.multiple && this.$menu.find('.bs-donebutton').length > 0 ? this.$menu.find('.bs-donebutton')[0].cloneNode(true) : null;
  1164.  
  1165. text.className = 'text';
  1166. newElement.className = this.$menu[0].parentNode.className + ' open';
  1167. menu.className = 'dropdown-menu open';
  1168. menuInner.className = 'dropdown-menu inner';
  1169. divider.className = 'divider';
  1170.  
  1171. text.appendChild(document.createTextNode('Inner text'));
  1172. a.appendChild(text);
  1173. li.appendChild(a);
  1174. menuInner.appendChild(li);
  1175. menuInner.appendChild(divider);
  1176. if (header) menu.appendChild(header);
  1177. if (search) {
  1178. var input = document.createElement('input');
  1179. search.className = 'bs-searchbox';
  1180. input.className = 'form-control';
  1181. search.appendChild(input);
  1182. menu.appendChild(search);
  1183. }
  1184. if (actions) menu.appendChild(actions);
  1185. menu.appendChild(menuInner);
  1186. if (doneButton) menu.appendChild(doneButton);
  1187. newElement.appendChild(menu);
  1188.  
  1189. document.body.appendChild(newElement);
  1190.  
  1191. var liHeight = a.offsetHeight,
  1192. headerHeight = header ? header.offsetHeight : 0,
  1193. searchHeight = search ? search.offsetHeight : 0,
  1194. actionsHeight = actions ? actions.offsetHeight : 0,
  1195. doneButtonHeight = doneButton ? doneButton.offsetHeight : 0,
  1196. dividerHeight = $(divider).outerHeight(true),
  1197. // fall back to jQuery if getComputedStyle is not supported
  1198. menuStyle = typeof getComputedStyle === 'function' ? getComputedStyle(menu) : false,
  1199. $menu = menuStyle ? null : $(menu),
  1200. menuPadding = {
  1201. vert: parseInt(menuStyle ? menuStyle.paddingTop : $menu.css('paddingTop')) +
  1202. parseInt(menuStyle ? menuStyle.paddingBottom : $menu.css('paddingBottom')) +
  1203. parseInt(menuStyle ? menuStyle.borderTopWidth : $menu.css('borderTopWidth')) +
  1204. parseInt(menuStyle ? menuStyle.borderBottomWidth : $menu.css('borderBottomWidth')),
  1205. horiz: parseInt(menuStyle ? menuStyle.paddingLeft : $menu.css('paddingLeft')) +
  1206. parseInt(menuStyle ? menuStyle.paddingRight : $menu.css('paddingRight')) +
  1207. parseInt(menuStyle ? menuStyle.borderLeftWidth : $menu.css('borderLeftWidth')) +
  1208. parseInt(menuStyle ? menuStyle.borderRightWidth : $menu.css('borderRightWidth'))
  1209. },
  1210. menuExtras = {
  1211. vert: menuPadding.vert +
  1212. parseInt(menuStyle ? menuStyle.marginTop : $menu.css('marginTop')) +
  1213. parseInt(menuStyle ? menuStyle.marginBottom : $menu.css('marginBottom')) + 2,
  1214. horiz: menuPadding.horiz +
  1215. parseInt(menuStyle ? menuStyle.marginLeft : $menu.css('marginLeft')) +
  1216. parseInt(menuStyle ? menuStyle.marginRight : $menu.css('marginRight')) + 2
  1217. }
  1218.  
  1219. document.body.removeChild(newElement);
  1220.  
  1221. this.sizeInfo = {
  1222. liHeight: liHeight,
  1223. headerHeight: headerHeight,
  1224. searchHeight: searchHeight,
  1225. actionsHeight: actionsHeight,
  1226. doneButtonHeight: doneButtonHeight,
  1227. dividerHeight: dividerHeight,
  1228. menuPadding: menuPadding,
  1229. menuExtras: menuExtras
  1230. };
  1231. },
  1232.  
  1233. setSize: function () {
  1234. this.findLis();
  1235. this.liHeight();
  1236.  
  1237. if (this.options.header) this.$menu.css('padding-top', 0);
  1238. if (this.options.size === false) return;
  1239.  
  1240. var that = this,
  1241. $menu = this.$menu,
  1242. $menuInner = this.$menuInner,
  1243. $window = $(window),
  1244. selectHeight = this.$newElement[0].offsetHeight,
  1245. selectWidth = this.$newElement[0].offsetWidth,
  1246. liHeight = this.sizeInfo['liHeight'],
  1247. headerHeight = this.sizeInfo['headerHeight'],
  1248. searchHeight = this.sizeInfo['searchHeight'],
  1249. actionsHeight = this.sizeInfo['actionsHeight'],
  1250. doneButtonHeight = this.sizeInfo['doneButtonHeight'],
  1251. divHeight = this.sizeInfo['dividerHeight'],
  1252. menuPadding = this.sizeInfo['menuPadding'],
  1253. menuExtras = this.sizeInfo['menuExtras'],
  1254. notDisabled = this.options.hideDisabled ? '.disabled' : '',
  1255. menuHeight,
  1256. menuWidth,
  1257. getHeight,
  1258. getWidth,
  1259. selectOffsetTop,
  1260. selectOffsetBot,
  1261. selectOffsetLeft,
  1262. selectOffsetRight,
  1263. getPos = function() {
  1264. var pos = that.$newElement.offset(),
  1265. $container = $(that.options.container),
  1266. containerPos;
  1267.  
  1268. if (that.options.container && !$container.is('body')) {
  1269. containerPos = $container.offset();
  1270. containerPos.top += parseInt($container.css('borderTopWidth'));
  1271. containerPos.left += parseInt($container.css('borderLeftWidth'));
  1272. } else {
  1273. containerPos = { top: 0, left: 0 };
  1274. }
  1275.  
  1276. var winPad = that.options.windowPadding;
  1277. selectOffsetTop = pos.top - containerPos.top - $window.scrollTop();
  1278. selectOffsetBot = $window.height() - selectOffsetTop - selectHeight - containerPos.top - winPad[2];
  1279. selectOffsetLeft = pos.left - containerPos.left - $window.scrollLeft();
  1280. selectOffsetRight = $window.width() - selectOffsetLeft - selectWidth - containerPos.left - winPad[1];
  1281. selectOffsetTop -= winPad[0];
  1282. selectOffsetLeft -= winPad[3];
  1283. };
  1284.  
  1285. getPos();
  1286.  
  1287. if (this.options.size === 'auto') {
  1288. var getSize = function () {
  1289. var minHeight,
  1290. hasClass = function (className, include) {
  1291. return function (element) {
  1292. if (include) {
  1293. return (element.classList ? element.classList.contains(className) : $(element).hasClass(className));
  1294. } else {
  1295. return !(element.classList ? element.classList.contains(className) : $(element).hasClass(className));
  1296. }
  1297. };
  1298. },
  1299. lis = that.$menuInner[0].getElementsByTagName('li'),
  1300. lisVisible = Array.prototype.filter ? Array.prototype.filter.call(lis, hasClass('hidden', false)) : that.$lis.not('.hidden'),
  1301. optGroup = Array.prototype.filter ? Array.prototype.filter.call(lisVisible, hasClass('dropdown-header', true)) : lisVisible.filter('.dropdown-header');
  1302.  
  1303. getPos();
  1304. menuHeight = selectOffsetBot - menuExtras.vert;
  1305. menuWidth = selectOffsetRight - menuExtras.horiz;
  1306.  
  1307. if (that.options.container) {
  1308. if (!$menu.data('height')) $menu.data('height', $menu.height());
  1309. getHeight = $menu.data('height');
  1310.  
  1311. if (!$menu.data('width')) $menu.data('width', $menu.width());
  1312. getWidth = $menu.data('width');
  1313. } else {
  1314. getHeight = $menu.height();
  1315. getWidth = $menu.width();
  1316. }
  1317.  
  1318. if (that.options.dropupAuto) {
  1319. that.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && (menuHeight - menuExtras.vert) < getHeight);
  1320. }
  1321.  
  1322. if (that.$newElement.hasClass('dropup')) {
  1323. menuHeight = selectOffsetTop - menuExtras.vert;
  1324. }
  1325.  
  1326. if (that.options.dropdownAlignRight === 'auto') {
  1327. $menu.toggleClass('dropdown-menu-right', selectOffsetLeft > selectOffsetRight && (menuWidth - menuExtras.horiz) < (getWidth - selectWidth));
  1328. }
  1329.  
  1330. if ((lisVisible.length + optGroup.length) > 3) {
  1331. minHeight = liHeight * 3 + menuExtras.vert - 2;
  1332. } else {
  1333. minHeight = 0;
  1334. }
  1335.  
  1336. $menu.css({
  1337. 'max-height': menuHeight + 'px',
  1338. 'overflow': 'hidden',
  1339. 'min-height': minHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px'
  1340. });
  1341. $menuInner.css({
  1342. 'max-height': menuHeight - headerHeight - searchHeight - actionsHeight - doneButtonHeight - menuPadding.vert + 'px',
  1343. 'overflow-y': 'auto',
  1344. 'min-height': Math.max(minHeight - menuPadding.vert, 0) + 'px'
  1345. });
  1346. };
  1347. getSize();
  1348. this.$searchbox.off('input.getSize propertychange.getSize').on('input.getSize propertychange.getSize', getSize);
  1349. $window.off('resize.getSize scroll.getSize').on('resize.getSize scroll.getSize', getSize);
  1350. } else if (this.options.size && this.options.size != 'auto' && this.$lis.not(notDisabled).length > this.options.size) {
  1351. var optIndex = this.$lis.not('.divider').not(notDisabled).children().slice(0, this.options.size).last().parent().index(),
  1352. divLength = this.$lis.slice(0, optIndex + 1).filter('.divider').length;
  1353. menuHeight = liHeight * this.options.size + divLength * divHeight + menuPadding.vert;
  1354.  
  1355. if (that.options.container) {
  1356. if (!$menu.data('height')) $menu.data('height', $menu.height());
  1357. getHeight = $menu.data('height');
  1358. } else {
  1359. getHeight = $menu.height();
  1360. }
  1361.  
  1362. if (that.options.dropupAuto) {
  1363. //noinspection JSUnusedAssignment
  1364. this.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && (menuHeight - menuExtras.vert) < getHeight);
  1365. }
  1366. $menu.css({
  1367. 'max-height': menuHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px',
  1368. 'overflow': 'hidden',
  1369. 'min-height': ''
  1370. });
  1371. $menuInner.css({
  1372. 'max-height': menuHeight - menuPadding.vert + 'px',
  1373. 'overflow-y': 'auto',
  1374. 'min-height': ''
  1375. });
  1376. }
  1377. },
  1378.  
  1379. setWidth: function () {
  1380. if (this.options.width === 'auto') {
  1381. this.$menu.css('min-width', '0');
  1382.  
  1383. // Get correct width if element is hidden
  1384. var $selectClone = this.$menu.parent().clone().appendTo('body'),
  1385. $selectClone2 = this.options.container ? this.$newElement.clone().appendTo('body') : $selectClone,
  1386. ulWidth = $selectClone.children('.dropdown-menu').outerWidth(),
  1387. btnWidth = $selectClone2.css('width', 'auto').children('button').outerWidth();
  1388.  
  1389. $selectClone.remove();
  1390. $selectClone2.remove();
  1391.  
  1392. // Set width to whatever's larger, button title or longest option
  1393. this.$newElement.css('width', Math.max(ulWidth, btnWidth) + 'px');
  1394. } else if (this.options.width === 'fit') {
  1395. // Remove inline min-width so width can be changed from 'auto'
  1396. this.$menu.css('min-width', '');
  1397. this.$newElement.css('width', '').addClass('fit-width');
  1398. } else if (this.options.width) {
  1399. // Remove inline min-width so width can be changed from 'auto'
  1400. this.$menu.css('min-width', '');
  1401. this.$newElement.css('width', this.options.width);
  1402. } else {
  1403. // Remove inline min-width/width so width can be changed
  1404. this.$menu.css('min-width', '');
  1405. this.$newElement.css('width', '');
  1406. }
  1407. // Remove fit-width class if width is changed programmatically
  1408. if (this.$newElement.hasClass('fit-width') && this.options.width !== 'fit') {
  1409. this.$newElement.removeClass('fit-width');
  1410. }
  1411. },
  1412.  
  1413. selectPosition: function () {
  1414. this.$bsContainer = $('<div class="bs-container" />');
  1415.  
  1416. var that = this,
  1417. $container = $(this.options.container),
  1418. pos,
  1419. containerPos,
  1420. actualHeight,
  1421. getPlacement = function ($element) {
  1422. that.$bsContainer.addClass($element.attr('class').replace(/form-control|fit-width/gi, '')).toggleClass('dropup', $element.hasClass('dropup'));
  1423. pos = $element.offset();
  1424.  
  1425. if (!$container.is('body')) {
  1426. containerPos = $container.offset();
  1427. containerPos.top += parseInt($container.css('borderTopWidth')) - $container.scrollTop();
  1428. containerPos.left += parseInt($container.css('borderLeftWidth')) - $container.scrollLeft();
  1429. } else {
  1430. containerPos = { top: 0, left: 0 };
  1431. }
  1432.  
  1433. actualHeight = $element.hasClass('dropup') ? 0 : $element[0].offsetHeight;
  1434.  
  1435. that.$bsContainer.css({
  1436. 'top': pos.top - containerPos.top + actualHeight,
  1437. 'left': pos.left - containerPos.left,
  1438. 'width': $element[0].offsetWidth
  1439. });
  1440. };
  1441.  
  1442. this.$button.on('click', function () {
  1443. var $this = $(this);
  1444.  
  1445. if (that.isDisabled()) {
  1446. return;
  1447. }
  1448.  
  1449. getPlacement(that.$newElement);
  1450.  
  1451. that.$bsContainer
  1452. .appendTo(that.options.container)
  1453. .toggleClass('open', !$this.hasClass('open'))
  1454. .append(that.$menu);
  1455. });
  1456.  
  1457. $(window).on('resize scroll', function () {
  1458. getPlacement(that.$newElement);
  1459. });
  1460.  
  1461. this.$element.on('hide.bs.select', function () {
  1462. that.$menu.data('height', that.$menu.height());
  1463. that.$bsContainer.detach();
  1464. });
  1465. },
  1466.  
  1467. /**
  1468. * @param {number} index - the index of the option that is being changed
  1469. * @param {boolean} selected - true if the option is being selected, false if being deselected
  1470. * @param {JQuery} $lis - the 'li' element that is being modified
  1471. */
  1472. setSelected: function (index, selected, $lis) {
  1473. if (!$lis) {
  1474. this.togglePlaceholder(); // check if setSelected is being called by changing the value of the select
  1475. $lis = this.findLis().eq(this.liObj[index]);
  1476. }
  1477.  
  1478. $lis.toggleClass('selected', selected).find('a').attr('aria-selected', selected);
  1479. },
  1480.  
  1481. /**
  1482. * @param {number} index - the index of the option that is being disabled
  1483. * @param {boolean} disabled - true if the option is being disabled, false if being enabled
  1484. * @param {JQuery} $lis - the 'li' element that is being modified
  1485. */
  1486. setDisabled: function (index, disabled, $lis) {
  1487. if (!$lis) {
  1488. $lis = this.findLis().eq(this.liObj[index]);
  1489. }
  1490.  
  1491. if (disabled) {
  1492. $lis.addClass('disabled').children('a').attr('href', '#').attr('tabindex', -1).attr('aria-disabled', true);
  1493. } else {
  1494. $lis.removeClass('disabled').children('a').removeAttr('href').attr('tabindex', 0).attr('aria-disabled', false);
  1495. }
  1496. },
  1497.  
  1498. isDisabled: function () {
  1499. return this.$element[0].disabled;
  1500. },
  1501.  
  1502. checkDisabled: function () {
  1503. var that = this;
  1504.  
  1505. if (this.isDisabled()) {
  1506. this.$newElement.addClass('disabled');
  1507. this.$button.addClass('disabled').attr('tabindex', -1).attr('aria-disabled', true);
  1508. } else {
  1509. if (this.$button.hasClass('disabled')) {
  1510. this.$newElement.removeClass('disabled');
  1511. this.$button.removeClass('disabled').attr('aria-disabled', false);
  1512. }
  1513.  
  1514. if (this.$button.attr('tabindex') == -1 && !this.$element.data('tabindex')) {
  1515. this.$button.removeAttr('tabindex');
  1516. }
  1517. }
  1518.  
  1519. this.$button.click(function () {
  1520. return !that.isDisabled();
  1521. });
  1522. },
  1523.  
  1524. togglePlaceholder: function () {
  1525. var value = this.$element.val();
  1526. this.$button.toggleClass('bs-placeholder', value === null || value === '' || (value.constructor === Array && value.length === 0));
  1527. },
  1528.  
  1529. tabIndex: function () {
  1530. if (this.$element.data('tabindex') !== this.$element.attr('tabindex') &&
  1531. (this.$element.attr('tabindex') !== -98 && this.$element.attr('tabindex') !== '-98')) {
  1532. this.$element.data('tabindex', this.$element.attr('tabindex'));
  1533. this.$button.attr('tabindex', this.$element.data('tabindex'));
  1534. }
  1535.  
  1536. this.$element.attr('tabindex', -98);
  1537. },
  1538.  
  1539. clickListener: function () {
  1540. var that = this,
  1541. $document = $(document);
  1542.  
  1543. $document.data('spaceSelect', false);
  1544.  
  1545. this.$button.on('keyup', function (e) {
  1546. if (/(32)/.test(e.keyCode.toString(10)) && $document.data('spaceSelect')) {
  1547. e.preventDefault();
  1548. $document.data('spaceSelect', false);
  1549. }
  1550. });
  1551.  
  1552. this.$button.on('click', function () {
  1553. that.setSize();
  1554. });
  1555.  
  1556. this.$element.on('shown.bs.select', function () {
  1557. if (!that.options.liveSearch && !that.multiple) {
  1558. that.$menuInner.find('.selected a').focus();
  1559. } else if (!that.multiple) {
  1560. var selectedIndex = that.liObj[that.$element[0].selectedIndex];
  1561.  
  1562. if (typeof selectedIndex !== 'number' || that.options.size === false) return;
  1563.  
  1564. // scroll to selected option
  1565. var offset = that.$lis.eq(selectedIndex)[0].offsetTop - that.$menuInner[0].offsetTop;
  1566. offset = offset - that.$menuInner[0].offsetHeight/2 + that.sizeInfo.liHeight/2;
  1567. that.$menuInner[0].scrollTop = offset;
  1568. }
  1569. });
  1570.  
  1571. this.$menuInner.on('click', 'li a', function (e) {
  1572. var $this = $(this),
  1573. clickedIndex = $this.parent().data('originalIndex'),
  1574. prevValue = that.$element.val(),
  1575. prevIndex = that.$element.prop('selectedIndex'),
  1576. triggerChange = true;
  1577.  
  1578. // Don't close on multi choice menu
  1579. if (that.multiple && that.options.maxOptions !== 1) {
  1580. e.stopPropagation();
  1581. }
  1582.  
  1583. e.preventDefault();
  1584.  
  1585. //Don't run if we have been disabled
  1586. if (!that.isDisabled() && !$this.parent().hasClass('disabled')) {
  1587. var $options = that.$element.find('option'),
  1588. $option = $options.eq(clickedIndex),
  1589. state = $option.prop('selected'),
  1590. $optgroup = $option.parent('optgroup'),
  1591. maxOptions = that.options.maxOptions,
  1592. maxOptionsGrp = $optgroup.data('maxOptions') || false;
  1593.  
  1594. if (!that.multiple) { // Deselect all others if not multi select box
  1595. $options.prop('selected', false);
  1596. $option.prop('selected', true);
  1597. that.$menuInner.find('.selected').removeClass('selected').find('a').attr('aria-selected', false);
  1598. that.setSelected(clickedIndex, true);
  1599. } else { // Toggle the one we have chosen if we are multi select.
  1600. $option.prop('selected', !state);
  1601. that.setSelected(clickedIndex, !state);
  1602. $this.blur();
  1603.  
  1604. if (maxOptions !== false || maxOptionsGrp !== false) {
  1605. var maxReached = maxOptions < $options.filter(':selected').length,
  1606. maxReachedGrp = maxOptionsGrp < $optgroup.find('option:selected').length;
  1607.  
  1608. if ((maxOptions && maxReached) || (maxOptionsGrp && maxReachedGrp)) {
  1609. if (maxOptions && maxOptions == 1) {
  1610. $options.prop('selected', false);
  1611. $option.prop('selected', true);
  1612. that.$menuInner.find('.selected').removeClass('selected');
  1613. that.setSelected(clickedIndex, true);
  1614. } else if (maxOptionsGrp && maxOptionsGrp == 1) {
  1615. $optgroup.find('option:selected').prop('selected', false);
  1616. $option.prop('selected', true);
  1617. var optgroupID = $this.parent().data('optgroup');
  1618. that.$menuInner.find('[data-optgroup="' + optgroupID + '"]').removeClass('selected');
  1619. that.setSelected(clickedIndex, true);
  1620. } else {
  1621. var maxOptionsText = typeof that.options.maxOptionsText === 'string' ? [that.options.maxOptionsText, that.options.maxOptionsText] : that.options.maxOptionsText,
  1622. maxOptionsArr = typeof maxOptionsText === 'function' ? maxOptionsText(maxOptions, maxOptionsGrp) : maxOptionsText,
  1623. maxTxt = maxOptionsArr[0].replace('{n}', maxOptions),
  1624. maxTxtGrp = maxOptionsArr[1].replace('{n}', maxOptionsGrp),
  1625. $notify = $('<div class="notify"></div>');
  1626. // If {var} is set in array, replace it
  1627. /** @deprecated */
  1628. if (maxOptionsArr[2]) {
  1629. maxTxt = maxTxt.replace('{var}', maxOptionsArr[2][maxOptions > 1 ? 0 : 1]);
  1630. maxTxtGrp = maxTxtGrp.replace('{var}', maxOptionsArr[2][maxOptionsGrp > 1 ? 0 : 1]);
  1631. }
  1632.  
  1633. $option.prop('selected', false);
  1634.  
  1635. that.$menu.append($notify);
  1636.  
  1637. if (maxOptions && maxReached) {
  1638. $notify.append($('<div>' + maxTxt + '</div>'));
  1639. triggerChange = false;
  1640. that.$element.trigger('maxReached.bs.select');
  1641. }
  1642.  
  1643. if (maxOptionsGrp && maxReachedGrp) {
  1644. $notify.append($('<div>' + maxTxtGrp + '</div>'));
  1645. triggerChange = false;
  1646. that.$element.trigger('maxReachedGrp.bs.select');
  1647. }
  1648.  
  1649. setTimeout(function () {
  1650. that.setSelected(clickedIndex, false);
  1651. }, 10);
  1652.  
  1653. $notify.delay(750).fadeOut(300, function () {
  1654. $(this).remove();
  1655. });
  1656. }
  1657. }
  1658. }
  1659. }
  1660.  
  1661. if (!that.multiple || (that.multiple && that.options.maxOptions === 1)) {
  1662. that.$button.focus();
  1663. } else if (that.options.liveSearch) {
  1664. that.$searchbox.focus();
  1665. }
  1666.  
  1667. // Trigger select 'change'
  1668. if (triggerChange) {
  1669. if ((prevValue != that.$element.val() && that.multiple) || (prevIndex != that.$element.prop('selectedIndex') && !that.multiple)) {
  1670. // $option.prop('selected') is current option state (selected/unselected). state is previous option state.
  1671. changed_arguments = [clickedIndex, $option.prop('selected'), state];
  1672. that.$element
  1673. .triggerNative('change');
  1674. }
  1675. }
  1676. }
  1677. });
  1678.  
  1679. this.$menu.on('click', 'li.disabled a, .popover-title, .popover-title :not(.close)', function (e) {
  1680. if (e.currentTarget == this) {
  1681. e.preventDefault();
  1682. e.stopPropagation();
  1683. if (that.options.liveSearch && !$(e.target).hasClass('close')) {
  1684. that.$searchbox.focus();
  1685. } else {
  1686. that.$button.focus();
  1687. }
  1688. }
  1689. });
  1690.  
  1691. this.$menuInner.on('click', '.divider, .dropdown-header', function (e) {
  1692. e.preventDefault();
  1693. e.stopPropagation();
  1694. if (that.options.liveSearch) {
  1695. that.$searchbox.focus();
  1696. } else {
  1697. that.$button.focus();
  1698. }
  1699. });
  1700.  
  1701. this.$menu.on('click', '.popover-title .close', function () {
  1702. that.$button.click();
  1703. });
  1704.  
  1705. this.$searchbox.on('click', function (e) {
  1706. e.stopPropagation();
  1707. });
  1708.  
  1709. this.$menu.on('click', '.actions-btn', function (e) {
  1710. if (that.options.liveSearch) {
  1711. that.$searchbox.focus();
  1712. } else {
  1713. that.$button.focus();
  1714. }
  1715.  
  1716. e.preventDefault();
  1717. e.stopPropagation();
  1718.  
  1719. if ($(this).hasClass('bs-select-all')) {
  1720. that.selectAll();
  1721. } else {
  1722. that.deselectAll();
  1723. }
  1724. });
  1725.  
  1726. this.$element.change(function () {
  1727. that.render(false);
  1728. that.$element.trigger('changed.bs.select', changed_arguments);
  1729. changed_arguments = null;
  1730. });
  1731. },
  1732.  
  1733. liveSearchListener: function () {
  1734. var that = this,
  1735. $no_results = $('<li class="no-results"></li>');
  1736.  
  1737. this.$button.on('click.dropdown.data-api', function () {
  1738. that.$menuInner.find('.active').removeClass('active');
  1739. if (!!that.$searchbox.val()) {
  1740. that.$searchbox.val('');
  1741. that.$lis.not('.is-hidden').removeClass('hidden');
  1742. if (!!$no_results.parent().length) $no_results.remove();
  1743. }
  1744. if (!that.multiple) that.$menuInner.find('.selected').addClass('active');
  1745. setTimeout(function () {
  1746. that.$searchbox.focus();
  1747. }, 10);
  1748. });
  1749.  
  1750. this.$searchbox.on('click.dropdown.data-api focus.dropdown.data-api touchend.dropdown.data-api', function (e) {
  1751. e.stopPropagation();
  1752. });
  1753.  
  1754. this.$searchbox.on('input propertychange', function () {
  1755. that.$lis.not('.is-hidden').removeClass('hidden');
  1756. that.$lis.filter('.active').removeClass('active');
  1757. $no_results.remove();
  1758.  
  1759. if (that.$searchbox.val()) {
  1760. var $searchBase = that.$lis.not('.is-hidden, .divider, .dropdown-header'),
  1761. $hideItems;
  1762. if (that.options.liveSearchNormalize) {
  1763. $hideItems = $searchBase.find('a').not(':a' + that._searchStyle() + '("' + normalizeToBase(that.$searchbox.val()) + '")');
  1764. } else {
  1765. $hideItems = $searchBase.find('a').not(':' + that._searchStyle() + '("' + that.$searchbox.val() + '")');
  1766. }
  1767.  
  1768. if ($hideItems.length === $searchBase.length) {
  1769. $no_results.html(that.options.noneResultsText.replace('{0}', '"' + htmlEscape(that.$searchbox.val()) + '"'));
  1770. that.$menuInner.append($no_results);
  1771. that.$lis.addClass('hidden');
  1772. } else {
  1773. $hideItems.parent().addClass('hidden');
  1774.  
  1775. var $lisVisible = that.$lis.not('.hidden'),
  1776. $foundDiv;
  1777.  
  1778. // hide divider if first or last visible, or if followed by another divider
  1779. $lisVisible.each(function (index) {
  1780. var $this = $(this);
  1781.  
  1782. if ($this.hasClass('divider')) {
  1783. if ($foundDiv === undefined) {
  1784. $this.addClass('hidden');
  1785. } else {
  1786. if ($foundDiv) $foundDiv.addClass('hidden');
  1787. $foundDiv = $this;
  1788. }
  1789. } else if ($this.hasClass('dropdown-header') && $lisVisible.eq(index + 1).data('optgroup') !== $this.data('optgroup')) {
  1790. $this.addClass('hidden');
  1791. } else {
  1792. $foundDiv = null;
  1793. }
  1794. });
  1795. if ($foundDiv) $foundDiv.addClass('hidden');
  1796.  
  1797. $searchBase.not('.hidden').first().addClass('active');
  1798. }
  1799. }
  1800. });
  1801. },
  1802.  
  1803. _searchStyle: function () {
  1804. var styles = {
  1805. begins: 'ibegins',
  1806. startsWith: 'ibegins'
  1807. };
  1808.  
  1809. return styles[this.options.liveSearchStyle] || 'icontains';
  1810. },
  1811.  
  1812. val: function (value) {
  1813. if (typeof value !== 'undefined') {
  1814. this.$element.val(value);
  1815. this.render();
  1816.  
  1817. return this.$element;
  1818. } else {
  1819. return this.$element.val();
  1820. }
  1821. },
  1822.  
  1823. changeAll: function (status) {
  1824. if (!this.multiple) return;
  1825. if (typeof status === 'undefined') status = true;
  1826.  
  1827. this.findLis();
  1828.  
  1829. var $options = this.$element.find('option'),
  1830. $lisVisible = this.$lis.not('.divider, .dropdown-header, .disabled, .hidden'),
  1831. lisVisLen = $lisVisible.length,
  1832. selectedOptions = [];
  1833.  
  1834. if (status) {
  1835. if ($lisVisible.filter('.selected').length === $lisVisible.length) return;
  1836. } else {
  1837. if ($lisVisible.filter('.selected').length === 0) return;
  1838. }
  1839.  
  1840. $lisVisible.toggleClass('selected', status);
  1841.  
  1842. for (var i = 0; i < lisVisLen; i++) {
  1843. var origIndex = $lisVisible[i].getAttribute('data-original-index');
  1844. selectedOptions[selectedOptions.length] = $options.eq(origIndex)[0];
  1845. }
  1846.  
  1847. $(selectedOptions).prop('selected', status);
  1848.  
  1849. this.render(false);
  1850.  
  1851. this.togglePlaceholder();
  1852.  
  1853. this.$element
  1854. .triggerNative('change');
  1855. },
  1856.  
  1857. selectAll: function () {
  1858. return this.changeAll(true);
  1859. },
  1860.  
  1861. deselectAll: function () {
  1862. return this.changeAll(false);
  1863. },
  1864.  
  1865. toggle: function (e) {
  1866. e = e || window.event;
  1867.  
  1868. if (e) e.stopPropagation();
  1869.  
  1870. this.$button.trigger('click');
  1871. },
  1872.  
  1873. keydown: function (e) {
  1874. var $this = $(this),
  1875. $parent = $this.is('input') ? $this.parent().parent() : $this.parent(),
  1876. $items,
  1877. that = $parent.data('this'),
  1878. index,
  1879. next,
  1880. first,
  1881. last,
  1882. prev,
  1883. nextPrev,
  1884. prevIndex,
  1885. isActive,
  1886. selector = ':not(.disabled, .hidden, .dropdown-header, .divider)',
  1887. keyCodeMap = {
  1888. 32: ' ',
  1889. 48: '0',
  1890. 49: '1',
  1891. 50: '2',
  1892. 51: '3',
  1893. 52: '4',
  1894. 53: '5',
  1895. 54: '6',
  1896. 55: '7',
  1897. 56: '8',
  1898. 57: '9',
  1899. 59: ';',
  1900. 65: 'a',
  1901. 66: 'b',
  1902. 67: 'c',
  1903. 68: 'd',
  1904. 69: 'e',
  1905. 70: 'f',
  1906. 71: 'g',
  1907. 72: 'h',
  1908. 73: 'i',
  1909. 74: 'j',
  1910. 75: 'k',
  1911. 76: 'l',
  1912. 77: 'm',
  1913. 78: 'n',
  1914. 79: 'o',
  1915. 80: 'p',
  1916. 81: 'q',
  1917. 82: 'r',
  1918. 83: 's',
  1919. 84: 't',
  1920. 85: 'u',
  1921. 86: 'v',
  1922. 87: 'w',
  1923. 88: 'x',
  1924. 89: 'y',
  1925. 90: 'z',
  1926. 96: '0',
  1927. 97: '1',
  1928. 98: '2',
  1929. 99: '3',
  1930. 100: '4',
  1931. 101: '5',
  1932. 102: '6',
  1933. 103: '7',
  1934. 104: '8',
  1935. 105: '9'
  1936. };
  1937.  
  1938. if (that.options.liveSearch) $parent = $this.parent().parent();
  1939.  
  1940. if (that.options.container) $parent = that.$menu;
  1941.  
  1942. $items = $('[role="listbox"] li', $parent);
  1943.  
  1944. isActive = that.$newElement.hasClass('open');
  1945.  
  1946. if (!isActive && (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105 || e.keyCode >= 65 && e.keyCode <= 90)) {
  1947. if (!that.options.container) {
  1948. that.setSize();
  1949. that.$menu.parent().addClass('open');
  1950. isActive = true;
  1951. } else {
  1952. that.$button.trigger('click');
  1953. }
  1954. that.$searchbox.focus();
  1955. return;
  1956. }
  1957.  
  1958. if (that.options.liveSearch) {
  1959. if (/(^9$|27)/.test(e.keyCode.toString(10)) && isActive) {
  1960. e.preventDefault();
  1961. e.stopPropagation();
  1962. that.$menuInner.click();
  1963. that.$button.focus();
  1964. }
  1965. // $items contains li elements when liveSearch is enabled
  1966. $items = $('[role="listbox"] li' + selector, $parent);
  1967. if (!$this.val() && !/(38|40)/.test(e.keyCode.toString(10))) {
  1968. if ($items.filter('.active').length === 0) {
  1969. $items = that.$menuInner.find('li');
  1970. if (that.options.liveSearchNormalize) {
  1971. $items = $items.filter(':a' + that._searchStyle() + '(' + normalizeToBase(keyCodeMap[e.keyCode]) + ')');
  1972. } else {
  1973. $items = $items.filter(':' + that._searchStyle() + '(' + keyCodeMap[e.keyCode] + ')');
  1974. }
  1975. }
  1976. }
  1977. }
  1978.  
  1979. if (!$items.length) return;
  1980.  
  1981. if (/(38|40)/.test(e.keyCode.toString(10))) {
  1982. index = $items.index($items.find('a').filter(':focus').parent());
  1983. first = $items.filter(selector).first().index();
  1984. last = $items.filter(selector).last().index();
  1985. next = $items.eq(index).nextAll(selector).eq(0).index();
  1986. prev = $items.eq(index).prevAll(selector).eq(0).index();
  1987. nextPrev = $items.eq(next).prevAll(selector).eq(0).index();
  1988.  
  1989. if (that.options.liveSearch) {
  1990. $items.each(function (i) {
  1991. if (!$(this).hasClass('disabled')) {
  1992. $(this).data('index', i);
  1993. }
  1994. });
  1995. index = $items.index($items.filter('.active'));
  1996. first = $items.first().data('index');
  1997. last = $items.last().data('index');
  1998. next = $items.eq(index).nextAll().eq(0).data('index');
  1999. prev = $items.eq(index).prevAll().eq(0).data('index');
  2000. nextPrev = $items.eq(next).prevAll().eq(0).data('index');
  2001. }
  2002.  
  2003. prevIndex = $this.data('prevIndex');
  2004.  
  2005. if (e.keyCode == 38) {
  2006. if (that.options.liveSearch) index--;
  2007. if (index != nextPrev && index > prev) index = prev;
  2008. if (index < first) index = first;
  2009. if (index == prevIndex) index = last;
  2010. } else if (e.keyCode == 40) {
  2011. if (that.options.liveSearch) index++;
  2012. if (index == -1) index = 0;
  2013. if (index != nextPrev && index < next) index = next;
  2014. if (index > last) index = last;
  2015. if (index == prevIndex) index = first;
  2016. }
  2017.  
  2018. $this.data('prevIndex', index);
  2019.  
  2020. if (!that.options.liveSearch) {
  2021. $items.eq(index).children('a').focus();
  2022. } else {
  2023. e.preventDefault();
  2024. if (!$this.hasClass('dropdown-toggle')) {
  2025. $items.removeClass('active').eq(index).addClass('active').children('a').focus();
  2026. $this.focus();
  2027. }
  2028. }
  2029.  
  2030. } else if (!$this.is('input')) {
  2031. var keyIndex = [],
  2032. count,
  2033. prevKey;
  2034.  
  2035. $items.each(function () {
  2036. if (!$(this).hasClass('disabled')) {
  2037. if ($.trim($(this).children('a').text().toLowerCase()).substring(0, 1) == keyCodeMap[e.keyCode]) {
  2038. keyIndex.push($(this).index());
  2039. }
  2040. }
  2041. });
  2042.  
  2043. count = $(document).data('keycount');
  2044. count++;
  2045. $(document).data('keycount', count);
  2046.  
  2047. prevKey = $.trim($(':focus').text().toLowerCase()).substring(0, 1);
  2048.  
  2049. if (prevKey != keyCodeMap[e.keyCode]) {
  2050. count = 1;
  2051. $(document).data('keycount', count);
  2052. } else if (count >= keyIndex.length) {
  2053. $(document).data('keycount', 0);
  2054. if (count > keyIndex.length) count = 1;
  2055. }
  2056.  
  2057. $items.eq(keyIndex[count - 1]).children('a').focus();
  2058. }
  2059.  
  2060. // Select focused option if "Enter", "Spacebar" or "Tab" (when selectOnTab is true) are pressed inside the menu.
  2061. if ((/(13|32)/.test(e.keyCode.toString(10)) || (/(^9$)/.test(e.keyCode.toString(10)) && that.options.selectOnTab)) && isActive) {
  2062. if (!/(32)/.test(e.keyCode.toString(10))) e.preventDefault();
  2063. if (!that.options.liveSearch) {
  2064. var elem = $(':focus');
  2065. elem.click();
  2066. // Bring back focus for multiselects
  2067. elem.focus();
  2068. // Prevent screen from scrolling if the user hit the spacebar
  2069. e.preventDefault();
  2070. // Fixes spacebar selection of dropdown items in FF & IE
  2071. $(document).data('spaceSelect', true);
  2072. } else if (!/(32)/.test(e.keyCode.toString(10))) {
  2073. that.$menuInner.find('.active a').click();
  2074. $this.focus();
  2075. }
  2076. $(document).data('keycount', 0);
  2077. }
  2078.  
  2079. if ((/(^9$|27)/.test(e.keyCode.toString(10)) && isActive && (that.multiple || that.options.liveSearch)) || (/(27)/.test(e.keyCode.toString(10)) && !isActive)) {
  2080. that.$menu.parent().removeClass('open');
  2081. if (that.options.container) that.$newElement.removeClass('open');
  2082. that.$button.focus();
  2083. }
  2084. },
  2085.  
  2086. mobile: function () {
  2087. this.$element.addClass('mobile-device');
  2088. },
  2089.  
  2090. refresh: function () {
  2091. this.$lis = null;
  2092. this.liObj = {};
  2093. this.reloadLi();
  2094. this.render();
  2095. this.checkDisabled();
  2096. this.liHeight(true);
  2097. this.setStyle();
  2098. this.setWidth();
  2099. if (this.$lis) this.$searchbox.trigger('propertychange');
  2100.  
  2101. this.$element.trigger('refreshed.bs.select');
  2102. },
  2103.  
  2104. hide: function () {
  2105. this.$newElement.hide();
  2106. },
  2107.  
  2108. show: function () {
  2109. this.$newElement.show();
  2110. },
  2111.  
  2112. remove: function () {
  2113. this.$newElement.remove();
  2114. this.$element.remove();
  2115. },
  2116.  
  2117. destroy: function () {
  2118. this.$newElement.before(this.$element).remove();
  2119.  
  2120. if (this.$bsContainer) {
  2121. this.$bsContainer.remove();
  2122. } else {
  2123. this.$menu.remove();
  2124. }
  2125.  
  2126. this.$element
  2127. .off('.bs.select')
  2128. .removeData('selectpicker')
  2129. .removeClass('bs-select-hidden selectpicker');
  2130. }
  2131. };
  2132.  
  2133. // SELECTPICKER PLUGIN DEFINITION
  2134. // ==============================
  2135. function Plugin(option) {
  2136. // get the args of the outer function..
  2137. var args = arguments;
  2138. // The arguments of the function are explicitly re-defined from the argument list, because the shift causes them
  2139. // to get lost/corrupted in android 2.3 and IE9 #715 #775
  2140. var _option = option;
  2141.  
  2142. [].shift.apply(args);
  2143.  
  2144. var value;
  2145. var chain = this.each(function () {
  2146. var $this = $(this);
  2147. if ($this.is('select')) {
  2148. var data = $this.data('selectpicker'),
  2149. options = typeof _option == 'object' && _option;
  2150.  
  2151. if (!data) {
  2152. var config = $.extend({}, Selectpicker.DEFAULTS, $.fn.selectpicker.defaults || {}, $this.data(), options);
  2153. config.template = $.extend({}, Selectpicker.DEFAULTS.template, ($.fn.selectpicker.defaults ? $.fn.selectpicker.defaults.template : {}), $this.data().template, options.template);
  2154. $this.data('selectpicker', (data = new Selectpicker(this, config)));
  2155. } else if (options) {
  2156. for (var i in options) {
  2157. if (options.hasOwnProperty(i)) {
  2158. data.options[i] = options[i];
  2159. }
  2160. }
  2161. }
  2162.  
  2163. if (typeof _option == 'string') {
  2164. if (data[_option] instanceof Function) {
  2165. value = data[_option].apply(data, args);
  2166. } else {
  2167. value = data.options[_option];
  2168. }
  2169. }
  2170. }
  2171. });
  2172.  
  2173. if (typeof value !== 'undefined') {
  2174. //noinspection JSUnusedAssignment
  2175. return value;
  2176. } else {
  2177. return chain;
  2178. }
  2179. }
  2180.  
  2181. var old = $.fn.selectpicker;
  2182. $.fn.selectpicker = Plugin;
  2183. $.fn.selectpicker.Constructor = Selectpicker;
  2184.  
  2185. // SELECTPICKER NO CONFLICT
  2186. // ========================
  2187. $.fn.selectpicker.noConflict = function () {
  2188. $.fn.selectpicker = old;
  2189. return this;
  2190. };
  2191.  
  2192. $(document)
  2193. .data('keycount', 0)
  2194. .on('keydown.bs.select', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role="listbox"], .bs-searchbox input', Selectpicker.prototype.keydown)
  2195. .on('focusin.modal', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role="listbox"], .bs-searchbox input', function (e) {
  2196. e.stopPropagation();
  2197. });
  2198.  
  2199. // SELECTPICKER DATA-API
  2200. // =====================
  2201. $(window).on('load.bs.select.data-api', function () {
  2202. $('.selectpicker').each(function () {
  2203. var $selectpicker = $(this);
  2204. Plugin.call($selectpicker, $selectpicker.data());
  2205. })
  2206. });
  2207. })(jQuery);
  2208.  
  2209.  
  2210. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement