move the parameter type converting into a seperate method

This commit is contained in:
Damien Churchill 2009-10-13 15:22:24 +00:00
parent aa274eca74
commit 4a00edc066
1 changed files with 86 additions and 77 deletions

View File

@ -65,7 +65,6 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
/**
* Add a set of default options and values to the options manager
* @param {String} id
* @param {Object} options The default options.
*/
addOptions: function(options) {
@ -97,6 +96,33 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
this.reset();
},
/**
* Converts the value so it matches the originals type
* @param {Mixed} oldValue The original value
* @param {Mixed} value The new value to convert
*/
convertValueType: function(oldValue, value) {
if (Ext.type(oldValue) != Ext.type(value)) {
switch (Ext.type(oldValue)) {
case 'string':
value = String(value);
break;
case 'number':
value = Number(value);
break;
case 'boolean':
if (Ext.type(value) == 'string') {
value = value.toLowerCase();
value = (value == 'true' || newValue == '1' || value == 'on') ? true : false;
} else {
value = Boolean(value);
}
break;
}
}
return value;
},
/**
* Get the value for an option or options.
* @param {String} [option] A single option or an array of options to return.
@ -198,24 +224,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
}
} else {
var defaultValue = this.getDefault(option);
if (Ext.type(defaultValue) != Ext.type(value)) {
switch (Ext.type(defaultValue)) {
case 'string':
value = String(value);
break;
case 'number':
value = Number(value);
break;
case 'boolean':
if (Ext.type(value) == 'string') {
value = value.toLowerCase();
value = (value == 'true' || value == '1' || value == 'on') ? true : false;
} else {
value = Boolean(value);
}
break;
}
}
value = this.convertValueType(defaultValue, value);
var oldValue = this.get(option);
if (oldValue == value) return;