1
0
mirror of synced 2025-02-09 16:49:24 +03:00

Clicking on a selected option triggers an unselect event

This commit is contained in:
Kevin Brown 2014-08-30 18:53:05 -04:00
parent 94b460f930
commit d57f8b2203
7 changed files with 235 additions and 25 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

52
dist/js/select2.js vendored
View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

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