From d57f8b2203740f62a9f25992ded23c73e11e8f7b Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 30 Aug 2014 18:53:05 -0400 Subject: [PATCH] Clicking on a selected option triggers an unselect event --- dist/js/select2.amd.full.js | 52 +++++++++++++++++++++++++++++++---- dist/js/select2.amd.js | 52 +++++++++++++++++++++++++++++++---- dist/js/select2.full.js | 52 +++++++++++++++++++++++++++++++---- dist/js/select2.js | 52 +++++++++++++++++++++++++++++++---- src/js/select2/core.js | 6 ++++ src/js/select2/data/select.js | 31 ++++++++++++++++++--- src/js/select2/results.js | 15 +++++++++- 7 files changed, 235 insertions(+), 25 deletions(-) diff --git a/dist/js/select2.amd.full.js b/dist/js/select2.amd.full.js index b10bcbca..e3f05189 100644 --- a/dist/js/select2.amd.full.js +++ b/dist/js/select2.amd.full.js @@ -190,18 +190,41 @@ define('select2/data/select',[ 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) { - var current = self.current(function (data) { - // - }); - self.select(params.data); }); + + container.on("unselect", function (params) { + self.unselect(params.data); + }); } SelectAdapter.prototype.query = function (params, callback) { @@ -323,6 +346,7 @@ define('select2/results',[ this.on("results:all", function (data) { self.clear(); self.append(data); + self.setClasses(); }); @@ -333,7 +357,19 @@ define('select2/results',[ }) this.$results.on("click", ".option", function (evt) { - var data = $(this).data("data"); + var $this = $(this); + + var data = $this.data("data"); + if ($this.hasClass("selected")) { + self.trigger("unselected", { + originalEvent: evt, + data: data + }) + + self.setClasses(); + + return; + } self.trigger("selected", { originalEvent: evt, @@ -615,6 +651,12 @@ define('select2/core',[ $container.removeClass("open"); }); + this.results.on("unselected", function (params) { + self.trigger("unselect", params); + + $container.removeClass("open"); + }); + // Set the initial state this.data.current(function (initialData) { diff --git a/dist/js/select2.amd.js b/dist/js/select2.amd.js index b10bcbca..e3f05189 100644 --- a/dist/js/select2.amd.js +++ b/dist/js/select2.amd.js @@ -190,18 +190,41 @@ define('select2/data/select',[ 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) { - var current = self.current(function (data) { - // - }); - self.select(params.data); }); + + container.on("unselect", function (params) { + self.unselect(params.data); + }); } SelectAdapter.prototype.query = function (params, callback) { @@ -323,6 +346,7 @@ define('select2/results',[ this.on("results:all", function (data) { self.clear(); self.append(data); + self.setClasses(); }); @@ -333,7 +357,19 @@ define('select2/results',[ }) this.$results.on("click", ".option", function (evt) { - var data = $(this).data("data"); + var $this = $(this); + + var data = $this.data("data"); + if ($this.hasClass("selected")) { + self.trigger("unselected", { + originalEvent: evt, + data: data + }) + + self.setClasses(); + + return; + } self.trigger("selected", { originalEvent: evt, @@ -615,6 +651,12 @@ define('select2/core',[ $container.removeClass("open"); }); + this.results.on("unselected", function (params) { + self.trigger("unselect", params); + + $container.removeClass("open"); + }); + // Set the initial state this.data.current(function (initialData) { diff --git a/dist/js/select2.full.js b/dist/js/select2.full.js index 379515fe..b4d3a3b9 100644 --- a/dist/js/select2.full.js +++ b/dist/js/select2.full.js @@ -9727,18 +9727,41 @@ define('select2/data/select',[ 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) { - var current = self.current(function (data) { - // - }); - self.select(params.data); }); + + container.on("unselect", function (params) { + self.unselect(params.data); + }); } SelectAdapter.prototype.query = function (params, callback) { @@ -9860,6 +9883,7 @@ define('select2/results',[ this.on("results:all", function (data) { self.clear(); self.append(data); + self.setClasses(); }); @@ -9870,7 +9894,19 @@ define('select2/results',[ }) this.$results.on("click", ".option", function (evt) { - var data = $(this).data("data"); + var $this = $(this); + + var data = $this.data("data"); + if ($this.hasClass("selected")) { + self.trigger("unselected", { + originalEvent: evt, + data: data + }) + + self.setClasses(); + + return; + } self.trigger("selected", { originalEvent: evt, @@ -10152,6 +10188,12 @@ define('select2/core',[ $container.removeClass("open"); }); + this.results.on("unselected", function (params) { + self.trigger("unselect", params); + + $container.removeClass("open"); + }); + // Set the initial state this.data.current(function (initialData) { diff --git a/dist/js/select2.js b/dist/js/select2.js index 95c3adfa..dccc1154 100644 --- a/dist/js/select2.js +++ b/dist/js/select2.js @@ -618,18 +618,41 @@ define('select2/data/select',[ 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) { - var current = self.current(function (data) { - // - }); - self.select(params.data); }); + + container.on("unselect", function (params) { + self.unselect(params.data); + }); } SelectAdapter.prototype.query = function (params, callback) { @@ -751,6 +774,7 @@ define('select2/results',[ this.on("results:all", function (data) { self.clear(); self.append(data); + self.setClasses(); }); @@ -761,7 +785,19 @@ define('select2/results',[ }) this.$results.on("click", ".option", function (evt) { - var data = $(this).data("data"); + var $this = $(this); + + var data = $this.data("data"); + if ($this.hasClass("selected")) { + self.trigger("unselected", { + originalEvent: evt, + data: data + }) + + self.setClasses(); + + return; + } self.trigger("selected", { originalEvent: evt, @@ -1043,6 +1079,12 @@ define('select2/core',[ $container.removeClass("open"); }); + this.results.on("unselected", function (params) { + self.trigger("unselect", params); + + $container.removeClass("open"); + }); + // Set the initial state this.data.current(function (initialData) { diff --git a/src/js/select2/core.js b/src/js/select2/core.js index 792c086d..06ebd053 100644 --- a/src/js/select2/core.js +++ b/src/js/select2/core.js @@ -71,6 +71,12 @@ define([ $container.removeClass("open"); }); + this.results.on("unselected", function (params) { + self.trigger("unselect", params); + + $container.removeClass("open"); + }); + // Set the initial state this.data.current(function (initialData) { diff --git a/src/js/select2/data/select.js b/src/js/select2/data/select.js index 5ee72bee..04ceddba 100644 --- a/src/js/select2/data/select.js +++ b/src/js/select2/data/select.js @@ -52,18 +52,41 @@ define([ 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) { - var current = self.current(function (data) { - // - }); - self.select(params.data); }); + + container.on("unselect", function (params) { + self.unselect(params.data); + }); } SelectAdapter.prototype.query = function (params, callback) { diff --git a/src/js/select2/results.js b/src/js/select2/results.js index 1d274c03..bb0f5159 100644 --- a/src/js/select2/results.js +++ b/src/js/select2/results.js @@ -76,6 +76,7 @@ define([ this.on("results:all", function (data) { self.clear(); self.append(data); + self.setClasses(); }); @@ -86,7 +87,19 @@ define([ }) this.$results.on("click", ".option", function (evt) { - var data = $(this).data("data"); + var $this = $(this); + + var data = $this.data("data"); + if ($this.hasClass("selected")) { + self.trigger("unselected", { + originalEvent: evt, + data: data + }) + + self.setClasses(); + + return; + } self.trigger("selected", { originalEvent: evt,