This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
chromesoul/lib/options.js

158 lines
4 KiB
JavaScript
Raw Normal View History

2012-10-21 19:46:47 +02:00
//
// Copyright (c) 2012 Rodolphe Breard
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
var OptionsManager = function() {
this.storage = chrome.storage.sync;
2012-12-18 16:27:29 +01:00
this.pass_storage = chrome.storage.local;
2012-10-21 19:46:47 +02:00
this.status = null;
this.save_btn = null;
this.opts = null;
2012-12-18 16:27:29 +01:00
this.values = {};
2012-10-21 19:46:47 +02:00
};
2012-11-14 12:55:24 +01:00
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) {
if (!val) {
elem.removeAttribute('checked');
} else {
elem.setAttribute('checked');
}
}
}
};
2012-12-18 16:27:29 +01:00
OptionsManager.prototype.get = function(name) {
var ret = null;
if (typeof this.values[name] !== "undefined") {
ret = this.values[name];
}
return ret;
};
2012-10-21 19:46:47 +02:00
OptionsManager.prototype.init = function() {
this.status = document.getElementById("status");
this.save_btn = document.getElementById("save");
this.opts = document.getElementsByClassName("opt");
this.restore();
if (this.save !== null) {
2012-11-21 21:13:18 +01:00
this.save_btn.addEventListener("click", (function(elem) {
return function() {
elem.save();
};
})(this));
2012-10-21 19:46:47 +02:00
}
};
2012-12-18 16:27:29 +01:00
OptionsManager.prototype.savePart = function(pass) {
var i = 0, data = {}, storage;
if (pass) {
storage = this.pass_storage;
} else {
storage = this.storage;
}
2012-10-21 19:46:47 +02:00
if (this.opts !== null) {
for (i = this.opts.length - 1; i >= 0; --i) {
2012-12-18 16:27:29 +01:00
if ((pass && this.getElemType(this.opts[i]) === "password")
|| (!pass && this.getElemType(this.opts[i]) !== "password")) {
data[this.opts[i].id] = this.getElemValue(this.opts[i]);
this.values[this.opts[i].id] = data[this.opts[i].id];
}
2012-10-21 19:46:47 +02:00
}
2012-12-18 16:27:29 +01:00
storage.set(data, (function(elem) {
2012-11-21 21:13:18 +01:00
return function () {
if (elem !== null) {
elem.innerHTML = "Options saved.";
setTimeout(function() {
elem.innerHTML = "";
}, 3000);
}
};
})(this.status));
2012-10-21 19:46:47 +02:00
}
};
2012-12-18 16:27:29 +01:00
OptionsManager.prototype.save = function() {
this.savePart(true);
this.savePart(false);
};
2012-11-21 21:13:18 +01:00
2012-12-18 16:27:29 +01:00
OptionsManager.prototype.restore = function() {
var i, el, cb = function(elem) {
2012-11-21 21:13:18 +01:00
return function(items) {
2012-12-18 16:27:29 +01:00
console.log('----------------------------------');
console.log('restoring...');
console.log(items);
2012-11-21 21:13:18 +01:00
for (i in items) {
el = document.getElementById(i);
if (el !== null) {
2012-12-18 16:27:29 +01:00
elem.values[i] = items[i];
2012-11-21 21:13:18 +01:00
elem.setElemValue(el, items[i]);
}
}
2012-12-18 16:27:29 +01:00
console.log('----------------------------------');
2012-11-21 21:13:18 +01:00
};
2012-12-18 16:27:29 +01:00
};
this.storage.get(null, cb(this));
this.pass_storage.get(null, cb(this));
};
OptionsManager.prototype.getElemType = function(elem) {
return elem.getAttribute('type');
2012-10-21 19:46:47 +02:00
};
2012-11-14 12:55:24 +01:00
OptionsManager.prototype.getElemValue = function(elem) {
2012-12-18 16:27:29 +01:00
var val = null, type = this.getElemType(elem);
2012-11-14 12:55:24 +01:00
if (typeof this.types.get[type] !== "undefined") {
val = this.types.get[type](elem);
}
2012-11-28 15:38:21 +01:00
return val;
2012-11-14 12:55:24 +01:00
};
OptionsManager.prototype.setElemValue = function(elem, val) {
var type = elem.getAttribute('type');
if (typeof this.types.set[type] !== "undefined") {
2012-11-28 15:38:21 +01:00
this.types.set[type](elem, val);
2012-11-14 12:55:24 +01:00
}
};