pointvec/desktop/sources/scripts/lib/theme.js

114 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-08-28 00:34:17 -04:00
'use strict';
2017-11-06 21:10:09 -05:00
function Theme()
{
2018-09-11 21:20:31 -04:00
let theme = this;
2018-01-11 22:38:42 -05:00
2017-11-06 21:10:09 -05:00
this.el = document.createElement("style");
2018-01-11 22:22:50 -05:00
this.el.type = 'text/css';
2018-09-11 21:20:31 -04:00
this.callback = null;
this.collection = {
2018-09-11 23:27:01 -04:00
noir: {meta:{}, data: { 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: {meta:{}, data: { 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.active = this.collection.noir;
this.install = function(host = document.body,callback)
{
host.appendChild(this.el)
this.callback = callback;
}
2017-11-06 21:10:09 -05:00
this.start = function()
{
2018-09-11 21:20:31 -04:00
this.load(localStorage.theme ? localStorage.theme : this.collection.noir, this.collection.noir);
2017-11-06 21:10:09 -05:00
}
2018-09-11 21:20:31 -04:00
this.load = function(t, fall_back = this.collection.noir)
2017-11-06 21:10:09 -05:00
{
2018-08-26 15:39:15 -04:00
let theme = is_json(t) ? JSON.parse(t).data : t.data;
2017-11-06 21:10:09 -05:00
2018-03-05 14:19:21 -05:00
if(!theme || !theme.background){
if(fall_back) {
theme = fall_back.data;
} else {
return;
}
}
2017-11-06 21:10:09 -05:00
2018-08-26 15:39:15 -04:00
let css = `
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};
}`;
2017-11-06 21:10:09 -05:00
this.active = theme;
2018-09-11 21:20:31 -04:00
this.el.innerHTML = css;
2018-03-05 14:19:21 -05:00
localStorage.setItem("theme", JSON.stringify({data: theme}));
2018-09-11 21:20:31 -04:00
if(this.callback){
this.callback();
}
2017-11-06 21:10:09 -05:00
}
this.reset = function()
{
2018-09-11 21:20:31 -04:00
this.load(this.collection.noir);
}
// Defaults
this.pale = function()
{
this.load(this.collection.pale)
2018-01-11 22:22:50 -05:00
}
2018-09-11 21:20:31 -04:00
this.noir = function()
{
this.load(this.collection.noir)
}
this.invert = function()
{
this.load(this.active.background == this.collection.noir.data.background ? this.collection.pale : this.collection.noir)
}
// Drag
2018-01-11 22:33:58 -05:00
this.drag_enter = function(e)
{
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}
2018-01-11 22:22:50 -05:00
this.drag = function(e)
{
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
if(!file.name || !file.name.indexOf(".thm") < 0){ console.log("Theme","Not a theme"); return; }
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-11 21:20:31 -04:00
theme.load(e.target.result);
2018-01-11 22:22:50 -05:00
};
reader.readAsText(file);
2017-11-06 21:10:09 -05:00
}
2018-09-11 21:20:31 -04:00
window.addEventListener('dragover',this.drag_enter);
window.addEventListener('drop', this.drag);
function is_json(text){ try{ JSON.parse(text); return true; } catch (error){ return false; } }
2017-11-06 21:10:09 -05:00
}