| 1 | 'use strict' |
| 2 | |
| 3 | function Theme (_default) { |
| 4 | const fs = require('fs') |
| 5 | const themer = this |
| 6 | |
| 7 | this.active = _default |
| 8 | |
| 9 | this.el = document.createElement('style') |
| 10 | this.el.type = 'text/css' |
| 11 | |
| 12 | this.install = function (host = document.body, callback) { |
| 13 | host.appendChild(this.el) |
| 14 | this.callback = callback |
| 15 | } |
| 16 | |
| 17 | this.start = function () { |
| 18 | console.log('Theme', 'Starting..') |
| 19 | if (isJson(localStorage.theme)) { |
| 20 | const storage = JSON.parse(localStorage.theme) |
| 21 | if (validate(storage)) { |
| 22 | console.log('Theme', 'Loading localStorage..') |
| 23 | this.load(storage) |
| 24 | return |
| 25 | } |
| 26 | } |
| 27 | this.load(_default) |
| 28 | } |
| 29 | |
| 30 | this.load = function (data) { |
| 31 | const theme = parse(data) |
| 32 | if (!validate(theme)) { console.warn('Theme', 'Not a theme', theme); return } |
| 33 | console.log('Theme', 'Loaded theme!') |
| 34 | 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}; }` |
| 35 | localStorage.setItem('theme', JSON.stringify(theme)) |
| 36 | this.active = theme |
| 37 | if (this.callback) { |
| 38 | this.callback() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | this.reset = function () { |
| 43 | this.load(_default) |
| 44 | } |
| 45 | |
| 46 | this.setImage = function (path) { |
| 47 | document.body.style.backgroundImage = path && fs.existsSync(path) && document.body.style.backgroundImage !== `url(${path})` ? `url(${path})` : '' |
| 48 | } |
| 49 | |
| 50 | function parse (any) { |
| 51 | if (any && any.background) { return any } else if (any && any.data) { return any.data } else if (any && isJson(any)) { return JSON.parse(any) } else if (any && isHtml(any)) { return extract(any) } |
| 52 | return null |
| 53 | } |
| 54 | |
| 55 | // Drag |
| 56 | |
| 57 | this.drag = function (e) { |
| 58 | e.stopPropagation() |
| 59 | e.preventDefault() |
| 60 | e.dataTransfer.dropEffect = 'copy' |