      1 /*
      2 Script:
      3 	Deluge.OptionsManager.js
      4
      5 Copyright:
      6 	(C) Damien Churchill 2009 <damoxc@gmail.com>
      7 	This program is free software; you can redistribute it and/or modify
      8 	it under the terms of the GNU General Public License as published by
      9 	the Free Software Foundation; either version 3, or (at your option)
     10 	any later version.
     11
     12 	This program is distributed in the hope that it will be useful,
     13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 	GNU General Public License for more details.
     16
     17 	You should have received a copy of the GNU General Public License
     18 	along with this program.  If not, write to:
     19 		The Free Software Foundation, Inc.,
     20 		51 Franklin Street, Fifth Floor
     21 		Boston, MA  02110-1301, USA.
     22
     23     In addition, as a special exception, the copyright holders give
     24     permission to link the code of portions of this program with the OpenSSL
     25     library.
     26     You must obey the GNU General Public License in all respects for all of
     27     the code used other than OpenSSL. If you modify file(s) with this
     28     exception, you may extend this exception to your version of the file(s),
     29     but you are not obligated to do so. If you do not wish to do so, delete
     30     this exception statement from your version. If you delete this exception
     31     statement from all source files in the program, then also delete it here.
     32 */
     33
     34 /**
     35  * @description A class that can be used to manage options throughout the ui.
     36  * @namespace Deluge
     37  * @class Deluge.OptionsManager
     38  */
     39 Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
     40
     41 	constructor: function(config) {
     42 		this.binds = {};
     43 		this.changed = {};
     44 		this.defaults = config['defaults'] || {};
     45 		this.options = {};
     46
     47
     48 		this.addEvents({
     49 			'add': true,
     50 			'changed': true,
     51 			'reset': true
     52 		});
     53 		this.on('changed', this.onChange, this);
     54
     55 		Deluge.OptionsManager.superclass.constructor.call(this);
     56 	},
     57
     58 	/**
     59 	 * Add a set of default options and values to the options manager
     60 	 * @param {String} id
     61 	 * @param {Object} options The default options.
     62 	 */
     63 	addOptions: function(id, options) {
     64 		this.options[id] = options;
     65 	},
     66
     67 	/**
     68 	 * Binds a form field to the specified option.
     69 	 * @param {String} option
     70 	 * @param {Ext.form.Field} field
     71 	 */
     72 	bind: function(option, field) {
     73 		this.binds[option] = field;
     74 		this.binds[field] = option;
     75
     76 		switch (field.getXType()) {
     77 			case 'checkbox':
     78 				field.on('check', this.onFieldChange, this);
     79 				break;
     80 			case 'uxspinner':
     81 				field.on('spin', this.onFieldChange, this);
     82 				field.on('keypress', this.onFieldChange, this);
     83 				break;
     84 			default:
     85 				break;
     86 		}
     87 	},
     88
     89 	/**
     90 	 * Returns the changed values for a specified id.
     91 	 * @param {String} id
     92 	 * @returns {Object} the changed options
     93 	 */
     94 	getChanged: function(id) {
     95 		return (this.changed[id]) ? this.changed[id] : {};
     96 	},
     97
     98 	/**
     99 	 * Get the default value for an option given an id and option name.
    100 	 * @param {String} id
    101 	 * @param {String} option
    102 	 * @returns {Object} the value of the option
    103 	 */
    104 	getOption: function(id, option) {
    105 		return (this.hasOption(id, option)) ? this.options[id][option] : this.defaults[option];
    106 	},
    107
    108 	/**
    109 	 * Get the value for an option given an id and option name.
    110 	 * @param {String} id
    111 	 * @param {String} option
    112 	 * @returns {Object} the options value.
    113 	 */
    114 	 getValue: function(id, option) {
    115 	 	return (this.hasChanged(id, option)) ? this.changed[id][option] : this.getOption(id, option);
    116 	 },
    117
    118 	/**
    119 	 * Check to see if the option has been changed.
    120 	 * @param {String} id
    121 	 * @param {String} option
    122 	 * @returns {Boolean} true if the option has been changed, else false.
    123 	 */
    124 	hasChanged: function(id, option) {
    125 		return (this.changed[id] && !Ext.isEmpty(this.changed[id][option]));
    126 	},
    127
    128 	/**
    129 	 * Check to see if an id has had an option set to something other than the
    130 	 * default value.
    131 	 * @param {String} id
    132 	 * @param {String} option
    133 	 * @returns {Boolean} true if the id has an option, else false.
    134 	 */
    135 	hasOption: function(id, option) {
    136 		return (this.options[id] && !Ext.isEmpty(this.options[id][option]));
    137 	},
    138
    139 	/**
    140 	 * Reset the options back to the default values for the specified id.
    141 	 * @param {String} id
    142 	 */
    143 	resetOptions: function(id) {
    144 		if (!this.changed[id]) return;
    145 		delete this.changed[id];
    146 	},
    147
    148 	/**
    149 	 * Set the specified option for the passed in id.
    150 	 * @param {String} id
    151 	 * @param {String} option
    152 	 * @param {Object} value The value for the option
    153 	 */
    154 	setOption: function(id, option, value) {
    155 		this.options[id][option] = value;
    156 	},
    157
    158 	/**
    159 	 * Set the specified options for the passed in id.
    160 	 * @param {String} id
    161 	 * @param {Object} options The option values to change.
    162 	 */
    163 	setOptions: function(id, options) {
    164 		if (!this.changed[id]) this.changed[id] = {};
    165 		for (var key in options) {
    166 			this.setOption(id, key, options[key]);
    167 		}
    168 	},
    169
    170 	/**
    171 	 * Update the value for the specified option and id.
    172 	 * @param {String} id
    173 	 * @param {String} option
    174 	 * @param {Object} value;
    175 	 */
    176 	updateOption: function(id, option, value) {
    177 		if (!this.changed[id]) this.changed[id] = {};
    178
    179 		var oldValue = this.getValue(id, option);
    180 		if (oldValue == value) return;
    181
    182 		var defaultValue = this.getOption(id, option);
    183 		if (defaultValue == value) {
    184 			if (!Ext.isEmpty(this.changed[id][option])) delete this.changed[id][option];
    185 			this.fireEvent('changed', id, option, value, oldValue);
    186 			return;
    187 		}
    188
    189 		if (Ext.type(defaultValue) != Ext.type(value)) {
    190 			switch (Ext.type(defaultValue)) {
    191 				case 'string':
    192 					value = String(value);
    193 					break;
    194 				case 'number':
    195 					value = Number(value);
    196 					break;
    197 				case 'boolean':
    198 					value = Boolean(value);
    199 					break;
    200 			}
    201 		}
    202
    203 		this.changed[id][option] = value;
    204 		this.fireEvent('changed', id, option, value, oldValue);
    205 	},
    206
    207 	/**
    208 	 * Update the options for the specified id.
    209 	 * @param {String} id
    210 	 * @param {Object} options The options to change.
    211 	 */
    212 	updateOptions: function(id, options) {
    213 		for (var key in options) {
    214 			this.updateOption(id, key, options[key]);
    215 		}
    216 	},
    217
    218 	/* Event Handlers */
    219
    220 	/**
    221 	 * Stops a form fields value from being blocked by the change functions
    222 	 * @param {Ext.form.Field} field
    223 	 * @private
    224 	 */
    225 	onFieldChange: function(field) {
    226 		var option = this.binds[field];
    227 		//alert(option);
    228 	},
    229
    230 	onChange: function(id, option, newValue, oldValue) {
    231 		// If we don't have a bind there's nothing to do.
    232 		if (Ext.isEmpty(this.binds[option])) return;
    233
    234 		// Set the form field to the new value.
    235 		this.binds[option].setValue(newValue);
    236 	}
    237 });
    238