1
0
mirror of synced 2024-11-26 06:46:04 +03:00

Better support for tagging

This removes the special case for `<select>` elements and instead
handles everything tagging related within the `Tags` decorator.
This commit is contained in:
Kevin Brown 2014-11-01 17:49:53 -04:00
parent b87a4a55e4
commit 8bdd83c44f
8 changed files with 107 additions and 57 deletions

View File

@ -955,16 +955,6 @@ define('select2/data/select',[
SelectAdapter.prototype.select = function (data) { SelectAdapter.prototype.select = function (data) {
var self = this; var self = this;
// Create items marked as tags
if (data._tag === true) {
// Clear the tag flag from it
delete data._tag;
// Create and add the option
var $option = this.option(data);
this.$element.append($option);
}
if (this.$element.prop('multiple')) { if (this.$element.prop('multiple')) {
this.current(function (currentData) { this.current(function (currentData) {
var val = []; var val = [];
@ -1257,6 +1247,8 @@ define('select2/data/tags',[
Tags.prototype.query = function (decorated, params, callback) { Tags.prototype.query = function (decorated, params, callback) {
var self = this; var self = this;
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) { if (params.term == null || params.term === '' || params.page != null) {
decorated.call(this, params, callback); decorated.call(this, params, callback);
return; return;
@ -1288,7 +1280,11 @@ define('select2/data/tags',[
} }
var tag = self.createTag(params); var tag = self.createTag(params);
tag._tag = true;
var $option = self.option(tag);
$option.attr('data-select2-tag', true);
self.$element.append($option);
data.unshift(tag); data.unshift(tag);
@ -1305,6 +1301,20 @@ define('select2/data/tags',[
}; };
}; };
Tags.prototype._removeOldTags = function (_) {
var tag = this._lastTag;
var $options = this.$element.find('option[data-select2-tag]');
$options.each(function () {
if (this.selected) {
return;
}
$(this).remove();
});
};
return Tags; return Tags;
}); });

View File

@ -955,16 +955,6 @@ define('select2/data/select',[
SelectAdapter.prototype.select = function (data) { SelectAdapter.prototype.select = function (data) {
var self = this; var self = this;
// Create items marked as tags
if (data._tag === true) {
// Clear the tag flag from it
delete data._tag;
// Create and add the option
var $option = this.option(data);
this.$element.append($option);
}
if (this.$element.prop('multiple')) { if (this.$element.prop('multiple')) {
this.current(function (currentData) { this.current(function (currentData) {
var val = []; var val = [];
@ -1257,6 +1247,8 @@ define('select2/data/tags',[
Tags.prototype.query = function (decorated, params, callback) { Tags.prototype.query = function (decorated, params, callback) {
var self = this; var self = this;
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) { if (params.term == null || params.term === '' || params.page != null) {
decorated.call(this, params, callback); decorated.call(this, params, callback);
return; return;
@ -1288,7 +1280,11 @@ define('select2/data/tags',[
} }
var tag = self.createTag(params); var tag = self.createTag(params);
tag._tag = true;
var $option = self.option(tag);
$option.attr('data-select2-tag', true);
self.$element.append($option);
data.unshift(tag); data.unshift(tag);
@ -1305,6 +1301,20 @@ define('select2/data/tags',[
}; };
}; };
Tags.prototype._removeOldTags = function (_) {
var tag = this._lastTag;
var $options = this.$element.find('option[data-select2-tag]');
$options.each(function () {
if (this.selected) {
return;
}
$(this).remove();
});
};
return Tags; return Tags;
}); });

View File

@ -10490,16 +10490,6 @@ define('select2/data/select',[
SelectAdapter.prototype.select = function (data) { SelectAdapter.prototype.select = function (data) {
var self = this; var self = this;
// Create items marked as tags
if (data._tag === true) {
// Clear the tag flag from it
delete data._tag;
// Create and add the option
var $option = this.option(data);
this.$element.append($option);
}
if (this.$element.prop('multiple')) { if (this.$element.prop('multiple')) {
this.current(function (currentData) { this.current(function (currentData) {
var val = []; var val = [];
@ -10792,6 +10782,8 @@ define('select2/data/tags',[
Tags.prototype.query = function (decorated, params, callback) { Tags.prototype.query = function (decorated, params, callback) {
var self = this; var self = this;
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) { if (params.term == null || params.term === '' || params.page != null) {
decorated.call(this, params, callback); decorated.call(this, params, callback);
return; return;
@ -10823,7 +10815,11 @@ define('select2/data/tags',[
} }
var tag = self.createTag(params); var tag = self.createTag(params);
tag._tag = true;
var $option = self.option(tag);
$option.attr('data-select2-tag', true);
self.$element.append($option);
data.unshift(tag); data.unshift(tag);
@ -10840,6 +10836,20 @@ define('select2/data/tags',[
}; };
}; };
Tags.prototype._removeOldTags = function (_) {
var tag = this._lastTag;
var $options = this.$element.find('option[data-select2-tag]');
$options.each(function () {
if (this.selected) {
return;
}
$(this).remove();
});
};
return Tags; return Tags;
}); });

File diff suppressed because one or more lines are too long

32
dist/js/select2.js vendored
View File

@ -1383,16 +1383,6 @@ define('select2/data/select',[
SelectAdapter.prototype.select = function (data) { SelectAdapter.prototype.select = function (data) {
var self = this; var self = this;
// Create items marked as tags
if (data._tag === true) {
// Clear the tag flag from it
delete data._tag;
// Create and add the option
var $option = this.option(data);
this.$element.append($option);
}
if (this.$element.prop('multiple')) { if (this.$element.prop('multiple')) {
this.current(function (currentData) { this.current(function (currentData) {
var val = []; var val = [];
@ -1685,6 +1675,8 @@ define('select2/data/tags',[
Tags.prototype.query = function (decorated, params, callback) { Tags.prototype.query = function (decorated, params, callback) {
var self = this; var self = this;
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) { if (params.term == null || params.term === '' || params.page != null) {
decorated.call(this, params, callback); decorated.call(this, params, callback);
return; return;
@ -1716,7 +1708,11 @@ define('select2/data/tags',[
} }
var tag = self.createTag(params); var tag = self.createTag(params);
tag._tag = true;
var $option = self.option(tag);
$option.attr('data-select2-tag', true);
self.$element.append($option);
data.unshift(tag); data.unshift(tag);
@ -1733,6 +1729,20 @@ define('select2/data/tags',[
}; };
}; };
Tags.prototype._removeOldTags = function (_) {
var tag = this._lastTag;
var $options = this.$element.find('option[data-select2-tag]');
$options.each(function () {
if (this.selected) {
return;
}
$(this).remove();
});
};
return Tags; return Tags;
}); });

File diff suppressed because one or more lines are too long

View File

@ -29,16 +29,6 @@ define([
SelectAdapter.prototype.select = function (data) { SelectAdapter.prototype.select = function (data) {
var self = this; var self = this;
// Create items marked as tags
if (data._tag === true) {
// Clear the tag flag from it
delete data._tag;
// Create and add the option
var $option = this.option(data);
this.$element.append($option);
}
if (this.$element.prop('multiple')) { if (this.$element.prop('multiple')) {
this.current(function (currentData) { this.current(function (currentData) {
var val = []; var val = [];

View File

@ -10,6 +10,8 @@ define([
Tags.prototype.query = function (decorated, params, callback) { Tags.prototype.query = function (decorated, params, callback) {
var self = this; var self = this;
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) { if (params.term == null || params.term === '' || params.page != null) {
decorated.call(this, params, callback); decorated.call(this, params, callback);
return; return;
@ -41,7 +43,11 @@ define([
} }
var tag = self.createTag(params); var tag = self.createTag(params);
tag._tag = true;
var $option = self.option(tag);
$option.attr('data-select2-tag', true);
self.$element.append($option);
data.unshift(tag); data.unshift(tag);
@ -58,5 +64,19 @@ define([
}; };
}; };
Tags.prototype._removeOldTags = function (_) {
var tag = this._lastTag;
var $options = this.$element.find('option[data-select2-tag]');
$options.each(function () {
if (this.selected) {
return;
}
$(this).remove();
});
};
return Tags; return Tags;
}); });