瀏覽代碼

Added decorator to remove the placeholder object

This will remove the placeholder object from the results list.
Kevin Brown 10 年之前
父節點
當前提交
e57e0b1870

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

@@ -1395,6 +1395,49 @@ define('select2/dropdown/search',[
   return Search;
 });
 
+define('select2/dropdown/hidePlaceholder',[
+
+], function () {
+  function HidePlaceholder (decorated, $element, options, dataAdapter) {
+    this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  HidePlaceholder.prototype.append = function (decorated, data) {
+    data = this.removePlaceholder(data);
+
+    decorated.call(this, data);
+  };
+
+  HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
+    if (typeof placeholder === 'string') {
+      placeholder = {
+        id: '',
+        text: placeholder
+      };
+    }
+
+    return placeholder;
+  };
+
+  HidePlaceholder.prototype.removePlaceholder = function (_, data) {
+    var modifiedData = data.slice(0);
+
+    for (var d = data.length - 1; d >= 0; d--) {
+      var item = data[d];
+
+      if (this.placeholder.id === item.id) {
+        modifiedData.splice(d, 1);
+      }
+    }
+
+    return modifiedData;
+  };
+
+  return HidePlaceholder;
+});
+
 define('select2/i18n/en',[],function () {
   return {
     noResults: function () {
@@ -1421,13 +1464,15 @@ define('select2/defaults',[
 
   './dropdown',
   './dropdown/search',
+  './dropdown/hidePlaceholder',
 
   './i18n/en'
 ], function ($, ResultsList,
              SingleSelection, MultipleSelection, Placeholder,
              Utils, Translation,
              SelectData, ArrayData, AjaxData, Tags,
-             Dropdown, Search, EnglishTranslation) {
+             Dropdown, Search, HidePlaceholder,
+             EnglishTranslation) {
   function Defaults () {
     this.reset();
   }
@@ -1472,6 +1517,11 @@ define('select2/defaults',[
           options.selectionAdapter,
           Placeholder
         );
+
+        options.resultsAdapter = Utils.Decorate(
+          options.resultsAdapter,
+          HidePlaceholder
+        );
       }
     }
 

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

@@ -1395,6 +1395,49 @@ define('select2/dropdown/search',[
   return Search;
 });
 
+define('select2/dropdown/hidePlaceholder',[
+
+], function () {
+  function HidePlaceholder (decorated, $element, options, dataAdapter) {
+    this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  HidePlaceholder.prototype.append = function (decorated, data) {
+    data = this.removePlaceholder(data);
+
+    decorated.call(this, data);
+  };
+
+  HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
+    if (typeof placeholder === 'string') {
+      placeholder = {
+        id: '',
+        text: placeholder
+      };
+    }
+
+    return placeholder;
+  };
+
+  HidePlaceholder.prototype.removePlaceholder = function (_, data) {
+    var modifiedData = data.slice(0);
+
+    for (var d = data.length - 1; d >= 0; d--) {
+      var item = data[d];
+
+      if (this.placeholder.id === item.id) {
+        modifiedData.splice(d, 1);
+      }
+    }
+
+    return modifiedData;
+  };
+
+  return HidePlaceholder;
+});
+
 define('select2/i18n/en',[],function () {
   return {
     noResults: function () {
@@ -1421,13 +1464,15 @@ define('select2/defaults',[
 
   './dropdown',
   './dropdown/search',
+  './dropdown/hidePlaceholder',
 
   './i18n/en'
 ], function ($, ResultsList,
              SingleSelection, MultipleSelection, Placeholder,
              Utils, Translation,
              SelectData, ArrayData, AjaxData, Tags,
-             Dropdown, Search, EnglishTranslation) {
+             Dropdown, Search, HidePlaceholder,
+             EnglishTranslation) {
   function Defaults () {
     this.reset();
   }
@@ -1472,6 +1517,11 @@ define('select2/defaults',[
           options.selectionAdapter,
           Placeholder
         );
+
+        options.resultsAdapter = Utils.Decorate(
+          options.resultsAdapter,
+          HidePlaceholder
+        );
       }
     }
 

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

@@ -10930,6 +10930,49 @@ define('select2/dropdown/search',[
   return Search;
 });
 
+define('select2/dropdown/hidePlaceholder',[
+
+], function () {
+  function HidePlaceholder (decorated, $element, options, dataAdapter) {
+    this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  HidePlaceholder.prototype.append = function (decorated, data) {
+    data = this.removePlaceholder(data);
+
+    decorated.call(this, data);
+  };
+
+  HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
+    if (typeof placeholder === 'string') {
+      placeholder = {
+        id: '',
+        text: placeholder
+      };
+    }
+
+    return placeholder;
+  };
+
+  HidePlaceholder.prototype.removePlaceholder = function (_, data) {
+    var modifiedData = data.slice(0);
+
+    for (var d = data.length - 1; d >= 0; d--) {
+      var item = data[d];
+
+      if (this.placeholder.id === item.id) {
+        modifiedData.splice(d, 1);
+      }
+    }
+
+    return modifiedData;
+  };
+
+  return HidePlaceholder;
+});
+
 define('select2/i18n/en',[],function () {
   return {
     noResults: function () {
@@ -10956,13 +10999,15 @@ define('select2/defaults',[
 
   './dropdown',
   './dropdown/search',
+  './dropdown/hidePlaceholder',
 
   './i18n/en'
 ], function ($, ResultsList,
              SingleSelection, MultipleSelection, Placeholder,
              Utils, Translation,
              SelectData, ArrayData, AjaxData, Tags,
-             Dropdown, Search, EnglishTranslation) {
+             Dropdown, Search, HidePlaceholder,
+             EnglishTranslation) {
   function Defaults () {
     this.reset();
   }
@@ -11007,6 +11052,11 @@ define('select2/defaults',[
           options.selectionAdapter,
           Placeholder
         );
+
+        options.resultsAdapter = Utils.Decorate(
+          options.resultsAdapter,
+          HidePlaceholder
+        );
       }
     }
 

文件差異過大導致無法顯示
+ 0 - 0
dist/js/select2.full.min.js


+ 51 - 1
dist/js/select2.js

@@ -1823,6 +1823,49 @@ define('select2/dropdown/search',[
   return Search;
 });
 
+define('select2/dropdown/hidePlaceholder',[
+
+], function () {
+  function HidePlaceholder (decorated, $element, options, dataAdapter) {
+    this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  HidePlaceholder.prototype.append = function (decorated, data) {
+    data = this.removePlaceholder(data);
+
+    decorated.call(this, data);
+  };
+
+  HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
+    if (typeof placeholder === 'string') {
+      placeholder = {
+        id: '',
+        text: placeholder
+      };
+    }
+
+    return placeholder;
+  };
+
+  HidePlaceholder.prototype.removePlaceholder = function (_, data) {
+    var modifiedData = data.slice(0);
+
+    for (var d = data.length - 1; d >= 0; d--) {
+      var item = data[d];
+
+      if (this.placeholder.id === item.id) {
+        modifiedData.splice(d, 1);
+      }
+    }
+
+    return modifiedData;
+  };
+
+  return HidePlaceholder;
+});
+
 define('select2/i18n/en',[],function () {
   return {
     noResults: function () {
@@ -1849,13 +1892,15 @@ define('select2/defaults',[
 
   './dropdown',
   './dropdown/search',
+  './dropdown/hidePlaceholder',
 
   './i18n/en'
 ], function ($, ResultsList,
              SingleSelection, MultipleSelection, Placeholder,
              Utils, Translation,
              SelectData, ArrayData, AjaxData, Tags,
-             Dropdown, Search, EnglishTranslation) {
+             Dropdown, Search, HidePlaceholder,
+             EnglishTranslation) {
   function Defaults () {
     this.reset();
   }
@@ -1900,6 +1945,11 @@ define('select2/defaults',[
           options.selectionAdapter,
           Placeholder
         );
+
+        options.resultsAdapter = Utils.Decorate(
+          options.resultsAdapter,
+          HidePlaceholder
+        );
       }
     }
 

文件差異過大導致無法顯示
+ 0 - 0
dist/js/select2.min.js


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

@@ -16,13 +16,15 @@ define([
 
   './dropdown',
   './dropdown/search',
+  './dropdown/hidePlaceholder',
 
   './i18n/en'
 ], function ($, ResultsList,
              SingleSelection, MultipleSelection, Placeholder,
              Utils, Translation,
              SelectData, ArrayData, AjaxData, Tags,
-             Dropdown, Search, EnglishTranslation) {
+             Dropdown, Search, HidePlaceholder,
+             EnglishTranslation) {
   function Defaults () {
     this.reset();
   }
@@ -67,6 +69,11 @@ define([
           options.selectionAdapter,
           Placeholder
         );
+
+        options.resultsAdapter = Utils.Decorate(
+          options.resultsAdapter,
+          HidePlaceholder
+        );
       }
     }
 

+ 42 - 0
src/js/select2/dropdown/hidePlaceholder.js

@@ -0,0 +1,42 @@
+define([
+
+], function () {
+  function HidePlaceholder (decorated, $element, options, dataAdapter) {
+    this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
+
+    decorated.call(this, $element, options, dataAdapter);
+  }
+
+  HidePlaceholder.prototype.append = function (decorated, data) {
+    data = this.removePlaceholder(data);
+
+    decorated.call(this, data);
+  };
+
+  HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
+    if (typeof placeholder === 'string') {
+      placeholder = {
+        id: '',
+        text: placeholder
+      };
+    }
+
+    return placeholder;
+  };
+
+  HidePlaceholder.prototype.removePlaceholder = function (_, data) {
+    var modifiedData = data.slice(0);
+
+    for (var d = data.length - 1; d >= 0; d--) {
+      var item = data[d];
+
+      if (this.placeholder.id === item.id) {
+        modifiedData.splice(d, 1);
+      }
+    }
+
+    return modifiedData;
+  };
+
+  return HidePlaceholder;
+});

部分文件因文件數量過多而無法顯示