homepage/content/misc/twatch-config/twatch.js

83 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2015-11-22 08:27:58 -07:00
function getQueryParam(variable, defaultValue) {
var query = location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (pair[0] === variable) {
return decodeURIComponent(pair[1]);
}
}
return defaultValue || false;
}
function submit() {
var form = document.getElementById("main-form");
var inputs = form.getElementsByTagName("input");
2016-01-23 11:33:51 -07:00
var selects = form.getElementsByTagName("select");
2015-11-22 08:27:58 -07:00
var return_to = getQueryParam('return_to', 'pebblejs://close#');
var options = {};
console.log("Submitting");
2016-01-23 11:33:51 -07:00
for (var i = 0; i < (inputs.length + selects.length); i += 1) {
var input = inputs[i] || selects[i - inputs.length];
2015-11-22 08:27:58 -07:00
var k = input.name;
var t = input.type;
var v;
if (t == "checkbox") {
v = input.checked;
} else if (t == "text") {
v = Number(input.value);
2016-01-23 11:33:51 -07:00
} else if (t == "select-one") {
2017-04-29 13:13:10 -06:00
v = input.value;
2015-11-22 08:27:58 -07:00
} else {
2016-06-03 10:42:35 -06:00
console.log("Unknown type: " + t);
2015-11-22 08:27:58 -07:00
continue;
}
2016-06-03 10:42:35 -06:00
console.log(k, t, v);
2015-11-22 08:27:58 -07:00
localStorage[k] = v;
options[k] = v;
}
document.location = return_to + encodeURIComponent(JSON.stringify(options));
}
function restore() {
var form = document.getElementById("main-form");
var inputs = form.getElementsByTagName("input");
2016-01-23 11:33:51 -07:00
var selects = form.getElementsByTagName("select");
for (var i = 0; i < (inputs.length + selects.length); i += 1) {
var input = inputs[i] || selects[i - inputs.length];
2015-11-22 08:27:58 -07:00
var k = input.name;
var t = input.type;
var v = localStorage[k];
2016-01-23 11:33:51 -07:00
console.log(k, t, v);
2015-11-22 08:27:58 -07:00
if (v != undefined) {
if (t == "checkbox") {
input.checked = (v=="true")?true:false;
2016-01-23 11:33:51 -07:00
} else if (t == "select-one") {
input.value = v;
2015-11-22 08:27:58 -07:00
} else if (k.slice(0, 6) == "color-") {
var val = ("000000" + Number(v).toString(16)).slice(-6);
var hex = "0x" + val;
var color = "#" + val;
var $valueDisplay = $(input).parent().find('.value');
$valueDisplay.css('background', color);
$(input).val(hex);
}
}
}
}
function init() {
var submit_btn = document.getElementById("submit");
submit_btn.addEventListener("click", submit);
restore();
console.log("Initialized");
}
window.addEventListener("load", init);