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 @@ + + +
+ +