add in support to the optionsmanager for binding form fields

This commit is contained in:
Damien Churchill 2009-06-16 18:14:23 +00:00
parent 3458b238d8
commit 4edb79bcd7
3 changed files with 127 additions and 153 deletions

View File

@ -56,7 +56,21 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
Ext.deluge.details.OptionsTab.superclass.initComponent.call(this);
this.fieldsets = {}, this.fields = {};
this.optionsManager = new Deluge.OptionsManager();
this.optionsManager = new Deluge.OptionsManager({
defaults: {
'max_download_speed': -1,
'max_upload_speed': -1,
'max_connections': -1,
'max_upload_slots': -1,
'is_auto_managed': false,
'stop_at_ratio': false,
'stop_ratio': 2.0,
'remove_at_ratio': false,
'move_completed': null,
'private': false,
'prioritize_first_last': false
}
});
this.optionsManager.on('changed', this.onOptionChanged, this);
/*
@ -94,16 +108,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
decimalPrecision: 1,
minValue: -1,
maxValue: 99999
},
listeners: {
'spin': {
fn: this.onFieldChange,
scope: this
},
'keypress': {
fn: this.onFieldChange,
scope: this
}
}
});
this.fieldsets.bandwidth.add({
@ -131,16 +135,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
decimalPrecision: 1,
minValue: -1,
maxValue: 99999
},
listeners: {
'spin': {
fn: this.onFieldChange,
scope: this
},
'keypress': {
fn: this.onFieldChange,
scope: this
}
}
});
this.fieldsets.bandwidth.add({
@ -169,16 +163,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
minValue: -1,
maxValue: 99999
},
listeners: {
'spin': {
fn: this.onFieldChange,
scope: this
},
'keypress': {
fn: this.onFieldChange,
scope: this
}
},
colspan: 2
});
@ -202,16 +186,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
minValue: -1,
maxValue: 99999
},
listeners: {
'spin': {
fn: this.onFieldChange,
scope: this
},
'keypress': {
fn: this.onFieldChange,
scope: this
}
},
colspan: 2
});
@ -240,12 +214,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
id: 'is_auto_managed',
boxLabel: _('Auto Managed'),
width: 200,
listeners: {
'check': {
fn: this.onFieldChange,
scope: this
}
},
colspan: 2
});
@ -253,15 +221,10 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
fieldLabel: '',
labelSeparator: '',
id: 'stop_at_ratio',
width: 110,
boxLabel: _('Stop seed at ratio'),
listeners: {
'check': {
fn: this.onFieldChange,
scope: this
}
}
width: 120,
boxLabel: _('Stop seed at ratio')
});
this.fields.stop_ratio = this.fieldsets.queue.add({
xtype: 'uxspinner',
id: 'stop_ratio',
@ -276,16 +239,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
incrementValue: 0.1,
alternateIncrementValue: 1,
decimalPrecision: 1
},
listeners: {
'spin': {
fn: this.onFieldChange,
scope: this
},
'keypress': {
fn: this.onFieldChange,
scope: this
}
}
});
@ -296,12 +249,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
bodyStyle: 'padding-left: 10px',
boxLabel: _('Remove at ratio'),
disabled: true,
listeners: {
'check': {
fn: this.onFieldChange,
scope: this
}
},
colspan: 2
});
@ -310,12 +257,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
labelSeparator: '',
id: 'move_completed',
boxLabel: _('Move Completed'),
listeners: {
'check': {
fn: this.onFieldChange,
scope: this
}
},
colspan: 2
});
@ -342,28 +283,21 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
fieldLabel: '',
labelSeparator: '',
boxLabel: _('Private'),
id: 'private',
listeners: {
'check': {
fn: this.onFieldChange,
scope: this
}
}
id: 'private'
});
this.fields.prioritize_first_last = this.fieldsets.general.add({
fieldLabel: '',
labelSeparator: '',
boxLabel: _('Prioritize First/Last'),
id: 'prioritize_first_last',
listeners: {
'check': {
fn: this.onFieldChange,
scope: this
}
}
id: 'prioritize_first_last'
});
// Bind the fields so the options manager can manage them.
for (var id in this.fields) {
this.optionsManager.bind(id, this.fields[id]);
}
/*
* Buttons
*/
@ -425,15 +359,7 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
},
clear: function() {
this.fields.max_download_speed.setValue(0);
this.fields.max_upload_speed.setValue(0);
this.fields.max_connections.setValue(0);
this.fields.max_upload_slots.setValue(0);
this.fields.is_auto_managed.setValue(false);
this.fields.stop_at_ratio.setValue(false);
this.fields.remove_at_ratio.setValue(false);
this.fields['private'].setValue(false);
this.fields.prioritize_first_last.setValue(false);
this.optionsManager.resetOptions(this.torrentId);
},
reset: function() {
@ -456,14 +382,14 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
var value = changed['prioritize_first_last'];
Deluge.Client.core.set_torrent_prioritize_first_last(this.torrentId, value, {
success: function() {
this.optionsManager.updateOption(this.torrentId, 'prioritize_first_last', value);
this.optionsManager.setOption(this.torrentId, 'prioritize_first_last', value);
},
scope: this
});
}
Deluge.Client.core.set_torrent_options([this.torrentId], changed, {
success: function() {
this.optionsManager.updateOptions(this.torrentId, changed);
this.optionsManager.setOptions(this.torrentId, changed);
this.optionsManager.resetOptions(this.torrentId);
},
scope: this
@ -474,10 +400,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
Deluge.EditTrackers.show();
},
onFieldChange: function(field) {
this.optionsManager.setOption(this.torrentId, field.id, field.getValue());
},
onOptionChanged: function(id, key, newValue, oldValue) {
if (key == 'stop_at_ratio') {
this.fields.remove_at_ratio.setDisabled(!newValue);
@ -490,11 +412,6 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
delete torrent['private'];
this.optionsManager.updateOptions(this.torrentId, torrent);
for (var key in torrent) {
if (this.fields[key] && !this.optionsManager.hasChanged(this.torrentId, key)) {
if (!this.fields[key].disabled) this.fields[key].setValue(torrent[key]);
}
}
}
});
Deluge.Details.add(new Ext.deluge.details.OptionsTab());

View File

@ -39,26 +39,53 @@ Copyright:
Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
constructor: function(config) {
this.binds = {};
this.changed = {};
this.defaults = config['defaults'] || {};
this.options = {};
Deluge.OptionsManager.superclass.constructor.call(this);
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 for an id 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 for the id.
* @param {Object} options The default options.
*/
addOptions: function(id, options) {
this.options[id] = options;
},
/**
* Binds a form field to the specified option.
* @param {String} option
* @param {Ext.form.Field} field
*/
bind: function(option, field) {
this.binds[option] = field;
this.binds[field] = option;
switch (field.getXType()) {
case 'checkbox':
field.on('check', this.onFieldChange, this);
break;
case 'uxspinner':
field.on('spin', this.onFieldChange, this);
field.on('keypress', this.onFieldChange, this);
break;
default:
break;
}
},
/**
* Returns the changed values for a specified id.
* @param {String} id
@ -69,13 +96,13 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
},
/**
* Get the default value for an option given an id and option name
* Get the default value for an option given an id and option name.
* @param {String} id
* @param {String} option
* @returns {Object} the value of the option
*/
getDefault: function(id, option) {
return this.options[id][option];
getOption: function(id, option) {
return (this.hasOption(id, option)) ? this.options[id][option] : this.defaults[option];
},
/**
@ -85,7 +112,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* @returns {Object} the options value.
*/
getValue: function(id, option) {
return (this.hasChanged(id, option)) ? this.changed[id][option] : this.options[id][option];
return (this.hasChanged(id, option)) ? this.changed[id][option] : this.getOption(id, option);
},
/**
@ -97,6 +124,17 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
hasChanged: function(id, option) {
return (this.changed[id] && !Ext.isEmpty(this.changed[id][option]));
},
/**
* Check to see if an id has had an option set to something other than the
* default value.
* @param {String} id
* @param {String} option
* @returns {Boolean} true if the id has an option, else false.
*/
hasOption: function(id, option) {
return (this.options[id] && !Ext.isEmpty(this.options[id][option]));
},
/**
* Reset the options back to the default values for the specified id.
@ -111,20 +149,45 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* Set the specified option for the passed in id.
* @param {String} id
* @param {String} option
* @param {Object} value The new value for the option
* @param {Object} value The value for the option
*/
setOption: function(id, option, value) {
this.options[id][option] = value;
},
/**
* Set the specified options for the passed in id.
* @param {String} id
* @param {Object} options The option values to change.
*/
setOptions: function(id, options) {
if (!this.changed[id]) this.changed[id] = {};
for (var key in options) {
this.setOption(id, key, options[key]);
}
},
/**
* Update the value for the specified option and id.
* @param {String} id
* @param {String} option
* @param {Object} value;
*/
updateOption: function(id, option, value) {
if (!this.changed[id]) this.changed[id] = {};
var oldValue = this.getValue(id, option);
if (this.options[id][option] == value) {
if (oldValue == value) return;
var defaultValue = this.getOption(id, option);
if (defaultValue == value) {
if (!Ext.isEmpty(this.changed[id][option])) delete this.changed[id][option];
this.fireEvent('changed', id, option, value, oldValue);
return;
}
if (Ext.type(this.options[id][option]) != Ext.type(value)) {
switch (Ext.type(this.options[id][option])) {
if (Ext.type(defaultValue) != Ext.type(value)) {
switch (Ext.type(defaultValue)) {
case 'string':
value = String(value);
break;
@ -142,39 +205,33 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
},
/**
* Set the specified options for the passed in id.
* Update the options for the specified id.
* @param {String} id
* @param {Object} options The option values to change.
*/
setOptions: function(id, options) {
if (!this.changed[id]) this.changed[id] = {};
for (var key in options) {
this.setOption(id, key, options[key]);
}
},
/**
* Update the default value for the specified option and id.
* @param {String} id
* @param {String} option
* @param {Object} value;
*/
updateOption: function(id, option, value) {
this.options[id][option] = value;
},
/**
* Update the defaults for the specified id.
* @param {String} id
* @param {Object} options The option defaults to change.
* @param {Object} options The options to change.
*/
updateOptions: function(id, options) {
if (!this.options[id]) {
this.addOptions(id, options);
} else {
for (var key in options) {
this.updateOption(id, key, options[key]);
}
for (var key in options) {
this.updateOption(id, key, options[key]);
}
},
/* Event Handlers */
/**
* Stops a form fields value from being blocked by the change functions
* @param {Ext.form.Field} field
* @private
*/
onFieldChange: function(field) {
var option = this.binds[field];
//alert(option);
},
onChange: function(id, option, newValue, oldValue) {
// If we don't have a bind there's nothing to do.
if (Ext.isEmpty(this.binds[option])) return;
// Set the form field to the new value.
this.binds[option].setValue(newValue);
}
});

File diff suppressed because one or more lines are too long