1
0
mirror of synced 2024-11-25 22:36:03 +03:00

Expose the defaults

This exposes the default options for Select2 as
`$.fn.select2.defaults`. A default option can be set using the
`set(key, val)` option which is available. The key should use the
same formatting as the HTML data attributes.

This also adds some documentation about it.
This commit is contained in:
Kevin Brown 2015-01-17 21:27:53 -05:00
parent c5a3f34012
commit f427451853
12 changed files with 327 additions and 201 deletions

1
dist/js/i18n/it.js vendored Normal file
View File

@ -0,0 +1 @@
window.$=window.$||{},function(){$&&$.fn&&$.fn.select2&&$.fn.select2.amd&&(define=$.fn.select2.amd.define,require=$.fn.select2.amd.require),define("select2/i18n/it",[],function(){return{errorLoading:function(){return"I risultati non possono essere caricati."},inputTooLong:function(e){var t=e.input.length-e.maximum,n="Per favore cancella "+t+" caratter";return t!==1?n+="i":n+="e",n},inputTooShort:function(e){var t=e.minimum-e.input.length,n="Per favore inserisci "+t+" o più caratteri";return n},loadingMore:function(){return"Caricando più risultati…"},maximumSelected:function(e){var t="Puoi selezionare solo "+e.maximum+" element";return e.maximum!==1?t+="i":t+="o",t},noResults:function(){return"Nessun risultato trovato"},searching:function(){return"Sto cercando…"}}}),require("jquery.select2"),$.fn.select2.amd={define:define,require:require}}();

View File

@ -159,6 +159,40 @@ window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.se
};
};
Utils._convertData = function (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
};
return Utils;
});
@ -3740,6 +3774,17 @@ define('select2/defaults',[
};
};
Defaults.prototype.set = function (key, value) {
var camelKey = $.camelCase(key);
var data = {};
data[camelKey] = value;
var convertedData = Utils._convertData(data);
$.extend(this.defaults, convertedData);
};
var defaults = new Defaults();
return defaults;
@ -3747,8 +3792,9 @@ define('select2/defaults',[
define('select2/options',[
'jquery',
'./defaults'
], function ($, Defaults) {
'./defaults',
'./utils'
], function ($, Defaults, Utils) {
function Options (options, $element) {
this.options = options;
@ -3818,41 +3864,7 @@ define('select2/options',[
var data = $e.data();
function convertData (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
}
data = convertData(data);
data = Utils._convertData(data);
for (var key in data) {
if (excludedData.indexOf(key) > -1) {
@ -4720,8 +4732,9 @@ define('select2/dropdown/attachContainer',[
define('jquery.select2',[
'jquery',
'./select2/core'
], function ($, Select2) {
'./select2/core',
'./select2/defaults'
], function ($, Select2, Defaults) {
// Force jQuery.mousewheel to be loaded if it hasn't already
try {
require('jquery.mousewheel');
@ -4750,6 +4763,10 @@ define('jquery.select2',[
};
}
if ($.fn.select2.defaults == null) {
$.fn.select2.defaults = Defaults;
}
return Select2;
});

View File

@ -159,6 +159,40 @@ window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.se
};
};
Utils._convertData = function (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
};
return Utils;
});
@ -3740,6 +3774,17 @@ define('select2/defaults',[
};
};
Defaults.prototype.set = function (key, value) {
var camelKey = $.camelCase(key);
var data = {};
data[camelKey] = value;
var convertedData = Utils._convertData(data);
$.extend(this.defaults, convertedData);
};
var defaults = new Defaults();
return defaults;
@ -3747,8 +3792,9 @@ define('select2/defaults',[
define('select2/options',[
'jquery',
'./defaults'
], function ($, Defaults) {
'./defaults',
'./utils'
], function ($, Defaults, Utils) {
function Options (options, $element) {
this.options = options;
@ -3818,41 +3864,7 @@ define('select2/options',[
var data = $e.data();
function convertData (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
}
data = convertData(data);
data = Utils._convertData(data);
for (var key in data) {
if (excludedData.indexOf(key) > -1) {
@ -4366,8 +4378,9 @@ define('select2/core',[
define('jquery.select2',[
'jquery',
'./select2/core'
], function ($, Select2) {
'./select2/core',
'./select2/defaults'
], function ($, Select2, Defaults) {
// Force jQuery.mousewheel to be loaded if it hasn't already
try {
require('jquery.mousewheel');
@ -4396,6 +4409,10 @@ define('jquery.select2',[
};
}
if ($.fn.select2.defaults == null) {
$.fn.select2.defaults = Defaults;
}
return Select2;
});

View File

@ -9694,6 +9694,40 @@ define('select2/utils',[], function () {
};
};
Utils._convertData = function (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
};
return Utils;
});
@ -13275,6 +13309,17 @@ define('select2/defaults',[
};
};
Defaults.prototype.set = function (key, value) {
var camelKey = $.camelCase(key);
var data = {};
data[camelKey] = value;
var convertedData = Utils._convertData(data);
$.extend(this.defaults, convertedData);
};
var defaults = new Defaults();
return defaults;
@ -13282,8 +13327,9 @@ define('select2/defaults',[
define('select2/options',[
'jquery',
'./defaults'
], function ($, Defaults) {
'./defaults',
'./utils'
], function ($, Defaults, Utils) {
function Options (options, $element) {
this.options = options;
@ -13353,41 +13399,7 @@ define('select2/options',[
var data = $e.data();
function convertData (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
}
data = convertData(data);
data = Utils._convertData(data);
for (var key in data) {
if (excludedData.indexOf(key) > -1) {
@ -14255,8 +14267,9 @@ define('select2/dropdown/attachContainer',[
define('jquery.select2',[
'jquery',
'./select2/core'
], function ($, Select2) {
'./select2/core',
'./select2/defaults'
], function ($, Select2, Defaults) {
// Force jQuery.mousewheel to be loaded if it hasn't already
try {
require('jquery.mousewheel');
@ -14285,6 +14298,10 @@ define('jquery.select2',[
};
}
if ($.fn.select2.defaults == null) {
$.fn.select2.defaults = Defaults;
}
return Select2;
});

File diff suppressed because one or more lines are too long

95
dist/js/select2.js vendored
View File

@ -587,6 +587,40 @@ define('select2/utils',[], function () {
};
};
Utils._convertData = function (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
};
return Utils;
});
@ -4168,6 +4202,17 @@ define('select2/defaults',[
};
};
Defaults.prototype.set = function (key, value) {
var camelKey = $.camelCase(key);
var data = {};
data[camelKey] = value;
var convertedData = Utils._convertData(data);
$.extend(this.defaults, convertedData);
};
var defaults = new Defaults();
return defaults;
@ -4175,8 +4220,9 @@ define('select2/defaults',[
define('select2/options',[
'jquery',
'./defaults'
], function ($, Defaults) {
'./defaults',
'./utils'
], function ($, Defaults, Utils) {
function Options (options, $element) {
this.options = options;
@ -4246,41 +4292,7 @@ define('select2/options',[
var data = $e.data();
function convertData (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
}
data = convertData(data);
data = Utils._convertData(data);
for (var key in data) {
if (excludedData.indexOf(key) > -1) {
@ -4794,8 +4806,9 @@ define('select2/core',[
define('jquery.select2',[
'jquery',
'./select2/core'
], function ($, Select2) {
'./select2/core',
'./select2/defaults'
], function ($, Select2, Defaults) {
// Force jQuery.mousewheel to be loaded if it hasn't already
try {
require('jquery.mousewheel');
@ -4824,6 +4837,10 @@ define('jquery.select2',[
};
}
if ($.fn.select2.defaults == null) {
$.fn.select2.defaults = Defaults;
}
return Select2;
});

File diff suppressed because one or more lines are too long

View File

@ -102,7 +102,7 @@ $("select").select2({
HTML5 data attributes.
</p>
<h2>
<h2 id="amd">
AMD compatibility
</h2>
@ -997,6 +997,46 @@ DataAdapter.query = function (params, callback) {
</dl>
</section>
<section id="defaults">
<div class="page-header">
<h1>Setting default options</h1>
</div>
<p>
In some cases, you need to set the default options for all instances of
Select2 in your web application. This is especially useful when you are
migrating from past versions of Select2, or you are using non-standard
options <a href="#amd">like custom AMD builds</a>. Select2 exposes the
default options through <code>$.fn.select2.defaults</code>, which allows
you to set them globally.
</p>
<p>
When setting options globally, any past defaults that have been set will
be overriden. Default options are only used when an option is requested
that has not been set during initialization.
</p>
<p>
<strong>You can set default options</strong> by calling
<code>$.fn.select2.defaults.set("key", "value")</code>. The key that is
set should take the same format as keys set using
<a href="#data-attributes">HTML <code>data-*</code> attributes</a> which
means that two dashes (<code>--</code>) will be replaced by a level of
nesting, and a single dash (<code>-</code>) will convert it to a camelCase
string.
</p>
<pre class="prettyprint linenums">
$.fn.select2.default2.set("theme", "classic");
</pre>
<p>
<strong>You can reset the default options</strong> by calling
<code>$.fn.select2.defaults.reset()</code>.
</p>
</section>
<section id="compatibility">
<div class="page-header">
<h1>Backwards compatibility</h1>

View File

@ -1,7 +1,8 @@
define([
'jquery',
'./select2/core'
], function ($, Select2) {
'./select2/core',
'./select2/defaults'
], function ($, Select2, Defaults) {
// Force jQuery.mousewheel to be loaded if it hasn't already
try {
require('jquery.mousewheel');
@ -30,5 +31,9 @@ define([
};
}
if ($.fn.select2.defaults == null) {
$.fn.select2.defaults = Defaults;
}
return Select2;
});

View File

@ -307,6 +307,17 @@ define([
};
};
Defaults.prototype.set = function (key, value) {
var camelKey = $.camelCase(key);
var data = {};
data[camelKey] = value;
var convertedData = Utils._convertData(data);
$.extend(this.defaults, convertedData);
};
var defaults = new Defaults();
return defaults;

View File

@ -1,7 +1,8 @@
define([
'jquery',
'./defaults'
], function ($, Defaults) {
'./defaults',
'./utils'
], function ($, Defaults, Utils) {
function Options (options, $element) {
this.options = options;
@ -71,41 +72,7 @@ define([
var data = $e.data();
function convertData (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
}
data = convertData(data);
data = Utils._convertData(data);
for (var key in data) {
if (excludedData.indexOf(key) > -1) {

View File

@ -159,5 +159,39 @@ define([], function () {
};
};
Utils._convertData = function (data) {
for (var originalKey in data) {
var keys = originalKey.split('-');
var dataLevel = data;
if (keys.length === 1) {
continue;
}
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
// Lowercase the first letter
// By default, dash-separated becomes camelCase
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if (!(key in dataLevel)) {
dataLevel[key] = {};
}
if (k == keys.length - 1) {
dataLevel[key] = data[originalKey];
}
dataLevel = dataLevel[key];
}
delete data[originalKey];
}
return data;
};
return Utils;
});