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

114 lines
2.6 KiB
JavaScript
Raw Normal View History

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