1
0
mirror of synced 2025-02-03 21:59:24 +03:00

Handle minimumInputLength

This commit is contained in:
Kevin Brown 2014-11-01 22:59:59 -04:00
parent 8b7924fc72
commit 91cecd55ed
15 changed files with 479 additions and 89 deletions

2
dist/js/i18n/en.js vendored
View File

@ -1 +1 @@
window.$=window.$||{},function(){$&&$.fn&&$.fn.select2&&$.fn.select2.amd&&(define=$.fn.select2.amd.define,require=$.fn.select2.amd.require),define("select2/i18n/en",[],function(){return{noResults:function(){return"No results found"}}}),require("jquery.select2"),$.fn.select2.amd={define:define,require:require}}(); window.$=window.$||{},function(){$&&$.fn&&$.fn.select2&&$.fn.select2.amd&&(define=$.fn.select2.amd.define,require=$.fn.select2.amd.require),define("select2/i18n/en",[],function(){return{inputTooShort:function(e){var t=e.minimum-e.input.length,n="Please enter "+t+" or more character";return t!=1&&(n+="s"),n},noResults:function(){return"No results found"}}}),require("jquery.select2"),$.fn.select2.amd={define:define,require:require}}();

View File

@ -183,19 +183,25 @@ define('select2/results',[
this.$results.empty(); this.$results.empty();
}; };
Results.prototype.empty = function () { Results.prototype.displayMessage = function (params) {
var $empty = $('<li role="treeitem" class="option"></li>'); this.clear();
$empty.text(this.options.get('translations').get('noResults')); var $message = $('<li role="treeitem" class="option"></li>');
this.$results.append($empty); var message = this.options.get('translations').get(params.message);
$message.text(message(params.args));
this.$results.append($message);
}; };
Results.prototype.append = function (data) { Results.prototype.append = function (data) {
var $options = []; var $options = [];
if (data.length === 0) { if (data.length === 0) {
this.empty(); this.trigger('results:message', {
message: 'noResults'
});
return; return;
} }
@ -449,6 +455,14 @@ define('select2/results',[
params.element.addClass('highlighted'); params.element.addClass('highlighted');
}); });
container.on('results:message', function (params) {
self.trigger('results:message', params);
});
this.on('results:message', function (params) {
self.displayMessage(params);
});
this.$results.on('mouseup', '.option[aria-selected]', function (evt) { this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
var $this = $(this); var $this = $(this);
@ -741,10 +755,7 @@ define('select2/selection/multiple',[
'../utils' '../utils'
], function (BaseSelection, Utils) { ], function (BaseSelection, Utils) {
function MultipleSelection ($element, options) { function MultipleSelection ($element, options) {
this.$element = $element; MultipleSelection.__super__.constructor.apply(this, arguments);
this.options = options;
MultipleSelection.__super__.constructor.call(this);
} }
Utils.Extend(MultipleSelection, BaseSelection); Utils.Extend(MultipleSelection, BaseSelection);
@ -1361,6 +1372,37 @@ define('select2/data/tags',[
return Tags; return Tags;
}); });
define('select2/data/minimumInputLength',[
], function () {
function MinimumInputLength (decorated, $e, options) {
this.minimumInputLength = options.get('minimumInputLength');
decorated.call(this, $e, options);
}
MinimumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (params.term.length < this.minimumInputLength) {
this.trigger('results:message', {
message: 'inputTooShort',
args: {
minimum: this.minimumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MinimumInputLength;
});
define('select2/dropdown',[ define('select2/dropdown',[
'./utils' './utils'
], function (Utils) { ], function (Utils) {
@ -1421,13 +1463,7 @@ define('select2/dropdown/search',[
}); });
this.$search.on('keyup', function (evt) { this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) { self.handleSearch(evt);
self.trigger('query', {
term: $(this).val()
});
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {
@ -1453,6 +1489,18 @@ define('select2/dropdown/search',[
}); });
}; };
Search.prototype.handleSearch = function (evt) {
if (!this._keyUpPrevented) {
var input = this.$search.val();
this.trigger('query', {
term: input
});
}
this._keyUpPrevented = false;
};
Search.prototype.showSearch = function (_, params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };
@ -1505,6 +1553,17 @@ define('select2/dropdown/hidePlaceholder',[
define('select2/i18n/en',[],function () { define('select2/i18n/en',[],function () {
return { return {
inputTooShort: function (args) {
var remainingChars = args.minimum - args.input.length;
var message = 'Please enter ' + remainingChars + ' or more character';
if (remainingChars != 1) {
message += 's';
}
return message;
},
noResults: function () { noResults: function () {
return 'No results found'; return 'No results found';
} }
@ -1526,6 +1585,7 @@ define('select2/defaults',[
'./data/array', './data/array',
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -1535,7 +1595,7 @@ define('select2/defaults',[
], function ($, ResultsList, ], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder, SingleSelection, MultipleSelection, Placeholder,
Utils, Translation, Utils, Translation,
SelectData, ArrayData, AjaxData, Tags, SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
Dropdown, Search, HidePlaceholder, Dropdown, Search, HidePlaceholder,
EnglishTranslation) { EnglishTranslation) {
function Defaults () { function Defaults () {
@ -1555,6 +1615,14 @@ define('select2/defaults',[
} }
} }
if (options.minimumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MinimumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -1627,6 +1695,7 @@ define('select2/defaults',[
Defaults.prototype.reset = function () { Defaults.prototype.reset = function () {
this.defaults = { this.defaults = {
language: ['select2/i18n/en'], language: ['select2/i18n/en'],
minimumInputLength: 0,
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;
}, },
@ -1731,6 +1800,7 @@ define('select2/core',[
this._registerDomEvents(); this._registerDomEvents();
// Register any internal event handlers // Register any internal event handlers
this._registerDataEvents();
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents(); this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
@ -1812,6 +1882,14 @@ define('select2/core',[
}); });
}; };
Select2.prototype._registerDataEvents = function () {
var self = this;
this.data.on('results:message', function (params) {
self.trigger('results:message', params);
});
};
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;

112
dist/js/select2.amd.js vendored
View File

@ -183,19 +183,25 @@ define('select2/results',[
this.$results.empty(); this.$results.empty();
}; };
Results.prototype.empty = function () { Results.prototype.displayMessage = function (params) {
var $empty = $('<li role="treeitem" class="option"></li>'); this.clear();
$empty.text(this.options.get('translations').get('noResults')); var $message = $('<li role="treeitem" class="option"></li>');
this.$results.append($empty); var message = this.options.get('translations').get(params.message);
$message.text(message(params.args));
this.$results.append($message);
}; };
Results.prototype.append = function (data) { Results.prototype.append = function (data) {
var $options = []; var $options = [];
if (data.length === 0) { if (data.length === 0) {
this.empty(); this.trigger('results:message', {
message: 'noResults'
});
return; return;
} }
@ -449,6 +455,14 @@ define('select2/results',[
params.element.addClass('highlighted'); params.element.addClass('highlighted');
}); });
container.on('results:message', function (params) {
self.trigger('results:message', params);
});
this.on('results:message', function (params) {
self.displayMessage(params);
});
this.$results.on('mouseup', '.option[aria-selected]', function (evt) { this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
var $this = $(this); var $this = $(this);
@ -741,10 +755,7 @@ define('select2/selection/multiple',[
'../utils' '../utils'
], function (BaseSelection, Utils) { ], function (BaseSelection, Utils) {
function MultipleSelection ($element, options) { function MultipleSelection ($element, options) {
this.$element = $element; MultipleSelection.__super__.constructor.apply(this, arguments);
this.options = options;
MultipleSelection.__super__.constructor.call(this);
} }
Utils.Extend(MultipleSelection, BaseSelection); Utils.Extend(MultipleSelection, BaseSelection);
@ -1361,6 +1372,37 @@ define('select2/data/tags',[
return Tags; return Tags;
}); });
define('select2/data/minimumInputLength',[
], function () {
function MinimumInputLength (decorated, $e, options) {
this.minimumInputLength = options.get('minimumInputLength');
decorated.call(this, $e, options);
}
MinimumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (params.term.length < this.minimumInputLength) {
this.trigger('results:message', {
message: 'inputTooShort',
args: {
minimum: this.minimumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MinimumInputLength;
});
define('select2/dropdown',[ define('select2/dropdown',[
'./utils' './utils'
], function (Utils) { ], function (Utils) {
@ -1421,13 +1463,7 @@ define('select2/dropdown/search',[
}); });
this.$search.on('keyup', function (evt) { this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) { self.handleSearch(evt);
self.trigger('query', {
term: $(this).val()
});
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {
@ -1453,6 +1489,18 @@ define('select2/dropdown/search',[
}); });
}; };
Search.prototype.handleSearch = function (evt) {
if (!this._keyUpPrevented) {
var input = this.$search.val();
this.trigger('query', {
term: input
});
}
this._keyUpPrevented = false;
};
Search.prototype.showSearch = function (_, params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };
@ -1505,6 +1553,17 @@ define('select2/dropdown/hidePlaceholder',[
define('select2/i18n/en',[],function () { define('select2/i18n/en',[],function () {
return { return {
inputTooShort: function (args) {
var remainingChars = args.minimum - args.input.length;
var message = 'Please enter ' + remainingChars + ' or more character';
if (remainingChars != 1) {
message += 's';
}
return message;
},
noResults: function () { noResults: function () {
return 'No results found'; return 'No results found';
} }
@ -1526,6 +1585,7 @@ define('select2/defaults',[
'./data/array', './data/array',
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -1535,7 +1595,7 @@ define('select2/defaults',[
], function ($, ResultsList, ], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder, SingleSelection, MultipleSelection, Placeholder,
Utils, Translation, Utils, Translation,
SelectData, ArrayData, AjaxData, Tags, SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
Dropdown, Search, HidePlaceholder, Dropdown, Search, HidePlaceholder,
EnglishTranslation) { EnglishTranslation) {
function Defaults () { function Defaults () {
@ -1555,6 +1615,14 @@ define('select2/defaults',[
} }
} }
if (options.minimumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MinimumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -1627,6 +1695,7 @@ define('select2/defaults',[
Defaults.prototype.reset = function () { Defaults.prototype.reset = function () {
this.defaults = { this.defaults = {
language: ['select2/i18n/en'], language: ['select2/i18n/en'],
minimumInputLength: 0,
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;
}, },
@ -1731,6 +1800,7 @@ define('select2/core',[
this._registerDomEvents(); this._registerDomEvents();
// Register any internal event handlers // Register any internal event handlers
this._registerDataEvents();
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents(); this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
@ -1812,6 +1882,14 @@ define('select2/core',[
}); });
}; };
Select2.prototype._registerDataEvents = function () {
var self = this;
this.data.on('results:message', function (params) {
self.trigger('results:message', params);
});
};
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;

View File

@ -9718,19 +9718,25 @@ define('select2/results',[
this.$results.empty(); this.$results.empty();
}; };
Results.prototype.empty = function () { Results.prototype.displayMessage = function (params) {
var $empty = $('<li role="treeitem" class="option"></li>'); this.clear();
$empty.text(this.options.get('translations').get('noResults')); var $message = $('<li role="treeitem" class="option"></li>');
this.$results.append($empty); var message = this.options.get('translations').get(params.message);
$message.text(message(params.args));
this.$results.append($message);
}; };
Results.prototype.append = function (data) { Results.prototype.append = function (data) {
var $options = []; var $options = [];
if (data.length === 0) { if (data.length === 0) {
this.empty(); this.trigger('results:message', {
message: 'noResults'
});
return; return;
} }
@ -9984,6 +9990,14 @@ define('select2/results',[
params.element.addClass('highlighted'); params.element.addClass('highlighted');
}); });
container.on('results:message', function (params) {
self.trigger('results:message', params);
});
this.on('results:message', function (params) {
self.displayMessage(params);
});
this.$results.on('mouseup', '.option[aria-selected]', function (evt) { this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
var $this = $(this); var $this = $(this);
@ -10276,10 +10290,7 @@ define('select2/selection/multiple',[
'../utils' '../utils'
], function (BaseSelection, Utils) { ], function (BaseSelection, Utils) {
function MultipleSelection ($element, options) { function MultipleSelection ($element, options) {
this.$element = $element; MultipleSelection.__super__.constructor.apply(this, arguments);
this.options = options;
MultipleSelection.__super__.constructor.call(this);
} }
Utils.Extend(MultipleSelection, BaseSelection); Utils.Extend(MultipleSelection, BaseSelection);
@ -10896,6 +10907,37 @@ define('select2/data/tags',[
return Tags; return Tags;
}); });
define('select2/data/minimumInputLength',[
], function () {
function MinimumInputLength (decorated, $e, options) {
this.minimumInputLength = options.get('minimumInputLength');
decorated.call(this, $e, options);
}
MinimumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (params.term.length < this.minimumInputLength) {
this.trigger('results:message', {
message: 'inputTooShort',
args: {
minimum: this.minimumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MinimumInputLength;
});
define('select2/dropdown',[ define('select2/dropdown',[
'./utils' './utils'
], function (Utils) { ], function (Utils) {
@ -10956,13 +10998,7 @@ define('select2/dropdown/search',[
}); });
this.$search.on('keyup', function (evt) { this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) { self.handleSearch(evt);
self.trigger('query', {
term: $(this).val()
});
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {
@ -10988,6 +11024,18 @@ define('select2/dropdown/search',[
}); });
}; };
Search.prototype.handleSearch = function (evt) {
if (!this._keyUpPrevented) {
var input = this.$search.val();
this.trigger('query', {
term: input
});
}
this._keyUpPrevented = false;
};
Search.prototype.showSearch = function (_, params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };
@ -11040,6 +11088,17 @@ define('select2/dropdown/hidePlaceholder',[
define('select2/i18n/en',[],function () { define('select2/i18n/en',[],function () {
return { return {
inputTooShort: function (args) {
var remainingChars = args.minimum - args.input.length;
var message = 'Please enter ' + remainingChars + ' or more character';
if (remainingChars != 1) {
message += 's';
}
return message;
},
noResults: function () { noResults: function () {
return 'No results found'; return 'No results found';
} }
@ -11061,6 +11120,7 @@ define('select2/defaults',[
'./data/array', './data/array',
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -11070,7 +11130,7 @@ define('select2/defaults',[
], function ($, ResultsList, ], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder, SingleSelection, MultipleSelection, Placeholder,
Utils, Translation, Utils, Translation,
SelectData, ArrayData, AjaxData, Tags, SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
Dropdown, Search, HidePlaceholder, Dropdown, Search, HidePlaceholder,
EnglishTranslation) { EnglishTranslation) {
function Defaults () { function Defaults () {
@ -11090,6 +11150,14 @@ define('select2/defaults',[
} }
} }
if (options.minimumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MinimumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -11162,6 +11230,7 @@ define('select2/defaults',[
Defaults.prototype.reset = function () { Defaults.prototype.reset = function () {
this.defaults = { this.defaults = {
language: ['select2/i18n/en'], language: ['select2/i18n/en'],
minimumInputLength: 0,
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;
}, },
@ -11266,6 +11335,7 @@ define('select2/core',[
this._registerDomEvents(); this._registerDomEvents();
// Register any internal event handlers // Register any internal event handlers
this._registerDataEvents();
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents(); this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
@ -11347,6 +11417,14 @@ define('select2/core',[
}); });
}; };
Select2.prototype._registerDataEvents = function () {
var self = this;
this.data.on('results:message', function (params) {
self.trigger('results:message', params);
});
};
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;

File diff suppressed because one or more lines are too long

112
dist/js/select2.js vendored
View File

@ -611,19 +611,25 @@ define('select2/results',[
this.$results.empty(); this.$results.empty();
}; };
Results.prototype.empty = function () { Results.prototype.displayMessage = function (params) {
var $empty = $('<li role="treeitem" class="option"></li>'); this.clear();
$empty.text(this.options.get('translations').get('noResults')); var $message = $('<li role="treeitem" class="option"></li>');
this.$results.append($empty); var message = this.options.get('translations').get(params.message);
$message.text(message(params.args));
this.$results.append($message);
}; };
Results.prototype.append = function (data) { Results.prototype.append = function (data) {
var $options = []; var $options = [];
if (data.length === 0) { if (data.length === 0) {
this.empty(); this.trigger('results:message', {
message: 'noResults'
});
return; return;
} }
@ -877,6 +883,14 @@ define('select2/results',[
params.element.addClass('highlighted'); params.element.addClass('highlighted');
}); });
container.on('results:message', function (params) {
self.trigger('results:message', params);
});
this.on('results:message', function (params) {
self.displayMessage(params);
});
this.$results.on('mouseup', '.option[aria-selected]', function (evt) { this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
var $this = $(this); var $this = $(this);
@ -1169,10 +1183,7 @@ define('select2/selection/multiple',[
'../utils' '../utils'
], function (BaseSelection, Utils) { ], function (BaseSelection, Utils) {
function MultipleSelection ($element, options) { function MultipleSelection ($element, options) {
this.$element = $element; MultipleSelection.__super__.constructor.apply(this, arguments);
this.options = options;
MultipleSelection.__super__.constructor.call(this);
} }
Utils.Extend(MultipleSelection, BaseSelection); Utils.Extend(MultipleSelection, BaseSelection);
@ -1789,6 +1800,37 @@ define('select2/data/tags',[
return Tags; return Tags;
}); });
define('select2/data/minimumInputLength',[
], function () {
function MinimumInputLength (decorated, $e, options) {
this.minimumInputLength = options.get('minimumInputLength');
decorated.call(this, $e, options);
}
MinimumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (params.term.length < this.minimumInputLength) {
this.trigger('results:message', {
message: 'inputTooShort',
args: {
minimum: this.minimumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MinimumInputLength;
});
define('select2/dropdown',[ define('select2/dropdown',[
'./utils' './utils'
], function (Utils) { ], function (Utils) {
@ -1849,13 +1891,7 @@ define('select2/dropdown/search',[
}); });
this.$search.on('keyup', function (evt) { this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) { self.handleSearch(evt);
self.trigger('query', {
term: $(this).val()
});
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {
@ -1881,6 +1917,18 @@ define('select2/dropdown/search',[
}); });
}; };
Search.prototype.handleSearch = function (evt) {
if (!this._keyUpPrevented) {
var input = this.$search.val();
this.trigger('query', {
term: input
});
}
this._keyUpPrevented = false;
};
Search.prototype.showSearch = function (_, params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };
@ -1933,6 +1981,17 @@ define('select2/dropdown/hidePlaceholder',[
define('select2/i18n/en',[],function () { define('select2/i18n/en',[],function () {
return { return {
inputTooShort: function (args) {
var remainingChars = args.minimum - args.input.length;
var message = 'Please enter ' + remainingChars + ' or more character';
if (remainingChars != 1) {
message += 's';
}
return message;
},
noResults: function () { noResults: function () {
return 'No results found'; return 'No results found';
} }
@ -1954,6 +2013,7 @@ define('select2/defaults',[
'./data/array', './data/array',
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -1963,7 +2023,7 @@ define('select2/defaults',[
], function ($, ResultsList, ], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder, SingleSelection, MultipleSelection, Placeholder,
Utils, Translation, Utils, Translation,
SelectData, ArrayData, AjaxData, Tags, SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
Dropdown, Search, HidePlaceholder, Dropdown, Search, HidePlaceholder,
EnglishTranslation) { EnglishTranslation) {
function Defaults () { function Defaults () {
@ -1983,6 +2043,14 @@ define('select2/defaults',[
} }
} }
if (options.minimumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MinimumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -2055,6 +2123,7 @@ define('select2/defaults',[
Defaults.prototype.reset = function () { Defaults.prototype.reset = function () {
this.defaults = { this.defaults = {
language: ['select2/i18n/en'], language: ['select2/i18n/en'],
minimumInputLength: 0,
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;
}, },
@ -2159,6 +2228,7 @@ define('select2/core',[
this._registerDomEvents(); this._registerDomEvents();
// Register any internal event handlers // Register any internal event handlers
this._registerDataEvents();
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents(); this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
@ -2240,6 +2310,14 @@ define('select2/core',[
}); });
}; };
Select2.prototype._registerDataEvents = function () {
var self = this;
this.data.on('results:message', function (params) {
self.trigger('results:message', params);
});
};
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;

File diff suppressed because one or more lines are too long

View File

@ -463,6 +463,7 @@ $.fn.select2.amd.require(["select2/core", "select2/utils"], function (Select2, U
}, },
cache: true cache: true
}, },
minimumInputLength: 1,
templateResult: function (repo) { templateResult: function (repo) {
var markup = '<div class="row">' + var markup = '<div class="row">' +
'<div class="col-sm-1">' + '<div class="col-sm-1">' +

View File

@ -56,6 +56,7 @@ define([
this._registerDomEvents(); this._registerDomEvents();
// Register any internal event handlers // Register any internal event handlers
this._registerDataEvents();
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents(); this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
@ -137,6 +138,14 @@ define([
}); });
}; };
Select2.prototype._registerDataEvents = function () {
var self = this;
this.data.on('results:message', function (params) {
self.trigger('results:message', params);
});
};
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;

View File

@ -0,0 +1,30 @@
define([
], function () {
function MinimumInputLength (decorated, $e, options) {
this.minimumInputLength = options.get('minimumInputLength');
decorated.call(this, $e, options);
}
MinimumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (params.term.length < this.minimumInputLength) {
this.trigger('results:message', {
message: 'inputTooShort',
args: {
minimum: this.minimumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MinimumInputLength;
});

View File

@ -13,6 +13,7 @@ define([
'./data/array', './data/array',
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -22,7 +23,7 @@ define([
], function ($, ResultsList, ], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder, SingleSelection, MultipleSelection, Placeholder,
Utils, Translation, Utils, Translation,
SelectData, ArrayData, AjaxData, Tags, SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
Dropdown, Search, HidePlaceholder, Dropdown, Search, HidePlaceholder,
EnglishTranslation) { EnglishTranslation) {
function Defaults () { function Defaults () {
@ -42,6 +43,14 @@ define([
} }
} }
if (options.minimumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MinimumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -114,6 +123,7 @@ define([
Defaults.prototype.reset = function () { Defaults.prototype.reset = function () {
this.defaults = { this.defaults = {
language: ['select2/i18n/en'], language: ['select2/i18n/en'],
minimumInputLength: 0,
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;
}, },

View File

@ -32,13 +32,7 @@ define([
}); });
this.$search.on('keyup', function (evt) { this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) { self.handleSearch(evt);
self.trigger('query', {
term: $(this).val()
});
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {
@ -64,6 +58,18 @@ define([
}); });
}; };
Search.prototype.handleSearch = function (evt) {
if (!this._keyUpPrevented) {
var input = this.$search.val();
this.trigger('query', {
term: input
});
}
this._keyUpPrevented = false;
};
Search.prototype.showSearch = function (_, params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };

View File

@ -1,5 +1,16 @@
define(function () { define(function () {
return { return {
inputTooShort: function (args) {
var remainingChars = args.minimum - args.input.length;
var message = 'Please enter ' + remainingChars + ' or more character';
if (remainingChars != 1) {
message += 's';
}
return message;
},
noResults: function () { noResults: function () {
return 'No results found'; return 'No results found';
} }

View File

@ -29,19 +29,25 @@ define([
this.$results.empty(); this.$results.empty();
}; };
Results.prototype.empty = function () { Results.prototype.displayMessage = function (params) {
var $empty = $('<li role="treeitem" class="option"></li>'); this.clear();
$empty.text(this.options.get('translations').get('noResults')); var $message = $('<li role="treeitem" class="option"></li>');
this.$results.append($empty); var message = this.options.get('translations').get(params.message);
$message.text(message(params.args));
this.$results.append($message);
}; };
Results.prototype.append = function (data) { Results.prototype.append = function (data) {
var $options = []; var $options = [];
if (data.length === 0) { if (data.length === 0) {
this.empty(); this.trigger('results:message', {
message: 'noResults'
});
return; return;
} }
@ -295,6 +301,14 @@ define([
params.element.addClass('highlighted'); params.element.addClass('highlighted');
}); });
container.on('results:message', function (params) {
self.trigger('results:message', params);
});
this.on('results:message', function (params) {
self.displayMessage(params);
});
this.$results.on('mouseup', '.option[aria-selected]', function (evt) { this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
var $this = $(this); var $this = $(this);

View File

@ -3,10 +3,7 @@ define([
'../utils' '../utils'
], function (BaseSelection, Utils) { ], function (BaseSelection, Utils) {
function MultipleSelection ($element, options) { function MultipleSelection ($element, options) {
this.$element = $element; MultipleSelection.__super__.constructor.apply(this, arguments);
this.options = options;
MultipleSelection.__super__.constructor.call(this);
} }
Utils.Extend(MultipleSelection, BaseSelection); Utils.Extend(MultipleSelection, BaseSelection);