Browse Source

Handle `minimumInputLength`

Kevin Brown 10 years ago
parent
commit
91cecd55ed

+ 1 - 1
dist/js/i18n/en.js

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

+ 95 - 17
dist/js/select2.amd.full.js

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

+ 95 - 17
dist/js/select2.amd.js

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

+ 95 - 17
dist/js/select2.full.js

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

File diff suppressed because it is too large
+ 0 - 0
dist/js/select2.full.min.js


+ 95 - 17
dist/js/select2.js

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

File diff suppressed because it is too large
+ 0 - 0
dist/js/select2.min.js


+ 1 - 0
docs/examples.html

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

+ 9 - 0
src/js/select2/core.js

@@ -56,6 +56,7 @@ define([
     this._registerDomEvents();
 
     // Register any internal event handlers
+    this._registerDataEvents();
     this._registerSelectionEvents();
     this._registerDropdownEvents();
     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 () {
     var self = this;
 

+ 30 - 0
src/js/select2/data/minimumInputLength.js

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

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

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

+ 13 - 7
src/js/select2/dropdown/search.js

@@ -32,13 +32,7 @@ define([
     });
 
     this.$search.on('keyup', function (evt) {
-      if (!self._keyUpPrevented) {
-        self.trigger('query', {
-          term: $(this).val()
-        });
-      }
-
-      self._keyUpPrevented = false;
+      self.handleSearch(evt);
     });
 
     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) {
     return true;
   };

+ 11 - 0
src/js/select2/i18n/en.js

@@ -1,5 +1,16 @@
 define(function () {
   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 () {
       return 'No results found';
     }

+ 19 - 5
src/js/select2/results.js

@@ -29,19 +29,25 @@ define([
     this.$results.empty();
   };
 
-  Results.prototype.empty = function () {
-    var $empty = $('<li role="treeitem" class="option"></li>');
+  Results.prototype.displayMessage = function (params) {
+    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) {
     var $options = [];
 
     if (data.length === 0) {
-      this.empty();
+      this.trigger('results:message', {
+        message: 'noResults'
+      });
 
       return;
     }
@@ -295,6 +301,14 @@ define([
       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) {
       var $this = $(this);
 

+ 1 - 4
src/js/select2/selection/multiple.js

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

Some files were not shown because too many files changed in this diff