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

171 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-10-04 11:27:40 +12:00
'use strict'
2018-08-28 16:34:17 +12:00
2019-11-03 13:36:58 -05:00
/* global localStorage */
/* global FileReader */
/* global DOMParser */
2019-11-08 11:19:27 -05:00
function Theme (client) {
2018-10-04 11:27:40 +12:00
this.el = document.createElement('style')
2018-09-14 12:23:13 +12:00
this.el.type = 'text/css'
2018-09-14 13:50:20 +12:00
2019-11-06 20:32:09 -05:00
this.active = {}
this.default = {
2019-11-09 12:41:55 -05:00
background: '#eeeeee',
2019-11-10 10:48:31 -05:00
f_high: '#0a0a0a',
f_med: '#4a4a4a',
f_low: '#6a6a6a',
f_inv: '#111111',
b_high: '#a1a1a1',
b_med: '#c1c1c1',
b_low: '#ffffff',
2019-11-06 20:32:09 -05:00
b_inv: '#ffb545'
}
2019-11-09 12:41:55 -05:00
// Callbacks
this.onLoad = () => {}
2019-11-06 20:32:09 -05:00
this.install = (host = document.body) => {
window.addEventListener('dragover', this.drag)
window.addEventListener('drop', this.drop)
2018-09-12 13:20:31 +12:00
host.appendChild(this.el)
}
2017-11-07 15:10:09 +13:00
2019-11-03 13:36:58 -05:00
this.start = () => {
2018-10-04 11:27:40 +12:00
console.log('Theme', 'Starting..')
2018-12-22 10:07:46 +12:00
if (isJson(localStorage.theme)) {
const storage = JSON.parse(localStorage.theme)
2019-11-06 20:32:09 -05:00
if (isValid(storage)) {
2019-11-08 11:19:27 -05:00
console.log('Theme', 'Loading theme in localStorage..')
2018-12-22 10:07:46 +12:00
this.load(storage)
return
}
}
2019-11-06 20:32:09 -05:00
this.load(this.default)
2018-09-14 12:23:13 +12:00
}
2017-11-07 15:10:09 +13:00
2019-11-08 11:19:27 -05:00
this.open = () => {
console.log('Theme', 'Open theme..')
const input = document.createElement('input')
input.type = 'file'
input.onchange = (e) => {
this.read(e.target.files[0], this.load)
}
input.click()
}
2019-11-03 13:36:58 -05:00
this.load = (data) => {
2019-11-06 20:32:09 -05:00
const theme = this.parse(data)
if (!isValid(theme)) { console.warn('Theme', 'Invalid format'); return }
2019-11-03 13:36:58 -05:00
console.log('Theme', 'Loaded theme!')
2019-11-06 20:32:09 -05:00
this.el.innerHTML = `: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-10-04 11:27:40 +12:00
localStorage.setItem('theme', JSON.stringify(theme))
2018-12-22 10:07:46 +12:00
this.active = theme
2019-11-09 12:41:55 -05:00
if (this.onLoad) {
this.onLoad(data)
}
2018-09-14 13:50:20 +12:00
}
2019-11-03 13:36:58 -05:00
this.reset = () => {
this.load(this.default)
}
2019-11-09 12:41:55 -05:00
this.set = (key, val) => {
if (!val) { return }
const hex = (`${val}`.substr(0, 1) !== '#' ? '#' : '') + `${val}`
if (!isColor(hex)) { console.warn('Theme', `${hex} is not a valid color.`); return }
this.active[key] = hex
}
2019-11-06 20:32:09 -05:00
this.read = (key) => {
2019-11-03 13:36:58 -05:00
return this.active[key]
2018-09-14 13:50:20 +12:00
}
2019-11-06 20:32:09 -05:00
this.parse = (any) => {
if (isValid(any)) { return any }
if (isJson(any)) { return JSON.parse(any) }
if (isHtml(any)) { return extract(any) }
2018-09-14 13:50:20 +12:00
}
2018-09-12 13:20:31 +12:00
// Drag
2019-11-03 13:36:58 -05:00
this.drag = (e) => {
2018-10-04 11:27:40 +12:00
e.stopPropagation()
e.preventDefault()
e.dataTransfer.dropEffect = 'copy'
2018-01-12 16:33:58 +13:00
}
2019-11-03 13:36:58 -05:00
this.drop = (e) => {
2018-10-04 11:27:40 +12:00
e.preventDefault()
const file = e.dataTransfer.files[0]
2019-11-08 11:19:27 -05:00
if (file.name.indexOf('.svg') > -1) {
this.read(file, this.load)
2018-10-04 11:27:40 +12:00
}
2019-11-06 20:32:09 -05:00
e.stopPropagation()
2017-11-07 15:10:09 +13:00
}
2019-11-08 11:19:27 -05:00
this.read = (file, callback) => {
const reader = new FileReader()
reader.onload = (event) => {
callback(event.target.result)
}
reader.readAsText(file, 'UTF-8')
}
2018-12-22 10:07:46 +12:00
// Helpers
2019-11-06 20:32:09 -05:00
function extract (xml) {
const svg = new DOMParser().parseFromString(xml, 'text/xml')
2018-12-22 10:07:46 +12:00
try {
return {
2019-11-03 13:36:58 -05:00
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')
2018-12-22 10:07:46 +12:00
}
} catch (err) {
console.warn('Theme', 'Incomplete SVG Theme', err)
}
}
2019-11-06 20:32:09 -05:00
function isValid (json) {
if (!json) { return false }
2019-11-10 10:48:31 -05:00
if (!json.background || !isColor(json.background)) { return false }
if (!json.f_high || !isColor(json.f_high)) { return false }
if (!json.f_med || !isColor(json.f_med)) { return false }
if (!json.f_low || !isColor(json.f_low)) { return false }
if (!json.f_inv || !isColor(json.f_inv)) { return false }
if (!json.b_high || !isColor(json.b_high)) { return false }
if (!json.b_med || !isColor(json.b_med)) { return false }
if (!json.b_low || !isColor(json.b_low)) { return false }
if (!json.b_inv || !isColor(json.b_inv)) { return false }
2019-11-06 20:32:09 -05:00
return true
}
2019-11-09 12:41:55 -05:00
function isColor (hex) {
return /^#([0-9A-F]{3}){1,2}$/i.test(hex)
}
2018-12-22 10:07:46 +12:00
function isJson (text) {
try { JSON.parse(text); return true } catch (error) { return false }
}
function isHtml (text) {
try { new DOMParser().parseFromString(text, 'text/xml'); return true } catch (error) { return false }
}
2018-10-04 11:27:40 +12:00
}