the-glorious-startpage/js/theme-engine.js

236 lines
6.8 KiB
JavaScript
Raw Normal View History

2020-06-09 04:02:03 +02:00
class ThemeEngine {
2020-06-09 12:13:12 +02:00
2020-06-09 04:02:03 +02:00
constructor() {
this._localStorage = window.localStorage;
this._backgroundTextBox = document.querySelector('#backgroundSet');
this._backgroundOpacityTextBox = document.querySelector('#backgroundOpacitySet');
this._foregroundTextBox = document.querySelector('#foregroundSet');
this._foregroundOpacityTextBox = document.querySelector('#foregroundOpacitySet');
this._blurTextBox = document.querySelector('#blurSet');
this._animSpeedTextBox = document.querySelector('#animSpeedSet');
2020-06-09 04:02:03 +02:00
this._applyTheme = document.querySelector('#themeEngineAsDefault');
this._resetTheme = document.querySelector('#themeEngineReset');
this._init();
}
_saveDefaultCSS = () => {
const origBaseBG = this._localStorage.getItem('origBaseBG');
const origBaseColor = this._localStorage.getItem('origBaseColor');
const origBlurStrength = this._localStorage.getItem('origBlurStrength');
const origAnimSpeed = this._localStorage.getItem('origAnimSpeed');
2020-06-09 04:02:03 +02:00
if ((origBaseBG === null) || (origBaseColor === null) || (origBlurStrength === null) || (origAnimSpeed === null)) {
2020-06-09 04:02:03 +02:00
this._localStorage.setItem(
'origBaseBG',
window.getComputedStyle(document.documentElement).getPropertyValue('--base-bg')
);
this._localStorage.setItem(
'origBaseColor',
window.getComputedStyle(document.documentElement).getPropertyValue('--base-color')
);
this._localStorage.setItem(
'origBlurStrength',
window.getComputedStyle(document.documentElement).getPropertyValue('--blur-strength')
);
this._localStorage.setItem(
'origAnimSpeed',
window.getComputedStyle(document.documentElement).getPropertyValue('--transition-speed')
);
2020-06-09 04:02:03 +02:00
}
}
2020-06-09 04:02:03 +02:00
_checkColorValidity = colorStr => {
2020-06-09 04:02:03 +02:00
// Check if RGBA - (#RRGGBBAA)
const colorRGBA = /^#[0-9A-F]{8}$/i.test(colorStr);
2020-06-09 04:02:03 +02:00
// If not RGBA
if (!colorRGBA) {
2020-06-09 04:02:03 +02:00
// If RGB - (#RRGGBB)
if (/^#[0-9A-F]{3}$/i.test(colorStr)) {
2020-06-09 04:02:03 +02:00
// Add completely opaque alpha
return colorStr + 'FF';
2020-06-09 04:02:03 +02:00
// If three-charactered HEX color - (#RGB)
} else if (/^#[0-9A-F]{3}$/i.test(colorStr)) {
2020-06-09 04:02:03 +02:00
// Convert it to RRGGBB
return colorStr.replace(/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/, '#$1$1$2$2$3$3');
// If three-charactered HEX Color(#RGB) with AA - (#RGBAA)
2020-06-09 06:26:02 +02:00
} else if (colorStr.length === 6) {
2020-06-09 04:02:03 +02:00
const bg = colorStr.slice(0, -2);
const op = colorStr.slice(-2);
return bg.replace(/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/, '#$1$1$2$2$3$3') + op;
} else {
alert('Invalid color');
return false;
2020-06-09 04:02:03 +02:00
}
}
// It's RGBA and a valid color so just return it
return colorStr;
}
_updateTextBoxValues = (bgColor, bgOpacity, fgColor, fgOpacity, blurPower, animSpeed) => {
2020-06-09 04:02:03 +02:00
// Set placeholders
this._backgroundTextBox.value = '';
this._backgroundTextBox.placeholder = bgColor;
2020-06-09 04:02:03 +02:00
this._backgroundOpacityTextBox.value = '';
this._backgroundOpacityTextBox.placeholder = bgOpacity;
2020-06-09 04:02:03 +02:00
this._foregroundTextBox.value = '';
this._foregroundTextBox.placeholder = fgColor;
this._foregroundOpacityTextBox.value = '';
this._foregroundOpacityTextBox.placeholder = fgOpacity;
2020-06-09 04:02:03 +02:00
this._blurTextBox.value = '';
this._blurTextBox.placeholder = blurPower;
this._animSpeedTextBox.value = '';
this._animSpeedTextBox.placeholder = animSpeed;
2020-06-09 04:02:03 +02:00
}
_processTheme = () => {
// Retrieve custom colors
let baseBG = this._localStorage.getItem('baseBG');
let baseColor = this._localStorage.getItem('baseColor');
let blurStrength = this._localStorage.getItem('blurStrength');
let animSpeed = this._localStorage.getItem('animSpeed');
2020-06-09 04:02:03 +02:00
// If custom color doesn't exist, use the value in CSS
if (baseBG === null) {
baseBG = this._localStorage.getItem('origBaseBG');
}
2020-06-09 04:02:03 +02:00
if (baseColor === null) {
baseColor = this._localStorage.getItem('origBaseColor');
}
2020-06-09 04:02:03 +02:00
if (blurStrength === null) {
blurStrength = this._localStorage.getItem('origBlurStrength');
}
if (animSpeed === null) {
animSpeed = this._localStorage.getItem('origAnimSpeed');
}
2020-06-09 04:02:03 +02:00
// Remove whitespace
baseBG = baseBG.replace(/ /g,'');
baseColor = baseColor.replace(/ /g,'');
blurStrength = blurStrength.replace(/ /g,'');
animSpeed = animSpeed.replace(/ /g,'');
2020-06-09 04:02:03 +02:00
// Check validity
baseBG = this._checkColorValidity(baseBG);
baseColor = this._checkColorValidity(baseColor);
// Slice to separate RGB and A of background color
// Slice alpha out
const backgroundColor = baseBG.slice(0, -2);
// Get alpha
const backgroundOpacity = baseBG.slice(-2);
// Slice to separate RGB and A of foreground color
// Slice alpha out
const foregroundColor = baseColor.slice(0, -2);
// Get alpha
const foregroundOpacity = baseColor.slice(-2);
this._updateTextBoxValues(
backgroundColor,
backgroundOpacity,
foregroundColor,
foregroundOpacity,
blurStrength,
animSpeed
);
}
2020-06-09 04:02:03 +02:00
_updateCSSconstiables = () => {
2020-06-09 04:02:03 +02:00
// Get value from input fields
const background = (this._backgroundTextBox.value || this._backgroundTextBox.placeholder) +
(this._backgroundOpacityTextBox.value || this._backgroundOpacityTextBox.placeholder);
2020-06-09 04:02:03 +02:00
const foreground = (this._foregroundTextBox.value || this._foregroundTextBox.placeholder) +
(this._foregroundOpacityTextBox.value || this._foregroundOpacityTextBox.placeholder);
const blurPower = (this._blurTextBox.value || this._blurTextBox.placeholder);
const animSpeed = (this._animSpeedTextBox.value || this._animSpeedTextBox.placeholder);
2020-06-09 04:02:03 +02:00
// Check color validity
const bgColor = this._checkColorValidity(background);
const fgColor = this._checkColorValidity(foreground);
2020-06-09 04:02:03 +02:00
// Change CSS colors
document.documentElement.style.setProperty('--base-bg', bgColor);
document.documentElement.style.setProperty('--base-color', fgColor);
document.documentElement.style.setProperty('--blur-strength', blurPower);
document.documentElement.style.setProperty('--transition-speed', animSpeed);
2020-06-09 04:02:03 +02:00
// Save custom color
this._localStorage.setItem('baseBG', bgColor);
this._localStorage.setItem('baseColor', fgColor);
this._localStorage.setItem('blurStrength', blurPower);
this._localStorage.setItem('animSpeed', animSpeed);
2020-06-09 04:02:03 +02:00
this._processTheme();
}
2020-06-09 04:02:03 +02:00
_applyOnClickEvent = e => {
this._updateCSSconstiables();
alert('Success!');
}
2020-06-09 04:02:03 +02:00
_registerApplyOnClickEvent = () => {
this._applyTheme.onclick = this._applyOnClickEvent;
}
2020-06-09 04:02:03 +02:00
_resetOnClickEvent = e => {
this._localStorage.removeItem('baseBG');
this._localStorage.removeItem('baseColor');
this._localStorage.removeItem('blurStrength');
this._localStorage.removeItem('animSpeed');
2020-06-09 04:02:03 +02:00
this._saveDefaultCSS();
this._processTheme();
this._updateCSSconstiables();
alert('Success!');
}
_registerResetOnClickEvent = () => {
this._resetTheme.onclick = this._resetOnClickEvent;
}
_init = () => {
this._saveDefaultCSS();
// Update
this._processTheme();
// Update settings
this._updateCSSconstiables();
this._registerApplyOnClickEvent();
this._registerResetOnClickEvent();
}
2020-06-09 04:02:03 +02:00
}