1
0
mirror of synced 2024-12-02 01:26:02 +03:00

Added maximumInputLength support

This commit is contained in:
Kevin Brown 2014-11-26 21:52:49 -05:00
parent 43efd43d57
commit e3647051de
10 changed files with 374 additions and 8 deletions

View File

@ -2562,6 +2562,38 @@ define('select2/data/minimumInputLength',[
return MinimumInputLength; return MinimumInputLength;
}); });
define('select2/data/maximumInputLength',[
], function () {
function MaximumInputLength (decorated, $e, options) {
this.maximumInputLength = options.get('maximumInputLength');
decorated.call(this, $e, options);
}
MaximumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (this.maximumInputLength > 0 &&
params.term.length > this.maximumInputLength) {
this.trigger('results:message', {
message: 'inputTooLong',
args: {
minimum: this.maximumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MaximumInputLength;
});
define('select2/dropdown',[ define('select2/dropdown',[
'./utils' './utils'
], function (Utils) { ], function (Utils) {
@ -2949,6 +2981,7 @@ define('select2/defaults',[
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength', './data/minimumInputLength',
'./data/maximumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -2964,7 +2997,8 @@ define('select2/defaults',[
Utils, Translation, DIACRITICS, Utils, Translation, DIACRITICS,
SelectData, ArrayData, AjaxData, Tags, MinimumInputLength, SelectData, ArrayData, AjaxData, Tags,
MinimumInputLength, MaximumInputLength,
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll, Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
AttachBody, AttachBody,
@ -2995,6 +3029,13 @@ define('select2/defaults',[
); );
} }
if (options.maximumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MaximumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -3153,6 +3194,7 @@ define('select2/defaults',[
language: EnglishTranslation, language: EnglishTranslation,
matcher: matcher, matcher: matcher,
minimumInputLength: 0, minimumInputLength: 0,
maximumInputLength: 0,
theme: 'default', theme: 'default',
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;

View File

@ -2562,6 +2562,38 @@ define('select2/data/minimumInputLength',[
return MinimumInputLength; return MinimumInputLength;
}); });
define('select2/data/maximumInputLength',[
], function () {
function MaximumInputLength (decorated, $e, options) {
this.maximumInputLength = options.get('maximumInputLength');
decorated.call(this, $e, options);
}
MaximumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (this.maximumInputLength > 0 &&
params.term.length > this.maximumInputLength) {
this.trigger('results:message', {
message: 'inputTooLong',
args: {
minimum: this.maximumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MaximumInputLength;
});
define('select2/dropdown',[ define('select2/dropdown',[
'./utils' './utils'
], function (Utils) { ], function (Utils) {
@ -2949,6 +2981,7 @@ define('select2/defaults',[
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength', './data/minimumInputLength',
'./data/maximumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -2964,7 +2997,8 @@ define('select2/defaults',[
Utils, Translation, DIACRITICS, Utils, Translation, DIACRITICS,
SelectData, ArrayData, AjaxData, Tags, MinimumInputLength, SelectData, ArrayData, AjaxData, Tags,
MinimumInputLength, MaximumInputLength,
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll, Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
AttachBody, AttachBody,
@ -2995,6 +3029,13 @@ define('select2/defaults',[
); );
} }
if (options.maximumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MaximumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -3153,6 +3194,7 @@ define('select2/defaults',[
language: EnglishTranslation, language: EnglishTranslation,
matcher: matcher, matcher: matcher,
minimumInputLength: 0, minimumInputLength: 0,
maximumInputLength: 0,
theme: 'default', theme: 'default',
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;

View File

@ -12097,6 +12097,38 @@ define('select2/data/minimumInputLength',[
return MinimumInputLength; return MinimumInputLength;
}); });
define('select2/data/maximumInputLength',[
], function () {
function MaximumInputLength (decorated, $e, options) {
this.maximumInputLength = options.get('maximumInputLength');
decorated.call(this, $e, options);
}
MaximumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (this.maximumInputLength > 0 &&
params.term.length > this.maximumInputLength) {
this.trigger('results:message', {
message: 'inputTooLong',
args: {
minimum: this.maximumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MaximumInputLength;
});
define('select2/dropdown',[ define('select2/dropdown',[
'./utils' './utils'
], function (Utils) { ], function (Utils) {
@ -12484,6 +12516,7 @@ define('select2/defaults',[
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength', './data/minimumInputLength',
'./data/maximumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -12499,7 +12532,8 @@ define('select2/defaults',[
Utils, Translation, DIACRITICS, Utils, Translation, DIACRITICS,
SelectData, ArrayData, AjaxData, Tags, MinimumInputLength, SelectData, ArrayData, AjaxData, Tags,
MinimumInputLength, MaximumInputLength,
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll, Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
AttachBody, AttachBody,
@ -12530,6 +12564,13 @@ define('select2/defaults',[
); );
} }
if (options.maximumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MaximumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -12688,6 +12729,7 @@ define('select2/defaults',[
language: EnglishTranslation, language: EnglishTranslation,
matcher: matcher, matcher: matcher,
minimumInputLength: 0, minimumInputLength: 0,
maximumInputLength: 0,
theme: 'default', theme: 'default',
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;

File diff suppressed because one or more lines are too long

44
dist/js/select2.js vendored
View File

@ -2990,6 +2990,38 @@ define('select2/data/minimumInputLength',[
return MinimumInputLength; return MinimumInputLength;
}); });
define('select2/data/maximumInputLength',[
], function () {
function MaximumInputLength (decorated, $e, options) {
this.maximumInputLength = options.get('maximumInputLength');
decorated.call(this, $e, options);
}
MaximumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (this.maximumInputLength > 0 &&
params.term.length > this.maximumInputLength) {
this.trigger('results:message', {
message: 'inputTooLong',
args: {
minimum: this.maximumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MaximumInputLength;
});
define('select2/dropdown',[ define('select2/dropdown',[
'./utils' './utils'
], function (Utils) { ], function (Utils) {
@ -3377,6 +3409,7 @@ define('select2/defaults',[
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength', './data/minimumInputLength',
'./data/maximumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -3392,7 +3425,8 @@ define('select2/defaults',[
Utils, Translation, DIACRITICS, Utils, Translation, DIACRITICS,
SelectData, ArrayData, AjaxData, Tags, MinimumInputLength, SelectData, ArrayData, AjaxData, Tags,
MinimumInputLength, MaximumInputLength,
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll, Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
AttachBody, AttachBody,
@ -3423,6 +3457,13 @@ define('select2/defaults',[
); );
} }
if (options.maximumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MaximumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -3581,6 +3622,7 @@ define('select2/defaults',[
language: EnglishTranslation, language: EnglishTranslation,
matcher: matcher, matcher: matcher,
minimumInputLength: 0, minimumInputLength: 0,
maximumInputLength: 0,
theme: 'default', theme: 'default',
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,31 @@
define([
], function () {
function MaximumInputLength (decorated, $e, options) {
this.maximumInputLength = options.get('maximumInputLength');
decorated.call(this, $e, options);
}
MaximumInputLength.prototype.query = function (decorated, params, callback) {
params.term = params.term || '';
if (this.maximumInputLength > 0 &&
params.term.length > this.maximumInputLength) {
this.trigger('results:message', {
message: 'inputTooLong',
args: {
minimum: this.maximumInputLength,
input: params.term,
params: params
}
});
return;
}
decorated.call(this, params, callback);
};
return MaximumInputLength;
});

View File

@ -17,6 +17,7 @@ define([
'./data/ajax', './data/ajax',
'./data/tags', './data/tags',
'./data/minimumInputLength', './data/minimumInputLength',
'./data/maximumInputLength',
'./dropdown', './dropdown',
'./dropdown/search', './dropdown/search',
@ -32,7 +33,8 @@ define([
Utils, Translation, DIACRITICS, Utils, Translation, DIACRITICS,
SelectData, ArrayData, AjaxData, Tags, MinimumInputLength, SelectData, ArrayData, AjaxData, Tags,
MinimumInputLength, MaximumInputLength,
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll, Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
AttachBody, AttachBody,
@ -63,6 +65,13 @@ define([
); );
} }
if (options.maximumInputLength > 0) {
options.dataAdapter = Utils.Decorate(
options.dataAdapter,
MaximumInputLength
);
}
if (options.tags != null) { if (options.tags != null) {
options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags); options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
} }
@ -221,6 +230,7 @@ define([
language: EnglishTranslation, language: EnglishTranslation,
matcher: matcher, matcher: matcher,
minimumInputLength: 0, minimumInputLength: 0,
maximumInputLength: 0,
theme: 'default', theme: 'default',
templateResult: function (result) { templateResult: function (result) {
return result.text; return result.text;

View File

@ -0,0 +1,138 @@
module('Data adapters - Maximum input length');
var MaximumInputLength = require('select2/data/maximumInputLength');
var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');
function StubData () {
this.called = false;
}
StubData.prototype.query = function (params, callback) {
this.called = true;
};
var MaximumData = Utils.Decorate(StubData, MaximumInputLength);
test('0 never displays the notice', function (assert) {
var zeroOptions = new Options({
maximumInputLength: 0
});
var data = new MaximumData(null, zeroOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumData(null, zeroOptions);
data.query({
term: 'test'
});
assert.ok(data.called);
});
test('< 0 never displays the notice', function (assert) {
var negativeOptions = new Options({
maximumInputLength: -1
});
var data = new MaximumData(null, negativeOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumData(null, negativeOptions);
data.query({
term: 'test'
});
assert.ok(data.called);
});
test('triggers when input is too long', function (assert) {
var options = new Options({
maximumInputLength: 1
});
var data = new MaximumData(null, options);
data.trigger = function () {
assert.ok(true, 'The event should be triggered.');
};
data.query({
term: 'no'
});
assert.ok(!data.called);
});
test('does not trigger when equal', function (assert) {
var options = new Options({
maximumInputLength: 10
});
var data = new MaximumData(null, options);
data.trigger = function () {
assert.ok(false, 'The event should not be triggered.');
};
data.query({
term: '1234567890'
});
assert.ok(data.called);
});
test('does not trigger when less', function (assert) {
var options = new Options({
maximumInputLength: 10
});
var data = new MaximumData(null, options);
data.trigger = function () {
assert.ok(false, 'The event should not be triggered.');
};
data.query({
term: '123'
});
assert.ok(data.called);
});
test('works with null term', function (assert) {
var options = new Options({
maximumInputLength: 1
});
var data = new MaximumData(null, options);
data.trigger = function () {
assert.ok(false, 'The event should not be triggered');
};
data.query({});
assert.ok(data.called);
});

View File

@ -0,0 +1,18 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="../vendor/qunit-1.14.0.css" type="text/css" />
<link rel="stylesheet" href="../../dist/css/select2.css" type="text/css" />
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="../vendor/qunit-1.14.0.js" type="text/javascript"></script>
<script src="../../vendor/almond-0.2.9.js" type="text/javascript"></script>
<script src="../../vendor/jquery-2.1.0.js" type="text/javascript"></script>
<script src="../../dist/js/select2.amd.js" type="text/javascript"></script>
<script src="maximumInputLength-tests.js" type="text/javascript"></script>
</body>
</html>