Browse Source

Add `minimumResultsForSearch`

Kevin Brown 11 years ago
parent
commit
0de516f17a

+ 1 - 1
dist/css/select2.css

@@ -84,7 +84,7 @@
   .select2-search--dropdown .select2-search__field {
     padding: 4px;
     width: 100%; }
-  .select2-search--dropdown .select2-search--hide {
+  .select2-search--dropdown.select2-search--hide {
     display: none; }
 
 .select2-close-mask {

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


+ 63 - 19
dist/js/select2.amd.full.js

@@ -2993,6 +2993,42 @@ define('select2/dropdown/attachBody',[
   return AttachBody;
 });
 
+define('select2/dropdown/minimumResultsForSearch',[
+
+], function () {
+  function countResults (data) {
+    count = 0;
+
+    for (var d = 0; d < data.length; d++) {
+      var item = data[d];
+
+      if (item.children) {
+        count += countResults(item.children);
+      } else {
+        count++;
+      }
+    }
+
+    return count;
+  }
+
+  function MinimumResultsForSearch (decorated, $element, options, dataAdapter) {
+    this.minimumResultsForSearch = options.get('minimumResultsForSearch');
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
+    if (countResults(params.data) < this.minimumResultsForSearch) {
+      return false;
+    }
+
+    return decorated.call(this, params);
+  };
+
+  return MinimumResultsForSearch;
+});
+
 define('select2/i18n/en',[],function () {
   return {
     errorLoading: function () {
@@ -3063,6 +3099,7 @@ define('select2/defaults',[
   './dropdown/hidePlaceholder',
   './dropdown/infiniteScroll',
   './dropdown/attachBody',
+  './dropdown/minimumResultsForSearch',
 
   './i18n/en'
 ], function ($, ResultsList,
@@ -3076,7 +3113,7 @@ define('select2/defaults',[
              MinimumInputLength, MaximumInputLength,
 
              Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
-             AttachBody,
+             AttachBody, MinimumResultsForSearch,
 
              EnglishTranslation) {
   function Defaults () {
@@ -3094,25 +3131,24 @@ define('select2/defaults',[
       } else {
         options.dataAdapter = SelectData;
       }
-    }
-
 
-    if (options.minimumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MinimumInputLength
-      );
-    }
+      if (options.minimumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MinimumInputLength
+        );
+      }
 
-    if (options.maximumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MaximumInputLength
-      );
-    }
+      if (options.maximumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MaximumInputLength
+        );
+      }
 
-    if (options.tags != null) {
-      options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      if (options.tags != null) {
+        options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      }
     }
 
     if (options.resultsAdapter == null) {
@@ -3142,6 +3178,13 @@ define('select2/defaults',[
         options.dropdownAdapter = SearchableDropdown;
       }
 
+      if (options.minimumResultsForSearch > 0) {
+        options.dropdownAdapter = Utils.Decorate(
+          options.dropdownAdapter,
+          MinimumResultsForSearch
+        );
+      }
+
       options.dropdownAdapter = Utils.Decorate(
         options.dropdownAdapter,
         AttachBody
@@ -3275,13 +3318,14 @@ define('select2/defaults',[
       },
       minimumInputLength: 0,
       maximumInputLength: 0,
-      theme: 'default',
+      minimumResultsForSearch: 0,
       templateResult: function (result) {
         return result.text;
       },
       templateSelection: function (selection) {
         return selection.text;
-      }
+      },
+      theme: 'default'
     };
   };
 

+ 63 - 19
dist/js/select2.amd.js

@@ -2993,6 +2993,42 @@ define('select2/dropdown/attachBody',[
   return AttachBody;
 });
 
+define('select2/dropdown/minimumResultsForSearch',[
+
+], function () {
+  function countResults (data) {
+    count = 0;
+
+    for (var d = 0; d < data.length; d++) {
+      var item = data[d];
+
+      if (item.children) {
+        count += countResults(item.children);
+      } else {
+        count++;
+      }
+    }
+
+    return count;
+  }
+
+  function MinimumResultsForSearch (decorated, $element, options, dataAdapter) {
+    this.minimumResultsForSearch = options.get('minimumResultsForSearch');
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
+    if (countResults(params.data) < this.minimumResultsForSearch) {
+      return false;
+    }
+
+    return decorated.call(this, params);
+  };
+
+  return MinimumResultsForSearch;
+});
+
 define('select2/i18n/en',[],function () {
   return {
     errorLoading: function () {
@@ -3063,6 +3099,7 @@ define('select2/defaults',[
   './dropdown/hidePlaceholder',
   './dropdown/infiniteScroll',
   './dropdown/attachBody',
+  './dropdown/minimumResultsForSearch',
 
   './i18n/en'
 ], function ($, ResultsList,
@@ -3076,7 +3113,7 @@ define('select2/defaults',[
              MinimumInputLength, MaximumInputLength,
 
              Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
-             AttachBody,
+             AttachBody, MinimumResultsForSearch,
 
              EnglishTranslation) {
   function Defaults () {
@@ -3094,25 +3131,24 @@ define('select2/defaults',[
       } else {
         options.dataAdapter = SelectData;
       }
-    }
-
 
-    if (options.minimumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MinimumInputLength
-      );
-    }
+      if (options.minimumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MinimumInputLength
+        );
+      }
 
-    if (options.maximumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MaximumInputLength
-      );
-    }
+      if (options.maximumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MaximumInputLength
+        );
+      }
 
-    if (options.tags != null) {
-      options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      if (options.tags != null) {
+        options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      }
     }
 
     if (options.resultsAdapter == null) {
@@ -3142,6 +3178,13 @@ define('select2/defaults',[
         options.dropdownAdapter = SearchableDropdown;
       }
 
+      if (options.minimumResultsForSearch > 0) {
+        options.dropdownAdapter = Utils.Decorate(
+          options.dropdownAdapter,
+          MinimumResultsForSearch
+        );
+      }
+
       options.dropdownAdapter = Utils.Decorate(
         options.dropdownAdapter,
         AttachBody
@@ -3275,13 +3318,14 @@ define('select2/defaults',[
       },
       minimumInputLength: 0,
       maximumInputLength: 0,
-      theme: 'default',
+      minimumResultsForSearch: 0,
       templateResult: function (result) {
         return result.text;
       },
       templateSelection: function (selection) {
         return selection.text;
-      }
+      },
+      theme: 'default'
     };
   };
 

+ 63 - 19
dist/js/select2.full.js

@@ -12528,6 +12528,42 @@ define('select2/dropdown/attachBody',[
   return AttachBody;
 });
 
+define('select2/dropdown/minimumResultsForSearch',[
+
+], function () {
+  function countResults (data) {
+    count = 0;
+
+    for (var d = 0; d < data.length; d++) {
+      var item = data[d];
+
+      if (item.children) {
+        count += countResults(item.children);
+      } else {
+        count++;
+      }
+    }
+
+    return count;
+  }
+
+  function MinimumResultsForSearch (decorated, $element, options, dataAdapter) {
+    this.minimumResultsForSearch = options.get('minimumResultsForSearch');
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
+    if (countResults(params.data) < this.minimumResultsForSearch) {
+      return false;
+    }
+
+    return decorated.call(this, params);
+  };
+
+  return MinimumResultsForSearch;
+});
+
 define('select2/i18n/en',[],function () {
   return {
     errorLoading: function () {
@@ -12598,6 +12634,7 @@ define('select2/defaults',[
   './dropdown/hidePlaceholder',
   './dropdown/infiniteScroll',
   './dropdown/attachBody',
+  './dropdown/minimumResultsForSearch',
 
   './i18n/en'
 ], function ($, ResultsList,
@@ -12611,7 +12648,7 @@ define('select2/defaults',[
              MinimumInputLength, MaximumInputLength,
 
              Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
-             AttachBody,
+             AttachBody, MinimumResultsForSearch,
 
              EnglishTranslation) {
   function Defaults () {
@@ -12629,25 +12666,24 @@ define('select2/defaults',[
       } else {
         options.dataAdapter = SelectData;
       }
-    }
-
 
-    if (options.minimumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MinimumInputLength
-      );
-    }
+      if (options.minimumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MinimumInputLength
+        );
+      }
 
-    if (options.maximumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MaximumInputLength
-      );
-    }
+      if (options.maximumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MaximumInputLength
+        );
+      }
 
-    if (options.tags != null) {
-      options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      if (options.tags != null) {
+        options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      }
     }
 
     if (options.resultsAdapter == null) {
@@ -12677,6 +12713,13 @@ define('select2/defaults',[
         options.dropdownAdapter = SearchableDropdown;
       }
 
+      if (options.minimumResultsForSearch > 0) {
+        options.dropdownAdapter = Utils.Decorate(
+          options.dropdownAdapter,
+          MinimumResultsForSearch
+        );
+      }
+
       options.dropdownAdapter = Utils.Decorate(
         options.dropdownAdapter,
         AttachBody
@@ -12810,13 +12853,14 @@ define('select2/defaults',[
       },
       minimumInputLength: 0,
       maximumInputLength: 0,
-      theme: 'default',
+      minimumResultsForSearch: 0,
       templateResult: function (result) {
         return result.text;
       },
       templateSelection: function (selection) {
         return selection.text;
-      }
+      },
+      theme: 'default'
     };
   };
 

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


+ 63 - 19
dist/js/select2.js

@@ -3421,6 +3421,42 @@ define('select2/dropdown/attachBody',[
   return AttachBody;
 });
 
+define('select2/dropdown/minimumResultsForSearch',[
+
+], function () {
+  function countResults (data) {
+    count = 0;
+
+    for (var d = 0; d < data.length; d++) {
+      var item = data[d];
+
+      if (item.children) {
+        count += countResults(item.children);
+      } else {
+        count++;
+      }
+    }
+
+    return count;
+  }
+
+  function MinimumResultsForSearch (decorated, $element, options, dataAdapter) {
+    this.minimumResultsForSearch = options.get('minimumResultsForSearch');
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
+    if (countResults(params.data) < this.minimumResultsForSearch) {
+      return false;
+    }
+
+    return decorated.call(this, params);
+  };
+
+  return MinimumResultsForSearch;
+});
+
 define('select2/i18n/en',[],function () {
   return {
     errorLoading: function () {
@@ -3491,6 +3527,7 @@ define('select2/defaults',[
   './dropdown/hidePlaceholder',
   './dropdown/infiniteScroll',
   './dropdown/attachBody',
+  './dropdown/minimumResultsForSearch',
 
   './i18n/en'
 ], function ($, ResultsList,
@@ -3504,7 +3541,7 @@ define('select2/defaults',[
              MinimumInputLength, MaximumInputLength,
 
              Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
-             AttachBody,
+             AttachBody, MinimumResultsForSearch,
 
              EnglishTranslation) {
   function Defaults () {
@@ -3522,25 +3559,24 @@ define('select2/defaults',[
       } else {
         options.dataAdapter = SelectData;
       }
-    }
-
 
-    if (options.minimumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MinimumInputLength
-      );
-    }
+      if (options.minimumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MinimumInputLength
+        );
+      }
 
-    if (options.maximumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MaximumInputLength
-      );
-    }
+      if (options.maximumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MaximumInputLength
+        );
+      }
 
-    if (options.tags != null) {
-      options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      if (options.tags != null) {
+        options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      }
     }
 
     if (options.resultsAdapter == null) {
@@ -3570,6 +3606,13 @@ define('select2/defaults',[
         options.dropdownAdapter = SearchableDropdown;
       }
 
+      if (options.minimumResultsForSearch > 0) {
+        options.dropdownAdapter = Utils.Decorate(
+          options.dropdownAdapter,
+          MinimumResultsForSearch
+        );
+      }
+
       options.dropdownAdapter = Utils.Decorate(
         options.dropdownAdapter,
         AttachBody
@@ -3703,13 +3746,14 @@ define('select2/defaults',[
       },
       minimumInputLength: 0,
       maximumInputLength: 0,
-      theme: 'default',
+      minimumResultsForSearch: 0,
       templateResult: function (result) {
         return result.text;
       },
       templateSelection: function (selection) {
         return selection.text;
-      }
+      },
+      theme: 'default'
     };
   };
 

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


+ 27 - 19
src/js/select2/defaults.js

@@ -24,6 +24,7 @@ define([
   './dropdown/hidePlaceholder',
   './dropdown/infiniteScroll',
   './dropdown/attachBody',
+  './dropdown/minimumResultsForSearch',
 
   './i18n/en'
 ], function ($, ResultsList,
@@ -37,7 +38,7 @@ define([
              MinimumInputLength, MaximumInputLength,
 
              Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
-             AttachBody,
+             AttachBody, MinimumResultsForSearch,
 
              EnglishTranslation) {
   function Defaults () {
@@ -55,25 +56,24 @@ define([
       } else {
         options.dataAdapter = SelectData;
       }
-    }
-
 
-    if (options.minimumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MinimumInputLength
-      );
-    }
+      if (options.minimumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MinimumInputLength
+        );
+      }
 
-    if (options.maximumInputLength > 0) {
-      options.dataAdapter = Utils.Decorate(
-        options.dataAdapter,
-        MaximumInputLength
-      );
-    }
+      if (options.maximumInputLength > 0) {
+        options.dataAdapter = Utils.Decorate(
+          options.dataAdapter,
+          MaximumInputLength
+        );
+      }
 
-    if (options.tags != null) {
-      options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      if (options.tags != null) {
+        options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
+      }
     }
 
     if (options.resultsAdapter == null) {
@@ -103,6 +103,13 @@ define([
         options.dropdownAdapter = SearchableDropdown;
       }
 
+      if (options.minimumResultsForSearch > 0) {
+        options.dropdownAdapter = Utils.Decorate(
+          options.dropdownAdapter,
+          MinimumResultsForSearch
+        );
+      }
+
       options.dropdownAdapter = Utils.Decorate(
         options.dropdownAdapter,
         AttachBody
@@ -236,13 +243,14 @@ define([
       },
       minimumInputLength: 0,
       maximumInputLength: 0,
-      theme: 'default',
+      minimumResultsForSearch: 0,
       templateResult: function (result) {
         return result.text;
       },
       templateSelection: function (selection) {
         return selection.text;
-      }
+      },
+      theme: 'default'
     };
   };
 

+ 35 - 0
src/js/select2/dropdown/minimumResultsForSearch.js

@@ -0,0 +1,35 @@
+define([
+
+], function () {
+  function countResults (data) {
+    count = 0;
+
+    for (var d = 0; d < data.length; d++) {
+      var item = data[d];
+
+      if (item.children) {
+        count += countResults(item.children);
+      } else {
+        count++;
+      }
+    }
+
+    return count;
+  }
+
+  function MinimumResultsForSearch (decorated, $element, options, dataAdapter) {
+    this.minimumResultsForSearch = options.get('minimumResultsForSearch');
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
+    if (countResults(params.data) < this.minimumResultsForSearch) {
+      return false;
+    }
+
+    return decorated.call(this, params);
+  };
+
+  return MinimumResultsForSearch;
+});

+ 1 - 1
src/scss/_dropdown.scss

@@ -62,7 +62,7 @@
     width: 100%;
   }
 
-  .select2-search--hide {
+  &.select2-search--hide {
     display: none;
   }
 }

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