1
0
mirror of synced 2024-11-26 06:46:04 +03:00

Add getters and setters for options

This also gets an object set up that handles the default options.
This commit is contained in:
Kevin Brown 2014-10-16 18:19:15 -04:00
parent d0fe07f954
commit 6d5b0a6c59
10 changed files with 366 additions and 152 deletions

View File

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

View File

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

View File

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

File diff suppressed because one or more lines are too long

95
dist/js/select2.js vendored
View File

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

File diff suppressed because one or more lines are too long

View File

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

View File

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

63
src/js/select2/defaults.js vendored Normal file
View File

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

View File

@ -1,45 +1,21 @@
define([ define([
'./results', './defaults'
], function (Defaults) {
'./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 Options (options) { function Options (options) {
this.options = options; this.options = Defaults.apply(options);
if (options.ajax) {
this.dataAdapter = this.dataAdapter || AjaxData;
} else if (options.data) {
this.dataAdapter = this.dataAdapter || ArrayData;
} else {
this.dataAdapter = this.dataAdapter || SelectData;
}
var SearchableDropdown = Utils.Decorate(Dropdown, Search);
this.resultsAdapter = ResultsList;
this.dropdownAdapter = options.dropdownAdapter || SearchableDropdown;
this.selectionAdapter = options.selectionAdapter;
if (this.selectionAdapter == null) {
if (this.options.multiple) {
this.selectionAdapter = MultipleSelection;
} else {
this.selectionAdapter = SingleSelection;
}
}
} }
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; return Options;
}); });