Skip to content

Commit 2bdb5db

Browse files
author
Zubair
committed
add maximum selection length option
1 parent 5dad831 commit 2bdb5db

8 files changed

Lines changed: 207 additions & 13 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,6 +2852,36 @@ define('select2/data/maximumInputLength',[
28522852
return MaximumInputLength;
28532853
});
28542854

2855+
define('select2/data/maximumSelectionLength',[
2856+
2857+
], function (){
2858+
function MaximumSelectionLength (decorated, $e, options) {
2859+
this.maximumSelectionLength = options.get('maximumSelectionLength');
2860+
2861+
decorated.call(this, $e, options);
2862+
}
2863+
2864+
MaximumSelectionLength.prototype.query =
2865+
function (decorated, params, callback) {
2866+
2867+
var count = this.$element.val() != null ? this.$element.val().length : 0;
2868+
if (count >= this.maximumSelectionLength) {
2869+
this.trigger('results:message', {
2870+
message: 'maximumSelected',
2871+
args: {
2872+
maximum: this.maximumSelectionLength
2873+
}
2874+
});
2875+
2876+
return;
2877+
}
2878+
2879+
decorated.call(this, params, callback);
2880+
};
2881+
2882+
return MaximumSelectionLength;
2883+
});
2884+
28552885
define('select2/dropdown',[
28562886
'./utils'
28572887
], function (Utils) {
@@ -3403,6 +3433,7 @@ define('select2/defaults',[
34033433
'./data/tokenizer',
34043434
'./data/minimumInputLength',
34053435
'./data/maximumInputLength',
3436+
'./data/maximumSelectionLength',
34063437

34073438
'./dropdown',
34083439
'./dropdown/search',
@@ -3421,7 +3452,7 @@ define('select2/defaults',[
34213452
Utils, Translation, DIACRITICS,
34223453

34233454
SelectData, ArrayData, AjaxData, Tags, Tokenizer,
3424-
MinimumInputLength, MaximumInputLength,
3455+
MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
34253456

34263457
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
34273458
AttachBody, MinimumResultsForSearch, SelectOnClose,
@@ -3457,6 +3488,13 @@ define('select2/defaults',[
34573488
);
34583489
}
34593490

3491+
if (options.maximumSelectionLength > 0) {
3492+
options.dataAdapter = Utils.Decorate(
3493+
options.dataAdapter,
3494+
MaximumSelectionLength
3495+
);
3496+
}
3497+
34603498
if (options.tags != null) {
34613499
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
34623500
}
@@ -3570,7 +3608,7 @@ define('select2/defaults',[
35703608
language = Translation.loadPath(name);
35713609
} catch (e) {
35723610
// If we couldn't load it, check if it wasn't the full path
3573-
name = this.get('amdTranslationBase') + name;
3611+
name = this.defaults.amdLanguageBase + name;
35743612
language = Translation.loadPath(name);
35753613
}
35763614

@@ -3647,6 +3685,7 @@ define('select2/defaults',[
36473685
matcher: matcher,
36483686
minimumInputLength: 0,
36493687
maximumInputLength: 0,
3688+
maximumSelectionLength: 0,
36503689
minimumResultsForSearch: 0,
36513690
selectOnClose: false,
36523691
sorter: function (data) {

dist/js/select2.amd.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,6 +2852,36 @@ define('select2/data/maximumInputLength',[
28522852
return MaximumInputLength;
28532853
});
28542854

2855+
define('select2/data/maximumSelectionLength',[
2856+
2857+
], function (){
2858+
function MaximumSelectionLength (decorated, $e, options) {
2859+
this.maximumSelectionLength = options.get('maximumSelectionLength');
2860+
2861+
decorated.call(this, $e, options);
2862+
}
2863+
2864+
MaximumSelectionLength.prototype.query =
2865+
function (decorated, params, callback) {
2866+
2867+
var count = this.$element.val() != null ? this.$element.val().length : 0;
2868+
if (count >= this.maximumSelectionLength) {
2869+
this.trigger('results:message', {
2870+
message: 'maximumSelected',
2871+
args: {
2872+
maximum: this.maximumSelectionLength
2873+
}
2874+
});
2875+
2876+
return;
2877+
}
2878+
2879+
decorated.call(this, params, callback);
2880+
};
2881+
2882+
return MaximumSelectionLength;
2883+
});
2884+
28552885
define('select2/dropdown',[
28562886
'./utils'
28572887
], function (Utils) {
@@ -3403,6 +3433,7 @@ define('select2/defaults',[
34033433
'./data/tokenizer',
34043434
'./data/minimumInputLength',
34053435
'./data/maximumInputLength',
3436+
'./data/maximumSelectionLength',
34063437

34073438
'./dropdown',
34083439
'./dropdown/search',
@@ -3421,7 +3452,7 @@ define('select2/defaults',[
34213452
Utils, Translation, DIACRITICS,
34223453

34233454
SelectData, ArrayData, AjaxData, Tags, Tokenizer,
3424-
MinimumInputLength, MaximumInputLength,
3455+
MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
34253456

34263457
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
34273458
AttachBody, MinimumResultsForSearch, SelectOnClose,
@@ -3457,6 +3488,13 @@ define('select2/defaults',[
34573488
);
34583489
}
34593490

3491+
if (options.maximumSelectionLength > 0) {
3492+
options.dataAdapter = Utils.Decorate(
3493+
options.dataAdapter,
3494+
MaximumSelectionLength
3495+
);
3496+
}
3497+
34603498
if (options.tags != null) {
34613499
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
34623500
}
@@ -3570,7 +3608,7 @@ define('select2/defaults',[
35703608
language = Translation.loadPath(name);
35713609
} catch (e) {
35723610
// If we couldn't load it, check if it wasn't the full path
3573-
name = this.get('amdTranslationBase') + name;
3611+
name = this.defaults.amdLanguageBase + name;
35743612
language = Translation.loadPath(name);
35753613
}
35763614

@@ -3647,6 +3685,7 @@ define('select2/defaults',[
36473685
matcher: matcher,
36483686
minimumInputLength: 0,
36493687
maximumInputLength: 0,
3688+
maximumSelectionLength: 0,
36503689
minimumResultsForSearch: 0,
36513690
selectOnClose: false,
36523691
sorter: function (data) {

dist/js/select2.full.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12387,6 +12387,36 @@ define('select2/data/maximumInputLength',[
1238712387
return MaximumInputLength;
1238812388
});
1238912389

12390+
define('select2/data/maximumSelectionLength',[
12391+
12392+
], function (){
12393+
function MaximumSelectionLength (decorated, $e, options) {
12394+
this.maximumSelectionLength = options.get('maximumSelectionLength');
12395+
12396+
decorated.call(this, $e, options);
12397+
}
12398+
12399+
MaximumSelectionLength.prototype.query =
12400+
function (decorated, params, callback) {
12401+
12402+
var count = this.$element.val() != null ? this.$element.val().length : 0;
12403+
if (count >= this.maximumSelectionLength) {
12404+
this.trigger('results:message', {
12405+
message: 'maximumSelected',
12406+
args: {
12407+
maximum: this.maximumSelectionLength
12408+
}
12409+
});
12410+
12411+
return;
12412+
}
12413+
12414+
decorated.call(this, params, callback);
12415+
};
12416+
12417+
return MaximumSelectionLength;
12418+
});
12419+
1239012420
define('select2/dropdown',[
1239112421
'./utils'
1239212422
], function (Utils) {
@@ -12938,6 +12968,7 @@ define('select2/defaults',[
1293812968
'./data/tokenizer',
1293912969
'./data/minimumInputLength',
1294012970
'./data/maximumInputLength',
12971+
'./data/maximumSelectionLength',
1294112972

1294212973
'./dropdown',
1294312974
'./dropdown/search',
@@ -12956,7 +12987,7 @@ define('select2/defaults',[
1295612987
Utils, Translation, DIACRITICS,
1295712988

1295812989
SelectData, ArrayData, AjaxData, Tags, Tokenizer,
12959-
MinimumInputLength, MaximumInputLength,
12990+
MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
1296012991

1296112992
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
1296212993
AttachBody, MinimumResultsForSearch, SelectOnClose,
@@ -12992,6 +13023,13 @@ define('select2/defaults',[
1299213023
);
1299313024
}
1299413025

13026+
if (options.maximumSelectionLength > 0) {
13027+
options.dataAdapter = Utils.Decorate(
13028+
options.dataAdapter,
13029+
MaximumSelectionLength
13030+
);
13031+
}
13032+
1299513033
if (options.tags != null) {
1299613034
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
1299713035
}
@@ -13105,7 +13143,7 @@ define('select2/defaults',[
1310513143
language = Translation.loadPath(name);
1310613144
} catch (e) {
1310713145
// If we couldn't load it, check if it wasn't the full path
13108-
name = this.get('amdTranslationBase') + name;
13146+
name = this.defaults.amdLanguageBase + name;
1310913147
language = Translation.loadPath(name);
1311013148
}
1311113149

@@ -13182,6 +13220,7 @@ define('select2/defaults',[
1318213220
matcher: matcher,
1318313221
minimumInputLength: 0,
1318413222
maximumInputLength: 0,
13223+
maximumSelectionLength: 0,
1318513224
minimumResultsForSearch: 0,
1318613225
selectOnClose: false,
1318713226
sorter: function (data) {

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.

dist/js/select2.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3280,6 +3280,36 @@ define('select2/data/maximumInputLength',[
32803280
return MaximumInputLength;
32813281
});
32823282

3283+
define('select2/data/maximumSelectionLength',[
3284+
3285+
], function (){
3286+
function MaximumSelectionLength (decorated, $e, options) {
3287+
this.maximumSelectionLength = options.get('maximumSelectionLength');
3288+
3289+
decorated.call(this, $e, options);
3290+
}
3291+
3292+
MaximumSelectionLength.prototype.query =
3293+
function (decorated, params, callback) {
3294+
3295+
var count = this.$element.val() != null ? this.$element.val().length : 0;
3296+
if (count >= this.maximumSelectionLength) {
3297+
this.trigger('results:message', {
3298+
message: 'maximumSelected',
3299+
args: {
3300+
maximum: this.maximumSelectionLength
3301+
}
3302+
});
3303+
3304+
return;
3305+
}
3306+
3307+
decorated.call(this, params, callback);
3308+
};
3309+
3310+
return MaximumSelectionLength;
3311+
});
3312+
32833313
define('select2/dropdown',[
32843314
'./utils'
32853315
], function (Utils) {
@@ -3831,6 +3861,7 @@ define('select2/defaults',[
38313861
'./data/tokenizer',
38323862
'./data/minimumInputLength',
38333863
'./data/maximumInputLength',
3864+
'./data/maximumSelectionLength',
38343865

38353866
'./dropdown',
38363867
'./dropdown/search',
@@ -3849,7 +3880,7 @@ define('select2/defaults',[
38493880
Utils, Translation, DIACRITICS,
38503881

38513882
SelectData, ArrayData, AjaxData, Tags, Tokenizer,
3852-
MinimumInputLength, MaximumInputLength,
3883+
MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
38533884

38543885
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
38553886
AttachBody, MinimumResultsForSearch, SelectOnClose,
@@ -3885,6 +3916,13 @@ define('select2/defaults',[
38853916
);
38863917
}
38873918

3919+
if (options.maximumSelectionLength > 0) {
3920+
options.dataAdapter = Utils.Decorate(
3921+
options.dataAdapter,
3922+
MaximumSelectionLength
3923+
);
3924+
}
3925+
38883926
if (options.tags != null) {
38893927
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
38903928
}
@@ -3998,7 +4036,7 @@ define('select2/defaults',[
39984036
language = Translation.loadPath(name);
39994037
} catch (e) {
40004038
// If we couldn't load it, check if it wasn't the full path
4001-
name = this.get('amdTranslationBase') + name;
4039+
name = this.defaults.amdLanguageBase + name;
40024040
language = Translation.loadPath(name);
40034041
}
40044042

@@ -4075,6 +4113,7 @@ define('select2/defaults',[
40754113
matcher: matcher,
40764114
minimumInputLength: 0,
40774115
maximumInputLength: 0,
4116+
maximumSelectionLength: 0,
40784117
minimumResultsForSearch: 0,
40794118
selectOnClose: false,
40804119
sorter: function (data) {

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.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
define([
2+
3+
], function (){
4+
function MaximumSelectionLength (decorated, $e, options) {
5+
this.maximumSelectionLength = options.get('maximumSelectionLength');
6+
7+
decorated.call(this, $e, options);
8+
}
9+
10+
MaximumSelectionLength.prototype.query =
11+
function (decorated, params, callback) {
12+
13+
var count = this.$element.val() != null ? this.$element.val().length : 0;
14+
if (count >= this.maximumSelectionLength) {
15+
this.trigger('results:message', {
16+
message: 'maximumSelected',
17+
args: {
18+
maximum: this.maximumSelectionLength
19+
}
20+
});
21+
22+
return;
23+
}
24+
25+
decorated.call(this, params, callback);
26+
};
27+
28+
return MaximumSelectionLength;
29+
});

0 commit comments

Comments
 (0)