PageRenderTime 183ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/public/components/common/forms/elements/bootstrap-select/assets/lib/js/bootstrap-select.js

https://github.com/klvh-mb/MB-Admin
JavaScript | 860 lines | 698 code | 126 blank | 36 comment | 197 complexity | 2f22ab8b5965f238b567868723dd58b8 MD5 | raw file
Possible License(s): MIT, Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. /*!
  2. * bootstrap-select v1.4.2
  3. * http://silviomoreto.github.io/bootstrap-select/
  4. *
  5. * Copyright 2013 bootstrap-select
  6. * Licensed under the MIT license
  7. */
  8. !function($) {
  9. 'use strict';
  10. $.expr[':'].icontains = function(obj, index, meta) {
  11. return $(obj).text().toUpperCase().indexOf(meta[3].toUpperCase()) >= 0;
  12. };
  13. var Selectpicker = function(element, options, e) {
  14. if (e) {
  15. e.stopPropagation();
  16. e.preventDefault();
  17. }
  18. this.$element = $(element);
  19. this.$newElement = null;
  20. this.$button = null;
  21. this.$menu = null;
  22. //Merge defaults, options and data-attributes to make our options
  23. this.options = $.extend({}, $.fn.selectpicker.defaults, this.$element.data(), typeof options == 'object' && options);
  24. //If we have no title yet, check the attribute 'title' (this is missed by jq as its not a data-attribute
  25. if (this.options.title === null) {
  26. this.options.title = this.$element.attr('title');
  27. }
  28. //Expose public methods
  29. this.val = Selectpicker.prototype.val;
  30. this.render = Selectpicker.prototype.render;
  31. this.refresh = Selectpicker.prototype.refresh;
  32. this.setStyle = Selectpicker.prototype.setStyle;
  33. this.selectAll = Selectpicker.prototype.selectAll;
  34. this.deselectAll = Selectpicker.prototype.deselectAll;
  35. this.init();
  36. };
  37. Selectpicker.prototype = {
  38. constructor: Selectpicker,
  39. init: function() {
  40. this.$element.hide();
  41. this.multiple = this.$element.prop('multiple');
  42. var id = this.$element.attr('id');
  43. this.$newElement = this.createView();
  44. this.$element.after(this.$newElement);
  45. this.$menu = this.$newElement.find('> .dropdown-menu');
  46. this.$button = this.$newElement.find('> button');
  47. this.$searchbox = this.$newElement.find('input');
  48. if (id !== undefined) {
  49. var that = this;
  50. this.$button.attr('data-id', id);
  51. $('label[for="' + id + '"]').click(function(e) {
  52. e.preventDefault();
  53. that.$button.focus();
  54. });
  55. }
  56. this.checkDisabled();
  57. this.clickListener();
  58. if (this.options.liveSearch) {
  59. this.liveSearchListener();
  60. }
  61. this.render();
  62. this.liHeight();
  63. this.setStyle();
  64. this.setWidth();
  65. if (this.options.container) {
  66. this.selectPosition();
  67. }
  68. this.$menu.data('this', this);
  69. this.$newElement.data('this', this);
  70. },
  71. createDropdown: function() {
  72. //If we are multiple, then add the show-tick class by default
  73. var multiple = this.multiple ? ' show-tick' : '';
  74. var header = this.options.header ? '<div class="popover-title"><button type="button" class="close" aria-hidden="true">&times;</button>' + this.options.header + '</div>' : '';
  75. var searchbox = this.options.liveSearch ? '<div class="bootstrap-select-searchbox"><input type="text" class="input-block-level form-control" /></div>' : '';
  76. var drop =
  77. '<div class="btn-group bootstrap-select' + multiple + '">' +
  78. '<button type="button" class="btn dropdown-toggle selectpicker" data-toggle="dropdown">' +
  79. '<span class="filter-option pull-left"></span>&nbsp;' +
  80. '<span class="caret"></span>' +
  81. '</button>' +
  82. '<div class="dropdown-menu open">' +
  83. header +
  84. searchbox +
  85. '<ul class="dropdown-menu inner selectpicker" role="menu">' +
  86. '</ul>' +
  87. '</div>' +
  88. '</div>';
  89. return $(drop);
  90. },
  91. createView: function() {
  92. var $drop = this.createDropdown();
  93. var $li = this.createLi();
  94. $drop.find('ul').append($li);
  95. return $drop;
  96. },
  97. reloadLi: function() {
  98. //Remove all children.
  99. this.destroyLi();
  100. //Re build
  101. var $li = this.createLi();
  102. this.$menu.find('ul').append( $li );
  103. },
  104. destroyLi: function() {
  105. this.$menu.find('li').remove();
  106. },
  107. createLi: function() {
  108. var that = this,
  109. _liA = [],
  110. _liHtml = '';
  111. this.$element.find('option').each(function() {
  112. var $this = $(this);
  113. //Get the class and text for the option
  114. var optionClass = $this.attr('class') || '';
  115. var inline = $this.attr('style') || '';
  116. var text = $this.data('content') ? $this.data('content') : $this.html();
  117. var subtext = $this.data('subtext') !== undefined ? '<small class="muted text-muted">' + $this.data('subtext') + '</small>' : '';
  118. var icon = $this.data('icon') !== undefined ? '<i class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></i> ' : '';
  119. if (icon !== '' && ($this.is(':disabled') || $this.parent().is(':disabled'))) {
  120. icon = '<span>'+icon+'</span>';
  121. }
  122. if (!$this.data('content')) {
  123. //Prepend any icon and append any subtext to the main text.
  124. text = icon + '<span class="text">' + text + subtext + '</span>';
  125. }
  126. if (that.options.hideDisabled && ($this.is(':disabled') || $this.parent().is(':disabled'))) {
  127. _liA.push('<a style="min-height: 0; padding: 0"></a>');
  128. } else if ($this.parent().is('optgroup') && $this.data('divider') !== true) {
  129. if ($this.index() === 0) {
  130. //Get the opt group label
  131. var label = $this.parent().attr('label');
  132. var labelSubtext = $this.parent().data('subtext') !== undefined ? '<small class="muted text-muted">'+$this.parent().data('subtext')+'</small>' : '';
  133. var labelIcon = $this.parent().data('icon') ? '<i class="'+$this.parent().data('icon')+'"></i> ' : '';
  134. label = labelIcon + '<span class="text">' + label + labelSubtext + '</span>';
  135. if ($this[0].index !== 0) {
  136. _liA.push(
  137. '<div class="div-contain"><div class="divider"></div></div>'+
  138. '<dt>'+label+'</dt>'+
  139. that.createA(text, 'opt ' + optionClass, inline )
  140. );
  141. } else {
  142. _liA.push(
  143. '<dt>'+label+'</dt>'+
  144. that.createA(text, 'opt ' + optionClass, inline ));
  145. }
  146. } else {
  147. _liA.push(that.createA(text, 'opt ' + optionClass, inline ));
  148. }
  149. } else if ($this.data('divider') === true) {
  150. _liA.push('<div class="div-contain"><div class="divider"></div></div>');
  151. } else if ($(this).data('hidden') === true) {
  152. _liA.push('');
  153. } else {
  154. _liA.push(that.createA(text, optionClass, inline ));
  155. }
  156. });
  157. $.each(_liA, function(i, item) {
  158. _liHtml += '<li rel=' + i + '>' + item + '</li>';
  159. });
  160. //If we are not multiple, and we dont have a selected item, and we dont have a title, select the first element so something is set in the button
  161. if (!this.multiple && this.$element.find('option:selected').length===0 && !this.options.title) {
  162. this.$element.find('option').eq(0).prop('selected', true).attr('selected', 'selected');
  163. }
  164. return $(_liHtml);
  165. },
  166. createA: function(text, classes, inline) {
  167. return '<a tabindex="0" class="'+classes+'" style="'+inline+'">' +
  168. text +
  169. '<i class="' + this.options.iconBase + ' ' + this.options.tickIcon + ' icon-ok check-mark"></i>' +
  170. '</a>';
  171. },
  172. render: function() {
  173. var that = this;
  174. //Update the LI to match the SELECT
  175. this.$element.find('option').each(function(index) {
  176. that.setDisabled(index, $(this).is(':disabled') || $(this).parent().is(':disabled') );
  177. that.setSelected(index, $(this).is(':selected') );
  178. });
  179. this.tabIndex();
  180. var selectedItems = this.$element.find('option:selected').map(function() {
  181. var $this = $(this);
  182. var icon = $this.data('icon') && that.options.showIcon ? '<i class="' + that.options.iconBase + ' ' + $this.data('icon') + '"></i> ' : '';
  183. var subtext;
  184. if (that.options.showSubtext && $this.attr('data-subtext') && !that.multiple) {
  185. subtext = ' <small class="muted text-muted">'+$this.data('subtext') +'</small>';
  186. } else {
  187. subtext = '';
  188. }
  189. if ($this.data('content') && that.options.showContent) {
  190. return $this.data('content');
  191. } else if ($this.attr('title') !== undefined) {
  192. return $this.attr('title');
  193. } else {
  194. return icon + $this.html() + subtext;
  195. }
  196. }).toArray();
  197. //Fixes issue in IE10 occurring when no default option is selected and at least one option is disabled
  198. //Convert all the values into a comma delimited string
  199. var title = !this.multiple ? selectedItems[0] : selectedItems.join(this.options.multipleSeparator);
  200. //If this is multi select, and the selectText type is count, the show 1 of 2 selected etc..
  201. if (this.multiple && this.options.selectedTextFormat.indexOf('count') > -1) {
  202. var max = this.options.selectedTextFormat.split('>');
  203. var notDisabled = this.options.hideDisabled ? ':not([disabled])' : '';
  204. if ( (max.length>1 && selectedItems.length > max[1]) || (max.length==1 && selectedItems.length>=2)) {
  205. title = this.options.countSelectedText.replace('{0}', selectedItems.length).replace('{1}', this.$element.find('option:not([data-divider="true"]):not([data-hidden="true"])'+notDisabled).length);
  206. }
  207. }
  208. //If we dont have a title, then use the default, or if nothing is set at all, use the not selected text
  209. if (!title) {
  210. title = this.options.title !== undefined ? this.options.title : this.options.noneSelectedText;
  211. }
  212. this.$button.attr('title', $.trim(title));
  213. this.$newElement.find('.filter-option').html(title);
  214. },
  215. setStyle: function(style, status) {
  216. if (this.$element.attr('class')) {
  217. this.$newElement.addClass(this.$element.attr('class').replace(/selectpicker|mobile-device/gi, ''));
  218. }
  219. var buttonClass = style ? style : this.options.style;
  220. if (status == 'add') {
  221. this.$button.addClass(buttonClass);
  222. } else if (status == 'remove') {
  223. this.$button.removeClass(buttonClass);
  224. } else {
  225. this.$button.removeClass(this.options.style);
  226. this.$button.addClass(buttonClass);
  227. }
  228. },
  229. liHeight: function() {
  230. var $selectClone = this.$menu.parent().clone().appendTo('body'),
  231. $menuClone = $selectClone.addClass('open').find('> .dropdown-menu'),
  232. liHeight = $menuClone.find('li > a').outerHeight(),
  233. headerHeight = this.options.header ? $menuClone.find('.popover-title').outerHeight() : 0,
  234. searchHeight = this.options.liveSearch ? $menuClone.find('.bootstrap-select-searchbox').outerHeight() : 0;
  235. $selectClone.remove();
  236. this.$newElement
  237. .data('liHeight', liHeight)
  238. .data('headerHeight', headerHeight)
  239. .data('searchHeight', searchHeight);
  240. },
  241. setSize: function() {
  242. var that = this,
  243. menu = this.$menu,
  244. menuInner = menu.find('.inner'),
  245. selectHeight = this.$newElement.outerHeight(),
  246. liHeight = this.$newElement.data('liHeight'),
  247. headerHeight = this.$newElement.data('headerHeight'),
  248. searchHeight = this.$newElement.data('searchHeight'),
  249. divHeight = menu.find('li .divider').outerHeight(true),
  250. menuPadding = parseInt(menu.css('padding-top')) +
  251. parseInt(menu.css('padding-bottom')) +
  252. parseInt(menu.css('border-top-width')) +
  253. parseInt(menu.css('border-bottom-width')),
  254. notDisabled = this.options.hideDisabled ? ':not(.disabled)' : '',
  255. $window = $(window),
  256. menuExtras = menuPadding + parseInt(menu.css('margin-top')) + parseInt(menu.css('margin-bottom')) + 2,
  257. menuHeight,
  258. selectOffsetTop,
  259. selectOffsetBot,
  260. posVert = function() {
  261. selectOffsetTop = that.$newElement.offset().top - $window.scrollTop();
  262. selectOffsetBot = $window.height() - selectOffsetTop - selectHeight;
  263. };
  264. posVert();
  265. if (this.options.header) menu.css('padding-top', 0);
  266. if (this.options.size == 'auto') {
  267. var getSize = function() {
  268. var minHeight;
  269. posVert();
  270. menuHeight = selectOffsetBot - menuExtras;
  271. if (that.options.dropupAuto) {
  272. that.$newElement.toggleClass('dropup', (selectOffsetTop > selectOffsetBot) && ((menuHeight - menuExtras) < menu.height()));
  273. }
  274. if (that.$newElement.hasClass('dropup')) {
  275. menuHeight = selectOffsetTop - menuExtras;
  276. }
  277. if ((menu.find('li').length + menu.find('dt').length) > 3) {
  278. minHeight = liHeight*3 + menuExtras - 2;
  279. } else {
  280. minHeight = 0;
  281. }
  282. menu.css({'max-height' : menuHeight + 'px', 'overflow' : 'hidden', 'min-height' : minHeight + 'px'});
  283. menuInner.css({'max-height' : menuHeight - headerHeight - searchHeight- menuPadding + 'px', 'overflow-y' : 'auto', 'min-height' : minHeight - menuPadding + 'px'});
  284. };
  285. getSize();
  286. $(window).resize(getSize);
  287. $(window).scroll(getSize);
  288. } else if (this.options.size && this.options.size != 'auto' && menu.find('li'+notDisabled).length > this.options.size) {
  289. var optIndex = menu.find('li'+notDisabled+' > *').filter(':not(.div-contain)').slice(0,this.options.size).last().parent().index();
  290. var divLength = menu.find('li').slice(0,optIndex + 1).find('.div-contain').length;
  291. menuHeight = liHeight*this.options.size + divLength*divHeight + menuPadding;
  292. if (that.options.dropupAuto) {
  293. this.$newElement.toggleClass('dropup', (selectOffsetTop > selectOffsetBot) && (menuHeight < menu.height()));
  294. }
  295. menu.css({'max-height' : menuHeight + headerHeight + searchHeight + 'px', 'overflow' : 'hidden'});
  296. menuInner.css({'max-height' : menuHeight - menuPadding + 'px', 'overflow-y' : 'auto'});
  297. }
  298. },
  299. setWidth: function() {
  300. if (this.options.width == 'auto') {
  301. this.$menu.css('min-width', '0');
  302. // Get correct width if element hidden
  303. var selectClone = this.$newElement.clone().appendTo('body');
  304. var ulWidth = selectClone.find('> .dropdown-menu').css('width');
  305. selectClone.remove();
  306. this.$newElement.css('width', ulWidth);
  307. } else if (this.options.width == 'fit') {
  308. // Remove inline min-width so width can be changed from 'auto'
  309. this.$menu.css('min-width', '');
  310. this.$newElement.css('width', '').addClass('fit-width');
  311. } else if (this.options.width) {
  312. // Remove inline min-width so width can be changed from 'auto'
  313. this.$menu.css('min-width', '');
  314. this.$newElement.css('width', this.options.width);
  315. } else {
  316. // Remove inline min-width/width so width can be changed
  317. this.$menu.css('min-width', '');
  318. this.$newElement.css('width', '');
  319. }
  320. // Remove fit-width class if width is changed programmatically
  321. if (this.$newElement.hasClass('fit-width') && this.options.width !== 'fit') {
  322. this.$newElement.removeClass('fit-width');
  323. }
  324. },
  325. selectPosition: function() {
  326. var that = this,
  327. drop = '<div />',
  328. $drop = $(drop),
  329. pos,
  330. actualHeight,
  331. getPlacement = function($element) {
  332. $drop.addClass($element.attr('class')).toggleClass('dropup', $element.hasClass('dropup'));
  333. pos = $element.offset();
  334. actualHeight = $element.hasClass('dropup') ? 0 : $element[0].offsetHeight;
  335. $drop.css({'top' : pos.top + actualHeight, 'left' : pos.left, 'width' : $element[0].offsetWidth, 'position' : 'absolute'});
  336. };
  337. this.$newElement.on('click', function() {
  338. getPlacement($(this));
  339. $drop.appendTo(that.options.container);
  340. $drop.toggleClass('open', !$(this).hasClass('open'));
  341. $drop.append(that.$menu);
  342. });
  343. $(window).resize(function() {
  344. getPlacement(that.$newElement);
  345. });
  346. $(window).on('scroll', function() {
  347. getPlacement(that.$newElement);
  348. });
  349. $('html').on('click', function(e) {
  350. if ($(e.target).closest(that.$newElement).length < 1) {
  351. $drop.removeClass('open');
  352. }
  353. });
  354. },
  355. mobile: function() {
  356. this.$element.addClass('mobile-device').appendTo(this.$newElement);
  357. if (this.options.container) this.$menu.hide();
  358. },
  359. refresh: function() {
  360. this.reloadLi();
  361. this.render();
  362. this.setWidth();
  363. this.setStyle();
  364. this.checkDisabled();
  365. this.liHeight();
  366. },
  367. update: function() {
  368. this.reloadLi();
  369. this.setWidth();
  370. this.setStyle();
  371. this.checkDisabled();
  372. this.liHeight();
  373. },
  374. setSelected: function(index, selected) {
  375. this.$menu.find('li').eq(index).toggleClass('selected', selected);
  376. },
  377. setDisabled: function(index, disabled) {
  378. if (disabled) {
  379. this.$menu.find('li').eq(index).addClass('disabled').find('a').attr('href', '#').attr('tabindex', -1);
  380. } else {
  381. this.$menu.find('li').eq(index).removeClass('disabled').find('a').removeAttr('href').attr('tabindex', 0);
  382. }
  383. },
  384. isDisabled: function() {
  385. return this.$element.is(':disabled');
  386. },
  387. checkDisabled: function() {
  388. var that = this;
  389. if (this.isDisabled()) {
  390. this.$button.addClass('disabled').attr('tabindex', -1);
  391. } else {
  392. if (this.$button.hasClass('disabled')) {
  393. this.$button.removeClass('disabled');
  394. }
  395. if (this.$button.attr('tabindex') == -1) {
  396. if (!this.$element.data('tabindex')) this.$button.removeAttr('tabindex');
  397. }
  398. }
  399. this.$button.click(function() {
  400. return !that.isDisabled();
  401. });
  402. },
  403. tabIndex: function() {
  404. if (this.$element.is('[tabindex]')) {
  405. this.$element.data('tabindex', this.$element.attr('tabindex'));
  406. this.$button.attr('tabindex', this.$element.data('tabindex'));
  407. }
  408. },
  409. clickListener: function() {
  410. var that = this;
  411. $('body').on('touchstart.dropdown', '.dropdown-menu', function(e) {
  412. e.stopPropagation();
  413. });
  414. this.$newElement.on('click', function() {
  415. that.setSize();
  416. if (!that.options.liveSearch && !that.multiple) {
  417. setTimeout(function() {
  418. that.$menu.find('.selected a').focus();
  419. }, 10);
  420. }
  421. });
  422. this.$menu.on('click', 'li a', function(e) {
  423. var clickedIndex = $(this).parent().index(),
  424. prevValue = that.$element.val(),
  425. prevIndex = that.$element.prop('selectedIndex');
  426. //Dont close on multi choice menu
  427. if (that.multiple) {
  428. e.stopPropagation();
  429. }
  430. e.preventDefault();
  431. //Dont run if we have been disabled
  432. if (!that.isDisabled() && !$(this).parent().hasClass('disabled')) {
  433. var $options = that.$element.find('option');
  434. var $option = $options.eq(clickedIndex);
  435. //Deselect all others if not multi select box
  436. if (!that.multiple) {
  437. $options.prop('selected', false);
  438. $option.prop('selected', true);
  439. }
  440. //Else toggle the one we have chosen if we are multi select.
  441. else {
  442. var state = $option.prop('selected');
  443. $option.prop('selected', !state);
  444. }
  445. if (!that.multiple) {
  446. that.$button.focus();
  447. } else if (that.options.liveSearch) {
  448. that.$searchbox.focus();
  449. }
  450. // Trigger select 'change'
  451. if ((prevValue != that.$element.val() && that.multiple) || (prevIndex != that.$element.prop('selectedIndex') && !that.multiple)) {
  452. that.$element.change();
  453. }
  454. }
  455. });
  456. this.$menu.on('click', 'li.disabled a, li dt, li .div-contain, .popover-title, .popover-title :not(.close)', function(e) {
  457. if (e.target == this) {
  458. e.preventDefault();
  459. e.stopPropagation();
  460. if (!that.options.liveSearch) {
  461. that.$button.focus();
  462. } else {
  463. that.$searchbox.focus();
  464. }
  465. }
  466. });
  467. this.$menu.on('click', '.popover-title .close', function() {
  468. that.$button.focus();
  469. });
  470. this.$searchbox.on('click', function(e) {
  471. e.stopPropagation();
  472. });
  473. this.$element.change(function() {
  474. that.render();
  475. });
  476. },
  477. liveSearchListener: function() {
  478. var that = this,
  479. no_results = $('<li class="no-results"></li>');
  480. this.$newElement.on('click.dropdown.data-api', function() {
  481. that.$menu.find('.active').removeClass('active');
  482. if (!!that.$searchbox.val()) {
  483. that.$searchbox.val('');
  484. that.$menu.find('li').show();
  485. if (!!no_results.parent().length) no_results.remove();
  486. }
  487. if (!that.multiple) that.$menu.find('.selected').addClass('active');
  488. setTimeout(function() {
  489. that.$searchbox.focus();
  490. }, 10);
  491. });
  492. this.$searchbox.on('input propertychange', function() {
  493. if (that.$searchbox.val()) {
  494. that.$menu.find('li').show().not(':icontains(' + that.$searchbox.val() + ')').hide();
  495. if (!that.$menu.find('li').filter(':visible:not(.no-results)').length) {
  496. if (!!no_results.parent().length) no_results.remove();
  497. no_results.html('No results match "'+ that.$searchbox.val() + '"').show();
  498. that.$menu.find('li').last().after(no_results);
  499. } else if (!!no_results.parent().length) {
  500. no_results.remove();
  501. }
  502. } else {
  503. that.$menu.find('li').show();
  504. if (!!no_results.parent().length) no_results.remove();
  505. }
  506. that.$menu.find('li.active').removeClass('active');
  507. that.$menu.find('li').filter(':visible:not(.divider)').eq(0).addClass('active').find('a').focus();
  508. $(this).focus();
  509. });
  510. this.$menu.on('mouseenter', 'a', function(e) {
  511. that.$menu.find('.active').removeClass('active');
  512. $(e.currentTarget).parent().not('.disabled').addClass('active');
  513. });
  514. this.$menu.on('mouseleave', 'a', function() {
  515. that.$menu.find('.active').removeClass('active');
  516. });
  517. },
  518. val: function(value) {
  519. if (value !== undefined) {
  520. this.$element.val( value );
  521. this.$element.change();
  522. return this.$element;
  523. } else {
  524. return this.$element.val();
  525. }
  526. },
  527. selectAll: function() {
  528. this.$element.find('option').prop('selected', true).attr('selected', 'selected');
  529. this.render();
  530. },
  531. deselectAll: function() {
  532. this.$element.find('option').prop('selected', false).removeAttr('selected');
  533. this.render();
  534. },
  535. keydown: function(e) {
  536. var $this,
  537. $items,
  538. $parent,
  539. index,
  540. next,
  541. first,
  542. last,
  543. prev,
  544. nextPrev,
  545. that,
  546. prevIndex,
  547. isActive,
  548. keyCodeMap = {
  549. 32:' ', 48:'0', 49:'1', 50:'2', 51:'3', 52:'4', 53:'5', 54:'6', 55:'7', 56:'8', 57:'9', 59:';',
  550. 65:'a', 66:'b', 67:'c', 68:'d', 69:'e', 70:'f', 71:'g', 72:'h', 73:'i', 74:'j', 75:'k', 76:'l',
  551. 77:'m', 78:'n', 79:'o', 80:'p', 81:'q', 82:'r', 83:'s', 84:'t', 85:'u', 86:'v', 87:'w', 88:'x',
  552. 89:'y', 90:'z', 96:'0', 97:'1', 98:'2', 99:'3', 100:'4', 101:'5', 102:'6', 103:'7', 104:'8', 105:'9'
  553. };
  554. $this = $(this);
  555. $parent = $this.parent();
  556. if ($this.is('input')) $parent = $this.parent().parent();
  557. that = $parent.data('this');
  558. if (that.options.liveSearch) $parent = $this.parent().parent();
  559. if (that.options.container) $parent = that.$menu;
  560. $items = $('[role=menu] li:not(.divider) a', $parent);
  561. isActive = that.$menu.parent().hasClass('open');
  562. if (that.options.liveSearch) {
  563. if (/(^9$|27)/.test(e.keyCode) && isActive && that.$menu.find('.active').length === 0) {
  564. e.preventDefault();
  565. that.$menu.parent().removeClass('open');
  566. that.$button.focus();
  567. }
  568. $items = $('[role=menu] li:not(.divider):visible', $parent);
  569. if (!$this.val() && !/(38|40)/.test(e.keyCode)) {
  570. if ($items.filter('.active').length === 0) {
  571. $items = that.$newElement.find('li').filter(':icontains(' + keyCodeMap[e.keyCode] + ')');
  572. }
  573. }
  574. }
  575. if (!$items.length) return;
  576. if (/(38|40)/.test(e.keyCode)) {
  577. if (!isActive) {
  578. that.$menu.parent().addClass('open');
  579. }
  580. index = $items.index($items.filter(':focus'));
  581. first = $items.parent(':not(.disabled):visible').first().index();
  582. last = $items.parent(':not(.disabled):visible').last().index();
  583. next = $items.eq(index).parent().nextAll(':not(.disabled):visible').eq(0).index();
  584. prev = $items.eq(index).parent().prevAll(':not(.disabled):visible').eq(0).index();
  585. nextPrev = $items.eq(next).parent().prevAll(':not(.disabled):visible').eq(0).index();
  586. if (that.options.liveSearch) {
  587. $items.each(function(i) {
  588. if ($(this).is(':not(.disabled)')) {
  589. $(this).data('index', i);
  590. }
  591. });
  592. index = $items.index($items.filter('.active'));
  593. first = $items.filter(':not(.disabled):visible').first().data('index');
  594. last = $items.filter(':not(.disabled):visible').last().data('index');
  595. next = $items.eq(index).nextAll(':not(.disabled):visible').eq(0).data('index');
  596. prev = $items.eq(index).prevAll(':not(.disabled):visible').eq(0).data('index');
  597. nextPrev = $items.eq(next).prevAll(':not(.disabled):visible').eq(0).data('index');
  598. }
  599. prevIndex = $this.data('prevIndex');
  600. if (e.keyCode == 38) {
  601. if (that.options.liveSearch) index -= 1;
  602. if (index != nextPrev && index > prev) index = prev;
  603. if (index < first) index = first;
  604. if (index == prevIndex) index = last;
  605. }
  606. if (e.keyCode == 40) {
  607. if (that.options.liveSearch) index += 1;
  608. if (index == -1) index = 0;
  609. if (index != nextPrev && index < next) index = next;
  610. if (index > last) index = last;
  611. if (index == prevIndex) index = first;
  612. }
  613. $this.data('prevIndex', index);
  614. if (!that.options.liveSearch) {
  615. $items.eq(index).focus();
  616. } else {
  617. e.preventDefault();
  618. if (!$this.is('.dropdown-toggle')) {
  619. $items.removeClass('active');
  620. $items.eq(index).addClass('active').find('a').focus();
  621. $this.focus();
  622. }
  623. }
  624. } else if (!$this.is('input')) {
  625. var keyIndex = [],
  626. count,
  627. prevKey;
  628. $items.each(function() {
  629. if ($(this).parent().is(':not(.disabled)')) {
  630. if ($.trim($(this).text().toLowerCase()).substring(0,1) == keyCodeMap[e.keyCode]) {
  631. keyIndex.push($(this).parent().index());
  632. }
  633. }
  634. });
  635. count = $(document).data('keycount');
  636. count++;
  637. $(document).data('keycount',count);
  638. prevKey = $.trim($(':focus').text().toLowerCase()).substring(0,1);
  639. if (prevKey != keyCodeMap[e.keyCode]) {
  640. count = 1;
  641. $(document).data('keycount', count);
  642. } else if (count >= keyIndex.length) {
  643. $(document).data('keycount', 0);
  644. if (count > keyIndex.length) count = 1;
  645. }
  646. $items.eq(keyIndex[count - 1]).focus();
  647. }
  648. // Select focused option if "Enter", "Spacebar", "Tab" are pressed inside the menu.
  649. if (/(13|32|^9$)/.test(e.keyCode) && isActive) {
  650. if (!/(32)/.test(e.keyCode)) e.preventDefault();
  651. if (!that.options.liveSearch) {
  652. $(':focus').click();
  653. } else if (!/(32)/.test(e.keyCode)) {
  654. that.$menu.find('.active a').click();
  655. $this.focus();
  656. }
  657. $(document).data('keycount',0);
  658. }
  659. if ((/(^9$|27)/.test(e.keyCode) && isActive && (that.multiple || that.options.liveSearch)) || (/(27)/.test(e.keyCode) && !isActive)) {
  660. that.$menu.parent().removeClass('open');
  661. that.$button.focus();
  662. }
  663. },
  664. hide: function() {
  665. this.$newElement.hide();
  666. },
  667. show: function() {
  668. this.$newElement.show();
  669. },
  670. destroy: function() {
  671. this.$newElement.remove();
  672. this.$element.remove();
  673. }
  674. };
  675. $.fn.selectpicker = function(option, event) {
  676. //get the args of the outer function..
  677. var args = arguments;
  678. var value;
  679. var chain = this.each(function() {
  680. if ($(this).is('select')) {
  681. var $this = $(this),
  682. data = $this.data('selectpicker'),
  683. options = typeof option == 'object' && option;
  684. if (!data) {
  685. $this.data('selectpicker', (data = new Selectpicker(this, options, event)));
  686. } else if (options) {
  687. for(var i in options) {
  688. data.options[i] = options[i];
  689. }
  690. }
  691. if (typeof option == 'string') {
  692. //Copy the value of option, as once we shift the arguments
  693. //it also shifts the value of option.
  694. var property = option;
  695. if (data[property] instanceof Function) {
  696. [].shift.apply(args);
  697. value = data[property].apply(data, args);
  698. } else {
  699. value = data.options[property];
  700. }
  701. }
  702. }
  703. });
  704. if (value !== undefined) {
  705. return value;
  706. } else {
  707. return chain;
  708. }
  709. };
  710. $.fn.selectpicker.defaults = {
  711. style: 'btn-default',
  712. size: 'auto',
  713. title: null,
  714. selectedTextFormat : 'values',
  715. noneSelectedText : 'Nothing selected',
  716. countSelectedText: '{0} of {1} selected',
  717. width: false,
  718. container: false,
  719. hideDisabled: false,
  720. showSubtext: false,
  721. showIcon: true,
  722. showContent: true,
  723. dropupAuto: true,
  724. header: false,
  725. liveSearch: false,
  726. multipleSeparator: ', ',
  727. iconBase: 'glyphicon',
  728. tickIcon: 'glyphicon-ok'
  729. };
  730. $(document)
  731. .data('keycount', 0)
  732. .on('keydown', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role=menu], .bootstrap-select-searchbox input', Selectpicker.prototype.keydown)
  733. .on('focusin.modal', '.bootstrap-select [data-toggle=dropdown], .bootstrap-select [role=menu], .bootstrap-select-searchbox input', function (e) { e.stopPropagation(); });
  734. }(window.jQuery);