Browse Source

Add getters and setters for options

This also gets an object set up that handles the default options.
Kevin Brown 10 năm trước cách đây
mục cha
commit
6d5b0a6c59

+ 66 - 23
dist/js/select2.amd.full.js

@@ -710,7 +710,7 @@ define('select2/data/ajax',[
   'jquery'
 ], function (ArrayAdapter, Utils, $) {
   function AjaxAdapter ($element, options) {
-    this.ajaxOptions = options.options.ajax;
+    this.ajaxOptions = options.get('ajax');
 
     this.processResults = this.ajaxOptions.processResults ||
       function (results) {
@@ -810,7 +810,7 @@ define('select2/dropdown/search',[
   return Search;
 });
 
-define('select2/options',[
+define('select2/defaults',[
   './results',
 
   './selection/single',
@@ -827,32 +827,72 @@ define('select2/options',[
 ], function (ResultsList, SingleSelection, MultipleSelection, Utils,
              SelectData, ArrayData, AjaxData,
              Dropdown, Search) {
-  function Options (options) {
-    this.options = options;
+  function Defaults () {
+    this.reset();
+  }
 
-    if (options.ajax) {
-      this.dataAdapter = this.dataAdapter || AjaxData;
-    } else if (options.data) {
-      this.dataAdapter = this.dataAdapter || ArrayData;
-    } else {
-      this.dataAdapter = this.dataAdapter || SelectData;
+  Defaults.prototype.apply = function (options) {
+    options = $.extend({}, options, this.defaults);
+
+    if (options.dataAdapter == null) {
+      if (options.ajax) {
+        options.dataAdapter = AjaxData;
+      } else if (options.data) {
+        options.dataAdapter = ArrayData;
+      } else {
+        options.dataAdapter = SelectData;
+      }
+    }
+
+    if (options.resultsAdapter == null) {
+      options.resultsAdapter = ResultsList;
     }
 
-    var SearchableDropdown = Utils.Decorate(Dropdown, Search);
+    if (options.dropdownAdapter == null) {
+      var SearchableDropdown = Utils.Decorate(Dropdown, Search);
 
-    this.resultsAdapter = ResultsList;
-    this.dropdownAdapter = options.dropdownAdapter || SearchableDropdown;
-    this.selectionAdapter = options.selectionAdapter;
+      options.dropdownAdapter = SearchableDropdown;
+    }
 
-    if (this.selectionAdapter == null) {
-      if (this.options.multiple) {
-        this.selectionAdapter = MultipleSelection;
+    if (options.selectionAdapter == null) {
+      if (options.multiple) {
+        options.selectionAdapter = MultipleSelection;
       } else {
-        this.selectionAdapter = SingleSelection;
+        options.selectionAdapter = SingleSelection;
       }
     }
+
+    return options;
+  };
+
+  Defaults.prototype.reset = function () {
+    this.defaults = { };
+  };
+
+  var defaults = new Defaults();
+
+  return defaults;
+});
+
+define('select2/options',[
+  './defaults'
+], function (Defaults) {
+  function Options (options) {
+    this.options = Defaults.apply(options);
   }
 
+  Options.prototype.fromElement = function ($e) {
+    return this;
+  };
+
+  Options.prototype.get = function (key) {
+    return this.options[key];
+  };
+
+  Options.prototype.set = function (key, val) {
+    this.options[key] = val;
+  };
+
   return Options;
 });
 
@@ -874,7 +914,8 @@ define('select2/core',[
 
     // Set up containers and adapters
 
-    this.data = new this.options.dataAdapter($element, this.options);
+    var DataAdapter = this.options.get('dataAdapter');
+    this.data = new DataAdapter($element, this.options);
 
     var $container = this.render();
     this.$container = $container;
@@ -883,22 +924,24 @@ define('select2/core',[
 
     $container.width($element.outerWidth(false));
 
-    this.selection = new this.options.selectionAdapter($element, this.options);
+    var SelectionAdapter = this.options.get('selectionAdapter');
+    this.selection = new SelectionAdapter($element, this.options);
 
     var $selectionContainer = $container.find('.selection');
     var $selection = this.selection.render();
 
     $selectionContainer.append($selection);
 
-    this.dropdown = new this.options.dropdownAdapter($element, this.options);
+    var DropdownAdapter = this.options.get('dropdownAdapter');
+    this.dropdown = new DropdownAdapter($element, this.options);
 
     var $dropdownContainer = $container.find('.dropdown-wrapper');
     var $dropdown = this.dropdown.render();
 
     $dropdownContainer.append($dropdown);
 
-    this.results = new this.options.resultsAdapter(
-      $element, this.options, this.data);
+    var ResultsAdapter = this.options.get('resultsAdapter');
+    this.results = new ResultsAdapter($element, this.options, this.data);
 
     var $resultsContainer = $dropdown.find('.results');
     var $results = this.results.render();

+ 66 - 23
dist/js/select2.amd.js

@@ -710,7 +710,7 @@ define('select2/data/ajax',[
   'jquery'
 ], function (ArrayAdapter, Utils, $) {
   function AjaxAdapter ($element, options) {
-    this.ajaxOptions = options.options.ajax;
+    this.ajaxOptions = options.get('ajax');
 
     this.processResults = this.ajaxOptions.processResults ||
       function (results) {
@@ -810,7 +810,7 @@ define('select2/dropdown/search',[
   return Search;
 });
 
-define('select2/options',[
+define('select2/defaults',[
   './results',
 
   './selection/single',
@@ -827,32 +827,72 @@ define('select2/options',[
 ], function (ResultsList, SingleSelection, MultipleSelection, Utils,
              SelectData, ArrayData, AjaxData,
              Dropdown, Search) {
-  function Options (options) {
-    this.options = options;
+  function Defaults () {
+    this.reset();
+  }
 
-    if (options.ajax) {
-      this.dataAdapter = this.dataAdapter || AjaxData;
-    } else if (options.data) {
-      this.dataAdapter = this.dataAdapter || ArrayData;
-    } else {
-      this.dataAdapter = this.dataAdapter || SelectData;
+  Defaults.prototype.apply = function (options) {
+    options = $.extend({}, options, this.defaults);
+
+    if (options.dataAdapter == null) {
+      if (options.ajax) {
+        options.dataAdapter = AjaxData;
+      } else if (options.data) {
+        options.dataAdapter = ArrayData;
+      } else {
+        options.dataAdapter = SelectData;
+      }
+    }
+
+    if (options.resultsAdapter == null) {
+      options.resultsAdapter = ResultsList;
     }
 
-    var SearchableDropdown = Utils.Decorate(Dropdown, Search);
+    if (options.dropdownAdapter == null) {
+      var SearchableDropdown = Utils.Decorate(Dropdown, Search);
 
-    this.resultsAdapter = ResultsList;
-    this.dropdownAdapter = options.dropdownAdapter || SearchableDropdown;
-    this.selectionAdapter = options.selectionAdapter;
+      options.dropdownAdapter = SearchableDropdown;
+    }
 
-    if (this.selectionAdapter == null) {
-      if (this.options.multiple) {
-        this.selectionAdapter = MultipleSelection;
+    if (options.selectionAdapter == null) {
+      if (options.multiple) {
+        options.selectionAdapter = MultipleSelection;
       } else {
-        this.selectionAdapter = SingleSelection;
+        options.selectionAdapter = SingleSelection;
       }
     }
+
+    return options;
+  };
+
+  Defaults.prototype.reset = function () {
+    this.defaults = { };
+  };
+
+  var defaults = new Defaults();
+
+  return defaults;
+});
+
+define('select2/options',[
+  './defaults'
+], function (Defaults) {
+  function Options (options) {
+    this.options = Defaults.apply(options);
   }
 
+  Options.prototype.fromElement = function ($e) {
+    return this;
+  };
+
+  Options.prototype.get = function (key) {
+    return this.options[key];
+  };
+
+  Options.prototype.set = function (key, val) {
+    this.options[key] = val;
+  };
+
   return Options;
 });
 
@@ -874,7 +914,8 @@ define('select2/core',[
 
     // Set up containers and adapters
 
-    this.data = new this.options.dataAdapter($element, this.options);
+    var DataAdapter = this.options.get('dataAdapter');
+    this.data = new DataAdapter($element, this.options);
 
     var $container = this.render();
     this.$container = $container;
@@ -883,22 +924,24 @@ define('select2/core',[
 
     $container.width($element.outerWidth(false));
 
-    this.selection = new this.options.selectionAdapter($element, this.options);
+    var SelectionAdapter = this.options.get('selectionAdapter');
+    this.selection = new SelectionAdapter($element, this.options);
 
     var $selectionContainer = $container.find('.selection');
     var $selection = this.selection.render();
 
     $selectionContainer.append($selection);
 
-    this.dropdown = new this.options.dropdownAdapter($element, this.options);
+    var DropdownAdapter = this.options.get('dropdownAdapter');
+    this.dropdown = new DropdownAdapter($element, this.options);
 
     var $dropdownContainer = $container.find('.dropdown-wrapper');
     var $dropdown = this.dropdown.render();
 
     $dropdownContainer.append($dropdown);
 
-    this.results = new this.options.resultsAdapter(
-      $element, this.options, this.data);
+    var ResultsAdapter = this.options.get('resultsAdapter');
+    this.results = new ResultsAdapter($element, this.options, this.data);
 
     var $resultsContainer = $dropdown.find('.results');
     var $results = this.results.render();

+ 66 - 23
dist/js/select2.full.js

@@ -10248,7 +10248,7 @@ define('select2/data/ajax',[
   'jquery'
 ], function (ArrayAdapter, Utils, $) {
   function AjaxAdapter ($element, options) {
-    this.ajaxOptions = options.options.ajax;
+    this.ajaxOptions = options.get('ajax');
 
     this.processResults = this.ajaxOptions.processResults ||
       function (results) {
@@ -10348,7 +10348,7 @@ define('select2/dropdown/search',[
   return Search;
 });
 
-define('select2/options',[
+define('select2/defaults',[
   './results',
 
   './selection/single',
@@ -10365,32 +10365,72 @@ define('select2/options',[
 ], function (ResultsList, SingleSelection, MultipleSelection, Utils,
              SelectData, ArrayData, AjaxData,
              Dropdown, Search) {
-  function Options (options) {
-    this.options = options;
+  function Defaults () {
+    this.reset();
+  }
 
-    if (options.ajax) {
-      this.dataAdapter = this.dataAdapter || AjaxData;
-    } else if (options.data) {
-      this.dataAdapter = this.dataAdapter || ArrayData;
-    } else {
-      this.dataAdapter = this.dataAdapter || SelectData;
+  Defaults.prototype.apply = function (options) {
+    options = $.extend({}, options, this.defaults);
+
+    if (options.dataAdapter == null) {
+      if (options.ajax) {
+        options.dataAdapter = AjaxData;
+      } else if (options.data) {
+        options.dataAdapter = ArrayData;
+      } else {
+        options.dataAdapter = SelectData;
+      }
+    }
+
+    if (options.resultsAdapter == null) {
+      options.resultsAdapter = ResultsList;
     }
 
-    var SearchableDropdown = Utils.Decorate(Dropdown, Search);
+    if (options.dropdownAdapter == null) {
+      var SearchableDropdown = Utils.Decorate(Dropdown, Search);
 
-    this.resultsAdapter = ResultsList;
-    this.dropdownAdapter = options.dropdownAdapter || SearchableDropdown;
-    this.selectionAdapter = options.selectionAdapter;
+      options.dropdownAdapter = SearchableDropdown;
+    }
 
-    if (this.selectionAdapter == null) {
-      if (this.options.multiple) {
-        this.selectionAdapter = MultipleSelection;
+    if (options.selectionAdapter == null) {
+      if (options.multiple) {
+        options.selectionAdapter = MultipleSelection;
       } else {
-        this.selectionAdapter = SingleSelection;
+        options.selectionAdapter = SingleSelection;
       }
     }
+
+    return options;
+  };
+
+  Defaults.prototype.reset = function () {
+    this.defaults = { };
+  };
+
+  var defaults = new Defaults();
+
+  return defaults;
+});
+
+define('select2/options',[
+  './defaults'
+], function (Defaults) {
+  function Options (options) {
+    this.options = Defaults.apply(options);
   }
 
+  Options.prototype.fromElement = function ($e) {
+    return this;
+  };
+
+  Options.prototype.get = function (key) {
+    return this.options[key];
+  };
+
+  Options.prototype.set = function (key, val) {
+    this.options[key] = val;
+  };
+
   return Options;
 });
 
@@ -10412,7 +10452,8 @@ define('select2/core',[
 
     // Set up containers and adapters
 
-    this.data = new this.options.dataAdapter($element, this.options);
+    var DataAdapter = this.options.get('dataAdapter');
+    this.data = new DataAdapter($element, this.options);
 
     var $container = this.render();
     this.$container = $container;
@@ -10421,22 +10462,24 @@ define('select2/core',[
 
     $container.width($element.outerWidth(false));
 
-    this.selection = new this.options.selectionAdapter($element, this.options);
+    var SelectionAdapter = this.options.get('selectionAdapter');
+    this.selection = new SelectionAdapter($element, this.options);
 
     var $selectionContainer = $container.find('.selection');
     var $selection = this.selection.render();
 
     $selectionContainer.append($selection);
 
-    this.dropdown = new this.options.dropdownAdapter($element, this.options);
+    var DropdownAdapter = this.options.get('dropdownAdapter');
+    this.dropdown = new DropdownAdapter($element, this.options);
 
     var $dropdownContainer = $container.find('.dropdown-wrapper');
     var $dropdown = this.dropdown.render();
 
     $dropdownContainer.append($dropdown);
 
-    this.results = new this.options.resultsAdapter(
-      $element, this.options, this.data);
+    var ResultsAdapter = this.options.get('resultsAdapter');
+    this.results = new ResultsAdapter($element, this.options, this.data);
 
     var $resultsContainer = $dropdown.find('.results');
     var $results = this.results.render();

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/js/select2.full.min.js


+ 66 - 23
dist/js/select2.js

@@ -1139,7 +1139,7 @@ define('select2/data/ajax',[
   'jquery'
 ], function (ArrayAdapter, Utils, $) {
   function AjaxAdapter ($element, options) {
-    this.ajaxOptions = options.options.ajax;
+    this.ajaxOptions = options.get('ajax');
 
     this.processResults = this.ajaxOptions.processResults ||
       function (results) {
@@ -1239,7 +1239,7 @@ define('select2/dropdown/search',[
   return Search;
 });
 
-define('select2/options',[
+define('select2/defaults',[
   './results',
 
   './selection/single',
@@ -1256,32 +1256,72 @@ define('select2/options',[
 ], function (ResultsList, SingleSelection, MultipleSelection, Utils,
              SelectData, ArrayData, AjaxData,
              Dropdown, Search) {
-  function Options (options) {
-    this.options = options;
+  function Defaults () {
+    this.reset();
+  }
 
-    if (options.ajax) {
-      this.dataAdapter = this.dataAdapter || AjaxData;
-    } else if (options.data) {
-      this.dataAdapter = this.dataAdapter || ArrayData;
-    } else {
-      this.dataAdapter = this.dataAdapter || SelectData;
+  Defaults.prototype.apply = function (options) {
+    options = $.extend({}, options, this.defaults);
+
+    if (options.dataAdapter == null) {
+      if (options.ajax) {
+        options.dataAdapter = AjaxData;
+      } else if (options.data) {
+        options.dataAdapter = ArrayData;
+      } else {
+        options.dataAdapter = SelectData;
+      }
+    }
+
+    if (options.resultsAdapter == null) {
+      options.resultsAdapter = ResultsList;
     }
 
-    var SearchableDropdown = Utils.Decorate(Dropdown, Search);
+    if (options.dropdownAdapter == null) {
+      var SearchableDropdown = Utils.Decorate(Dropdown, Search);
 
-    this.resultsAdapter = ResultsList;
-    this.dropdownAdapter = options.dropdownAdapter || SearchableDropdown;
-    this.selectionAdapter = options.selectionAdapter;
+      options.dropdownAdapter = SearchableDropdown;
+    }
 
-    if (this.selectionAdapter == null) {
-      if (this.options.multiple) {
-        this.selectionAdapter = MultipleSelection;
+    if (options.selectionAdapter == null) {
+      if (options.multiple) {
+        options.selectionAdapter = MultipleSelection;
       } else {
-        this.selectionAdapter = SingleSelection;
+        options.selectionAdapter = SingleSelection;
       }
     }
+
+    return options;
+  };
+
+  Defaults.prototype.reset = function () {
+    this.defaults = { };
+  };
+
+  var defaults = new Defaults();
+
+  return defaults;
+});
+
+define('select2/options',[
+  './defaults'
+], function (Defaults) {
+  function Options (options) {
+    this.options = Defaults.apply(options);
   }
 
+  Options.prototype.fromElement = function ($e) {
+    return this;
+  };
+
+  Options.prototype.get = function (key) {
+    return this.options[key];
+  };
+
+  Options.prototype.set = function (key, val) {
+    this.options[key] = val;
+  };
+
   return Options;
 });
 
@@ -1303,7 +1343,8 @@ define('select2/core',[
 
     // Set up containers and adapters
 
-    this.data = new this.options.dataAdapter($element, this.options);
+    var DataAdapter = this.options.get('dataAdapter');
+    this.data = new DataAdapter($element, this.options);
 
     var $container = this.render();
     this.$container = $container;
@@ -1312,22 +1353,24 @@ define('select2/core',[
 
     $container.width($element.outerWidth(false));
 
-    this.selection = new this.options.selectionAdapter($element, this.options);
+    var SelectionAdapter = this.options.get('selectionAdapter');
+    this.selection = new SelectionAdapter($element, this.options);
 
     var $selectionContainer = $container.find('.selection');
     var $selection = this.selection.render();
 
     $selectionContainer.append($selection);
 
-    this.dropdown = new this.options.dropdownAdapter($element, this.options);
+    var DropdownAdapter = this.options.get('dropdownAdapter');
+    this.dropdown = new DropdownAdapter($element, this.options);
 
     var $dropdownContainer = $container.find('.dropdown-wrapper');
     var $dropdown = this.dropdown.render();
 
     $dropdownContainer.append($dropdown);
 
-    this.results = new this.options.resultsAdapter(
-      $element, this.options, this.data);
+    var ResultsAdapter = this.options.get('resultsAdapter');
+    this.results = new ResultsAdapter($element, this.options, this.data);
 
     var $resultsContainer = $dropdown.find('.results');
     var $results = this.results.render();

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/js/select2.min.js


+ 8 - 5
src/js/select2/core.js

@@ -16,7 +16,8 @@ define([
 
     // Set up containers and adapters
 
-    this.data = new this.options.dataAdapter($element, this.options);
+    var DataAdapter = this.options.get('dataAdapter');
+    this.data = new DataAdapter($element, this.options);
 
     var $container = this.render();
     this.$container = $container;
@@ -25,22 +26,24 @@ define([
 
     $container.width($element.outerWidth(false));
 
-    this.selection = new this.options.selectionAdapter($element, this.options);
+    var SelectionAdapter = this.options.get('selectionAdapter');
+    this.selection = new SelectionAdapter($element, this.options);
 
     var $selectionContainer = $container.find('.selection');
     var $selection = this.selection.render();
 
     $selectionContainer.append($selection);
 
-    this.dropdown = new this.options.dropdownAdapter($element, this.options);
+    var DropdownAdapter = this.options.get('dropdownAdapter');
+    this.dropdown = new DropdownAdapter($element, this.options);
 
     var $dropdownContainer = $container.find('.dropdown-wrapper');
     var $dropdown = this.dropdown.render();
 
     $dropdownContainer.append($dropdown);
 
-    this.results = new this.options.resultsAdapter(
-      $element, this.options, this.data);
+    var ResultsAdapter = this.options.get('resultsAdapter');
+    this.results = new ResultsAdapter($element, this.options, this.data);
 
     var $resultsContainer = $dropdown.find('.results');
     var $results = this.results.render();

+ 1 - 1
src/js/select2/data/ajax.js

@@ -4,7 +4,7 @@ define([
   'jquery'
 ], function (ArrayAdapter, Utils, $) {
   function AjaxAdapter ($element, options) {
-    this.ajaxOptions = options.options.ajax;
+    this.ajaxOptions = options.get('ajax');
 
     this.processResults = this.ajaxOptions.processResults ||
       function (results) {

+ 63 - 0
src/js/select2/defaults.js

@@ -0,0 +1,63 @@
+define([
+  './results',
+
+  './selection/single',
+  './selection/multiple',
+
+  './utils',
+
+  './data/select',
+  './data/array',
+  './data/ajax',
+
+  './dropdown',
+  './dropdown/search'
+], function (ResultsList, SingleSelection, MultipleSelection, Utils,
+             SelectData, ArrayData, AjaxData,
+             Dropdown, Search) {
+  function Defaults () {
+    this.reset();
+  }
+
+  Defaults.prototype.apply = function (options) {
+    options = $.extend({}, options, this.defaults);
+
+    if (options.dataAdapter == null) {
+      if (options.ajax) {
+        options.dataAdapter = AjaxData;
+      } else if (options.data) {
+        options.dataAdapter = ArrayData;
+      } else {
+        options.dataAdapter = SelectData;
+      }
+    }
+
+    if (options.resultsAdapter == null) {
+      options.resultsAdapter = ResultsList;
+    }
+
+    if (options.dropdownAdapter == null) {
+      var SearchableDropdown = Utils.Decorate(Dropdown, Search);
+
+      options.dropdownAdapter = SearchableDropdown;
+    }
+
+    if (options.selectionAdapter == null) {
+      if (options.multiple) {
+        options.selectionAdapter = MultipleSelection;
+      } else {
+        options.selectionAdapter = SingleSelection;
+      }
+    }
+
+    return options;
+  };
+
+  Defaults.prototype.reset = function () {
+    this.defaults = { };
+  };
+
+  var defaults = new Defaults();
+
+  return defaults;
+});

+ 13 - 37
src/js/select2/options.js

@@ -1,45 +1,21 @@
 define([
-  './results',
-
-  './selection/single',
-  './selection/multiple',
-
-  './utils',
-
-  './data/select',
-  './data/array',
-  './data/ajax',
-
-  './dropdown',
-  './dropdown/search'
-], function (ResultsList, SingleSelection, MultipleSelection, Utils,
-             SelectData, ArrayData, AjaxData,
-             Dropdown, Search) {
+  './defaults'
+], function (Defaults) {
   function Options (options) {
-    this.options = options;
-
-    if (options.ajax) {
-      this.dataAdapter = this.dataAdapter || AjaxData;
-    } else if (options.data) {
-      this.dataAdapter = this.dataAdapter || ArrayData;
-    } else {
-      this.dataAdapter = this.dataAdapter || SelectData;
-    }
+    this.options = Defaults.apply(options);
+  }
 
-    var SearchableDropdown = Utils.Decorate(Dropdown, Search);
+  Options.prototype.fromElement = function ($e) {
+    return this;
+  };
 
-    this.resultsAdapter = ResultsList;
-    this.dropdownAdapter = options.dropdownAdapter || SearchableDropdown;
-    this.selectionAdapter = options.selectionAdapter;
+  Options.prototype.get = function (key) {
+    return this.options[key];
+  };
 
-    if (this.selectionAdapter == null) {
-      if (this.options.multiple) {
-        this.selectionAdapter = MultipleSelection;
-      } else {
-        this.selectionAdapter = SingleSelection;
-      }
-    }
-  }
+  Options.prototype.set = function (key, val) {
+    this.options[key] = val;
+  };
 
   return Options;
 });

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác