| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994 |
- /**
- * bootstrap-multiselect.js
- * 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,
- config = ko.utils.unwrapObservable(valueAccessor());
- $(element).multiselect(config);
- if (isObservableArray(listOfSelectedItems)) {
- // Subscribe to the selectedOptions: ko.observableArray
- listOfSelectedItems.subscribe(function (changes) {
- var addedArray = [], deletedArray = [];
- changes.forEach(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);
- }
- /**
- * Constructor to create a new multiselect using the given select.
- *
- * @param {jQuery} select
- * @param {Object} options
- * @returns {Multiselect}
- */
- function Multiselect(select, options) {
- this.options = this.mergeOptions(options);
- this.$select = $(select);
- // 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);
- // Build select all if enabled.
- this.buildContainer();
- this.buildButton();
- this.buildSelectAll();
- this.buildDropdown();
- this.buildDropdownOptions();
- this.buildFilter();
-
- this.updateButtonText();
- this.updateSelectAll();
-
- 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="caret"></b>';
- }
- else {
- if (options.length > this.numberDisplayed) {
- return options.length + ' ' + this.nSelectedText + ' <b class="caret"></b>';
- }
- 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="caret"></b>';
- }
- }
- },
- /**
- * 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) {
-
- },
- buttonClass: 'btn btn-default',
- dropRight: false,
- selectedClass: 'active',
- buttonWidth: 'auto',
- buttonContainer: '<div class="btn-group" />',
- // Maximum height of the dropdown menu.
- // If maximum height is exceeded a scrollbar will be displayed.
- maxHeight: false,
- checkboxName: 'multiselect',
- includeSelectAllOption: false,
- includeSelectAllIfMoreThan: 0,
- selectAllText: ' Select all',
- selectAllValue: 'multiselect-all',
- enableFiltering: false,
- enableCaseInsensitiveFiltering: false,
- filterPlaceholder: 'Search',
- // possible options: 'text', 'value', 'both'
- filterBehavior: 'text',
- preventInputChangeEvent: false,
- nonSelectedText: 'None selected',
- nSelectedText: 'selected',
- numberDisplayed: 3,
- templates: {
- button: '<button type="button" class="multiselect dropdown-toggle" data-toggle="dropdown"></button>',
- ul: '<ul class="multiselect-container dropdown-menu"></ul>',
- 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: '<li><a href="javascript:void(0);"><label></label></a></li>',
- divider: '<li class="divider"></li>',
- liGroup: '<li><label class="multiselect-group"></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);
- },
- /**
- * 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
- });
- }
- // 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) {
-
- // Support optgroups and options without a group simultaneously.
- var tag = $(element).prop('tagName')
- .toLowerCase();
- 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.parents('li')
- .addClass(this.options.selectedClass);
- }
- else {
- $target.parents('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) {
- var values = [];
-
- // Select the visible checkboxes except the "select-all" and possible divider.
- var availableInputs = $('li input[value!="' + this.options.selectAllValue + '"][data-role!="divider"]', this.$ul).filter(':visible');
- for (var i = 0, j = availableInputs.length; i < j; i++) {
- values.push(availableInputs[i].value);
- }
- if (checked) {
- this.select(values);
- }
- else {
- this.deselect(values);
- }
- }
- 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).parents('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.parents("a").css("outline", "");
- }
- }
- else {
- // Unselect option.
- $option.prop('selected', false);
- }
- this.$select.change();
- this.options.onChange($option, checked);
-
- this.updateButtonText();
- this.updateSelectAll();
- if(this.options.preventInputChangeEvent) {
- return false;
- }
- }, this));
- $('li a', this.$ul).on('touchstart click', function(event) {
- event.stopPropagation();
- var $target = $(event.target);
- if (event.shiftKey) {
- var checked = $target.prop('checked') || false;
- if (checked) {
- var prev = $target.parents('li:last')
- .siblings('li[class="active"]:first');
- var currentIdx = $target.parents('li')
- .index();
- var prevIdx = prev.index();
- if (currentIdx > prevIdx) {
- $target.parents("li:last").prevUntil(prev).each(
- function() {
- $(this).find("input:first").prop("checked", true)
- .trigger("change");
- }
- );
- }
- else {
- $target.parents("li:last").nextUntil(prev).each(
- function() {
- $(this).find("input:first").prop("checked", true)
- .trigger("change");
- }
- );
- }
- }
- }
- $target.blur();
- });
- // Keyboard support.
- this.$container.on('keydown', $.proxy(function(event) {
- if ($('input[type="text"]', this.$container).is(':focus')) {
- return;
- }
- if ((event.keyCode === 9 || event.keyCode === 27)
- && this.$container.hasClass('open')) {
-
- // Close on tab or escape.
- this.$button.click();
- }
- else {
- var $items = $(this.$container).find("li:not(.divider):visible a");
- 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));
- },
- /**
- * Create an option using the given select option.
- *
- * @param {jQuery} element
- */
- createOptionValue: function(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);
- $('label', $li).addClass(inputType);
- $('label', $li).append('<input type="' + inputType + '" name="' + this.options.checkboxName + '" />');
- var selected = $(element).prop('selected') || false;
- var $checkbox = $('input', $li);
- $checkbox.val(value);
- if (value === this.options.selectAllValue) {
- $checkbox.parent().parent()
- .addClass('multiselect-all');
- }
- $('label', $li).append(" " + label);
- this.$ul.append($li);
- if ($(element).is(':disabled')) {
- $checkbox.attr('disabled', 'disabled')
- .prop('disabled', true)
- .parents('li')
- .addClass('disabled');
- }
- $checkbox.prop('checked', selected);
- if (selected && this.options.selectedClass) {
- $checkbox.parents('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);
- 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 ahs already been created.
- */
- buildSelectAll: function() {
- var alreadyHasSelectAll = this.hasSelectAll();
-
- if (!alreadyHasSelectAll && this.options.includeSelectAllOption && this.options.multiple
- && $('option[data-role!="divider"]', this.$select).length > this.options.includeSelectAllIfMoreThan) {
-
- // Check whether to add a divider after the select all.
- if (this.options.includeSelectAllDivider) {
- this.$select.prepend('<option value="" disabled="disabled" data-role="divider">');
- }
-
- this.$select.prepend('<option value="' + this.options.selectAllValue + '">' + this.options.selectAllText + '</option>');
- }
- },
- /**
- * 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);
- this.$ul.prepend(this.$filter);
- this.$filter.val(this.query).on('click', function(event) {
- event.stopPropagation();
- }).on('input keydown', $.proxy(function(event) {
- // 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;
- $.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;
- }
- if (showElement) {
- $(element).show();
- }
- else {
- $(element).hide();
- }
- }
- }, this));
- }
- // TODO: check whether select all option needs to be updated.
- }, 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.parents('li')
- .addClass(this.options.selectedClass);
- }
- }
- else {
- $input.prop('checked', false);
- if (this.options.selectedClass) {
- $input.parents('li')
- .removeClass(this.options.selectedClass);
- }
- }
- if ($(element).is(":disabled")) {
- $input.attr('disabled', 'disabled')
- .prop('disabled', true)
- .parents('li')
- .addClass('disabled');
- }
- else {
- $input.prop('disabled', false)
- .parents('li')
- .removeClass('disabled');
- }
- }, this));
- this.updateButtonText();
- this.updateSelectAll();
- },
- /**
- * Select all options of the given values.
- *
- * @param {Array} selectValues
- */
- select: function(selectValues) {
- if(!$.isArray(selectValues)) {
- selectValues = [selectValues];
- }
- for (var i = 0; i < selectValues.length; i++) {
- var value = selectValues[i];
- var $option = this.getOptionByValue(value);
- var $checkbox = this.getInputByValue(value);
- if (this.options.selectedClass) {
- $checkbox.parents('li')
- .addClass(this.options.selectedClass);
- }
- $checkbox.prop('checked', true);
- $option.prop('selected', true);
- }
- this.updateButtonText();
- },
- /**
- * Clears all selected items
- *
- */
- clearSelection: function () {
- var selected = this.getSelected();
- if (selected.length) {
- var arry = [];
- for (var i = 0; i < selected.length; i = i + 1) {
- arry.push(selected[i].value);
- }
- this.deselect(arry);
- this.$select.change();
- }
- },
- /**
- * Deselects all options of the given values.
- *
- * @param {Array} deselectValues
- */
- deselect: function(deselectValues) {
- if(!$.isArray(deselectValues)) {
- deselectValues = [deselectValues];
- }
- for (var i = 0; i < deselectValues.length; i++) {
- var value = deselectValues[i];
- var $option = this.getOptionByValue(value);
- var $checkbox = this.getInputByValue(value);
- if (this.options.selectedClass) {
- $checkbox.parents('li')
- .removeClass(this.options.selectedClass);
- }
- $checkbox.prop('checked', false);
- $option.prop('selected', false);
- }
- this.updateButtonText();
- },
- /**
- * Rebuild the plugin.
- * Rebuilds the dropdown, the filter and the select all option.
- */
- rebuild: function() {
- this.$ul.html('');
- // Remove select all option in select.
- $('option[value="' + this.options.selectAllValue + '"]', this.$select).remove();
- // 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();
- },
- /**
- * The provided data will be used to build the dropdown.
- *
- * @param {Array} dataprovider
- */
- dataprovider: function(dataprovider) {
- var optionDOM = "";
- dataprovider.forEach(function (option) {
- optionDOM += '<option value="' + option.value + '">' + option.label + '</option>';
- });
- this.$select.html(optionDOM);
- 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 option is present.
- *
- * @returns {Boolean}
- */
- hasSelectAll: function() {
- return $('option[value="' + this.options.selectAllValue + '"]', this.$select).length > 0;
- },
-
- /**
- * Updates the select all option based on the currently selected options.
- */
- updateSelectAll: function() {
- if (this.hasSelectAll()) {
- var selected = this.getSelected();
-
- if (selected.length === $('option:not([data-role=divider])', this.$select).length - 1) {
- this.select(this.options.selectAllValue);
- }
- else {
- this.deselect(this.options.selectAllValue);
- }
- }
- },
-
- /**
- * 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.
- $('button', this.$container).html(this.options.buttonText(options, this.$select));
-
- // Now update the title attribute of the button.
- $('button', this.$container).attr('title', this.options.buttonTitle(options, this.$select));
- },
- /**
- * Get all selected options.
- *
- * @returns {jQUery}
- */
- getSelected: function() {
- return $('option[value!="' + this.options.selectAllValue + '"]:selected', this.$select).filter(function() {
- return $(this).prop('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) {
- 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);
-
- if (option === 'destroy') {
- $(this).data('multiselect', false);
- }
- }
- });
- };
- $.fn.multiselect.Constructor = Multiselect;
- $(function() {
- $("select[data-role=multiselect]").multiselect();
- });
- }(window.jQuery);
|