diff --git a/dist/js/select2.amd.full.js b/dist/js/select2.amd.full.js index 6ba9e1c9..14cfebb7 100644 --- a/dist/js/select2.amd.full.js +++ b/dist/js/select2.amd.full.js @@ -449,6 +449,11 @@ define('select2/selection/single',[ var self = this; this.$selection.on('mousedown', function (evt) { + // Only respond to left clicks + if (evt.which !== 1) { + return; + } + self.trigger("toggle", { originalEvent: evt }); @@ -557,12 +562,74 @@ define('select2/selection/multiple',[ return MultipleSelection; }); +define('select2/data/array',[ + "./select", + "../utils" +], function (SelectAdapter, Utils) { + function ArrayAdapter ($element, options) { + this.data = options.options.data; + this.selection = []; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(ArrayAdapter, SelectAdapter); + + ArrayAdapter.prototype.select = function (data) { + var self = this; + + this.$element.find("option").each(function () { + var $option = $(this); + var option = self.item($option); + + if (option.id == data.id) { + $option.remove(); + } + }); + + var $option = this.option(data); + + this.$element.append($option); + + ArrayAdapter.__super__.select.call(this, data); + } + + ArrayAdapter.prototype.option = function (data) { + var $option = $(""); + + $option.text(data.text); + $option.val(data.id); + $option.data("data", data); + + return $option; + } + + ArrayAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + $.each(this.data, function () { + var option = this; + + if (self.matches(params, option)) { + matches.push(option); + } + }); + + callback(matches); + } + + return ArrayAdapter; +}); + define('select2/options',[ './data/select', './results', './dropdown', './selection/single', - './selection/multiple' + './selection/multiple', + + './data/array' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) { diff --git a/dist/js/select2.amd.js b/dist/js/select2.amd.js index 6ba9e1c9..14cfebb7 100644 --- a/dist/js/select2.amd.js +++ b/dist/js/select2.amd.js @@ -449,6 +449,11 @@ define('select2/selection/single',[ var self = this; this.$selection.on('mousedown', function (evt) { + // Only respond to left clicks + if (evt.which !== 1) { + return; + } + self.trigger("toggle", { originalEvent: evt }); @@ -557,12 +562,74 @@ define('select2/selection/multiple',[ return MultipleSelection; }); +define('select2/data/array',[ + "./select", + "../utils" +], function (SelectAdapter, Utils) { + function ArrayAdapter ($element, options) { + this.data = options.options.data; + this.selection = []; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(ArrayAdapter, SelectAdapter); + + ArrayAdapter.prototype.select = function (data) { + var self = this; + + this.$element.find("option").each(function () { + var $option = $(this); + var option = self.item($option); + + if (option.id == data.id) { + $option.remove(); + } + }); + + var $option = this.option(data); + + this.$element.append($option); + + ArrayAdapter.__super__.select.call(this, data); + } + + ArrayAdapter.prototype.option = function (data) { + var $option = $(""); + + $option.text(data.text); + $option.val(data.id); + $option.data("data", data); + + return $option; + } + + ArrayAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + $.each(this.data, function () { + var option = this; + + if (self.matches(params, option)) { + matches.push(option); + } + }); + + callback(matches); + } + + return ArrayAdapter; +}); + define('select2/options',[ './data/select', './results', './dropdown', './selection/single', - './selection/multiple' + './selection/multiple', + + './data/array' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) { diff --git a/dist/js/select2.full.js b/dist/js/select2.full.js index f3cba9c5..50aaf97b 100644 --- a/dist/js/select2.full.js +++ b/dist/js/select2.full.js @@ -9986,6 +9986,11 @@ define('select2/selection/single',[ var self = this; this.$selection.on('mousedown', function (evt) { + // Only respond to left clicks + if (evt.which !== 1) { + return; + } + self.trigger("toggle", { originalEvent: evt }); @@ -10094,12 +10099,74 @@ define('select2/selection/multiple',[ return MultipleSelection; }); +define('select2/data/array',[ + "./select", + "../utils" +], function (SelectAdapter, Utils) { + function ArrayAdapter ($element, options) { + this.data = options.options.data; + this.selection = []; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(ArrayAdapter, SelectAdapter); + + ArrayAdapter.prototype.select = function (data) { + var self = this; + + this.$element.find("option").each(function () { + var $option = $(this); + var option = self.item($option); + + if (option.id == data.id) { + $option.remove(); + } + }); + + var $option = this.option(data); + + this.$element.append($option); + + ArrayAdapter.__super__.select.call(this, data); + } + + ArrayAdapter.prototype.option = function (data) { + var $option = $(""); + + $option.text(data.text); + $option.val(data.id); + $option.data("data", data); + + return $option; + } + + ArrayAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + $.each(this.data, function () { + var option = this; + + if (self.matches(params, option)) { + matches.push(option); + } + }); + + callback(matches); + } + + return ArrayAdapter; +}); + define('select2/options',[ './data/select', './results', './dropdown', './selection/single', - './selection/multiple' + './selection/multiple', + + './data/array' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) { diff --git a/dist/js/select2.js b/dist/js/select2.js index 6281994c..334e6b68 100644 --- a/dist/js/select2.js +++ b/dist/js/select2.js @@ -877,6 +877,11 @@ define('select2/selection/single',[ var self = this; this.$selection.on('mousedown', function (evt) { + // Only respond to left clicks + if (evt.which !== 1) { + return; + } + self.trigger("toggle", { originalEvent: evt }); @@ -985,12 +990,74 @@ define('select2/selection/multiple',[ return MultipleSelection; }); +define('select2/data/array',[ + "./select", + "../utils" +], function (SelectAdapter, Utils) { + function ArrayAdapter ($element, options) { + this.data = options.options.data; + this.selection = []; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(ArrayAdapter, SelectAdapter); + + ArrayAdapter.prototype.select = function (data) { + var self = this; + + this.$element.find("option").each(function () { + var $option = $(this); + var option = self.item($option); + + if (option.id == data.id) { + $option.remove(); + } + }); + + var $option = this.option(data); + + this.$element.append($option); + + ArrayAdapter.__super__.select.call(this, data); + } + + ArrayAdapter.prototype.option = function (data) { + var $option = $(""); + + $option.text(data.text); + $option.val(data.id); + $option.data("data", data); + + return $option; + } + + ArrayAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + $.each(this.data, function () { + var option = this; + + if (self.matches(params, option)) { + matches.push(option); + } + }); + + callback(matches); + } + + return ArrayAdapter; +}); + define('select2/options',[ './data/select', './results', './dropdown', './selection/single', - './selection/multiple' + './selection/multiple', + + './data/array' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) { diff --git a/playground/basic/adapters.html b/playground/basic/adapters.html new file mode 100644 index 00000000..d7a1601e --- /dev/null +++ b/playground/basic/adapters.html @@ -0,0 +1,114 @@ + + + + + QUnit Example + + + + + + + + + + + +
+ + + +
+ + + + + + + diff --git a/playground/basic/decorators.html b/playground/basic/decorators.html index 8e6f3d24..a44b61ef 100644 --- a/playground/basic/decorators.html +++ b/playground/basic/decorators.html @@ -178,11 +178,11 @@ require(["select2/core", "select2/utils", "select2/selection/single", this.$container = $container; - this.$clear.on("click", function (evt) { + this.$clear.on("mousedown", function (evt) { evt.stopPropagation(); var $first = $container.find("li").first(); - $first.click(); + $first.mouseup(); }); }; diff --git a/src/js/select2/data/array.js b/src/js/select2/data/array.js new file mode 100644 index 00000000..5c7bf1c7 --- /dev/null +++ b/src/js/select2/data/array.js @@ -0,0 +1,59 @@ +define([ + "./select", + "../utils" +], function (SelectAdapter, Utils) { + function ArrayAdapter ($element, options) { + this.data = options.options.data; + this.selection = []; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(ArrayAdapter, SelectAdapter); + + ArrayAdapter.prototype.select = function (data) { + var self = this; + + this.$element.find("option").each(function () { + var $option = $(this); + var option = self.item($option); + + if (option.id == data.id) { + $option.remove(); + } + }); + + var $option = this.option(data); + + this.$element.append($option); + + ArrayAdapter.__super__.select.call(this, data); + } + + ArrayAdapter.prototype.option = function (data) { + var $option = $(""); + + $option.text(data.text); + $option.val(data.id); + $option.data("data", data); + + return $option; + } + + ArrayAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + $.each(this.data, function () { + var option = this; + + if (self.matches(params, option)) { + matches.push(option); + } + }); + + callback(matches); + } + + return ArrayAdapter; +}); diff --git a/src/js/select2/options.js b/src/js/select2/options.js index b0ce465d..0b25e462 100644 --- a/src/js/select2/options.js +++ b/src/js/select2/options.js @@ -3,7 +3,9 @@ define([ './results', './dropdown', './selection/single', - './selection/multiple' + './selection/multiple', + + './data/array' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) { diff --git a/src/js/select2/selection/single.js b/src/js/select2/selection/single.js index 5e77e7c3..7d853763 100644 --- a/src/js/select2/selection/single.js +++ b/src/js/select2/selection/single.js @@ -26,6 +26,11 @@ define([ var self = this; this.$selection.on('mousedown', function (evt) { + // Only respond to left clicks + if (evt.which !== 1) { + return; + } + self.trigger("toggle", { originalEvent: evt });