Skip to content

Commit 8bdd83c

Browse files
committed
Better support for tagging
This removes the special case for `<select>` elements and instead handles everything tagging related within the `Tags` decorator.
1 parent b87a4a5 commit 8bdd83c

8 files changed

Lines changed: 107 additions & 57 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -955,16 +955,6 @@ define('select2/data/select',[
955955
SelectAdapter.prototype.select = function (data) {
956956
var self = this;
957957

958-
// Create items marked as tags
959-
if (data._tag === true) {
960-
// Clear the tag flag from it
961-
delete data._tag;
962-
963-
// Create and add the option
964-
var $option = this.option(data);
965-
this.$element.append($option);
966-
}
967-
968958
if (this.$element.prop('multiple')) {
969959
this.current(function (currentData) {
970960
var val = [];
@@ -1257,6 +1247,8 @@ define('select2/data/tags',[
12571247
Tags.prototype.query = function (decorated, params, callback) {
12581248
var self = this;
12591249

1250+
this._removeOldTags();
1251+
12601252
if (params.term == null || params.term === '' || params.page != null) {
12611253
decorated.call(this, params, callback);
12621254
return;
@@ -1288,7 +1280,11 @@ define('select2/data/tags',[
12881280
}
12891281

12901282
var tag = self.createTag(params);
1291-
tag._tag = true;
1283+
1284+
var $option = self.option(tag);
1285+
$option.attr('data-select2-tag', true);
1286+
1287+
self.$element.append($option);
12921288

12931289
data.unshift(tag);
12941290

@@ -1305,6 +1301,20 @@ define('select2/data/tags',[
13051301
};
13061302
};
13071303

1304+
Tags.prototype._removeOldTags = function (_) {
1305+
var tag = this._lastTag;
1306+
1307+
var $options = this.$element.find('option[data-select2-tag]');
1308+
1309+
$options.each(function () {
1310+
if (this.selected) {
1311+
return;
1312+
}
1313+
1314+
$(this).remove();
1315+
});
1316+
};
1317+
13081318
return Tags;
13091319
});
13101320

dist/js/select2.amd.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -955,16 +955,6 @@ define('select2/data/select',[
955955
SelectAdapter.prototype.select = function (data) {
956956
var self = this;
957957

958-
// Create items marked as tags
959-
if (data._tag === true) {
960-
// Clear the tag flag from it
961-
delete data._tag;
962-
963-
// Create and add the option
964-
var $option = this.option(data);
965-
this.$element.append($option);
966-
}
967-
968958
if (this.$element.prop('multiple')) {
969959
this.current(function (currentData) {
970960
var val = [];
@@ -1257,6 +1247,8 @@ define('select2/data/tags',[
12571247
Tags.prototype.query = function (decorated, params, callback) {
12581248
var self = this;
12591249

1250+
this._removeOldTags();
1251+
12601252
if (params.term == null || params.term === '' || params.page != null) {
12611253
decorated.call(this, params, callback);
12621254
return;
@@ -1288,7 +1280,11 @@ define('select2/data/tags',[
12881280
}
12891281

12901282
var tag = self.createTag(params);
1291-
tag._tag = true;
1283+
1284+
var $option = self.option(tag);
1285+
$option.attr('data-select2-tag', true);
1286+
1287+
self.$element.append($option);
12921288

12931289
data.unshift(tag);
12941290

@@ -1305,6 +1301,20 @@ define('select2/data/tags',[
13051301
};
13061302
};
13071303

1304+
Tags.prototype._removeOldTags = function (_) {
1305+
var tag = this._lastTag;
1306+
1307+
var $options = this.$element.find('option[data-select2-tag]');
1308+
1309+
$options.each(function () {
1310+
if (this.selected) {
1311+
return;
1312+
}
1313+
1314+
$(this).remove();
1315+
});
1316+
};
1317+
13081318
return Tags;
13091319
});
13101320

dist/js/select2.full.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10490,16 +10490,6 @@ define('select2/data/select',[
1049010490
SelectAdapter.prototype.select = function (data) {
1049110491
var self = this;
1049210492

10493-
// Create items marked as tags
10494-
if (data._tag === true) {
10495-
// Clear the tag flag from it
10496-
delete data._tag;
10497-
10498-
// Create and add the option
10499-
var $option = this.option(data);
10500-
this.$element.append($option);
10501-
}
10502-
1050310493
if (this.$element.prop('multiple')) {
1050410494
this.current(function (currentData) {
1050510495
var val = [];
@@ -10792,6 +10782,8 @@ define('select2/data/tags',[
1079210782
Tags.prototype.query = function (decorated, params, callback) {
1079310783
var self = this;
1079410784

10785+
this._removeOldTags();
10786+
1079510787
if (params.term == null || params.term === '' || params.page != null) {
1079610788
decorated.call(this, params, callback);
1079710789
return;
@@ -10823,7 +10815,11 @@ define('select2/data/tags',[
1082310815
}
1082410816

1082510817
var tag = self.createTag(params);
10826-
tag._tag = true;
10818+
10819+
var $option = self.option(tag);
10820+
$option.attr('data-select2-tag', true);
10821+
10822+
self.$element.append($option);
1082710823

1082810824
data.unshift(tag);
1082910825

@@ -10840,6 +10836,20 @@ define('select2/data/tags',[
1084010836
};
1084110837
};
1084210838

10839+
Tags.prototype._removeOldTags = function (_) {
10840+
var tag = this._lastTag;
10841+
10842+
var $options = this.$element.find('option[data-select2-tag]');
10843+
10844+
$options.each(function () {
10845+
if (this.selected) {
10846+
return;
10847+
}
10848+
10849+
$(this).remove();
10850+
});
10851+
};
10852+
1084310853
return Tags;
1084410854
});
1084510855

dist/js/select2.full.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,16 +1383,6 @@ define('select2/data/select',[
13831383
SelectAdapter.prototype.select = function (data) {
13841384
var self = this;
13851385

1386-
// Create items marked as tags
1387-
if (data._tag === true) {
1388-
// Clear the tag flag from it
1389-
delete data._tag;
1390-
1391-
// Create and add the option
1392-
var $option = this.option(data);
1393-
this.$element.append($option);
1394-
}
1395-
13961386
if (this.$element.prop('multiple')) {
13971387
this.current(function (currentData) {
13981388
var val = [];
@@ -1685,6 +1675,8 @@ define('select2/data/tags',[
16851675
Tags.prototype.query = function (decorated, params, callback) {
16861676
var self = this;
16871677

1678+
this._removeOldTags();
1679+
16881680
if (params.term == null || params.term === '' || params.page != null) {
16891681
decorated.call(this, params, callback);
16901682
return;
@@ -1716,7 +1708,11 @@ define('select2/data/tags',[
17161708
}
17171709

17181710
var tag = self.createTag(params);
1719-
tag._tag = true;
1711+
1712+
var $option = self.option(tag);
1713+
$option.attr('data-select2-tag', true);
1714+
1715+
self.$element.append($option);
17201716

17211717
data.unshift(tag);
17221718

@@ -1733,6 +1729,20 @@ define('select2/data/tags',[
17331729
};
17341730
};
17351731

1732+
Tags.prototype._removeOldTags = function (_) {
1733+
var tag = this._lastTag;
1734+
1735+
var $options = this.$element.find('option[data-select2-tag]');
1736+
1737+
$options.each(function () {
1738+
if (this.selected) {
1739+
return;
1740+
}
1741+
1742+
$(this).remove();
1743+
});
1744+
};
1745+
17361746
return Tags;
17371747
});
17381748

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/data/select.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ define([
2929
SelectAdapter.prototype.select = function (data) {
3030
var self = this;
3131

32-
// Create items marked as tags
33-
if (data._tag === true) {
34-
// Clear the tag flag from it
35-
delete data._tag;
36-
37-
// Create and add the option
38-
var $option = this.option(data);
39-
this.$element.append($option);
40-
}
41-
4232
if (this.$element.prop('multiple')) {
4333
this.current(function (currentData) {
4434
var val = [];

src/js/select2/data/tags.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ define([
1010
Tags.prototype.query = function (decorated, params, callback) {
1111
var self = this;
1212

13+
this._removeOldTags();
14+
1315
if (params.term == null || params.term === '' || params.page != null) {
1416
decorated.call(this, params, callback);
1517
return;
@@ -41,7 +43,11 @@ define([
4143
}
4244

4345
var tag = self.createTag(params);
44-
tag._tag = true;
46+
47+
var $option = self.option(tag);
48+
$option.attr('data-select2-tag', true);
49+
50+
self.$element.append($option);
4551

4652
data.unshift(tag);
4753

@@ -58,5 +64,19 @@ define([
5864
};
5965
};
6066

67+
Tags.prototype._removeOldTags = function (_) {
68+
var tag = this._lastTag;
69+
70+
var $options = this.$element.find('option[data-select2-tag]');
71+
72+
$options.each(function () {
73+
if (this.selected) {
74+
return;
75+
}
76+
77+
$(this).remove();
78+
});
79+
};
80+
6181
return Tags;
6282
});

0 commit comments

Comments
 (0)