Commit 55d573f1 authored by Nicholas Nieslanik's avatar Nicholas Nieslanik

Adding require.js / AMD header to allow easy inclusion in module based projects

parent 1782cb98
...@@ -6,10 +6,17 @@ ...@@ -6,10 +6,17 @@
* *
* Dual licensed under the BSD-3-Clause and the Apache License, Version 2.0. * Dual licensed under the BSD-3-Clause and the Apache License, Version 2.0.
*/ */
!function($) { (function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'knockout'], factory);
} else {
// Browser globals
factory(root.jQuery, root.ko);
}
})(this, function ($, ko) {
"use strict";// jshint ;_; "use strict";// jshint ;_;
if (Array.prototype.forEach === null || Array.prototype.forEach === undefined) { if (Array.prototype.forEach === null || Array.prototype.forEach === undefined) {
Array.prototype.forEach = function (func) { Array.prototype.forEach = function (func) {
var index; var index;
...@@ -84,7 +91,7 @@ ...@@ -84,7 +91,7 @@
/** /**
* Constructor to create a new multiselect using the given select. * Constructor to create a new multiselect using the given select.
* *
* @param {jQuery} select * @param {jQuery} select
* @param {Object} options * @param {Object} options
* @returns {Multiselect} * @returns {Multiselect}
...@@ -112,10 +119,10 @@ ...@@ -112,10 +119,10 @@
this.buildSelectAll(); this.buildSelectAll();
this.buildDropdownOptions(); this.buildDropdownOptions();
this.buildFilter(); this.buildFilter();
this.updateButtonText(); this.updateButtonText();
this.updateSelectAll(); this.updateSelectAll();
this.$select.hide().after(this.$container); this.$select.hide().after(this.$container);
}; };
...@@ -125,7 +132,7 @@ ...@@ -125,7 +132,7 @@
/** /**
* Default text function will either print 'None selected' in case no * 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. * option is selected or a list of the selected options up to a length of 3 selected options.
* *
* @param {jQuery} options * @param {jQuery} options
* @param {jQuery} select * @param {jQuery} select
* @returns {String} * @returns {String}
...@@ -169,7 +176,7 @@ ...@@ -169,7 +176,7 @@
}, },
/** /**
* Create a label. * Create a label.
* *
* @param {jQuery} element * @param {jQuery} element
* @returns {String} * @returns {String}
*/ */
...@@ -179,7 +186,7 @@ ...@@ -179,7 +186,7 @@
/** /**
* Triggered on change of the multiselect. * Triggered on change of the multiselect.
* Not triggered when selecting/deselecting options manually. * Not triggered when selecting/deselecting options manually.
* *
* @param {jQuery} option * @param {jQuery} option
* @param {Boolean} checked * @param {Boolean} checked
*/ */
...@@ -188,19 +195,19 @@ ...@@ -188,19 +195,19 @@
}, },
/** /**
* Triggered when the dropdown is shown. * Triggered when the dropdown is shown.
* *
* @param {jQuery} event * @param {jQuery} event
*/ */
onDropdownShow: function(event) { onDropdownShow: function(event) {
}, },
/** /**
* Triggered when the dropdown is hidden. * Triggered when the dropdown is hidden.
* *
* @param {jQuery} event * @param {jQuery} event
*/ */
onDropdownHide: function(event) { onDropdownHide: function(event) {
}, },
buttonClass: 'btn btn-default', buttonClass: 'btn btn-default',
dropRight: false, dropRight: false,
...@@ -307,11 +314,11 @@ ...@@ -307,11 +314,11 @@
buildDropdownOptions: function() { buildDropdownOptions: function() {
this.$select.children().each($.proxy(function(index, element) { this.$select.children().each($.proxy(function(index, element) {
// Support optgroups and options without a group simultaneously. // Support optgroups and options without a group simultaneously.
var tag = $(element).prop('tagName') var tag = $(element).prop('tagName')
.toLowerCase(); .toLowerCase();
if ($(element).prop('value') == this.options.selectAllValue) { if ($(element).prop('value') == this.options.selectAllValue) {
return; return;
} }
...@@ -329,7 +336,7 @@ ...@@ -329,7 +336,7 @@
} }
} }
// Other illegal tags will be ignored. // Other illegal tags will be ignored.
}, this)); }, this));
...@@ -403,7 +410,7 @@ ...@@ -403,7 +410,7 @@
this.updateButtonText(); this.updateButtonText();
this.updateSelectAll(); this.updateSelectAll();
this.options.onChange($option, checked); this.options.onChange($option, checked);
if(this.options.preventInputChangeEvent) { if(this.options.preventInputChangeEvent) {
...@@ -456,7 +463,7 @@ ...@@ -456,7 +463,7 @@
} }
if ((event.keyCode === 9 || event.keyCode === 27) if ((event.keyCode === 9 || event.keyCode === 27)
&& this.$container.hasClass('open')) { && this.$container.hasClass('open')) {
// Close on tab or escape. // Close on tab or escape.
this.$button.click(); this.$button.click();
} }
...@@ -499,7 +506,7 @@ ...@@ -499,7 +506,7 @@
/** /**
* Create an option using the given select option. * Create an option using the given select option.
* *
* @param {jQuery} element * @param {jQuery} element
*/ */
createOptionValue: function(element) { createOptionValue: function(element) {
...@@ -549,7 +556,7 @@ ...@@ -549,7 +556,7 @@
/** /**
* Creates a divider using the given select option. * Creates a divider using the given select option.
* *
* @param {jQuery} element * @param {jQuery} element
*/ */
createDivider: function(element) { createDivider: function(element) {
...@@ -559,7 +566,7 @@ ...@@ -559,7 +566,7 @@
/** /**
* Creates an optgroup. * Creates an optgroup.
* *
* @param {jQuery} group * @param {jQuery} group
*/ */
createOptgroup: function(group) { createOptgroup: function(group) {
...@@ -587,10 +594,10 @@ ...@@ -587,10 +594,10 @@
*/ */
buildSelectAll: function() { buildSelectAll: function() {
var alreadyHasSelectAll = this.hasSelectAll(); var alreadyHasSelectAll = this.hasSelectAll();
if (!alreadyHasSelectAll && this.options.includeSelectAllOption && this.options.multiple if (!alreadyHasSelectAll && this.options.includeSelectAllOption && this.options.multiple
&& $('option', this.$select).length > this.options.includeSelectAllIfMoreThan) { && $('option', this.$select).length > this.options.includeSelectAllIfMoreThan) {
// Check whether to add a divider after the select all. // Check whether to add a divider after the select all.
if (this.options.includeSelectAllDivider) { if (this.options.includeSelectAllDivider) {
this.$ul.prepend($(this.options.templates.divider)); this.$ul.prepend($(this.options.templates.divider));
...@@ -739,7 +746,7 @@ ...@@ -739,7 +746,7 @@
/** /**
* Select all options of the given values. * Select all options of the given values.
* *
* @param {Array} selectValues * @param {Array} selectValues
*/ */
select: function(selectValues) { select: function(selectValues) {
...@@ -757,7 +764,7 @@ ...@@ -757,7 +764,7 @@
{ {
continue; continue;
} }
if (this.options.selectedClass) { if (this.options.selectedClass) {
$checkbox.parents('li') $checkbox.parents('li')
.addClass(this.options.selectedClass); .addClass(this.options.selectedClass);
...@@ -772,7 +779,7 @@ ...@@ -772,7 +779,7 @@
/** /**
* Clears all selected items * Clears all selected items
* *
*/ */
clearSelection: function () { clearSelection: function () {
this.deselectall(false); this.deselectall(false);
...@@ -782,7 +789,7 @@ ...@@ -782,7 +789,7 @@
/** /**
* Deselects all options of the given values. * Deselects all options of the given values.
* *
* @param {Array} deselectValues * @param {Array} deselectValues
*/ */
deselect: function(deselectValues) { deselect: function(deselectValues) {
...@@ -813,20 +820,20 @@ ...@@ -813,20 +820,20 @@
this.updateButtonText(); this.updateButtonText();
}, },
/** /**
* Selects all enabled & visible options. * Selects all enabled & visible options.
* *
*/ */
selectall: function () { selectall: function () {
var allCheckboxes = $("li input[type='checkbox']:enabled", this.$ul), var allCheckboxes = $("li input[type='checkbox']:enabled", this.$ul),
visibleCheckboxes = allCheckboxes.filter(":visible"), visibleCheckboxes = allCheckboxes.filter(":visible"),
allCheckboxesCount = allCheckboxes.length, allCheckboxesCount = allCheckboxes.length,
visibleCheckboxesCount = visibleCheckboxes.length; visibleCheckboxesCount = visibleCheckboxes.length;
visibleCheckboxes.prop('checked', true); visibleCheckboxes.prop('checked', true);
$("li:not(.divider):not(.disabled)", this.$ul).filter(":visible").addClass(this.options.selectedClass); $("li:not(.divider):not(.disabled)", this.$ul).filter(":visible").addClass(this.options.selectedClass);
if (allCheckboxesCount === visibleCheckboxesCount) { if (allCheckboxesCount === visibleCheckboxesCount) {
$("option:enabled", this.$select).prop('selected', true); $("option:enabled", this.$select).prop('selected', true);
} }
...@@ -839,25 +846,25 @@ ...@@ -839,25 +846,25 @@
/** /**
* Deselects all options. * Deselects all options.
* If justVisible is true or not specified, only visible options are deselected. * If justVisible is true or not specified, only visible options are deselected.
* *
* @param {Boolean} justVisible * @param {Boolean} justVisible
*/ */
deselectall: function (justVisible) { deselectall: function (justVisible) {
var allCheckboxes = $("li input[type='checkbox']:enabled", this.$ul), var allCheckboxes = $("li input[type='checkbox']:enabled", this.$ul),
justVisible = typeof justVisible === 'undefined' ? true : justVisible, justVisible = typeof justVisible === 'undefined' ? true : justVisible,
visibleCheckboxes = void(0); visibleCheckboxes = void(0);
if(justVisible) { if(justVisible) {
var values = void(0); var values = void(0);
visibleCheckboxes = allCheckboxes.filter(":visible"); visibleCheckboxes = allCheckboxes.filter(":visible");
visibleCheckboxes.prop('checked', false); visibleCheckboxes.prop('checked', false);
values = visibleCheckboxes.map(function() { return $(this).val() }).get(); values = visibleCheckboxes.map(function() { return $(this).val() }).get();
$("option:enabled", this.$select).filter(function(index){ return $.inArray($(this).val(), values) !== -1; }).prop('selected', false); $("option:enabled", this.$select).filter(function(index){ return $.inArray($(this).val(), values) !== -1; }).prop('selected', false);
$("li:not(.divider):not(.disabled)", this.$ul).filter(":visible").removeClass(this.options.selectedClass); $("li:not(.divider):not(.disabled)", this.$ul).filter(":visible").removeClass(this.options.selectedClass);
}else { }else {
allCheckboxes.prop('checked', false); allCheckboxes.prop('checked', false);
$("option:enabled", this.$select).prop('selected', false); $("option:enabled", this.$select).prop('selected', false);
...@@ -878,10 +885,10 @@ ...@@ -878,10 +885,10 @@
this.buildSelectAll(); this.buildSelectAll();
this.buildDropdownOptions(); this.buildDropdownOptions();
this.buildFilter(); this.buildFilter();
this.updateButtonText(); this.updateButtonText();
this.updateSelectAll(); this.updateSelectAll();
if (this.options.dropRight) { if (this.options.dropRight) {
this.$ul.addClass('pull-right'); this.$ul.addClass('pull-right');
} }
...@@ -889,7 +896,7 @@ ...@@ -889,7 +896,7 @@
/** /**
* The provided data will be used to build the dropdown. * The provided data will be used to build the dropdown.
* *
* @param {Array} dataprovider * @param {Array} dataprovider
*/ */
dataprovider: function(dataprovider) { dataprovider: function(dataprovider) {
...@@ -911,7 +918,7 @@ ...@@ -911,7 +918,7 @@
optionDOM += '<option value="' + option.value + '">' + (option.label || option.value) + '</option>'; optionDOM += '<option value="' + option.value + '">' + (option.label || option.value) + '</option>';
} }
}); });
this.$select.html(optionDOM); this.$select.html(optionDOM);
this.rebuild(); this.rebuild();
}, },
...@@ -936,7 +943,7 @@ ...@@ -936,7 +943,7 @@
/** /**
* Set the options. * Set the options.
* *
* @param {Array} options * @param {Array} options
*/ */
setOptions: function(options) { setOptions: function(options) {
...@@ -945,23 +952,23 @@ ...@@ -945,23 +952,23 @@
/** /**
* Merges the given options with the default options. * Merges the given options with the default options.
* *
* @param {Array} options * @param {Array} options
* @returns {Array} * @returns {Array}
*/ */
mergeOptions: function(options) { mergeOptions: function(options) {
return $.extend(true, {}, this.defaults, options); return $.extend(true, {}, this.defaults, options);
}, },
/** /**
* Checks whether a select all checkbox is present. * Checks whether a select all checkbox is present.
* *
* @returns {Boolean} * @returns {Boolean}
*/ */
hasSelectAll: function() { hasSelectAll: function() {
return $('li.' + this.options.selectAllValue, this.$ul).length > 0; return $('li.' + this.options.selectAllValue, this.$ul).length > 0;
}, },
/** /**
* Updates the select all checkbox based on the currently displayed and selected checkboxes. * Updates the select all checkbox based on the currently displayed and selected checkboxes.
*/ */
...@@ -972,7 +979,7 @@ ...@@ -972,7 +979,7 @@
checkedBoxesLength = allBoxes.filter(":checked").length, checkedBoxesLength = allBoxes.filter(":checked").length,
selectAllLi = $("li." + this.options.selectAllValue, this.$ul), selectAllLi = $("li." + this.options.selectAllValue, this.$ul),
selectAllInput = selectAllLi.find("input"); selectAllInput = selectAllLi.find("input");
if (checkedBoxesLength > 0 && checkedBoxesLength === allBoxesLength) { if (checkedBoxesLength > 0 && checkedBoxesLength === allBoxesLength) {
selectAllInput.prop("checked", true); selectAllInput.prop("checked", true);
selectAllLi.addClass(this.options.selectedClass); selectAllLi.addClass(this.options.selectedClass);
...@@ -983,16 +990,16 @@ ...@@ -983,16 +990,16 @@
} }
} }
}, },
/** /**
* Update the button text and its title based on the currently selected options. * Update the button text and its title based on the currently selected options.
*/ */
updateButtonText: function() { updateButtonText: function() {
var options = this.getSelected(); var options = this.getSelected();
// First update the displayed button text. // First update the displayed button text.
$('button', this.$container).html(this.options.buttonText(options, this.$select)); $('button', this.$container).html(this.options.buttonText(options, this.$select));
// Now update the title attribute of the button. // Now update the title attribute of the button.
$('button', this.$container).attr('title', this.options.buttonTitle(options, this.$select)); $('button', this.$container).attr('title', this.options.buttonTitle(options, this.$select));
...@@ -1000,7 +1007,7 @@ ...@@ -1000,7 +1007,7 @@
/** /**
* Get all selected options. * Get all selected options.
* *
* @returns {jQUery} * @returns {jQUery}
*/ */
getSelected: function() { getSelected: function() {
...@@ -1009,7 +1016,7 @@ ...@@ -1009,7 +1016,7 @@
/** /**
* Gets a select option by its value. * Gets a select option by its value.
* *
* @param {String} value * @param {String} value
* @returns {jQuery} * @returns {jQuery}
*/ */
...@@ -1028,7 +1035,7 @@ ...@@ -1028,7 +1035,7 @@
/** /**
* Get the input (radio/checkbox) by its value. * Get the input (radio/checkbox) by its value.
* *
* @param {String} value * @param {String} value
* @returns {jQuery} * @returns {jQuery}
*/ */
...@@ -1064,7 +1071,7 @@ ...@@ -1064,7 +1071,7 @@
return this.each(function() { return this.each(function() {
var data = $(this).data('multiselect'); var data = $(this).data('multiselect');
var options = typeof option === 'object' && option; var options = typeof option === 'object' && option;
// Initialize the multiselect. // Initialize the multiselect.
if (!data) { if (!data) {
data = new Multiselect(this, options); data = new Multiselect(this, options);
...@@ -1074,7 +1081,7 @@ ...@@ -1074,7 +1081,7 @@
// Call multiselect method. // Call multiselect method.
if (typeof option === 'string') { if (typeof option === 'string') {
data[option](parameter); data[option](parameter);
if (option === 'destroy') { if (option === 'destroy') {
$(this).data('multiselect', false); $(this).data('multiselect', false);
} }
...@@ -1088,4 +1095,4 @@ ...@@ -1088,4 +1095,4 @@
$("select[data-role=multiselect]").multiselect(); $("select[data-role=multiselect]").multiselect();
}); });
}(window.jQuery); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment