From 3ae9beb803ece2f27314adc0bd511d970d7a9b9d Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Wed, 14 Nov 2012 12:55:24 +0100 Subject: [PATCH] improved options getter/setter --- background.html | 5 +++-- lib/options.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/background.html b/background.html index 52d2b0b..d298792 100644 --- a/background.html +++ b/background.html @@ -7,9 +7,10 @@

Welcome to chromesoul

- Connection informations + Configuration Login:
- Password (socks):
+ Password (socks):
+ Enable messages:

Status:

diff --git a/lib/options.js b/lib/options.js index 8c6bea5..e19ba7c 100644 --- a/lib/options.js +++ b/lib/options.js @@ -21,6 +21,36 @@ var OptionsManager = function() { this.opts = null; }; +OptionsManager.prototype.types = { + "get": { + "text": function(elem) { + return elem.value; + }, + "password": function(elem) { + return elem.value; + }, + "checkbox": function(elem) { + return elem.checked; + } + }, + "set": { + "text": function(elem, val) { + elem.value = val; + }, + "password": function(elem, val) { + elem.value = val; + }, + "checkbox": function(elem, val) { + console.log(val); + if (!val) { + elem.removeAttribute('checked'); + } else { + elem.setAttribute('checked'); + } + } + } +}; + OptionsManager.prototype.init = function() { var saveOpts = function(elem) { return function() { @@ -55,7 +85,7 @@ OptionsManager.prototype.save = function() { if (this.opts !== null) { for (i = this.opts.length - 1; i >= 0; --i) { - data[this.opts[i].id] = this.encrypt(this.opts[i].value); + data[this.opts[i].id] = this.getElemValue(this.opts[i]); } this.storage.set(data, notif(this.status)); } @@ -69,7 +99,7 @@ OptionsManager.prototype.restore = function() { for (i in items) { el = document.getElementById(i); if (el !== null) { - el.value = elem.decrypt(items[i]); + elem.setElemValue(el, items[i]); } } }; @@ -78,6 +108,25 @@ OptionsManager.prototype.restore = function() { this.storage.get(null, rst(this)); }; +OptionsManager.prototype.getElemValue = function(elem) { + var val = null, + type = elem.getAttribute('type'); + + if (typeof this.types.get[type] !== "undefined") { + val = this.types.get[type](elem); + } + + return this.encrypt(val); +}; + +OptionsManager.prototype.setElemValue = function(elem, val) { + var type = elem.getAttribute('type'); + + if (typeof this.types.set[type] !== "undefined") { + this.types.set[type](elem, this.decrypt(val)); + } +}; + OptionsManager.prototype.encrypt = function(value) { return value; };