Skip to content

Commit f8fdcb6

Browse files
committed
Added support for tokenization
This brings up both the `tokenizer` and `tokenSeparators` options.
1 parent 9389933 commit f8fdcb6

9 files changed

Lines changed: 527 additions & 9 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,93 @@ define('select2/data/tags',[
26462646
return Tags;
26472647
});
26482648

2649+
define('select2/data/tokenizer',[
2650+
2651+
], function () {
2652+
function Tokenizer (decorated, $element, options) {
2653+
var tokenizer = options.get('tokenizer');
2654+
2655+
if (tokenizer !== undefined) {
2656+
this.tokenizer = tokenizer;
2657+
}
2658+
2659+
decorated.call(this, $element, options);
2660+
}
2661+
2662+
Tokenizer.prototype.bind = function (decorated, container, $container) {
2663+
decorated.call(this, container, $container);
2664+
2665+
this.$search = container.dropdown.$search || container.selection.$search ||
2666+
$container.find('.select2-search__field');
2667+
};
2668+
2669+
Tokenizer.prototype.query = function (decorated, params, callback) {
2670+
var self = this;
2671+
2672+
function select (data) {
2673+
self.select(data);
2674+
}
2675+
2676+
params.term = params.term || '';
2677+
2678+
var tokenData = this.tokenizer(params, this.options, select);
2679+
2680+
if (tokenData.term !== params.term) {
2681+
// Replace the search term if we have the search box
2682+
if (this.$search.length) {
2683+
this.$search.val(tokenData.term);
2684+
this.$search.focus();
2685+
}
2686+
2687+
params.term = tokenData.term;
2688+
}
2689+
2690+
decorated.call(this, params, callback);
2691+
};
2692+
2693+
Tokenizer.prototype.tokenizer = function (_, params, options, callback) {
2694+
var separators = options.get('tokenSeparators') || [];
2695+
var term = params.term;
2696+
var i = 0;
2697+
2698+
var createTag = this.createTag || function (params) {
2699+
return {
2700+
id: params.term,
2701+
text: params.term
2702+
};
2703+
};
2704+
2705+
while (i < term.length) {
2706+
var termChar = term[i];
2707+
2708+
if (separators.indexOf(termChar) === -1) {
2709+
i++;
2710+
2711+
continue;
2712+
}
2713+
2714+
var part = term.substr(0, i);
2715+
var partParams = $.extend({}, params, {
2716+
term: part
2717+
});
2718+
2719+
var data = createTag(partParams);
2720+
2721+
callback(data);
2722+
2723+
// Reset the term to not include the tokenized portion
2724+
term = term.substr(i + 1) || '';
2725+
i = 0;
2726+
}
2727+
2728+
return {
2729+
term: term
2730+
};
2731+
};
2732+
2733+
return Tokenizer;
2734+
});
2735+
26492736
define('select2/data/minimumInputLength',[
26502737

26512738
], function () {
@@ -3211,6 +3298,7 @@ define('select2/defaults',[
32113298
'./data/array',
32123299
'./data/ajax',
32133300
'./data/tags',
3301+
'./data/tokenizer',
32143302
'./data/minimumInputLength',
32153303
'./data/maximumInputLength',
32163304

@@ -3229,7 +3317,7 @@ define('select2/defaults',[
32293317

32303318
Utils, Translation, DIACRITICS,
32313319

3232-
SelectData, ArrayData, AjaxData, Tags,
3320+
SelectData, ArrayData, AjaxData, Tags, Tokenizer,
32333321
MinimumInputLength, MaximumInputLength,
32343322

32353323
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
@@ -3269,6 +3357,13 @@ define('select2/defaults',[
32693357
if (options.tags != null) {
32703358
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
32713359
}
3360+
3361+
if (options.tokenSeparators != null || options.tokenizer != null) {
3362+
options.dataAdapter = Utils.Decorate(
3363+
options.dataAdapter,
3364+
Tokenizer
3365+
);
3366+
}
32723367
}
32733368

32743369
if (options.resultsAdapter == null) {

dist/js/select2.amd.js

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,93 @@ define('select2/data/tags',[
26462646
return Tags;
26472647
});
26482648

2649+
define('select2/data/tokenizer',[
2650+
2651+
], function () {
2652+
function Tokenizer (decorated, $element, options) {
2653+
var tokenizer = options.get('tokenizer');
2654+
2655+
if (tokenizer !== undefined) {
2656+
this.tokenizer = tokenizer;
2657+
}
2658+
2659+
decorated.call(this, $element, options);
2660+
}
2661+
2662+
Tokenizer.prototype.bind = function (decorated, container, $container) {
2663+
decorated.call(this, container, $container);
2664+
2665+
this.$search = container.dropdown.$search || container.selection.$search ||
2666+
$container.find('.select2-search__field');
2667+
};
2668+
2669+
Tokenizer.prototype.query = function (decorated, params, callback) {
2670+
var self = this;
2671+
2672+
function select (data) {
2673+
self.select(data);
2674+
}
2675+
2676+
params.term = params.term || '';
2677+
2678+
var tokenData = this.tokenizer(params, this.options, select);
2679+
2680+
if (tokenData.term !== params.term) {
2681+
// Replace the search term if we have the search box
2682+
if (this.$search.length) {
2683+
this.$search.val(tokenData.term);
2684+
this.$search.focus();
2685+
}
2686+
2687+
params.term = tokenData.term;
2688+
}
2689+
2690+
decorated.call(this, params, callback);
2691+
};
2692+
2693+
Tokenizer.prototype.tokenizer = function (_, params, options, callback) {
2694+
var separators = options.get('tokenSeparators') || [];
2695+
var term = params.term;
2696+
var i = 0;
2697+
2698+
var createTag = this.createTag || function (params) {
2699+
return {
2700+
id: params.term,
2701+
text: params.term
2702+
};
2703+
};
2704+
2705+
while (i < term.length) {
2706+
var termChar = term[i];
2707+
2708+
if (separators.indexOf(termChar) === -1) {
2709+
i++;
2710+
2711+
continue;
2712+
}
2713+
2714+
var part = term.substr(0, i);
2715+
var partParams = $.extend({}, params, {
2716+
term: part
2717+
});
2718+
2719+
var data = createTag(partParams);
2720+
2721+
callback(data);
2722+
2723+
// Reset the term to not include the tokenized portion
2724+
term = term.substr(i + 1) || '';
2725+
i = 0;
2726+
}
2727+
2728+
return {
2729+
term: term
2730+
};
2731+
};
2732+
2733+
return Tokenizer;
2734+
});
2735+
26492736
define('select2/data/minimumInputLength',[
26502737

26512738
], function () {
@@ -3211,6 +3298,7 @@ define('select2/defaults',[
32113298
'./data/array',
32123299
'./data/ajax',
32133300
'./data/tags',
3301+
'./data/tokenizer',
32143302
'./data/minimumInputLength',
32153303
'./data/maximumInputLength',
32163304

@@ -3229,7 +3317,7 @@ define('select2/defaults',[
32293317

32303318
Utils, Translation, DIACRITICS,
32313319

3232-
SelectData, ArrayData, AjaxData, Tags,
3320+
SelectData, ArrayData, AjaxData, Tags, Tokenizer,
32333321
MinimumInputLength, MaximumInputLength,
32343322

32353323
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
@@ -3269,6 +3357,13 @@ define('select2/defaults',[
32693357
if (options.tags != null) {
32703358
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
32713359
}
3360+
3361+
if (options.tokenSeparators != null || options.tokenizer != null) {
3362+
options.dataAdapter = Utils.Decorate(
3363+
options.dataAdapter,
3364+
Tokenizer
3365+
);
3366+
}
32723367
}
32733368

32743369
if (options.resultsAdapter == null) {

dist/js/select2.full.js

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12181,6 +12181,93 @@ define('select2/data/tags',[
1218112181
return Tags;
1218212182
});
1218312183

12184+
define('select2/data/tokenizer',[
12185+
12186+
], function () {
12187+
function Tokenizer (decorated, $element, options) {
12188+
var tokenizer = options.get('tokenizer');
12189+
12190+
if (tokenizer !== undefined) {
12191+
this.tokenizer = tokenizer;
12192+
}
12193+
12194+
decorated.call(this, $element, options);
12195+
}
12196+
12197+
Tokenizer.prototype.bind = function (decorated, container, $container) {
12198+
decorated.call(this, container, $container);
12199+
12200+
this.$search = container.dropdown.$search || container.selection.$search ||
12201+
$container.find('.select2-search__field');
12202+
};
12203+
12204+
Tokenizer.prototype.query = function (decorated, params, callback) {
12205+
var self = this;
12206+
12207+
function select (data) {
12208+
self.select(data);
12209+
}
12210+
12211+
params.term = params.term || '';
12212+
12213+
var tokenData = this.tokenizer(params, this.options, select);
12214+
12215+
if (tokenData.term !== params.term) {
12216+
// Replace the search term if we have the search box
12217+
if (this.$search.length) {
12218+
this.$search.val(tokenData.term);
12219+
this.$search.focus();
12220+
}
12221+
12222+
params.term = tokenData.term;
12223+
}
12224+
12225+
decorated.call(this, params, callback);
12226+
};
12227+
12228+
Tokenizer.prototype.tokenizer = function (_, params, options, callback) {
12229+
var separators = options.get('tokenSeparators') || [];
12230+
var term = params.term;
12231+
var i = 0;
12232+
12233+
var createTag = this.createTag || function (params) {
12234+
return {
12235+
id: params.term,
12236+
text: params.term
12237+
};
12238+
};
12239+
12240+
while (i < term.length) {
12241+
var termChar = term[i];
12242+
12243+
if (separators.indexOf(termChar) === -1) {
12244+
i++;
12245+
12246+
continue;
12247+
}
12248+
12249+
var part = term.substr(0, i);
12250+
var partParams = $.extend({}, params, {
12251+
term: part
12252+
});
12253+
12254+
var data = createTag(partParams);
12255+
12256+
callback(data);
12257+
12258+
// Reset the term to not include the tokenized portion
12259+
term = term.substr(i + 1) || '';
12260+
i = 0;
12261+
}
12262+
12263+
return {
12264+
term: term
12265+
};
12266+
};
12267+
12268+
return Tokenizer;
12269+
});
12270+
1218412271
define('select2/data/minimumInputLength',[
1218512272

1218612273
], function () {
@@ -12746,6 +12833,7 @@ define('select2/defaults',[
1274612833
'./data/array',
1274712834
'./data/ajax',
1274812835
'./data/tags',
12836+
'./data/tokenizer',
1274912837
'./data/minimumInputLength',
1275012838
'./data/maximumInputLength',
1275112839

@@ -12764,7 +12852,7 @@ define('select2/defaults',[
1276412852

1276512853
Utils, Translation, DIACRITICS,
1276612854

12767-
SelectData, ArrayData, AjaxData, Tags,
12855+
SelectData, ArrayData, AjaxData, Tags, Tokenizer,
1276812856
MinimumInputLength, MaximumInputLength,
1276912857

1277012858
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
@@ -12804,6 +12892,13 @@ define('select2/defaults',[
1280412892
if (options.tags != null) {
1280512893
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
1280612894
}
12895+
12896+
if (options.tokenSeparators != null || options.tokenizer != null) {
12897+
options.dataAdapter = Utils.Decorate(
12898+
options.dataAdapter,
12899+
Tokenizer
12900+
);
12901+
}
1280712902
}
1280812903

1280912904
if (options.resultsAdapter == null) {

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)