Skip to content

Commit 91cecd5

Browse files
committed
Handle minimumInputLength
1 parent 8b7924f commit 91cecd5

15 files changed

Lines changed: 479 additions & 89 deletions

File tree

dist/js/i18n/en.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.amd.full.js

Lines changed: 95 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,25 @@ define('select2/results',[
183183
this.$results.empty();
184184
};
185185

186-
Results.prototype.empty = function () {
187-
var $empty = $('<li role="treeitem" class="option"></li>');
186+
Results.prototype.displayMessage = function (params) {
187+
this.clear();
188+
189+
var $message = $('<li role="treeitem" class="option"></li>');
190+
191+
var message = this.options.get('translations').get(params.message);
188192

189-
$empty.text(this.options.get('translations').get('noResults'));
193+
$message.text(message(params.args));
190194

191-
this.$results.append($empty);
195+
this.$results.append($message);
192196
};
193197

194198
Results.prototype.append = function (data) {
195199
var $options = [];
196200

197201
if (data.length === 0) {
198-
this.empty();
202+
this.trigger('results:message', {
203+
message: 'noResults'
204+
});
199205

200206
return;
201207
}
@@ -449,6 +455,14 @@ define('select2/results',[
449455
params.element.addClass('highlighted');
450456
});
451457

458+
container.on('results:message', function (params) {
459+
self.trigger('results:message', params);
460+
});
461+
462+
this.on('results:message', function (params) {
463+
self.displayMessage(params);
464+
});
465+
452466
this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
453467
var $this = $(this);
454468

@@ -741,10 +755,7 @@ define('select2/selection/multiple',[
741755
'../utils'
742756
], function (BaseSelection, Utils) {
743757
function MultipleSelection ($element, options) {
744-
this.$element = $element;
745-
this.options = options;
746-
747-
MultipleSelection.__super__.constructor.call(this);
758+
MultipleSelection.__super__.constructor.apply(this, arguments);
748759
}
749760

750761
Utils.Extend(MultipleSelection, BaseSelection);
@@ -1361,6 +1372,37 @@ define('select2/data/tags',[
13611372
return Tags;
13621373
});
13631374

1375+
define('select2/data/minimumInputLength',[
1376+
1377+
], function () {
1378+
function MinimumInputLength (decorated, $e, options) {
1379+
this.minimumInputLength = options.get('minimumInputLength');
1380+
1381+
decorated.call(this, $e, options);
1382+
}
1383+
1384+
MinimumInputLength.prototype.query = function (decorated, params, callback) {
1385+
params.term = params.term || '';
1386+
1387+
if (params.term.length < this.minimumInputLength) {
1388+
this.trigger('results:message', {
1389+
message: 'inputTooShort',
1390+
args: {
1391+
minimum: this.minimumInputLength,
1392+
input: params.term,
1393+
params: params
1394+
}
1395+
});
1396+
1397+
return;
1398+
}
1399+
1400+
decorated.call(this, params, callback);
1401+
};
1402+
1403+
return MinimumInputLength;
1404+
});
1405+
13641406
define('select2/dropdown',[
13651407
'./utils'
13661408
], function (Utils) {
@@ -1421,13 +1463,7 @@ define('select2/dropdown/search',[
14211463
});
14221464

14231465
this.$search.on('keyup', function (evt) {
1424-
if (!self._keyUpPrevented) {
1425-
self.trigger('query', {
1426-
term: $(this).val()
1427-
});
1428-
}
1429-
1430-
self._keyUpPrevented = false;
1466+
self.handleSearch(evt);
14311467
});
14321468

14331469
container.on('open', function () {
@@ -1453,6 +1489,18 @@ define('select2/dropdown/search',[
14531489
});
14541490
};
14551491

1492+
Search.prototype.handleSearch = function (evt) {
1493+
if (!this._keyUpPrevented) {
1494+
var input = this.$search.val();
1495+
1496+
this.trigger('query', {
1497+
term: input
1498+
});
1499+
}
1500+
1501+
this._keyUpPrevented = false;
1502+
};
1503+
14561504
Search.prototype.showSearch = function (_, params) {
14571505
return true;
14581506
};
@@ -1505,6 +1553,17 @@ define('select2/dropdown/hidePlaceholder',[
15051553

15061554
define('select2/i18n/en',[],function () {
15071555
return {
1556+
inputTooShort: function (args) {
1557+
var remainingChars = args.minimum - args.input.length;
1558+
1559+
var message = 'Please enter ' + remainingChars + ' or more character';
1560+
1561+
if (remainingChars != 1) {
1562+
message += 's';
1563+
}
1564+
1565+
return message;
1566+
},
15081567
noResults: function () {
15091568
return 'No results found';
15101569
}
@@ -1526,6 +1585,7 @@ define('select2/defaults',[
15261585
'./data/array',
15271586
'./data/ajax',
15281587
'./data/tags',
1588+
'./data/minimumInputLength',
15291589

15301590
'./dropdown',
15311591
'./dropdown/search',
@@ -1535,7 +1595,7 @@ define('select2/defaults',[
15351595
], function ($, ResultsList,
15361596
SingleSelection, MultipleSelection, Placeholder,
15371597
Utils, Translation,
1538-
SelectData, ArrayData, AjaxData, Tags,
1598+
SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
15391599
Dropdown, Search, HidePlaceholder,
15401600
EnglishTranslation) {
15411601
function Defaults () {
@@ -1555,6 +1615,14 @@ define('select2/defaults',[
15551615
}
15561616
}
15571617

1618+
1619+
if (options.minimumInputLength > 0) {
1620+
options.dataAdapter = Utils.Decorate(
1621+
options.dataAdapter,
1622+
MinimumInputLength
1623+
);
1624+
}
1625+
15581626
if (options.tags != null) {
15591627
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
15601628
}
@@ -1627,6 +1695,7 @@ define('select2/defaults',[
16271695
Defaults.prototype.reset = function () {
16281696
this.defaults = {
16291697
language: ['select2/i18n/en'],
1698+
minimumInputLength: 0,
16301699
templateResult: function (result) {
16311700
return result.text;
16321701
},
@@ -1731,6 +1800,7 @@ define('select2/core',[
17311800
this._registerDomEvents();
17321801

17331802
// Register any internal event handlers
1803+
this._registerDataEvents();
17341804
this._registerSelectionEvents();
17351805
this._registerDropdownEvents();
17361806
this._registerResultsEvents();
@@ -1812,6 +1882,14 @@ define('select2/core',[
18121882
});
18131883
};
18141884

1885+
Select2.prototype._registerDataEvents = function () {
1886+
var self = this;
1887+
1888+
this.data.on('results:message', function (params) {
1889+
self.trigger('results:message', params);
1890+
});
1891+
};
1892+
18151893
Select2.prototype._registerSelectionEvents = function () {
18161894
var self = this;
18171895

0 commit comments

Comments
 (0)