+function ($) { 'use strict'; // CHOICEPICKER PUBLIC CLASS DEFINITION // ==================================== var Choicepicker = function (element, options) { this.$element = $(element); this.settings = { choices: this.$element.data('choices'), fuzzySearch: this.$element.data('fuzzySearch'), text: { all: this.$element.data('text-all') || Choicepicker.DEFAULTS.text.all, choiceless: this.$element.data('text-choiceless') || Choicepicker.DEFAULTS.text.choiceless, none: this.$element.data('text-none') || Choicepicker.DEFAULTS.text.none, placeholder: this.$element.data('text-placeholder') || Choicepicker.DEFAULTS.text.placeholder, selectAll: this.$element.data('text-select-all') || Choicepicker.DEFAULTS.text.selectAll }, type: this.$element.data('type') }; this.options = $.extend({}, Choicepicker.DEFAULTS, this.settings, options); this.$checkAll = false; this.$selector = 'bsChoicepicker-label-' + this.randomNumber() + '-'; this.$fuzzyId = 'bsChoicepicker-fuzzy-' + this.randomNumber(); this.$widget = $(this.initWidget()).on('click', $.proxy(this.clickWidget, this)); this.init(); }; if (!$.fn.dropdown) throw new Error('Choicepicker requires dropdown.js'); if (!$.fn.list) throw new Error('Choicepicker requires list.js'); Choicepicker.VERSION = '1.0.0'; Choicepicker.DEFAULTS = { callback: function (choice) {}, choices: [], choiceClass: 'form-align-vertical', fuzzySearch: true, item: '
', menu: ' ', text: { all: 'All', choiceless: 'No choices available', none: 'None', placeholder: 'Filter options...', selectAll: 'Select all' }, type: 'checkbox' }; Choicepicker.prototype.constructor = Choicepicker; Choicepicker.prototype.init = function () { if (!this.hasChoices()) return; this.elementReadOnly(); this.setWidget(); this.setVal(); this.clickCheckAll(); this.$element.on({ 'focus.bs.choicepicker': $.proxy(this.showWidget, this), 'click.bs.choicepicker': $.proxy(this.showWidget, this), 'blur.bs.choicepicker': $.proxy(this.setVal, this) }); }; Choicepicker.prototype.clickWidget = function (e) { e.stopPropagation(); if (!this.hasChoices()) return; this.setVal(); }; Choicepicker.prototype.initWidget = function () { var _self = this; var menu = $(this.options.menu); if (this.options.fuzzySearch && this.hasFuzzyAmount()) { menu.attr('data-toggle', 'list') .attr('data-input', '#' + this.$fuzzyId) .find('span') .append(this.fuzzyTemplate()); } if (this.options.type === 'checkbox') { menu.find('span') .append(this.allTemplate()); } $.each(this.options.choices, function (index, hash) { var item = $(_self.options.item); item.append(_self.optionTemplate(hash)) .append(_self.labelTemplate(hash).text(hash.label)); menu.find('span') .append(item); }); return menu; }; Choicepicker.prototype.setWidget = function () { this.$container = $('') .addClass('btn-group dropdown bsChoicepicker') .css({ height: 0, position: 'absolute', width: 0 }); try { this.$container.append(this.$widget); this.$element.after(this.$container); } catch(e) { // Do nothing } }; Choicepicker.prototype.showWidget = function () { this.$container.addClass('open'); var _self = this; $(document).on('mousedown.bs.choicepicker, touchend.bs.choicepicker', function (e) { if (!(_self.$element.parent().find(e.target).length || _self.$widget.is(e.target) || _self.$widget.find(e.target).length)) { _self.hideWidget(); } }); }; Choicepicker.prototype.hideWidget = function () { this.$container.removeClass('open'); }; Choicepicker.prototype.type = function () { var type = this.options.type; if (type !== 'checkbox' && type !== 'radio') { type = 'checkbox'; } return type; }; Choicepicker.prototype.elementReadOnly = function () { this.$element.prop('readonly', true); }; Choicepicker.prototype.selector = function (hash) { var selector = this.$selector + hash.selector; return selector; }; Choicepicker.prototype.checkAllSelector = function () { var selector = this.$selector + 'checkall'; return selector; }; Choicepicker.prototype.clickCheckAll = function () { var _self = this; var checkAll = $('#' + this.checkAllSelector()); checkAll.on('change', function () { $.each(_self.options.choices, function (index, hash) { var selector = $('#' + _self.selector(hash)); if (selector) selector.prop('checked', checkAll.is(':checked')); }); }); }; Choicepicker.prototype.setCheckAll = function (checked) { var checkbox = $('#' + this.checkAllSelector()); this.$checkAll = checked; checkbox.prop('checked', checked); }; Choicepicker.prototype.labelTemplate = function (hash) { var selector = this.selector(hash); return $('