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

@ -52,20 +52,19 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
this.changed = {}; this.changed = {};
this.options = (config && config['options']) || {}; this.options = (config && config['options']) || {};
this.focused = null; this.focused = null;
this.addEvents({ this.addEvents({
'add': true, 'add': true,
'changed': true, 'changed': true,
'reset': true 'reset': true
}); });
this.on('changed', this.onChange, this); this.on('changed', this.onChange, this);
Deluge.OptionsManager.superclass.constructor.call(this); Deluge.OptionsManager.superclass.constructor.call(this);
}, },
/** /**
* Add a set of default options and values to the options manager * Add a set of default options and values to the options manager
* @param {String} id
* @param {Object} options The default options. * @param {Object} options The default options.
*/ */
addOptions: function(options) { addOptions: function(options) {
@ -81,7 +80,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
this.binds[option] = this.binds[option] || []; this.binds[option] = this.binds[option] || [];
this.binds[option].push(field); this.binds[option].push(field);
field._doption = option; field._doption = option;
field.on('focus', this.onFieldFocus, this); field.on('focus', this.onFieldFocus, this);
field.on('blur', this.onFieldBlur, this); field.on('blur', this.onFieldBlur, this);
field.on('change', this.onFieldChange, this); field.on('change', this.onFieldChange, this);
@ -97,23 +96,50 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
this.reset(); 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. * Get the value for an option or options.
* @param {String} [option] A single option or an array of options to return. * @param {String} [option] A single option or an array of options to return.
* @returns {Object} the options value. * @returns {Object} the options value.
*/ */
get: function() { get: function() {
if (arguments.length == 1) { if (arguments.length == 1) {
var option = arguments[0]; var option = arguments[0];
return (this.isDirty(option)) ? this.changed[option] : this.options[option]; return (this.isDirty(option)) ? this.changed[option] : this.options[option];
} else { } else {
var options = {}; var options = {};
Ext.each(arguments, function(option) { Ext.each(arguments, function(option) {
if (!this.has(option)) return; if (!this.has(option)) return;
options[option] = (this.isDirty(option)) ? this.changed[option] : this.options[option]; options[option] = (this.isDirty(option)) ? this.changed[option] : this.options[option];
}, this); }, this);
return options; return options;
} }
}, },
/** /**
@ -122,7 +148,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* @returns {Object} the value of the option * @returns {Object} the value of the option
*/ */
getDefault: function(option) { getDefault: function(option) {
return this.options[option]; return this.options[option];
}, },
/** /**
@ -130,7 +156,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* @returns {Object} the changed options * @returns {Object} the changed options
*/ */
getDirty: function() { getDirty: function() {
return this.changed; return this.changed;
}, },
/** /**
@ -138,7 +164,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* @returns {Boolean} true if the option has been changed from the default. * @returns {Boolean} true if the option has been changed from the default.
*/ */
isDirty: function(option) { isDirty: function(option) {
return !Ext.isEmpty(this.changed[option]); return !Ext.isEmpty(this.changed[option]);
}, },
/** /**
@ -148,7 +174,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* @returns {Boolean} true if the option has been changed, else false. * @returns {Boolean} true if the option has been changed, else false.
*/ */
hasChanged: function(id, option) { hasChanged: function(id, option) {
return (this.changed[id] && !Ext.isEmpty(this.changed[id][option])); return (this.changed[id] && !Ext.isEmpty(this.changed[id][option]));
}, },
/** /**
@ -157,7 +183,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* @returns {Boolean} true if the option exists, else false. * @returns {Boolean} true if the option exists, else false.
*/ */
has: function(option) { has: function(option) {
return (this.options[option]); return (this.options[option]);
}, },
/** /**
@ -165,7 +191,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* @param {String} id * @param {String} id
*/ */
reset: function() { reset: function() {
this.changed = {}; this.changed = {};
}, },
/** /**
@ -174,16 +200,16 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* @param {Object} value The value for the option * @param {Object} value The value for the option
*/ */
set: function(option, value) { set: function(option, value) {
if (typeof option == 'object') { if (typeof option == 'object') {
var options = option; var options = option;
this.options = Ext.apply(this.options, options); this.options = Ext.apply(this.options, options);
for (var option in options) { for (var option in options) {
this.onChange(option, options[option]); this.onChange(option, options[option]);
}
} else {
this.options[option] = value;
this.onChange(option, value)
} }
} else {
this.options[option] = value;
this.onChange(option, value)
}
}, },
/** /**
@ -192,43 +218,26 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* @param {Object} [value]; * @param {Object} [value];
*/ */
update: function(option, value) { update: function(option, value) {
if (typeof value === undefined) { if (typeof value === undefined) {
for (var key in option) { for (var key in option) {
this.update(key, option[key]); this.update(key, option[key]);
}
} 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;
}
}
var oldValue = this.get(option);
if (oldValue == value) return;
if (defaultValue == value) {
if (this.isDirty(option)) delete this.changed[option];
this.fireEvent('changed', option, value, oldValue);
return;
}
this.changed[option] = value;
this.fireEvent('changed', option, value, oldValue);
} }
} else {
var defaultValue = this.getDefault(option);
value = this.convertValueType(defaultValue, value);
var oldValue = this.get(option);
if (oldValue == value) return;
if (defaultValue == value) {
if (this.isDirty(option)) delete this.changed[option];
this.fireEvent('changed', option, value, oldValue);
return;
}
this.changed[option] = value;
this.fireEvent('changed', option, value, oldValue);
}
}, },
/* Event Handlers */ /* Event Handlers */
@ -237,9 +246,9 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* so value changing operations can continue on that field. * so value changing operations can continue on that field.
*/ */
onFieldBlur: function(field, event) { onFieldBlur: function(field, event) {
if (this.focused == field) { if (this.focused == field) {
this.focused = null; this.focused = null;
} }
}, },
/** /**
@ -257,20 +266,20 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* field. * field.
*/ */
onFieldFocus: function(field, event) { onFieldFocus: function(field, event) {
this.focused = field; this.focused = field;
}, },
onChange: function(option, newValue, oldValue) { onChange: function(option, newValue, oldValue) {
// If we don't have a bind there's nothing to do. // If we don't have a bind there's nothing to do.
if (Ext.isEmpty(this.binds[option])) return; if (Ext.isEmpty(this.binds[option])) return;
Ext.each(this.binds[option], function(bind) { Ext.each(this.binds[option], function(bind) {
// The field is currently focused so we don't want to // The field is currently focused so we don't want to
// change it. // change it.
if (bind == this.focused) return; if (bind == this.focused) return;
// Set the form field to the new value. // Set the form field to the new value.
bind.setValue(newValue); bind.setValue(newValue);
}, this) }, this)
} }
}); });