From d386849c1f6321150181ffed3e53eef1a06eecbf Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Tue, 14 Oct 2014 21:12:57 -0400 Subject: [PATCH] Started building out the examples This also adds the `select2` function back to jQuery, so you can still initialize Select2 using the old syntax. --- Gruntfile.js | 36 ++-- dist/js/select2.amd.full.js | 364 +++++++++++++++++--------------- dist/js/select2.amd.js | 339 +++++++++++++++--------------- dist/js/select2.full.js | 367 ++++++++++++++++++--------------- dist/js/select2.full.min.js | 4 +- dist/js/select2.js | 367 ++++++++++++++++++--------------- dist/js/select2.min.js | 2 +- docs/_includes/head.html | 2 + docs/examples.html | 198 ++++++++++++++++-- docs/index.html | 2 +- docs/vendor/css/prettify.css | 30 +++ docs/vendor/js/prettify.min.js | 28 +++ src/js/end.js | 1 + src/js/jquery.select2.js | 24 +++ src/js/select2/data/array.js | 2 +- src/js/select2/options.js | 15 +- src/js/select2/results.js | 2 +- src/js/start.js | 0 18 files changed, 1080 insertions(+), 703 deletions(-) create mode 100644 docs/vendor/css/prettify.css create mode 100644 docs/vendor/js/prettify.min.js create mode 100644 src/js/end.js create mode 100644 src/js/jquery.select2.js create mode 100644 src/js/start.js diff --git a/Gruntfile.js b/Gruntfile.js index 8f48a5a9..cf77d96d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,20 +1,24 @@ module.exports = function (grunt) { // Full list of files that must be included by RequireJS - amd_includes = [ - "almond" - ] + includes = [ + 'jquery.select2' + ]; - full_includes = [ - "jquery" - ] + amdIncludes = [ + 'almond' + ]; + + fullIncludes = [ + 'jquery' + ].concat(includes); grunt.initConfig({ uglify: { - "dist": { + 'dist': { src: 'dist/js/select2.js', dest: 'dist/js/select2.min.js' }, - "dist.full": { + 'dist.full': { src: 'dist/js/select2.full.js', dest: 'dist/js/select2.full.min.js' } @@ -22,7 +26,7 @@ module.exports = function (grunt) { qunit: { all: [ - "tests/**/*.html" + 'tests/**/*.html' ] }, @@ -70,7 +74,7 @@ module.exports = function (grunt) { optimize: "none", name: "select2/core", out: "dist/js/select2.js", - include: amd_includes, + include: amdIncludes.concat(includes), paths: { almond: "../../vendor/almond-0.2.9", jquery: "jquery.shim" @@ -83,7 +87,7 @@ module.exports = function (grunt) { optimize: "none", name: "select2/core", out: "dist/js/select2.full.js", - include: amd_includes.concat(full_includes), + include: amdIncludes.concat(fullIncludes), paths: { almond: "../../vendor/almond-0.2.9", jquery: "../../vendor/jquery-2.1.0" @@ -107,7 +111,7 @@ module.exports = function (grunt) { optimize: "none", name: "select2/core", out: "dist/js/select2.amd.full.js", - include: full_includes, + include: fullIncludes, paths: { jquery: "empty:" } @@ -118,17 +122,17 @@ module.exports = function (grunt) { concat: { "dist": { src: [ - "src/coffee/start.js", + "src/js/start.js", "dist/js/select2.js", - "src/coffee/end.js" + "src/js/end.js" ], dest: "dist/js/select2.js" }, "dist.full": { src: [ - "src/coffee/start.js", + "src/js/start.js", "dist/js/select2.full.js", - "src/coffee/end.js" + "src/js/end.js" ], dest: "dist/js/select2.full.js" } diff --git a/dist/js/select2.amd.full.js b/dist/js/select2.amd.full.js index a5cd25d8..7fab65a9 100644 --- a/dist/js/select2.amd.full.js +++ b/dist/js/select2.amd.full.js @@ -137,166 +137,6 @@ define('select2/utils',[], function () { return Utils; }); -define('select2/data/base',[ - '../utils' -], function (Utils) { - function BaseAdapter ($element, options) { - BaseAdapter.__super__.constructor.call(this); - } - - Utils.Extend(BaseAdapter, Utils.Observable); - - BaseAdapter.prototype.current = function (callback) { - throw new Error('The `current` method must be defined in child classes.'); - }; - - BaseAdapter.prototype.query = function (params, callback) { - throw new Error('The `query` method must be defined in child classes.'); - }; - - return BaseAdapter; -}); - -define('select2/data/select',[ - './base', - '../utils', - 'jquery' -], function (BaseAdapter, Utils, $) { - function SelectAdapter ($element, options) { - this.$element = $element; - - SelectAdapter.__super__.constructor.call(this); - } - - Utils.Extend(SelectAdapter, BaseAdapter); - - SelectAdapter.prototype.current = function (callback) { - var data = []; - var self = this; - - this.$element.find(':selected').each(function () { - var $option = $(this); - - var option = self.item($option); - - data.push(option); - }); - - callback(data); - }; - - SelectAdapter.prototype.select = function (data) { - var self = this; - - if (this.$element.prop('multiple')) { - this.current(function (currentData) { - var val = []; - - data = [data]; - data.push.apply(data, currentData); - - for (var d = 0; d < data.length; d++) { - id = data[d].id; - - if (val.indexOf(id) === -1) { - val.push(id); - } - } - - self.$element.val(val); - self.$element.trigger('change'); - }); - } else { - var val = data.id; - - this.$element.val(val); - this.$element.trigger('change'); - } - }; - - SelectAdapter.prototype.unselect = function (data) { - var self = this; - - if (!this.$element.prop('multiple')) { - return; - } - - this.current(function (currentData) { - var val = []; - - for (var d = 0; d < currentData.length; d++) { - id = currentData[d].id; - - if (id !== data.id && val.indexOf(id) === -1) { - val.push(id); - } - } - - self.$element.val(val); - self.$element.trigger('change'); - }); - }; - - SelectAdapter.prototype.bind = function (container, $container) { - var self = this; - - container.on('select', function (params) { - self.select(params.data); - }); - - container.on('unselect', function (params) { - self.unselect(params.data); - }); - }; - - SelectAdapter.prototype.query = function (params, callback) { - var data = []; - var self = this; - - this.$element.find('option').each(function () { - var $option = $(this); - - var option = self.item($option); - - if (self.matches(params, option)) { - data.push(option); - } - }); - - callback(data); - }; - - SelectAdapter.prototype.item = function ($option) { - var data = $option.data('data'); - - // If the data has already be generated, use it - if (data == null) { - data = { - id: $option.val(), - text: $option.html() - }; - - $option.data('data', data); - } - - return data; - }; - - SelectAdapter.prototype.matches = function (params, data) { - if ($.trim(params.term) === '') { - return true; - } - - if (data.text.indexOf(params.term) > -1) { - return true; - } - - return false; - }; - - return SelectAdapter; -}); - define('select2/results',[ './utils' ], function (Utils) { @@ -351,7 +191,7 @@ define('select2/results',[ var $option = $(this); var item = $option.data('data'); - if (selected.indexOf(item.id) > -1) { + if (selected.indexOf(item.id.toString()) > -1) { $option.addClass('selected'); } }); @@ -584,6 +424,166 @@ define('select2/selection/multiple',[ return MultipleSelection; }); +define('select2/data/base',[ + '../utils' +], function (Utils) { + function BaseAdapter ($element, options) { + BaseAdapter.__super__.constructor.call(this); + } + + Utils.Extend(BaseAdapter, Utils.Observable); + + BaseAdapter.prototype.current = function (callback) { + throw new Error('The `current` method must be defined in child classes.'); + }; + + BaseAdapter.prototype.query = function (params, callback) { + throw new Error('The `query` method must be defined in child classes.'); + }; + + return BaseAdapter; +}); + +define('select2/data/select',[ + './base', + '../utils', + 'jquery' +], function (BaseAdapter, Utils, $) { + function SelectAdapter ($element, options) { + this.$element = $element; + + SelectAdapter.__super__.constructor.call(this); + } + + Utils.Extend(SelectAdapter, BaseAdapter); + + SelectAdapter.prototype.current = function (callback) { + var data = []; + var self = this; + + this.$element.find(':selected').each(function () { + var $option = $(this); + + var option = self.item($option); + + data.push(option); + }); + + callback(data); + }; + + SelectAdapter.prototype.select = function (data) { + var self = this; + + if (this.$element.prop('multiple')) { + this.current(function (currentData) { + var val = []; + + data = [data]; + data.push.apply(data, currentData); + + for (var d = 0; d < data.length; d++) { + id = data[d].id; + + if (val.indexOf(id) === -1) { + val.push(id); + } + } + + self.$element.val(val); + self.$element.trigger('change'); + }); + } else { + var val = data.id; + + this.$element.val(val); + this.$element.trigger('change'); + } + }; + + SelectAdapter.prototype.unselect = function (data) { + var self = this; + + if (!this.$element.prop('multiple')) { + return; + } + + this.current(function (currentData) { + var val = []; + + for (var d = 0; d < currentData.length; d++) { + id = currentData[d].id; + + if (id !== data.id && val.indexOf(id) === -1) { + val.push(id); + } + } + + self.$element.val(val); + self.$element.trigger('change'); + }); + }; + + SelectAdapter.prototype.bind = function (container, $container) { + var self = this; + + container.on('select', function (params) { + self.select(params.data); + }); + + container.on('unselect', function (params) { + self.unselect(params.data); + }); + }; + + SelectAdapter.prototype.query = function (params, callback) { + var data = []; + var self = this; + + this.$element.find('option').each(function () { + var $option = $(this); + + var option = self.item($option); + + if (self.matches(params, option)) { + data.push(option); + } + }); + + callback(data); + }; + + SelectAdapter.prototype.item = function ($option) { + var data = $option.data('data'); + + // If the data has already be generated, use it + if (data == null) { + data = { + id: $option.val(), + text: $option.html() + }; + + $option.data('data', data); + } + + return data; + }; + + SelectAdapter.prototype.matches = function (params, data) { + if ($.trim(params.term) === '') { + return true; + } + + if (data.text.indexOf(params.term) > -1) { + return true; + } + + return false; + }; + + return SelectAdapter; +}); + define('select2/data/array',[ './select', '../utils' @@ -603,7 +603,7 @@ define('select2/data/array',[ var $option = $(this); var option = self.item($option); - if (option.id == data.id) { + if (option.id == data.id.toString()) { $option.remove(); } }); @@ -690,7 +690,6 @@ define('select2/data/ajax',[ }); define('select2/options',[ - './data/select', './results', './dropdown', @@ -698,14 +697,22 @@ define('select2/options',[ './selection/single', './selection/multiple', + './data/select', './data/array', './data/ajax' -], function (SelectData, ResultsList, Dropdown, SingleSelection, - MultipleSelection) { +], function (ResultsList, Dropdown, SingleSelection, MultipleSelection, + SelectData, ArrayData, AjaxData) { function Options (options) { this.options = options; - this.dataAdapter = options.dataAdapter || SelectData; + if (options.ajax) { + this.dataAdapter = this.dataAdapter || AjaxData; + } else if (options.data) { + this.dataAdapter = this.dataAdapter || ArrayData; + } else { + this.dataAdapter = this.dataAdapter || SelectData; + } + this.resultsAdapter = ResultsList; this.dropdownAdapter = options.dropdownAdapter || Dropdown; this.selectionAdapter = options.selectionAdapter; @@ -852,3 +859,28 @@ define('select2/core',[ return Select2; }); +define('jquery.select2',[ + 'jquery', + 'select2/core' +], function ($, Select2) { + if ($.fn.select2 == null) { + $.fn.select2 = function (options) { + options = options || {}; + + if (typeof options === 'object') { + this.each(function () { + var instance = new Select2($(this), options); + }); + } else if (typeof options === 'string') { + var instance = this.data('select2'); + + instance[options](arguments.slice(1)); + } else { + throw new Error('Invalid arguments for Select2: ' + options); + } + }; + } + + return Select2; +}); + diff --git a/dist/js/select2.amd.js b/dist/js/select2.amd.js index a5cd25d8..d198758a 100644 --- a/dist/js/select2.amd.js +++ b/dist/js/select2.amd.js @@ -137,166 +137,6 @@ define('select2/utils',[], function () { return Utils; }); -define('select2/data/base',[ - '../utils' -], function (Utils) { - function BaseAdapter ($element, options) { - BaseAdapter.__super__.constructor.call(this); - } - - Utils.Extend(BaseAdapter, Utils.Observable); - - BaseAdapter.prototype.current = function (callback) { - throw new Error('The `current` method must be defined in child classes.'); - }; - - BaseAdapter.prototype.query = function (params, callback) { - throw new Error('The `query` method must be defined in child classes.'); - }; - - return BaseAdapter; -}); - -define('select2/data/select',[ - './base', - '../utils', - 'jquery' -], function (BaseAdapter, Utils, $) { - function SelectAdapter ($element, options) { - this.$element = $element; - - SelectAdapter.__super__.constructor.call(this); - } - - Utils.Extend(SelectAdapter, BaseAdapter); - - SelectAdapter.prototype.current = function (callback) { - var data = []; - var self = this; - - this.$element.find(':selected').each(function () { - var $option = $(this); - - var option = self.item($option); - - data.push(option); - }); - - callback(data); - }; - - SelectAdapter.prototype.select = function (data) { - var self = this; - - if (this.$element.prop('multiple')) { - this.current(function (currentData) { - var val = []; - - data = [data]; - data.push.apply(data, currentData); - - for (var d = 0; d < data.length; d++) { - id = data[d].id; - - if (val.indexOf(id) === -1) { - val.push(id); - } - } - - self.$element.val(val); - self.$element.trigger('change'); - }); - } else { - var val = data.id; - - this.$element.val(val); - this.$element.trigger('change'); - } - }; - - SelectAdapter.prototype.unselect = function (data) { - var self = this; - - if (!this.$element.prop('multiple')) { - return; - } - - this.current(function (currentData) { - var val = []; - - for (var d = 0; d < currentData.length; d++) { - id = currentData[d].id; - - if (id !== data.id && val.indexOf(id) === -1) { - val.push(id); - } - } - - self.$element.val(val); - self.$element.trigger('change'); - }); - }; - - SelectAdapter.prototype.bind = function (container, $container) { - var self = this; - - container.on('select', function (params) { - self.select(params.data); - }); - - container.on('unselect', function (params) { - self.unselect(params.data); - }); - }; - - SelectAdapter.prototype.query = function (params, callback) { - var data = []; - var self = this; - - this.$element.find('option').each(function () { - var $option = $(this); - - var option = self.item($option); - - if (self.matches(params, option)) { - data.push(option); - } - }); - - callback(data); - }; - - SelectAdapter.prototype.item = function ($option) { - var data = $option.data('data'); - - // If the data has already be generated, use it - if (data == null) { - data = { - id: $option.val(), - text: $option.html() - }; - - $option.data('data', data); - } - - return data; - }; - - SelectAdapter.prototype.matches = function (params, data) { - if ($.trim(params.term) === '') { - return true; - } - - if (data.text.indexOf(params.term) > -1) { - return true; - } - - return false; - }; - - return SelectAdapter; -}); - define('select2/results',[ './utils' ], function (Utils) { @@ -351,7 +191,7 @@ define('select2/results',[ var $option = $(this); var item = $option.data('data'); - if (selected.indexOf(item.id) > -1) { + if (selected.indexOf(item.id.toString()) > -1) { $option.addClass('selected'); } }); @@ -584,6 +424,166 @@ define('select2/selection/multiple',[ return MultipleSelection; }); +define('select2/data/base',[ + '../utils' +], function (Utils) { + function BaseAdapter ($element, options) { + BaseAdapter.__super__.constructor.call(this); + } + + Utils.Extend(BaseAdapter, Utils.Observable); + + BaseAdapter.prototype.current = function (callback) { + throw new Error('The `current` method must be defined in child classes.'); + }; + + BaseAdapter.prototype.query = function (params, callback) { + throw new Error('The `query` method must be defined in child classes.'); + }; + + return BaseAdapter; +}); + +define('select2/data/select',[ + './base', + '../utils', + 'jquery' +], function (BaseAdapter, Utils, $) { + function SelectAdapter ($element, options) { + this.$element = $element; + + SelectAdapter.__super__.constructor.call(this); + } + + Utils.Extend(SelectAdapter, BaseAdapter); + + SelectAdapter.prototype.current = function (callback) { + var data = []; + var self = this; + + this.$element.find(':selected').each(function () { + var $option = $(this); + + var option = self.item($option); + + data.push(option); + }); + + callback(data); + }; + + SelectAdapter.prototype.select = function (data) { + var self = this; + + if (this.$element.prop('multiple')) { + this.current(function (currentData) { + var val = []; + + data = [data]; + data.push.apply(data, currentData); + + for (var d = 0; d < data.length; d++) { + id = data[d].id; + + if (val.indexOf(id) === -1) { + val.push(id); + } + } + + self.$element.val(val); + self.$element.trigger('change'); + }); + } else { + var val = data.id; + + this.$element.val(val); + this.$element.trigger('change'); + } + }; + + SelectAdapter.prototype.unselect = function (data) { + var self = this; + + if (!this.$element.prop('multiple')) { + return; + } + + this.current(function (currentData) { + var val = []; + + for (var d = 0; d < currentData.length; d++) { + id = currentData[d].id; + + if (id !== data.id && val.indexOf(id) === -1) { + val.push(id); + } + } + + self.$element.val(val); + self.$element.trigger('change'); + }); + }; + + SelectAdapter.prototype.bind = function (container, $container) { + var self = this; + + container.on('select', function (params) { + self.select(params.data); + }); + + container.on('unselect', function (params) { + self.unselect(params.data); + }); + }; + + SelectAdapter.prototype.query = function (params, callback) { + var data = []; + var self = this; + + this.$element.find('option').each(function () { + var $option = $(this); + + var option = self.item($option); + + if (self.matches(params, option)) { + data.push(option); + } + }); + + callback(data); + }; + + SelectAdapter.prototype.item = function ($option) { + var data = $option.data('data'); + + // If the data has already be generated, use it + if (data == null) { + data = { + id: $option.val(), + text: $option.html() + }; + + $option.data('data', data); + } + + return data; + }; + + SelectAdapter.prototype.matches = function (params, data) { + if ($.trim(params.term) === '') { + return true; + } + + if (data.text.indexOf(params.term) > -1) { + return true; + } + + return false; + }; + + return SelectAdapter; +}); + define('select2/data/array',[ './select', '../utils' @@ -603,7 +603,7 @@ define('select2/data/array',[ var $option = $(this); var option = self.item($option); - if (option.id == data.id) { + if (option.id == data.id.toString()) { $option.remove(); } }); @@ -690,7 +690,6 @@ define('select2/data/ajax',[ }); define('select2/options',[ - './data/select', './results', './dropdown', @@ -698,14 +697,22 @@ define('select2/options',[ './selection/single', './selection/multiple', + './data/select', './data/array', './data/ajax' -], function (SelectData, ResultsList, Dropdown, SingleSelection, - MultipleSelection) { +], function (ResultsList, Dropdown, SingleSelection, MultipleSelection, + SelectData, ArrayData, AjaxData) { function Options (options) { this.options = options; - this.dataAdapter = options.dataAdapter || SelectData; + if (options.ajax) { + this.dataAdapter = this.dataAdapter || AjaxData; + } else if (options.data) { + this.dataAdapter = this.dataAdapter || ArrayData; + } else { + this.dataAdapter = this.dataAdapter || SelectData; + } + this.resultsAdapter = ResultsList; this.dropdownAdapter = options.dropdownAdapter || Dropdown; this.selectionAdapter = options.selectionAdapter; diff --git a/dist/js/select2.full.js b/dist/js/select2.full.js index 0d529ce2..87fe8b56 100644 --- a/dist/js/select2.full.js +++ b/dist/js/select2.full.js @@ -1,3 +1,4 @@ + /** * @license almond 0.2.9 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved. * Available via the MIT or new BSD license. @@ -9674,166 +9675,6 @@ define('select2/utils',[], function () { return Utils; }); -define('select2/data/base',[ - '../utils' -], function (Utils) { - function BaseAdapter ($element, options) { - BaseAdapter.__super__.constructor.call(this); - } - - Utils.Extend(BaseAdapter, Utils.Observable); - - BaseAdapter.prototype.current = function (callback) { - throw new Error('The `current` method must be defined in child classes.'); - }; - - BaseAdapter.prototype.query = function (params, callback) { - throw new Error('The `query` method must be defined in child classes.'); - }; - - return BaseAdapter; -}); - -define('select2/data/select',[ - './base', - '../utils', - 'jquery' -], function (BaseAdapter, Utils, $) { - function SelectAdapter ($element, options) { - this.$element = $element; - - SelectAdapter.__super__.constructor.call(this); - } - - Utils.Extend(SelectAdapter, BaseAdapter); - - SelectAdapter.prototype.current = function (callback) { - var data = []; - var self = this; - - this.$element.find(':selected').each(function () { - var $option = $(this); - - var option = self.item($option); - - data.push(option); - }); - - callback(data); - }; - - SelectAdapter.prototype.select = function (data) { - var self = this; - - if (this.$element.prop('multiple')) { - this.current(function (currentData) { - var val = []; - - data = [data]; - data.push.apply(data, currentData); - - for (var d = 0; d < data.length; d++) { - id = data[d].id; - - if (val.indexOf(id) === -1) { - val.push(id); - } - } - - self.$element.val(val); - self.$element.trigger('change'); - }); - } else { - var val = data.id; - - this.$element.val(val); - this.$element.trigger('change'); - } - }; - - SelectAdapter.prototype.unselect = function (data) { - var self = this; - - if (!this.$element.prop('multiple')) { - return; - } - - this.current(function (currentData) { - var val = []; - - for (var d = 0; d < currentData.length; d++) { - id = currentData[d].id; - - if (id !== data.id && val.indexOf(id) === -1) { - val.push(id); - } - } - - self.$element.val(val); - self.$element.trigger('change'); - }); - }; - - SelectAdapter.prototype.bind = function (container, $container) { - var self = this; - - container.on('select', function (params) { - self.select(params.data); - }); - - container.on('unselect', function (params) { - self.unselect(params.data); - }); - }; - - SelectAdapter.prototype.query = function (params, callback) { - var data = []; - var self = this; - - this.$element.find('option').each(function () { - var $option = $(this); - - var option = self.item($option); - - if (self.matches(params, option)) { - data.push(option); - } - }); - - callback(data); - }; - - SelectAdapter.prototype.item = function ($option) { - var data = $option.data('data'); - - // If the data has already be generated, use it - if (data == null) { - data = { - id: $option.val(), - text: $option.html() - }; - - $option.data('data', data); - } - - return data; - }; - - SelectAdapter.prototype.matches = function (params, data) { - if ($.trim(params.term) === '') { - return true; - } - - if (data.text.indexOf(params.term) > -1) { - return true; - } - - return false; - }; - - return SelectAdapter; -}); - define('select2/results',[ './utils' ], function (Utils) { @@ -9888,7 +9729,7 @@ define('select2/results',[ var $option = $(this); var item = $option.data('data'); - if (selected.indexOf(item.id) > -1) { + if (selected.indexOf(item.id.toString()) > -1) { $option.addClass('selected'); } }); @@ -10121,6 +9962,166 @@ define('select2/selection/multiple',[ return MultipleSelection; }); +define('select2/data/base',[ + '../utils' +], function (Utils) { + function BaseAdapter ($element, options) { + BaseAdapter.__super__.constructor.call(this); + } + + Utils.Extend(BaseAdapter, Utils.Observable); + + BaseAdapter.prototype.current = function (callback) { + throw new Error('The `current` method must be defined in child classes.'); + }; + + BaseAdapter.prototype.query = function (params, callback) { + throw new Error('The `query` method must be defined in child classes.'); + }; + + return BaseAdapter; +}); + +define('select2/data/select',[ + './base', + '../utils', + 'jquery' +], function (BaseAdapter, Utils, $) { + function SelectAdapter ($element, options) { + this.$element = $element; + + SelectAdapter.__super__.constructor.call(this); + } + + Utils.Extend(SelectAdapter, BaseAdapter); + + SelectAdapter.prototype.current = function (callback) { + var data = []; + var self = this; + + this.$element.find(':selected').each(function () { + var $option = $(this); + + var option = self.item($option); + + data.push(option); + }); + + callback(data); + }; + + SelectAdapter.prototype.select = function (data) { + var self = this; + + if (this.$element.prop('multiple')) { + this.current(function (currentData) { + var val = []; + + data = [data]; + data.push.apply(data, currentData); + + for (var d = 0; d < data.length; d++) { + id = data[d].id; + + if (val.indexOf(id) === -1) { + val.push(id); + } + } + + self.$element.val(val); + self.$element.trigger('change'); + }); + } else { + var val = data.id; + + this.$element.val(val); + this.$element.trigger('change'); + } + }; + + SelectAdapter.prototype.unselect = function (data) { + var self = this; + + if (!this.$element.prop('multiple')) { + return; + } + + this.current(function (currentData) { + var val = []; + + for (var d = 0; d < currentData.length; d++) { + id = currentData[d].id; + + if (id !== data.id && val.indexOf(id) === -1) { + val.push(id); + } + } + + self.$element.val(val); + self.$element.trigger('change'); + }); + }; + + SelectAdapter.prototype.bind = function (container, $container) { + var self = this; + + container.on('select', function (params) { + self.select(params.data); + }); + + container.on('unselect', function (params) { + self.unselect(params.data); + }); + }; + + SelectAdapter.prototype.query = function (params, callback) { + var data = []; + var self = this; + + this.$element.find('option').each(function () { + var $option = $(this); + + var option = self.item($option); + + if (self.matches(params, option)) { + data.push(option); + } + }); + + callback(data); + }; + + SelectAdapter.prototype.item = function ($option) { + var data = $option.data('data'); + + // If the data has already be generated, use it + if (data == null) { + data = { + id: $option.val(), + text: $option.html() + }; + + $option.data('data', data); + } + + return data; + }; + + SelectAdapter.prototype.matches = function (params, data) { + if ($.trim(params.term) === '') { + return true; + } + + if (data.text.indexOf(params.term) > -1) { + return true; + } + + return false; + }; + + return SelectAdapter; +}); + define('select2/data/array',[ './select', '../utils' @@ -10140,7 +10141,7 @@ define('select2/data/array',[ var $option = $(this); var option = self.item($option); - if (option.id == data.id) { + if (option.id == data.id.toString()) { $option.remove(); } }); @@ -10227,7 +10228,6 @@ define('select2/data/ajax',[ }); define('select2/options',[ - './data/select', './results', './dropdown', @@ -10235,14 +10235,22 @@ define('select2/options',[ './selection/single', './selection/multiple', + './data/select', './data/array', './data/ajax' -], function (SelectData, ResultsList, Dropdown, SingleSelection, - MultipleSelection) { +], function (ResultsList, Dropdown, SingleSelection, MultipleSelection, + SelectData, ArrayData, AjaxData) { function Options (options) { this.options = options; - this.dataAdapter = options.dataAdapter || SelectData; + if (options.ajax) { + this.dataAdapter = this.dataAdapter || AjaxData; + } else if (options.data) { + this.dataAdapter = this.dataAdapter || ArrayData; + } else { + this.dataAdapter = this.dataAdapter || SelectData; + } + this.resultsAdapter = ResultsList; this.dropdownAdapter = options.dropdownAdapter || Dropdown; this.selectionAdapter = options.selectionAdapter; @@ -10389,3 +10397,30 @@ define('select2/core',[ return Select2; }); +define('jquery.select2',[ + 'jquery', + 'select2/core' +], function ($, Select2) { + if ($.fn.select2 == null) { + $.fn.select2 = function (options) { + options = options || {}; + + if (typeof options === 'object') { + this.each(function () { + var instance = new Select2($(this), options); + }); + } else if (typeof options === 'string') { + var instance = this.data('select2'); + + instance[options](arguments.slice(1)); + } else { + throw new Error('Invalid arguments for Select2: ' + options); + } + }; + } + + return Select2; +}); + + +require('jquery.select2'); diff --git a/dist/js/select2.full.min.js b/dist/js/select2.full.min.js index dca13d4e..dc987a58 100644 --- a/dist/js/select2.full.min.js +++ b/dist/js/select2.full.min.js @@ -1,4 +1,4 @@ var requirejs,require,define;!function(a){function b(a,b){return r.call(a,b)}function c(a,b){var c,d,e,f,g,h,i,j,k,l,m,n=b&&b.split("/"),o=p.map,q=o&&o["*"]||{};if(a&&"."===a.charAt(0))if(b){for(n=n.slice(0,n.length-1),a=a.split("/"),g=a.length-1,p.nodeIdCompat&&t.test(a[g])&&(a[g]=a[g].replace(t,"")),a=n.concat(a),k=0;k0&&(a.splice(k-1,2),k-=2)}a=a.join("/")}else 0===a.indexOf("./")&&(a=a.substring(2));if((n||q)&&o){for(c=a.split("/"),k=c.length;k>0;k-=1){if(d=c.slice(0,k).join("/"),n)for(l=n.length;l>0;l-=1)if(e=o[n.slice(0,l).join("/")],e&&(e=e[d])){f=e,h=k;break}if(f)break;!i&&q&&q[d]&&(i=q[d],j=k)}!f&&i&&(f=i,h=j),f&&(c.splice(0,h,f),a=c.join("/"))}return a}function d(b,c){return function(){return k.apply(a,s.call(arguments,0).concat([b,c]))}}function e(a){return function(b){return c(b,a)}}function f(a){return function(b){n[a]=b}}function g(c){if(b(o,c)){var d=o[c];delete o[c],q[c]=!0,j.apply(a,d)}if(!b(n,c)&&!b(q,c))throw new Error("No "+c);return n[c]}function h(a){var b,c=a?a.indexOf("!"):-1;return c>-1&&(b=a.substring(0,c),a=a.substring(c+1,a.length)),[b,a]}function i(a){return function(){return p&&p.config&&p.config[a]||{}}}var j,k,l,m,n={},o={},p={},q={},r=Object.prototype.hasOwnProperty,s=[].slice,t=/\.js$/;l=function(a,b){var d,f=h(a),i=f[0];return a=f[1],i&&(i=c(i,b),d=g(i)),i?a=d&&d.normalize?d.normalize(a,e(b)):c(a,b):(a=c(a,b),f=h(a),i=f[0],a=f[1],i&&(d=g(i))),{f:i?i+"!"+a:a,n:a,pr:i,p:d}},m={require:function(a){return d(a)},exports:function(a){var b=n[a];return"undefined"!=typeof b?b:n[a]={}},module:function(a){return{id:a,uri:"",exports:n[a],config:i(a)}}},j=function(c,e,h,i){var j,k,p,r,s,t,u=[],v=typeof h;if(i=i||c,"undefined"===v||"function"===v){for(e=!e.length&&h.length?["require","exports","module"]:e,s=0;s0&&b-1 in a}function d(a,b,c){if(ab.isFunction(b))return ab.grep(a,function(a,d){return!!b.call(a,d,a)!==c});if(b.nodeType)return ab.grep(a,function(a){return a===b!==c});if("string"==typeof b){if(hb.test(b))return ab.filter(b,a,c);b=ab.filter(b,a)}return ab.grep(a,function(a){return U.call(b,a)>=0!==c})}function e(a,b){for(;(a=a[b])&&1!==a.nodeType;);return a}function f(a){var b=ob[a]={};return ab.each(a.match(nb)||[],function(a,c){b[c]=!0}),b}function g(){$.removeEventListener("DOMContentLoaded",g,!1),a.removeEventListener("load",g,!1),ab.ready()}function h(){Object.defineProperty(this.cache={},0,{get:function(){return{}}}),this.expando=ab.expando+Math.random()}function i(a,b,c){var d;if(void 0===c&&1===a.nodeType)if(d="data-"+b.replace(ub,"-$1").toLowerCase(),c=a.getAttribute(d),"string"==typeof c){try{c="true"===c?!0:"false"===c?!1:"null"===c?null:+c+""===c?+c:tb.test(c)?ab.parseJSON(c):c}catch(e){}sb.set(a,b,c)}else c=void 0;return c}function j(){return!0}function k(){return!1}function l(){try{return $.activeElement}catch(a){}}function m(a,b){return ab.nodeName(a,"table")&&ab.nodeName(11!==b.nodeType?b:b.firstChild,"tr")?a.getElementsByTagName("tbody")[0]||a.appendChild(a.ownerDocument.createElement("tbody")):a}function n(a){return a.type=(null!==a.getAttribute("type"))+"/"+a.type,a}function o(a){var b=Kb.exec(a.type);return b?a.type=b[1]:a.removeAttribute("type"),a}function p(a,b){for(var c=0,d=a.length;d>c;c++)rb.set(a[c],"globalEval",!b||rb.get(b[c],"globalEval"))}function q(a,b){var c,d,e,f,g,h,i,j;if(1===b.nodeType){if(rb.hasData(a)&&(f=rb.access(a),g=rb.set(b,f),j=f.events)){delete g.handle,g.events={};for(e in j)for(c=0,d=j[e].length;d>c;c++)ab.event.add(b,e,j[e][c])}sb.hasData(a)&&(h=sb.access(a),i=ab.extend({},h),sb.set(b,i))}}function r(a,b){var c=a.getElementsByTagName?a.getElementsByTagName(b||"*"):a.querySelectorAll?a.querySelectorAll(b||"*"):[];return void 0===b||b&&ab.nodeName(a,b)?ab.merge([a],c):c}function s(a,b){var c=b.nodeName.toLowerCase();"input"===c&&yb.test(a.type)?b.checked=a.checked:("input"===c||"textarea"===c)&&(b.defaultValue=a.defaultValue)}function t(b,c){var d=ab(c.createElement(b)).appendTo(c.body),e=a.getDefaultComputedStyle?a.getDefaultComputedStyle(d[0]).display:ab.css(d[0],"display");return d.detach(),e}function u(a){var b=$,c=Ob[a];return c||(c=t(a,b),"none"!==c&&c||(Nb=(Nb||ab("