2018-08-28 00:34:17 -04:00
|
|
|
'use strict';
|
|
|
|
|
2018-09-13 20:23:13 -04:00
|
|
|
function Theme(default_theme = {background: "#222", f_high: "#fff", f_med: "#777", f_low: "#444", f_inv: "#fff", b_high: "#000", b_med: "#aaa", b_low: "#000", b_inv: "#000"})
|
2017-11-06 21:10:09 -05:00
|
|
|
{
|
2018-09-13 20:23:13 -04:00
|
|
|
let themer = this;
|
2018-01-11 22:38:42 -05:00
|
|
|
|
2018-09-13 20:23:13 -04:00
|
|
|
this.el = document.createElement("style")
|
|
|
|
this.el.type = 'text/css'
|
2018-09-13 21:50:20 -04:00
|
|
|
|
2018-09-13 20:23:13 -04:00
|
|
|
this.callback;
|
|
|
|
this.active;
|
2018-09-11 21:20:31 -04:00
|
|
|
|
|
|
|
this.collection = {
|
2018-09-13 20:23:13 -04:00
|
|
|
default: default_theme,
|
|
|
|
noir: {background: "#222", f_high: "#fff", f_med: "#777", f_low: "#444", f_inv: "#fff", b_high: "#000", b_med: "#aaa", b_low: "#000", b_inv: "#000" },
|
|
|
|
pale: {background: "#e1e1e1", f_high: "#000", f_med: "#777", f_low: "#aaa", f_inv: "#000", b_high: "#000", b_med: "#aaa", b_low: "#ccc", b_inv: "#fff" }
|
2018-09-11 21:20:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.install = function(host = document.body,callback)
|
|
|
|
{
|
2018-09-13 23:03:12 -04:00
|
|
|
console.log("Theme","Installing..")
|
2018-09-11 21:20:31 -04:00
|
|
|
host.appendChild(this.el)
|
2018-09-13 20:23:13 -04:00
|
|
|
this.callback = callback
|
2018-09-11 21:20:31 -04:00
|
|
|
}
|
2017-11-06 21:10:09 -05:00
|
|
|
|
|
|
|
this.start = function()
|
|
|
|
{
|
2018-09-13 23:03:12 -04:00
|
|
|
console.log("Theme","Starting..")
|
2018-09-13 20:23:13 -04:00
|
|
|
let storage = is_json(localStorage.theme) ? JSON.parse(localStorage.theme) : this.collection.default;
|
|
|
|
this.load(!storage.background ? this.collection.default : storage)
|
|
|
|
}
|
2017-11-06 21:10:09 -05:00
|
|
|
|
2018-09-13 21:50:20 -04:00
|
|
|
this.save = function(theme)
|
2018-09-13 20:23:13 -04:00
|
|
|
{
|
2018-09-13 23:03:12 -04:00
|
|
|
console.log("Theme","Saving..")
|
2018-09-13 21:50:20 -04:00
|
|
|
this.active = theme;
|
|
|
|
localStorage.setItem("theme", JSON.stringify(theme));
|
2018-09-13 20:23:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.load = function(theme, fall_back = this.collection.noir)
|
|
|
|
{
|
|
|
|
if(!theme || !theme.background){ console.warn("Theme","Not a theme",theme); return; }
|
|
|
|
|
2018-09-13 21:50:20 -04:00
|
|
|
this.save(theme);
|
|
|
|
this.apply(theme);
|
2018-09-13 20:23:13 -04:00
|
|
|
|
2018-09-13 21:50:20 -04:00
|
|
|
if(this.callback){
|
|
|
|
this.callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.apply = function(theme)
|
|
|
|
{
|
2018-09-13 20:23:13 -04:00
|
|
|
this.el.innerHTML = `
|
2018-01-11 22:22:50 -05:00
|
|
|
:root {
|
|
|
|
--background: ${theme.background};
|
|
|
|
--f_high: ${theme.f_high};
|
|
|
|
--f_med: ${theme.f_med};
|
|
|
|
--f_low: ${theme.f_low};
|
|
|
|
--f_inv: ${theme.f_inv};
|
|
|
|
--b_high: ${theme.b_high};
|
|
|
|
--b_med: ${theme.b_med};
|
|
|
|
--b_low: ${theme.b_low};
|
|
|
|
--b_inv: ${theme.b_inv};
|
|
|
|
}`;
|
2018-09-13 20:23:13 -04:00
|
|
|
}
|
|
|
|
|
2018-09-13 21:50:20 -04:00
|
|
|
this.parse = function(any)
|
2018-09-11 21:20:31 -04:00
|
|
|
{
|
2018-09-13 21:50:20 -04:00
|
|
|
let theme;
|
2018-01-11 22:22:50 -05:00
|
|
|
|
2018-09-13 23:09:45 -04:00
|
|
|
if(any && any.background){ return any; }
|
|
|
|
else if(any && any.data){ return any.data; }
|
2018-09-13 21:50:20 -04:00
|
|
|
else if(any && is_json(any)){ return JSON.parse(any); }
|
|
|
|
else if(any && is_html(any)){ return this.extract(any); }
|
2018-09-11 21:20:31 -04:00
|
|
|
|
2018-09-13 21:50:20 -04:00
|
|
|
return null;
|
2018-09-11 21:20:31 -04:00
|
|
|
}
|
|
|
|
|
2018-09-13 20:23:13 -04:00
|
|
|
this.extract = function(text)
|
|
|
|
{
|
|
|
|
let svg = new DOMParser().parseFromString(text,"text/xml")
|
|
|
|
|
|
|
|
try{
|
|
|
|
return {
|
|
|
|
"background": svg.getElementById("background").getAttribute("fill"),
|
|
|
|
"f_high": svg.getElementById("f_high").getAttribute("fill"),
|
|
|
|
"f_med": svg.getElementById("f_med").getAttribute("fill"),
|
|
|
|
"f_low": svg.getElementById("f_low").getAttribute("fill"),
|
|
|
|
"f_inv": svg.getElementById("f_inv").getAttribute("fill"),
|
|
|
|
"b_high": svg.getElementById("b_high").getAttribute("fill"),
|
|
|
|
"b_med": svg.getElementById("b_med").getAttribute("fill"),
|
|
|
|
"b_low": svg.getElementById("b_low").getAttribute("fill"),
|
|
|
|
"b_inv": svg.getElementById("b_inv").getAttribute("fill")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(err){
|
|
|
|
console.warn("Theme","Incomplete SVG Theme", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 21:50:20 -04:00
|
|
|
this.reset = function()
|
|
|
|
{
|
|
|
|
this.load(this.collection.default);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
|
|
|
|
this.pale = function()
|
|
|
|
{
|
|
|
|
this.load(this.collection.pale)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.noir = function()
|
|
|
|
{
|
|
|
|
this.load(this.collection.noir)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.invert = function()
|
|
|
|
{
|
2018-09-13 23:09:45 -04:00
|
|
|
this.load(this.active.background == this.collection.noir.background ? this.collection.pale : this.collection.noir)
|
2018-09-13 21:50:20 -04:00
|
|
|
}
|
|
|
|
|
2018-09-11 21:20:31 -04:00
|
|
|
// Drag
|
|
|
|
|
2018-09-13 20:23:13 -04:00
|
|
|
this.drag = function(e)
|
2018-01-11 22:33:58 -05:00
|
|
|
{
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
e.dataTransfer.dropEffect = 'copy';
|
|
|
|
}
|
|
|
|
|
2018-09-13 20:23:13 -04:00
|
|
|
this.drop = function(e)
|
2018-01-11 22:22:50 -05:00
|
|
|
{
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-01-11 22:33:58 -05:00
|
|
|
|
2018-08-26 15:39:15 -04:00
|
|
|
let file = e.dataTransfer.files[0];
|
2018-01-11 22:22:50 -05:00
|
|
|
|
2018-09-13 23:03:12 -04:00
|
|
|
if(!file || !file.name){ console.warn("Theme","Unnamed file."); return; }
|
2018-09-13 20:23:13 -04:00
|
|
|
if(file.name.indexOf(".thm") < 0 && file.name.indexOf(".svg") < 0){ console.warn("Theme","Skipped, not a theme"); return; }
|
2018-01-11 22:22:50 -05:00
|
|
|
|
2018-08-26 15:39:15 -04:00
|
|
|
let reader = new FileReader();
|
2018-01-11 22:22:50 -05:00
|
|
|
reader.onload = function(e){
|
2018-09-13 21:50:20 -04:00
|
|
|
themer.load(themer.parse(e.target.result));
|
2018-01-11 22:22:50 -05:00
|
|
|
};
|
|
|
|
reader.readAsText(file);
|
2017-11-06 21:10:09 -05:00
|
|
|
}
|
|
|
|
|
2018-09-13 20:23:13 -04:00
|
|
|
window.addEventListener('dragover',this.drag);
|
|
|
|
window.addEventListener('drop', this.drop);
|
2018-09-11 21:20:31 -04:00
|
|
|
|
|
|
|
function is_json(text){ try{ JSON.parse(text); return true; } catch (error){ return false; } }
|
2018-09-13 20:23:13 -04:00
|
|
|
function is_html(text){ try{ new DOMParser().parseFromString(text,"text/xml"); return true; } catch (error){ return false; } }
|
2017-11-06 21:10:09 -05:00
|
|
|
}
|