modify the M.O.M so the API is practically the same as the O.M, so the only method that accepts an id is changeId

This commit is contained in:
Damien Churchill 2009-10-25 17:48:28 +00:00
commit ecd75ba424
3 changed files with 86 additions and 91 deletions

View file

@ -76,19 +76,7 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
folderSort: true folderSort: true
}); });
this.optionsManager = new Deluge.MultiOptionsManager({ this.optionsManager = new Deluge.MultiOptionsManager();
defaults: {
'add_paused': false,
'compact_allocation': false,
'download_location': '',
'max_connections_per_torrent': -1,
'max_download_speed_per_torrent': -1,
'max_upload_slots_per_torrent': -1,
'max_upload_speed_per_torrent': -1,
'prioritize_first_last_pieces': false,
'file_priorities': []
}
});
this.form = this.add({ this.form = this.add({
xtype: 'form', xtype: 'form',
@ -96,7 +84,8 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
title: _('Options'), title: _('Options'),
bodyStyle: 'padding: 5px;', bodyStyle: 'padding: 5px;',
border: false, border: false,
height: 170 height: 170,
disabled: true
}); });
var fieldset = this.form.add({ var fieldset = this.form.add({
@ -158,28 +147,28 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
width: 200, width: 200,
defaultType: 'uxspinner' defaultType: 'uxspinner'
}); });
this.optionsManager.bind('max_download_speed_per_torrent', fieldset.add({ this.optionsManager.bind('max_download_speed', fieldset.add({
fieldLabel: _('Max Down Speed'), fieldLabel: _('Max Down Speed'),
/*labelStyle: 'margin-left: 10px',*/ /*labelStyle: 'margin-left: 10px',*/
name: 'max_download_speed_per_torrent', name: 'max_download_speed',
width: 60 width: 60
})); }));
this.optionsManager.bind('max_upload_speed_per_torrent', fieldset.add({ this.optionsManager.bind('max_upload_speed', fieldset.add({
fieldLabel: _('Max Up Speed'), fieldLabel: _('Max Up Speed'),
/*labelStyle: 'margin-left: 10px',*/ /*labelStyle: 'margin-left: 10px',*/
name: 'max_upload_speed_per_torrent', name: 'max_upload_speed',
width: 60 width: 60
})); }));
this.optionsManager.bind('max_connections_per_torrent', fieldset.add({ this.optionsManager.bind('max_connections', fieldset.add({
fieldLabel: _('Max Connections'), fieldLabel: _('Max Connections'),
/*labelStyle: 'margin-left: 10px',*/ /*labelStyle: 'margin-left: 10px',*/
name: 'max_connections_per_torrent', name: 'max_connections',
width: 60 width: 60
})); }));
this.optionsManager.bind('max_upload_slots_per_torrent', fieldset.add({ this.optionsManager.bind('max_upload_slots', fieldset.add({
fieldLabel: _('Max Upload Slots'), fieldLabel: _('Max Upload Slots'),
/*labelStyle: 'margin-left: 10px',*/ /*labelStyle: 'margin-left: 10px',*/
name: 'max_upload_slots_per_torrent', name: 'max_upload_slots',
width: 60 width: 60
})); }));
@ -225,7 +214,10 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
Ext.each(Ext.keys(fileIndexes), function(index) { Ext.each(Ext.keys(fileIndexes), function(index) {
priorities[index] = fileIndexes[index]; priorities[index] = fileIndexes[index];
}); });
this.optionsManager.set(torrent['info_hash'], 'file_priorities', priorities);
var oldId = this.optionsManager.changeId(torrent['info_hash'], false);
this.optionsManager.set('file_priorities', priorities);
this.optionsManager.changeId(oldId, false);
}, },
clear: function() { clear: function() {
@ -249,8 +241,18 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
Deluge.Client.core.get_config_values(keys, { Deluge.Client.core.get_config_values(keys, {
success: function(config) { success: function(config) {
config['file_priorities'] = []; var options = {
this.optionsManager.options = config; 'file_priorities': [],
'add_paused': config.add_paused,
'compact_allocation': config.compact_allocation,
'download_location': config.download_location,
'max_connections': config.max_connections_per_torrent,
'max_download_speed': config.max_download_speed_per_torrent,
'max_upload_slots': config.max_upload_slots_per_torrent,
'max_upload_speed': config.max_upload_speed_per_torrent,
'prioritize_first_last_pieces': config.prioritize_first_last_pieces
}
this.optionsManager.options = options;
}, },
scope: this scope: this
}); });
@ -261,7 +263,9 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
}, },
getOptions: function(torrentId) { getOptions: function(torrentId) {
var options = this.optionsManager.get(torrentId); var oldId = this.optionsManager.changeId(torrentId, false);
var options = this.optionsManager.get();
this.optionsManager.changeId(oldTorrentId, false);
Ext.each(options['file_priorities'], function(priority, index) { Ext.each(options['file_priorities'], function(priority, index) {
options['file_priorities'][index] = (priority) ? 1 : 0; options['file_priorities'][index] = (priority) ? 1 : 0;
}); });
@ -276,7 +280,7 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
this.clearFiles(); this.clearFiles();
var root = this.files.getRootNode(); var root = this.files.getRootNode();
var priorities = this.optionsManager.get(this.torrentId, 'file_priorities'); var priorities = this.optionsManager.get('file_priorities');
this.walkFileTree(this.torrents[torrentId]['files_tree'], function(filename, type, entry, parent) { this.walkFileTree(this.torrents[torrentId]['files_tree'], function(filename, type, entry, parent) {
if (type == 'dir') { if (type == 'dir') {
@ -321,7 +325,7 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
}, },
onFolderCheck: function(node, checked) { onFolderCheck: function(node, checked) {
var priorities = this.optionsManager.get(this.torrentId, 'file_priorities'); var priorities = this.optionsManager.get('file_priorities');
node.cascade(function(child) { node.cascade(function(child) {
if (!child.ui.checkbox) { if (!child.ui.checkbox) {
child.attributes.checked = checked; child.attributes.checked = checked;
@ -330,13 +334,13 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
} }
priorities[child.attributes.fileindex] = checked; priorities[child.attributes.fileindex] = checked;
}, this); }, this);
this.optionsManager.update(this.torrentId, 'file_priorities', priorities); this.optionsManager.update('file_priorities', priorities);
}, },
onNodeCheck: function(node, checked) { onNodeCheck: function(node, checked) {
var priorities = this.optionsManager.get(this.torrentId, 'file_priorities'); var priorities = this.optionsManager.get('file_priorities');
priorities[node.attributes.fileindex] = checked; priorities[node.attributes.fileindex] = checked;
this.optionsManager.update(this.torrentId, 'file_priorities', priorities); this.optionsManager.update('file_priorities', priorities);
} }
}); });

View file

@ -369,7 +369,7 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
}, },
reset: function() { reset: function() {
if (this.torrentId) this.optionsManager.reset(this.torrentId); if (this.torrentId) this.optionsManager.reset();
}, },
update: function(torrentId) { update: function(torrentId) {
@ -388,19 +388,19 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
}, },
onApply: function() { onApply: function() {
var changed = this.optionsManager.getDirty(this.torrentId); var changed = this.optionsManager.getDirty();
if (!Ext.isEmpty(changed['prioritize_first_last'])) { if (!Ext.isEmpty(changed['prioritize_first_last'])) {
var value = changed['prioritize_first_last']; var value = changed['prioritize_first_last'];
Deluge.Client.core.set_torrent_prioritize_first_last(this.torrentId, value, { Deluge.Client.core.set_torrent_prioritize_first_last(this.torrentId, value, {
success: function() { success: function() {
this.optionsManager.set(this.torrentId, 'prioritize_first_last', value); this.optionsManager.set('prioritize_first_last', value);
}, },
scope: this scope: this
}); });
} }
Deluge.Client.core.set_torrent_options([this.torrentId], changed, { Deluge.Client.core.set_torrent_options([this.torrentId], changed, {
success: function() { success: function() {
this.optionsManager.commit(this.torrentId); this.optionsManager.commit();
}, },
scope: this scope: this
}); });
@ -419,8 +419,8 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
this.fields['private'].setValue(torrent['private']); this.fields['private'].setValue(torrent['private']);
this.fields['private'].setDisabled(true); this.fields['private'].setDisabled(true);
delete torrent['private']; delete torrent['private'];
this.optionsManager.setDefault(this.torrentId, torrent); this.optionsManager.setDefault(torrent);
var stop_at_ratio = this.optionsManager.get(this.torrentId, 'stop_at_ratio'); var stop_at_ratio = this.optionsManager.get('stop_at_ratio');
this.fields.remove_at_ratio.setDisabled(!stop_at_ratio); this.fields.remove_at_ratio.setDisabled(!stop_at_ratio);
this.fields.stop_ratio.setDisabled(!stop_at_ratio); this.fields.stop_ratio.setDisabled(!stop_at_ratio);
} }

View file

@ -48,47 +48,43 @@ Deluge.MultiOptionsManager = Ext.extend(Deluge.OptionsManager, {
* Changes bound fields to use the specified id. * Changes bound fields to use the specified id.
* @param {String} id * @param {String} id
*/ */
changeId: function(id) { changeId: function(id, updateBinds) {
var oldId = this.currentId;
this.currentId = id; this.currentId = id;
if (updateBinds) {
for (var option in this.options) { for (var option in this.options) {
if (!this.binds[option]) continue; if (!this.binds[option]) continue;
Ext.each(this.binds[option], function(bind) { Ext.each(this.binds[option], function(bind) {
bind.setValue(this.get(id, option)); bind.setValue(this.get(option));
}, this); }, this);
} }
}
return oldId;
}, },
/** /**
* Changes all the changed values to be the default values * Changes all the changed values to be the default values
* @param {String} id * @param {String} id
*/ */
commit: function(id) { commit: function() {
this.stored[id] = Ext.apply(this.stored[id], this.changed[id]); this.stored[this.currentId] = Ext.apply(this.stored[this.currentId], this.changed[this.currentId]);
this.reset(id); this.reset();
}, },
/** /**
* Get the value for an option * Get the value for an option
* @param {String} id
* @param {String|Array} [option] A single option or an array of options to return. * @param {String|Array} [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() {
var id = arguments[0];
if (arguments.length == 1) { if (arguments.length == 1) {
var options = {}; var option = arguments[0];
for (var option in this.options) { return (this.isDirty(option)) ? this.changed[this.currentId][option] : this.getDefault(option);
options[option] = (this.isDirty(id, option)) ? this.changed[id][option] : this.getDefault(id, option);
}
return options;
} else if (arguments.length == 2) {
var option = arguments[1];
return (this.isDirty(id, option)) ? this.changed[id][option] : this.getDefault(id, option);
} else { } else {
var options = {}; var options = {};
Ext.each(arguments, function(option) { Ext.each(arguments, function(option) {
if (option == id) return; if (!this.has(option)) return;
options[option] = (this.isDirty(id, option)) ? this.changed[id][option] : this.getDefault(id, option); options[option] = (this.isDirty(option)) ? this.changed[this.currentId][option] : this.getDefault(option);
}, this); }, this);
return options; return options;
} }
@ -96,51 +92,46 @@ Deluge.MultiOptionsManager = Ext.extend(Deluge.OptionsManager, {
/** /**
* Get the default value for an option. * Get the default value for an option.
* @param {String} id
* @param {String|Array} [option] A single option or an array of options to return. * @param {String|Array} [option] A single option or an array of options to return.
* @returns {Object} the value of the option * @returns {Object} the value of the option
*/ */
getDefault: function(id, option) { getDefault: function(option) {
return (this.has(id, option)) ? this.stored[id][option] : this.options[option]; return (this.has(option)) ? this.stored[this.currentId][option] : this.options[option];
}, },
/** /**
* Returns the dirty (changed) values. * Returns the dirty (changed) values.
* @param {String} id
* @returns {Object} the changed options * @returns {Object} the changed options
*/ */
getDirty: function(id) { getDirty: function() {
return (this.changed[id]) ? this.changed[id] : {}; return (this.changed[this.currentId]) ? this.changed[this.currentId] : {};
}, },
/** /**
* Check to see if the option has been changed. * Check to see if the option has been changed.
* @param {String} id
* @param {String} option * @param {String} option
* @returns {Boolean} true if the option has been changed, else false. * @returns {Boolean} true if the option has been changed, else false.
*/ */
isDirty: function(id, option) { isDirty: function(option) {
return (this.changed[id] && !Ext.isEmpty(this.changed[id][option])); return (this.changed[this.currentId] && !Ext.isEmpty(this.changed[this.currentId][option]));
}, },
/** /**
* Check to see if an id has had an option set to something other than the * Check to see if an id has had an option set to something other than the
* default value. * default value.
* @param {String} id
* @param {String} option * @param {String} option
* @returns {Boolean} true if the id has an option, else false. * @returns {Boolean} true if the id has an option, else false.
*/ */
has: function(id, option) { has: function(option) {
return (this.stored[id] && !Ext.isEmpty(this.stored[id][option])); return (this.stored[this.currentId] && !Ext.isEmpty(this.stored[this.currentId][option]));
}, },
/** /**
* Reset the options back to the default values for the specified id. * Reset the options back to the default values for the specified id.
* @param {String} id
*/ */
reset: function(id) { reset: function() {
if (!this.changed[id]) return; if (!this.changed[this.currentId]) return;
delete this.changed[id]; delete this.changed[this.currentId];
}, },
/** /**
@ -149,13 +140,13 @@ Deluge.MultiOptionsManager = Ext.extend(Deluge.OptionsManager, {
* @param {String} option * @param {String} option
* @param {Object} value The value for the option * @param {Object} value The value for the option
*/ */
setDefault: function(id, option, value) { setDefault: function(option, value) {
if (value === undefined) { if (value === undefined) {
for (var key in option) { for (var key in option) {
this.setDefault(id, key, option[key]); this.setDefault(key, option[key]);
} }
} else { } else {
var oldValue = this.getDefault(id, option); var oldValue = this.getDefault(option);
value = this.convertValueType(oldValue, value); value = this.convertValueType(oldValue, value);
// If the value is the same as the old value there is // If the value is the same as the old value there is
@ -163,11 +154,11 @@ Deluge.MultiOptionsManager = Ext.extend(Deluge.OptionsManager, {
if (oldValue == value) return; if (oldValue == value) return;
// Store the new default // Store the new default
if (!this.stored[id]) this.stored[id] = {}; if (!this.stored[this.currentId]) this.stored[this.currentId] = {};
this.stored[id][option] = value; this.stored[this.currentId][option] = value;
if (!this.isDirty(id, option)) { if (!this.isDirty(option)) {
this.fireEvent('changed', id, option, value, oldValue); this.fireEvent('changed', this.currentId, option, value, oldValue);
} }
} }
}, },
@ -178,27 +169,27 @@ Deluge.MultiOptionsManager = Ext.extend(Deluge.OptionsManager, {
* @param {String|Object} option or options to update * @param {String|Object} option or options to update
* @param {Object} [value]; * @param {Object} [value];
*/ */
update: function(id, option, value) { update: function(option, value) {
if (value === undefined) { if (value === undefined) {
for (var key in option) { for (var key in option) {
this.update(id, key, option[key]); this.update(key, option[key]);
} }
} else { } else {
if (!this.changed[id]) this.changed[id] = {}; if (!this.changed[this.currentId]) this.changed[this.currentId] = {};
var defaultValue = this.getDefault(id, option); var defaultValue = this.getDefault(option);
value = this.convertValueType(defaultValue, value); value = this.convertValueType(defaultValue, value);
var oldValue = this.get(id, option); var oldValue = this.get(option);
if (oldValue == value) return; if (oldValue == value) return;
if (defaultValue == value) { if (defaultValue == value) {
if (this.isDirty(id, option)) delete this.changed[id][option]; if (this.isDirty(option)) delete this.changed[this.currentId][option];
this.fireEvent('changed', id, option, value, oldValue); this.fireEvent('changed', this.currentId, option, value, oldValue);
return; return;
} else { } else {
this.changed[id][option] = value; this.changed[this.currentId][option] = value;
this.fireEvent('changed', id, option, value, oldValue); this.fireEvent('changed', this.currentId, option, value, oldValue);
} }
} }
}, },
@ -212,7 +203,7 @@ Deluge.MultiOptionsManager = Ext.extend(Deluge.OptionsManager, {
* @private * @private
*/ */
onFieldChange: function(field, event) { onFieldChange: function(field, event) {
this.update(this.currentId, field._doption, field.getValue()); this.update(field._doption, field.getValue());
}, },
onChange: function(id, option, newValue, oldValue) { onChange: function(id, option, newValue, oldValue) {