Author: damoxc
Revision: 5843
Log:
move the parameter type converting into a seperate method
Diff:
Modified: branches/1.2_RC/deluge/ui/web/js/Deluge.OptionsManager.js
===================================================================
--- branches/1.2_RC/deluge/ui/web/js/Deluge.OptionsManager.js 2009-10-13
15:20:45 UTC (rev 5842)
+++ branches/1.2_RC/deluge/ui/web/js/Deluge.OptionsManager.js 2009-10-13
15:22:24 UTC (rev 5843)
@@ -52,20 +52,19 @@
this.changed = {};
this.options = (config && config['options']) || {};
this.focused = null;
-
+
this.addEvents({
'add': true,
'changed': true,
'reset': true
});
this.on('changed', this.onChange, this);
-
+
Deluge.OptionsManager.superclass.constructor.call(this);
},
/**
* Add a set of default options and values to the options manager
- * @param {String} id
* @param {Object} options The default options.
*/
addOptions: function(options) {
@@ -81,7 +80,7 @@
this.binds[option] = this.binds[option] || [];
this.binds[option].push(field);
field._doption = option;
-
+
field.on('focus', this.onFieldFocus, this);
field.on('blur', this.onFieldBlur, this);
field.on('change', this.onFieldChange, this);
@@ -98,22 +97,49 @@
},
/**
+ * 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.
* @returns {Object} the options value.
*/
get: function() {
- if (arguments.length == 1) {
- var option = arguments[0];
- return (this.isDirty(option)) ? this.changed[option] :
this.options[option];
- } else {
+ if (arguments.length == 1) {
+ var option = arguments[0];
+ return (this.isDirty(option)) ? this.changed[option] :
this.options[option];
+ } else {
var options = {};
Ext.each(arguments, function(option) {
- if (!this.has(option)) return;
- options[option] = (this.isDirty(option)) ?
this.changed[option] : this.options[option];
+ if (!this.has(option)) return;
+ options[option] = (this.isDirty(option)) ?
this.changed[option] : this.options[option];
}, this);
return options;
- }
+ }
},
/**
@@ -122,7 +148,7 @@
* @returns {Object} the value of the option
*/
getDefault: function(option) {
- return this.options[option];
+ return this.options[option];
},
/**
@@ -130,7 +156,7 @@
* @returns {Object} the changed options
*/
getDirty: function() {
- return this.changed;
+ return this.changed;
},
/**
@@ -138,7 +164,7 @@
* @returns {Boolean} true if the option has been changed from the
default.
*/
isDirty: function(option) {
- return !Ext.isEmpty(this.changed[option]);
+ return !Ext.isEmpty(this.changed[option]);
},
/**
@@ -148,7 +174,7 @@
* @returns {Boolean} true if the option has been changed, else false.
*/
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 @@
* @returns {Boolean} true if the option exists, else false.
*/
has: function(option) {
- return (this.options[option]);
+ return (this.options[option]);
},
/**
@@ -165,7 +191,7 @@
* @param {String} id
*/
reset: function() {
- this.changed = {};
+ this.changed = {};
},
/**
@@ -174,16 +200,16 @@
* @param {Object} value The value for the option
*/
set: function(option, value) {
- if (typeof option == 'object') {
- var options = option;
- this.options = Ext.apply(this.options, options);
- for (var option in options) {
- this.onChange(option, options[option]);
- }
- } else {
- this.options[option] = value;
- this.onChange(option, value)
+ if (typeof option == 'object') {
+ var options = option;
+ this.options = Ext.apply(this.options, options);
+ for (var option in options) {
+ this.onChange(option, options[option]);
}
+ } else {
+ this.options[option] = value;
+ this.onChange(option, value)
+ }
},
/**
@@ -192,43 +218,26 @@
* @param {Object} [value];
*/
update: function(option, value) {
- if (typeof value === undefined) {
- for (var key in option) {
- 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;
- }
+ if (typeof value === undefined) {
+ for (var key in option) {
+ this.update(key, option[key]);
+ }
+ } 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);
- }
+ this.changed[option] = value;
+ this.fireEvent('changed', option, value, oldValue);
+ }
},
/* Event Handlers */
@@ -237,9 +246,9 @@
* so value changing operations can continue on that field.
*/
onFieldBlur: function(field, event) {
- if (this.focused == field) {
- this.focused = null;
- }
+ if (this.focused == field) {
+ this.focused = null;
+ }
},
/**
@@ -257,20 +266,20 @@
* field.
*/
onFieldFocus: function(field, event) {
- this.focused = field;
+ this.focused = field;
},
onChange: function(option, newValue, oldValue) {
- // If we don't have a bind there's nothing to do.
- if (Ext.isEmpty(this.binds[option])) return;
-
- Ext.each(this.binds[option], function(bind) {
- // The field is currently focused so we don't want to
- // change it.
- if (bind == this.focused) return;
-
- // Set the form field to the new value.
- bind.setValue(newValue);
- }, this)
+ // If we don't have a bind there's nothing to do.
+ if (Ext.isEmpty(this.binds[option])) return;
+
+ Ext.each(this.binds[option], function(bind) {
+ // The field is currently focused so we don't want to
+ // change it.
+ if (bind == this.focused) return;
+
+ // Set the form field to the new value.
+ bind.setValue(newValue);
+ }, this)
}
});
Modified: trunk/deluge/ui/web/js/Deluge.OptionsManager.js
===================================================================
--- trunk/deluge/ui/web/js/Deluge.OptionsManager.js 2009-10-13 15:20:45 UTC
(rev 5842)
+++ trunk/deluge/ui/web/js/Deluge.OptionsManager.js 2009-10-13 15:22:24 UTC
(rev 5843)
@@ -52,20 +52,19 @@
this.changed = {};
this.options = (config && config['options']) || {};
this.focused = null;
-
+
this.addEvents({
'add': true,
'changed': true,
'reset': true
});
this.on('changed', this.onChange, this);
-
+
Deluge.OptionsManager.superclass.constructor.call(this);
},
/**
* Add a set of default options and values to the options manager
- * @param {String} id
* @param {Object} options The default options.
*/
addOptions: function(options) {
@@ -81,7 +80,7 @@
this.binds[option] = this.binds[option] || [];
this.binds[option].push(field);
field._doption = option;
-
+
field.on('focus', this.onFieldFocus, this);
field.on('blur', this.onFieldBlur, this);
field.on('change', this.onFieldChange, this);
@@ -98,22 +97,49 @@
},
/**
+ * 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.
* @returns {Object} the options value.
*/
get: function() {
- if (arguments.length == 1) {
- var option = arguments[0];
- return (this.isDirty(option)) ? this.changed[option] :
this.options[option];
- } else {
+ if (arguments.length == 1) {
+ var option = arguments[0];
+ return (this.isDirty(option)) ? this.changed[option] :
this.options[option];
+ } else {
var options = {};
Ext.each(arguments, function(option) {
- if (!this.has(option)) return;
- options[option] = (this.isDirty(option)) ?
this.changed[option] : this.options[option];
+ if (!this.has(option)) return;
+ options[option] = (this.isDirty(option)) ?
this.changed[option] : this.options[option];
}, this);
return options;
- }
+ }
},
/**
@@ -122,7 +148,7 @@
* @returns {Object} the value of the option
*/
getDefault: function(option) {
- return this.options[option];
+ return this.options[option];
},
/**
@@ -130,7 +156,7 @@
* @returns {Object} the changed options
*/
getDirty: function() {
- return this.changed;
+ return this.changed;
},
/**
@@ -138,7 +164,7 @@
* @returns {Boolean} true if the option has been changed from the
default.
*/
isDirty: function(option) {
- return !Ext.isEmpty(this.changed[option]);
+ return !Ext.isEmpty(this.changed[option]);
},
/**
@@ -148,7 +174,7 @@
* @returns {Boolean} true if the option has been changed, else false.
*/
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 @@
* @returns {Boolean} true if the option exists, else false.
*/
has: function(option) {
- return (this.options[option]);
+ return (this.options[option]);
},
/**
@@ -165,7 +191,7 @@
* @param {String} id
*/
reset: function() {
- this.changed = {};
+ this.changed = {};
},
/**
@@ -174,16 +200,16 @@
* @param {Object} value The value for the option
*/
set: function(option, value) {
- if (typeof option == 'object') {
- var options = option;
- this.options = Ext.apply(this.options, options);
- for (var option in options) {
- this.onChange(option, options[option]);
- }
- } else {
- this.options[option] = value;
- this.onChange(option, value)
+ if (typeof option == 'object') {
+ var options = option;
+ this.options = Ext.apply(this.options, options);
+ for (var option in options) {
+ this.onChange(option, options[option]);
}
+ } else {
+ this.options[option] = value;
+ this.onChange(option, value)
+ }
},
/**
@@ -192,43 +218,26 @@
* @param {Object} [value];
*/
update: function(option, value) {
- if (typeof value === undefined) {
- for (var key in option) {
- 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;
- }
+ if (typeof value === undefined) {
+ for (var key in option) {
+ this.update(key, option[key]);
+ }
+ } 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);
- }
+ this.changed[option] = value;
+ this.fireEvent('changed', option, value, oldValue);
+ }
},
/* Event Handlers */
@@ -237,9 +246,9 @@
* so value changing operations can continue on that field.
*/
onFieldBlur: function(field, event) {
- if (this.focused == field) {
- this.focused = null;
- }
+ if (this.focused == field) {
+ this.focused = null;
+ }
},
/**
@@ -257,20 +266,20 @@
* field.
*/
onFieldFocus: function(field, event) {
- this.focused = field;
+ this.focused = field;
},
onChange: function(option, newValue, oldValue) {
- // If we don't have a bind there's nothing to do.
- if (Ext.isEmpty(this.binds[option])) return;
-
- Ext.each(this.binds[option], function(bind) {
- // The field is currently focused so we don't want to
- // change it.
- if (bind == this.focused) return;
-
- // Set the form field to the new value.
- bind.setValue(newValue);
- }, this)
+ // If we don't have a bind there's nothing to do.
+ if (Ext.isEmpty(this.binds[option])) return;
+
+ Ext.each(this.binds[option], function(bind) {
+ // The field is currently focused so we don't want to
+ // change it.
+ if (bind == this.focused) return;
+
+ // Set the form field to the new value.
+ bind.setValue(newValue);
+ }, this)
}
});
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"deluge-commit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/deluge-commit?hl=en
-~----------~----~----~----~------~----~------~--~---