Selaa lähdekoodia

Added `maximumInputLength` support

Kevin Brown 10 vuotta sitten
vanhempi
commit
e3647051de

+ 43 - 1
dist/js/select2.amd.full.js

@@ -2562,6 +2562,38 @@ define('select2/data/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',[
   './utils'
 ], function (Utils) {
@@ -2949,6 +2981,7 @@ define('select2/defaults',[
   './data/ajax',
   './data/tags',
   './data/minimumInputLength',
+  './data/maximumInputLength',
 
   './dropdown',
   './dropdown/search',
@@ -2964,7 +2997,8 @@ define('select2/defaults',[
 
              Utils, Translation, DIACRITICS,
 
-             SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
+             SelectData, ArrayData, AjaxData, Tags,
+             MinimumInputLength, MaximumInputLength,
 
              Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
              AttachBody,
@@ -2995,6 +3029,13 @@ define('select2/defaults',[
       );
     }
 
+    if (options.maximumInputLength > 0) {
+      options.dataAdapter = Utils.Decorate(
+        options.dataAdapter,
+        MaximumInputLength
+      );
+    }
+
     if (options.tags != null) {
       options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
     }
@@ -3153,6 +3194,7 @@ define('select2/defaults',[
       language: EnglishTranslation,
       matcher: matcher,
       minimumInputLength: 0,
+      maximumInputLength: 0,
       theme: 'default',
       templateResult: function (result) {
         return result.text;

+ 43 - 1
dist/js/select2.amd.js

@@ -2562,6 +2562,38 @@ define('select2/data/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',[
   './utils'
 ], function (Utils) {
@@ -2949,6 +2981,7 @@ define('select2/defaults',[
   './data/ajax',
   './data/tags',
   './data/minimumInputLength',
+  './data/maximumInputLength',
 
   './dropdown',
   './dropdown/search',
@@ -2964,7 +2997,8 @@ define('select2/defaults',[
 
              Utils, Translation, DIACRITICS,
 
-             SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
+             SelectData, ArrayData, AjaxData, Tags,
+             MinimumInputLength, MaximumInputLength,
 
              Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
              AttachBody,
@@ -2995,6 +3029,13 @@ define('select2/defaults',[
       );
     }
 
+    if (options.maximumInputLength > 0) {
+      options.dataAdapter = Utils.Decorate(
+        options.dataAdapter,
+        MaximumInputLength
+      );
+    }
+
     if (options.tags != null) {
       options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
     }
@@ -3153,6 +3194,7 @@ define('select2/defaults',[
       language: EnglishTranslation,
       matcher: matcher,
       minimumInputLength: 0,
+      maximumInputLength: 0,
       theme: 'default',
       templateResult: function (result) {
         return result.text;

+ 43 - 1
dist/js/select2.full.js

@@ -12097,6 +12097,38 @@ define('select2/data/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',[
   './utils'
 ], function (Utils) {
@@ -12484,6 +12516,7 @@ define('select2/defaults',[
   './data/ajax',
   './data/tags',
   './data/minimumInputLength',
+  './data/maximumInputLength',
 
   './dropdown',
   './dropdown/search',
@@ -12499,7 +12532,8 @@ define('select2/defaults',[
 
              Utils, Translation, DIACRITICS,
 
-             SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
+             SelectData, ArrayData, AjaxData, Tags,
+             MinimumInputLength, MaximumInputLength,
 
              Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
              AttachBody,
@@ -12530,6 +12564,13 @@ define('select2/defaults',[
       );
     }
 
+    if (options.maximumInputLength > 0) {
+      options.dataAdapter = Utils.Decorate(
+        options.dataAdapter,
+        MaximumInputLength
+      );
+    }
+
     if (options.tags != null) {
       options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
     }
@@ -12688,6 +12729,7 @@ define('select2/defaults',[
       language: EnglishTranslation,
       matcher: matcher,
       minimumInputLength: 0,
+      maximumInputLength: 0,
       theme: 'default',
       templateResult: function (result) {
         return result.text;

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/js/select2.full.min.js


+ 43 - 1
dist/js/select2.js

@@ -2990,6 +2990,38 @@ define('select2/data/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',[
   './utils'
 ], function (Utils) {
@@ -3377,6 +3409,7 @@ define('select2/defaults',[
   './data/ajax',
   './data/tags',
   './data/minimumInputLength',
+  './data/maximumInputLength',
 
   './dropdown',
   './dropdown/search',
@@ -3392,7 +3425,8 @@ define('select2/defaults',[
 
              Utils, Translation, DIACRITICS,
 
-             SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
+             SelectData, ArrayData, AjaxData, Tags,
+             MinimumInputLength, MaximumInputLength,
 
              Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
              AttachBody,
@@ -3423,6 +3457,13 @@ define('select2/defaults',[
       );
     }
 
+    if (options.maximumInputLength > 0) {
+      options.dataAdapter = Utils.Decorate(
+        options.dataAdapter,
+        MaximumInputLength
+      );
+    }
+
     if (options.tags != null) {
       options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
     }
@@ -3581,6 +3622,7 @@ define('select2/defaults',[
       language: EnglishTranslation,
       matcher: matcher,
       minimumInputLength: 0,
+      maximumInputLength: 0,
       theme: 'default',
       templateResult: function (result) {
         return result.text;

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/js/select2.min.js


+ 31 - 0
src/js/select2/data/maximumInputLength.js

@@ -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;
+});

+ 11 - 1
src/js/select2/defaults.js

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

+ 138 - 0
tests/data/maximumInputLength-tests.js

@@ -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);
+});

+ 18 - 0
tests/data/maximumInputLength.html

@@ -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>

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä