improved options getter/setter

This commit is contained in:
Rodolphe Breard 2012-11-14 12:55:24 +01:00
parent 3497037981
commit 3ae9beb803
2 changed files with 54 additions and 4 deletions

View file

@ -7,9 +7,10 @@
<body>
<h1>Welcome to chromesoul</h1>
<fieldset>
<legend>Connection informations</legend>
<legend>Configuration</legend>
Login: <input type="text" id="login" class="opt"><br>
Password (socks): <input type="password" id="pwd_socks", class="opt"><br>
Password (socks): <input type="password" id="pwd_socks" class="opt"><br>
Enable messages: <input type="checkbox" id="enable_msg" class="opt" checked="checked"><br>
<button id="save">Save</button> <span id="status"></span>
</fieldset>
<p>Status: <span id="status_txt"></span></p>

View file

@ -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;
};