12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280 |
- /**
- * Bootstrap Multiselect v0.9.8 (https://github.com/davidstutz/bootstrap-multiselect)
- *
- * Copyright 2012 - 2014 David Stutz
- *
- * Dual licensed under the BSD-3-Clause and the Apache License, Version 2.0.
- */
- !function($) {
-
- "use strict";// jshint ;_;
- if (typeof ko !== 'undefined' && ko.bindingHandlers && !ko.bindingHandlers.multiselect) {
- ko.bindingHandlers.multiselect = {
- init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
- var listOfSelectedItems = allBindingsAccessor().selectedOptions;
- var config = ko.utils.unwrapObservable(valueAccessor());
- $(element).multiselect(config);
- if (isObservableArray(listOfSelectedItems)) {
-
- // Set the initial selection state on the multiselect list.
- $(element).multiselect('select', ko.utils.unwrapObservable(listOfSelectedItems));
-
- // Subscribe to the selectedOptions: ko.observableArray
- listOfSelectedItems.subscribe(function (changes) {
- var addedArray = [], deletedArray = [];
- forEach(changes, function (change) {
- switch (change.status) {
- case 'added':
- addedArray.push(change.value);
- break;
- case 'deleted':
- deletedArray.push(change.value);
- break;
- }
- });
-
- if (addedArray.length > 0) {
- $(element).multiselect('select', addedArray);
- }
-
- if (deletedArray.length > 0) {
- $(element).multiselect('deselect', deletedArray);
- }
- }, null, "arrayChange");
- }
- },
- update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
- var listOfItems = allBindingsAccessor().options,
- ms = $(element).data('multiselect'),
- config = ko.utils.unwrapObservable(valueAccessor());
- if (isObservableArray(listOfItems)) {
- // Subscribe to the options: ko.observableArray incase it changes later
- listOfItems.subscribe(function (theArray) {
- $(element).multiselect('rebuild');
- });
- }
- if (!ms) {
- $(element).multiselect(config);
- }
- else {
- ms.updateOriginalOptions();
- }
- }
- };
- }
- function isObservableArray(obj) {
- return ko.isObservable(obj) && !(obj.destroyAll === undefined);
- }
- function forEach(array, callback) {
- for (var index = 0; index < array.length; ++index) {
- callback(array[index]);
- }
- }
- /**
- * Constructor to create a new multiselect using the given select.
- *
- * @param {jQuery} select
- * @param {Object} options
- * @returns {Multiselect}
- */
- function Multiselect(select, options) {
- this.$select = $(select);
- this.options = this.mergeOptions($.extend({}, options, this.$select.data()));
- // Initialization.
- // We have to clone to create a new reference.
- this.originalOptions = this.$select.clone()[0].options;
- this.query = '';
- this.searchTimeout = null;
- this.options.multiple = this.$select.attr('multiple') === "multiple";
- this.options.onChange = $.proxy(this.options.onChange, this);
- this.options.onDropdownShow = $.proxy(this.options.onDropdownShow, this);
- this.options.onDropdownHide = $.proxy(this.options.onDropdownHide, this);
- this.options.onDropdownShown = $.proxy(this.options.onDropdownShown, this);
- this.options.onDropdownHidden = $.proxy(this.options.onDropdownHidden, this);
-
- // Build select all if enabled.
- this.buildContainer();
- this.buildButton();
- this.buildDropdown();
- this.buildSelectAll();
- this.buildDropdownOptions();
- this.buildFilter();
-
- this.updateButtonText();
- this.updateSelectAll();
-
- if (this.options.disableIfEmpty && $('option', this.$select).length <= 0) {
- this.disable();
- }
-
- this.$select.hide().after(this.$container);
- };
- Multiselect.prototype = {
- defaults: {
- /**
- * Default text function will either print 'None selected' in case no
- * option is selected or a list of the selected options up to a length
- * of 3 selected options.
- *
- * @param {jQuery} options
- * @param {jQuery} select
- * @returns {String}
- */
- buttonText: function(options, select) {
- if (options.length === 0) {
- return this.nonSelectedText + ' <b class="fa fa-caret-down"></b>';//ACE
- }
- else if (options.length == $('option', $(select)).length) {
- return this.allSelectedText + ' <b class="fa fa-caret-down"></b>';//ACE
- }
- else if (options.length > this.numberDisplayed) {
- return options.length + ' ' + this.nSelectedText + ' <b class="fa fa-caret-down"></b>';//ACE
- }
- else {
- var selected = '';
- options.each(function() {
- var label = ($(this).attr('label') !== undefined) ? $(this).attr('label') : $(this).html();
- selected += label + ', ';
- });
-
- return selected.substr(0, selected.length - 2) + ' <b class="fa fa-caret-down"></b>';//ACE
- }
- },
- /**
- * Updates the title of the button similar to the buttonText function.
- *
- * @param {jQuery} options
- * @param {jQuery} select
- * @returns {@exp;selected@call;substr}
- */
- buttonTitle: function(options, select) {
- if (options.length === 0) {
- return this.nonSelectedText;
- }
- else {
- var selected = '';
- options.each(function () {
- selected += $(this).text() + ', ';
- });
- return selected.substr(0, selected.length - 2);
- }
- },
- /**
- * Create a label.
- *
- * @param {jQuery} element
- * @returns {String}
- */
- label: function(element){
- return $(element).attr('label') || $(element).html();
- },
- /**
- * Triggered on change of the multiselect.
- *
- * Not triggered when selecting/deselecting options manually.
- *
- * @param {jQuery} option
- * @param {Boolean} checked
- */
- onChange : function(option, checked) {
- },
- /**
- * Triggered when the dropdown is shown.
- *
- * @param {jQuery} event
- */
- onDropdownShow: function(event) {
-
- },
- /**
- * Triggered when the dropdown is hidden.
- *
- * @param {jQuery} event
- */
- onDropdownHide: function(event) {
-
- },
- /**
- * Triggered after the dropdown is shown.
- *
- * @param {jQuery} event
- */
- onDropdownShown: function(event) {
-
- },
- /**
- * Triggered after the dropdown is hidden.
- *
- * @param {jQuery} event
- */
- onDropdownHidden: function(event) {
-
- },
- buttonClass: 'btn btn-default',
- buttonWidth: 'auto',
- buttonContainer: '<div class="btn-group" />',
- dropRight: false,
- selectedClass: 'active',
- // Maximum height of the dropdown menu.
- // If maximum height is exceeded a scrollbar will be displayed.
- maxHeight: false,
- checkboxName: false,
- includeSelectAllOption: false,
- includeSelectAllIfMoreThan: 0,
- selectAllText: ' Select all',
- selectAllValue: 'multiselect-all',
- selectAllName: false,
- enableFiltering: false,
- enableCaseInsensitiveFiltering: false,
- enableClickableOptGroups: false,
- filterPlaceholder: 'Search',
- // possible options: 'text', 'value', 'both'
- filterBehavior: 'text',
- includeFilterClearBtn: true,
- preventInputChangeEvent: false,
- nonSelectedText: 'None selected',
- nSelectedText: 'selected',
- allSelectedText: 'All selected',
- numberDisplayed: 3,
- disableIfEmpty: false,
- templates: {
- button: '<button type="button" class="multiselect dropdown-toggle" data-toggle="dropdown"></button>',
- ul: '<ul class="multiselect-container dropdown-menu"></ul>',
- filter: '<li class="multiselect-item filter"><div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span><input class="form-control multiselect-search" type="text"></div></li>',
- filterClearBtn: '<span class="input-group-btn"><button class="btn btn-default multiselect-clear-filter" type="button"><i class="glyphicon glyphicon-remove-circle"></i></button></span>',
- li: '<li><a href="javascript:void(0);"><label></label></a></li>',
- divider: '<li class="multiselect-item divider"></li>',
- liGroup: '<li class="multiselect-item multiselect-group"><label></label></li>'
- }
- },
- constructor: Multiselect,
- /**
- * Builds the container of the multiselect.
- */
- buildContainer: function() {
- this.$container = $(this.options.buttonContainer);
- this.$container.on('show.bs.dropdown', this.options.onDropdownShow);
- this.$container.on('hide.bs.dropdown', this.options.onDropdownHide);
- this.$container.on('shown.bs.dropdown', this.options.onDropdownShown);
- this.$container.on('hidden.bs.dropdown', this.options.onDropdownHidden);
- },
- /**
- * Builds the button of the multiselect.
- */
- buildButton: function() {
- this.$button = $(this.options.templates.button).addClass(this.options.buttonClass);
- // Adopt active state.
- if (this.$select.prop('disabled')) {
- this.disable();
- }
- else {
- this.enable();
- }
- // Manually add button width if set.
- if (this.options.buttonWidth && this.options.buttonWidth !== 'auto') {
- this.$button.css({
- 'width' : this.options.buttonWidth
- });
- this.$container.css({
- 'width': this.options.buttonWidth
- });
- }
- // Keep the tab index from the select.
- var tabindex = this.$select.attr('tabindex');
- if (tabindex) {
- this.$button.attr('tabindex', tabindex);
- }
- this.$container.prepend(this.$button);
- },
- /**
- * Builds the ul representing the dropdown menu.
- */
- buildDropdown: function() {
- // Build ul.
- this.$ul = $(this.options.templates.ul);
- if (this.options.dropRight) {
- this.$ul.addClass('pull-right');
- }
- // Set max height of dropdown menu to activate auto scrollbar.
- if (this.options.maxHeight) {
- // TODO: Add a class for this option to move the css declarations.
- this.$ul.css({
- 'max-height': this.options.maxHeight + 'px',
- 'overflow-y': 'auto',
- 'overflow-x': 'hidden'
- });
- }
- this.$container.append(this.$ul);
- },
- /**
- * Build the dropdown options and binds all nessecary events.
- *
- * Uses createDivider and createOptionValue to create the necessary options.
- */
- buildDropdownOptions: function() {
- this.$select.children().each($.proxy(function(index, element) {
- var $element = $(element);
- // Support optgroups and options without a group simultaneously.
- var tag = $element.prop('tagName')
- .toLowerCase();
-
- if ($element.prop('value') === this.options.selectAllValue) {
- return;
- }
- if (tag === 'optgroup') {
- this.createOptgroup(element);
- }
- else if (tag === 'option') {
- if ($element.data('role') === 'divider') {
- this.createDivider();
- }
- else {
- this.createOptionValue(element);
- }
- }
-
- // Other illegal tags will be ignored.
- }, this));
- // Bind the change event on the dropdown elements.
- $('li input', this.$ul).on('change', $.proxy(function(event) {
- var $target = $(event.target);
- var checked = $target.prop('checked') || false;
- var isSelectAllOption = $target.val() === this.options.selectAllValue;
- // Apply or unapply the configured selected class.
- if (this.options.selectedClass) {
- if (checked) {
- $target.closest('li')
- .addClass(this.options.selectedClass);
- }
- else {
- $target.closest('li')
- .removeClass(this.options.selectedClass);
- }
- }
- // Get the corresponding option.
- var value = $target.val();
- var $option = this.getOptionByValue(value);
- var $optionsNotThis = $('option', this.$select).not($option);
- var $checkboxesNotThis = $('input', this.$container).not($target);
- if (isSelectAllOption) {
- if (checked) {
- this.selectAll();
- }
- else {
- this.deselectAll();
- }
- }
- if(!isSelectAllOption){
- if (checked) {
- $option.prop('selected', true);
- if (this.options.multiple) {
- // Simply select additional option.
- $option.prop('selected', true);
- }
- else {
- // Unselect all other options and corresponding checkboxes.
- if (this.options.selectedClass) {
- $($checkboxesNotThis).closest('li').removeClass(this.options.selectedClass);
- }
- $($checkboxesNotThis).prop('checked', false);
- $optionsNotThis.prop('selected', false);
- // It's a single selection, so close.
- this.$button.click();
- }
- if (this.options.selectedClass === "active") {
- $optionsNotThis.closest("a").css("outline", "");
- }
- }
- else {
- // Unselect option.
- $option.prop('selected', false);
- }
- }
- this.$select.change();
- this.updateButtonText();
- this.updateSelectAll();
-
- this.options.onChange($option, checked);
- if(this.options.preventInputChangeEvent) {
- return false;
- }
- }, this));
- $('li a', this.$ul).on('touchstart click', function(event) {
- event.stopPropagation();
- var $target = $(event.target);
- try {//ACE -- for IE8
- if (document.getSelection().type === 'Range') {
- var $input = $(this).find("input:first");
- $input.prop("checked", !$input.prop("checked"))
- .trigger("change");
- }
- } catch(e) {}
- if (event.shiftKey) {
- var checked = $target.prop('checked') || false;
- if (checked) {
- var prev = $target.closest('li')
- .siblings('li[class="active"]:first');
- var currentIdx = $target.closest('li')
- .index();
- var prevIdx = prev.index();
- if (currentIdx > prevIdx) {
- $target.closest("li").prevUntil(prev).each(
- function() {
- $(this).find("input:first").prop("checked", true)
- .trigger("change");
- }
- );
- }
- else {
- $target.closest("li").nextUntil(prev).each(
- function() {
- $(this).find("input:first").prop("checked", true)
- .trigger("change");
- }
- );
- }
- }
- }
- $target.blur();
- });
- // Keyboard support.
- this.$container.off('keydown.multiselect').on('keydown.multiselect', $.proxy(function(event) {
- if ($('input[type="text"]', this.$container).is(':focus')) {
- return;
- }
- if (event.keyCode === 9 && this.$container.hasClass('open')) {
- this.$button.click();
- }
- else {
- var $items = $(this.$container).find("li:not(.divider):not(.disabled) a").filter(":visible");
- if (!$items.length) {
- return;
- }
- var index = $items.index($items.filter(':focus'));
- // Navigation up.
- if (event.keyCode === 38 && index > 0) {
- index--;
- }
- // Navigate down.
- else if (event.keyCode === 40 && index < $items.length - 1) {
- index++;
- }
- else if (!~index) {
- index = 0;
- }
- var $current = $items.eq(index);
- $current.focus();
- if (event.keyCode === 32 || event.keyCode === 13) {
- var $checkbox = $current.find('input');
- $checkbox.prop("checked", !$checkbox.prop("checked"));
- $checkbox.change();
- }
- event.stopPropagation();
- event.preventDefault();
- }
- }, this));
- if(this.options.enableClickableOptGroups && this.options.multiple) {
- $('li.multiselect-group', this.$ul).on('click', $.proxy(function(event) {
- event.stopPropagation();
- var group = $(event.target).parent();
- // Search all option in optgroup
- var $options = group.nextUntil('li.multiselect-group');
- // check or uncheck items
- var allChecked = true;
- var optionInputs = $options.find('input');
- optionInputs.each(function() {
- allChecked = allChecked && $(this).prop('checked');
- });
- optionInputs.prop('checked', !allChecked).trigger('change');
- }, this));
- }
- },
- /**
- * Create an option using the given select option.
- *
- * @param {jQuery} element
- */
- createOptionValue: function(element) {
- var $element = $(element);
- if ($element.is(':selected')) {
- $element.prop('selected', true);
- }
- // Support the label attribute on options.
- var label = this.options.label(element);
- var value = $element.val();
- var inputType = this.options.multiple ? "checkbox" : "radio";
- var $li = $(this.options.templates.li);
- var $label = $('label', $li);
- $label.addClass(inputType);
- var $checkbox = $('<input/>').attr('type', inputType).addClass('ace');//ACE
- if (this.options.checkboxName) {
- $checkbox.attr('name', this.options.checkboxName);
- }
- $label.append($checkbox);
- $checkbox.after('<span class="lbl" />');//ACE
- var selected = $element.prop('selected') || false;
- $checkbox.val(value);
- if (value === this.options.selectAllValue) {
- $li.addClass("multiselect-item multiselect-all");
- $checkbox.parent().parent()
- .addClass('multiselect-all');
- }
- $label.append(" " + label);
- $label.attr('title', $element.attr('title'));
- this.$ul.append($li);
- if ($element.is(':disabled')) {
- $checkbox.attr('disabled', 'disabled')
- .prop('disabled', true)
- .closest('a')
- .attr("tabindex", "-1")
- .closest('li')
- .addClass('disabled');
- }
- $checkbox.prop('checked', selected);
- if (selected && this.options.selectedClass) {
- $checkbox.closest('li')
- .addClass(this.options.selectedClass);
- }
- },
- /**
- * Creates a divider using the given select option.
- *
- * @param {jQuery} element
- */
- createDivider: function(element) {
- var $divider = $(this.options.templates.divider);
- this.$ul.append($divider);
- },
- /**
- * Creates an optgroup.
- *
- * @param {jQuery} group
- */
- createOptgroup: function(group) {
- var groupName = $(group).prop('label');
- // Add a header for the group.
- var $li = $(this.options.templates.liGroup);
- $('label', $li).text(groupName);
- if (this.options.enableClickableOptGroups) {
- $li.addClass('multiselect-group-clickable');
- }
- this.$ul.append($li);
- if ($(group).is(':disabled')) {
- $li.addClass('disabled');
- }
- // Add the options of the group.
- $('option', group).each($.proxy(function(index, element) {
- this.createOptionValue(element);
- }, this));
- },
- /**
- * Build the selct all.
- *
- * Checks if a select all has already been created.
- */
- buildSelectAll: function() {
- if (typeof this.options.selectAllValue === 'number') {
- this.options.selectAllValue = this.options.selectAllValue.toString();
- }
-
- var alreadyHasSelectAll = this.hasSelectAll();
-
- if (!alreadyHasSelectAll && this.options.includeSelectAllOption && this.options.multiple
- && $('option', this.$select).length > this.options.includeSelectAllIfMoreThan) {
-
- // Check whether to add a divider after the select all.
- if (this.options.includeSelectAllDivider) {
- this.$ul.prepend($(this.options.templates.divider));
- }
- var $li = $(this.options.templates.li);
- $('label', $li).addClass("checkbox");
-
- if (this.options.selectAllName) {
- $('label', $li).append('<input type="checkbox" name="' + this.options.selectAllName + '" />');
- }
- else {
- $('label', $li).append('<input type="checkbox" />');
- }
-
- var $checkbox = $('input', $li);
- $checkbox.val(this.options.selectAllValue);
- $li.addClass("multiselect-item multiselect-all");
- $checkbox.parent().parent()
- .addClass('multiselect-all');
- $('label', $li).append(" " + this.options.selectAllText);
- this.$ul.prepend($li);
- $checkbox.prop('checked', false);
- }
- },
- /**
- * Builds the filter.
- */
- buildFilter: function() {
- // Build filter if filtering OR case insensitive filtering is enabled and the number of options exceeds (or equals) enableFilterLength.
- if (this.options.enableFiltering || this.options.enableCaseInsensitiveFiltering) {
- var enableFilterLength = Math.max(this.options.enableFiltering, this.options.enableCaseInsensitiveFiltering);
- if (this.$select.find('option').length >= enableFilterLength) {
- this.$filter = $(this.options.templates.filter);
- $('input', this.$filter).attr('placeholder', this.options.filterPlaceholder);
-
- // Adds optional filter clear button
- if(this.options.includeFilterClearBtn){
- var clearBtn = $(this.options.templates.filterClearBtn);
- clearBtn.on('click', $.proxy(function(event){
- clearTimeout(this.searchTimeout);
- this.$filter.find('.multiselect-search').val('');
- $('li', this.$ul).show().removeClass("filter-hidden");
- this.updateSelectAll();
- }, this));
- this.$filter.find('.input-group').append(clearBtn);
- }
-
- this.$ul.prepend(this.$filter);
- this.$filter.val(this.query).on('click', function(event) {
- event.stopPropagation();
- }).on('input keydown', $.proxy(function(event) {
- // Cancel enter key default behaviour
- if (event.which === 13) {
- event.preventDefault();
- }
-
- // This is useful to catch "keydown" events after the browser has updated the control.
- clearTimeout(this.searchTimeout);
- this.searchTimeout = this.asyncFunction($.proxy(function() {
- if (this.query !== event.target.value) {
- this.query = event.target.value;
- var currentGroup, currentGroupVisible;
- $.each($('li', this.$ul), $.proxy(function(index, element) {
- var value = $('input', element).val();
- var text = $('label', element).text();
- var filterCandidate = '';
- if ((this.options.filterBehavior === 'text')) {
- filterCandidate = text;
- }
- else if ((this.options.filterBehavior === 'value')) {
- filterCandidate = value;
- }
- else if (this.options.filterBehavior === 'both') {
- filterCandidate = text + '\n' + value;
- }
- if (value !== this.options.selectAllValue && text) {
- // By default lets assume that element is not
- // interesting for this search.
- var showElement = false;
- if (this.options.enableCaseInsensitiveFiltering && filterCandidate.toLowerCase().indexOf(this.query.toLowerCase()) > -1) {
- showElement = true;
- }
- else if (filterCandidate.indexOf(this.query) > -1) {
- showElement = true;
- }
- // Toggle current element (group or group item) according to showElement boolean.
- $(element).toggle(showElement).toggleClass('filter-hidden', !showElement);
-
- // Differentiate groups and group items.
- if ($(element).hasClass('multiselect-group')) {
- // Remember group status.
- currentGroup = element;
- currentGroupVisible = showElement;
- }
- else {
- // Show group name when at least one of its items is visible.
- if (showElement) {
- $(currentGroup).show().removeClass('filter-hidden');
- }
-
- // Show all group items when group name satisfies filter.
- if (!showElement && currentGroupVisible) {
- $(element).show().removeClass('filter-hidden');
- }
- }
- }
- }, this));
- }
- this.updateSelectAll();
- }, this), 300, this);
- }, this));
- }
- }
- },
- /**
- * Unbinds the whole plugin.
- */
- destroy: function() {
- this.$container.remove();
- this.$select.show();
- this.$select.data('multiselect', null);
- },
- /**
- * Refreshs the multiselect based on the selected options of the select.
- */
- refresh: function() {
- $('option', this.$select).each($.proxy(function(index, element) {
- var $input = $('li input', this.$ul).filter(function() {
- return $(this).val() === $(element).val();
- });
- if ($(element).is(':selected')) {
- $input.prop('checked', true);
- if (this.options.selectedClass) {
- $input.closest('li')
- .addClass(this.options.selectedClass);
- }
- }
- else {
- $input.prop('checked', false);
- if (this.options.selectedClass) {
- $input.closest('li')
- .removeClass(this.options.selectedClass);
- }
- }
- if ($(element).is(":disabled")) {
- $input.attr('disabled', 'disabled')
- .prop('disabled', true)
- .closest('li')
- .addClass('disabled');
- }
- else {
- $input.prop('disabled', false)
- .closest('li')
- .removeClass('disabled');
- }
- }, this));
- this.updateButtonText();
- this.updateSelectAll();
- },
- /**
- * Select all options of the given values.
- *
- * If triggerOnChange is set to true, the on change event is triggered if
- * and only if one value is passed.
- *
- * @param {Array} selectValues
- * @param {Boolean} triggerOnChange
- */
- select: function(selectValues, triggerOnChange) {
- if(!$.isArray(selectValues)) {
- selectValues = [selectValues];
- }
- for (var i = 0; i < selectValues.length; i++) {
- var value = selectValues[i];
- if (value === null || value === undefined) {
- continue;
- }
- var $option = this.getOptionByValue(value);
- var $checkbox = this.getInputByValue(value);
- if($option === undefined || $checkbox === undefined) {
- continue;
- }
-
- if (!this.options.multiple) {
- this.deselectAll(false);
- }
-
- if (this.options.selectedClass) {
- $checkbox.closest('li')
- .addClass(this.options.selectedClass);
- }
- $checkbox.prop('checked', true);
- $option.prop('selected', true);
- }
- this.updateButtonText();
- this.updateSelectAll();
- if (triggerOnChange && selectValues.length === 1) {
- this.options.onChange($option, true);
- }
- },
- /**
- * Clears all selected items.
- */
- clearSelection: function () {
- this.deselectAll(false);
- this.updateButtonText();
- this.updateSelectAll();
- },
- /**
- * Deselects all options of the given values.
- *
- * If triggerOnChange is set to true, the on change event is triggered, if
- * and only if one value is passed.
- *
- * @param {Array} deselectValues
- * @param {Boolean} triggerOnChange
- */
- deselect: function(deselectValues, triggerOnChange) {
- if(!$.isArray(deselectValues)) {
- deselectValues = [deselectValues];
- }
- for (var i = 0; i < deselectValues.length; i++) {
- var value = deselectValues[i];
- if (value === null || value === undefined) {
- continue;
- }
- var $option = this.getOptionByValue(value);
- var $checkbox = this.getInputByValue(value);
- if($option === undefined || $checkbox === undefined) {
- continue;
- }
- if (this.options.selectedClass) {
- $checkbox.closest('li')
- .removeClass(this.options.selectedClass);
- }
- $checkbox.prop('checked', false);
- $option.prop('selected', false);
- }
- this.updateButtonText();
- this.updateSelectAll();
-
- if (triggerOnChange && deselectValues.length === 1) {
- this.options.onChange($option, false);
- }
- },
-
- /**
- * Selects all enabled & visible options.
- *
- * If justVisible is true or not specified, only visible options are selected.
- *
- * @param {Boolean} justVisible
- */
- selectAll: function (justVisible) {
- var justVisible = typeof justVisible === 'undefined' ? true : justVisible;
- var allCheckboxes = $("li input[type='checkbox']:enabled", this.$ul);
- var visibleCheckboxes = allCheckboxes.filter(":visible");
- var allCheckboxesCount = allCheckboxes.length;
- var visibleCheckboxesCount = visibleCheckboxes.length;
-
- if(justVisible) {
- visibleCheckboxes.prop('checked', true);
- $("li:not(.divider):not(.disabled)", this.$ul).filter(":visible").addClass(this.options.selectedClass);
- }
- else {
- allCheckboxes.prop('checked', true);
- $("li:not(.divider):not(.disabled)", this.$ul).addClass(this.options.selectedClass);
- }
-
- if (allCheckboxesCount === visibleCheckboxesCount || justVisible === false) {
- $("option:enabled", this.$select).prop('selected', true);
- }
- else {
- var values = visibleCheckboxes.map(function() {
- return $(this).val();
- }).get();
-
- $("option:enabled", this.$select).filter(function(index) {
- return $.inArray($(this).val(), values) !== -1;
- }).prop('selected', true);
- }
- },
- /**
- * Deselects all options.
- *
- * If justVisible is true or not specified, only visible options are deselected.
- *
- * @param {Boolean} justVisible
- */
- deselectAll: function (justVisible) {
- var justVisible = typeof justVisible === 'undefined' ? true : justVisible;
-
- if(justVisible) {
- var visibleCheckboxes = $("li input[type='checkbox']:enabled", this.$ul).filter(":visible");
- visibleCheckboxes.prop('checked', false);
-
- var values = visibleCheckboxes.map(function() {
- return $(this).val();
- }).get();
-
- $("option:enabled", this.$select).filter(function(index) {
- return $.inArray($(this).val(), values) !== -1;
- }).prop('selected', false);
-
- if (this.options.selectedClass) {
- $("li:not(.divider):not(.disabled)", this.$ul).filter(":visible").removeClass(this.options.selectedClass);
- }
- }
- else {
- $("li input[type='checkbox']:enabled", this.$ul).prop('checked', false);
- $("option:enabled", this.$select).prop('selected', false);
-
- if (this.options.selectedClass) {
- $("li:not(.divider):not(.disabled)", this.$ul).removeClass(this.options.selectedClass);
- }
- }
- },
- /**
- * Rebuild the plugin.
- *
- * Rebuilds the dropdown, the filter and the select all option.
- */
- rebuild: function() {
- this.$ul.html('');
- // Important to distinguish between radios and checkboxes.
- this.options.multiple = this.$select.attr('multiple') === "multiple";
- this.buildSelectAll();
- this.buildDropdownOptions();
- this.buildFilter();
-
- this.updateButtonText();
- this.updateSelectAll();
-
- if (this.options.disableIfEmpty && $('option', this.$select).length <= 0) {
- this.disable();
- }
-
- if (this.options.dropRight) {
- this.$ul.addClass('pull-right');
- }
- },
- /**
- * The provided data will be used to build the dropdown.
- */
- dataprovider: function(dataprovider) {
- var optionDOM = "";
- var groupCounter = 0;
- var tags = $(''); // create empty jQuery array
- $.each(dataprovider, function (index, option) {
- var tag;
- if ($.isArray(option.children)) { // create optiongroup tag
- groupCounter++;
- tag = $('<optgroup/>').attr({
- label: option.label || 'Group ' + groupCounter
- });
- forEach(option.children, function(subOption) { // add children option tags
- tag.append($('<option/>').attr({
- value: subOption.value,
- label: subOption.label || subOption.value,
- title: subOption.title,
- selected: !!subOption.selected
- }));
- });
- optionDOM += '</optgroup>';
- }
- else { // create option tag
- tag = $('<option/>').attr({
- value: option.value,
- label: option.label || option.value,
- title: option.title,
- selected: !!option.selected
- });
- }
- tags = tags.add(tag);
- });
-
- this.$select.empty().append(tags);
- this.rebuild();
- },
- /**
- * Enable the multiselect.
- */
- enable: function() {
- this.$select.prop('disabled', false);
- this.$button.prop('disabled', false)
- .removeClass('disabled');
- },
- /**
- * Disable the multiselect.
- */
- disable: function() {
- this.$select.prop('disabled', true);
- this.$button.prop('disabled', true)
- .addClass('disabled');
- },
- /**
- * Set the options.
- *
- * @param {Array} options
- */
- setOptions: function(options) {
- this.options = this.mergeOptions(options);
- },
- /**
- * Merges the given options with the default options.
- *
- * @param {Array} options
- * @returns {Array}
- */
- mergeOptions: function(options) {
- return $.extend(true, {}, this.defaults, options);
- },
-
- /**
- * Checks whether a select all checkbox is present.
- *
- * @returns {Boolean}
- */
- hasSelectAll: function() {
- return $('li.' + this.options.selectAllValue, this.$ul).length > 0;
- },
-
- /**
- * Updates the select all checkbox based on the currently displayed and selected checkboxes.
- */
- updateSelectAll: function() {
- if (this.hasSelectAll()) {
- var allBoxes = $("li:not(.multiselect-item):not(.filter-hidden) input:enabled", this.$ul);
- var allBoxesLength = allBoxes.length;
- var checkedBoxesLength = allBoxes.filter(":checked").length;
- var selectAllLi = $("li." + this.options.selectAllValue, this.$ul);
- var selectAllInput = selectAllLi.find("input");
-
- if (checkedBoxesLength > 0 && checkedBoxesLength === allBoxesLength) {
- selectAllInput.prop("checked", true);
- selectAllLi.addClass(this.options.selectedClass);
- }
- else {
- selectAllInput.prop("checked", false);
- selectAllLi.removeClass(this.options.selectedClass);
- }
- }
- },
-
- /**
- * Update the button text and its title based on the currently selected options.
- */
- updateButtonText: function() {
- var options = this.getSelected();
-
- // First update the displayed button text.
- $('.multiselect', this.$container).html(this.options.buttonText(options, this.$select));
-
- // Now update the title attribute of the button.
- $('.multiselect', this.$container).attr('title', this.options.buttonTitle(options, this.$select));
- },
- /**
- * Get all selected options.
- *
- * @returns {jQUery}
- */
- getSelected: function() {
- return $('option', this.$select).filter(":selected");
- },
- /**
- * Gets a select option by its value.
- *
- * @param {String} value
- * @returns {jQuery}
- */
- getOptionByValue: function (value) {
- var options = $('option', this.$select);
- var valueToCompare = value.toString();
- for (var i = 0; i < options.length; i = i + 1) {
- var option = options[i];
- if (option.value === valueToCompare) {
- return $(option);
- }
- }
- },
- /**
- * Get the input (radio/checkbox) by its value.
- *
- * @param {String} value
- * @returns {jQuery}
- */
- getInputByValue: function (value) {
- var checkboxes = $('li input', this.$ul);
- var valueToCompare = value.toString();
- for (var i = 0; i < checkboxes.length; i = i + 1) {
- var checkbox = checkboxes[i];
- if (checkbox.value === valueToCompare) {
- return $(checkbox);
- }
- }
- },
- /**
- * Used for knockout integration.
- */
- updateOriginalOptions: function() {
- this.originalOptions = this.$select.clone()[0].options;
- },
- asyncFunction: function(callback, timeout, self) {
- var args = Array.prototype.slice.call(arguments, 3);
- return setTimeout(function() {
- callback.apply(self || window, args);
- }, timeout);
- }
- };
- $.fn.multiselect = function(option, parameter, extraOptions) {
- return this.each(function() {
- var data = $(this).data('multiselect');
- var options = typeof option === 'object' && option;
-
- // Initialize the multiselect.
- if (!data) {
- data = new Multiselect(this, options);
- $(this).data('multiselect', data);
- }
- // Call multiselect method.
- if (typeof option === 'string') {
- data[option](parameter, extraOptions);
-
- if (option === 'destroy') {
- $(this).data('multiselect', false);
- }
- }
- });
- };
- $.fn.multiselect.Constructor = Multiselect;
- $(function() {
- $("select[data-role=multiselect]").multiselect();
- });
- }(window.jQuery);
|