diff --git a/js/ThemeManager.js b/js/ThemeManager.js index ddcd2c4..0853a70 100644 --- a/js/ThemeManager.js +++ b/js/ThemeManager.js @@ -1,6 +1,6 @@ /** * @file felexdev-theme-manager.js - * @description Миниицированная версия + * @description Минифицированная версия * @version 0.0.1 * @author felex67 (admin@felexdev.ru) * @license Apache2.0 @@ -22,7 +22,7 @@ class ThemeManager { static toggle = () => { this.#s((this.#index + 1) % this.#themes.length); }; - static { + static /* Инициализация конфигурации */ { const config = this.#config; config.globalPrefix += '-'; config.themePrefix = config.globalPrefix + config.themePrefix; @@ -40,7 +40,7 @@ class ThemeManager { this.#themes.push({ name: name, icon: icon || this.#config.svg, - selector:`${this.themePrefix}-${n}`, + selector:`${this.themePrefix}-${name}`, }); }; static #set(index) { @@ -53,9 +53,10 @@ class ThemeManager { else this.#configl.add(this.#themes[index].selector); this.#save(this.#index = index); this.#button.innerHTML = this.#themes[index].icon; + this.#button.setAttribute('aria-current', this.#themes[index].name); }; static #save = (() => { - let save = (index) => { console.error(this.#config.globalPrefix + '#sv: Can`t save theme id, storages are unreachable.'); }; + let save = (index) => { console.error(this.#config.globalPrefix + '#save: Невозможно сохранить тему, хранилища не доступны.'); }; try { this.#index = parseInt(localStorage.getItem(this.#config.themePrefix)) || 0; save = (index) => { @@ -64,7 +65,7 @@ class ThemeManager { }; } catch(e) { - console.error(this.#config.globalPrefix + '#sv-IIFE: localStorage is unreachable, trying cookie.'); + console.error(this.#config.globalPrefix + '#sv-IIFE: localStorage не доступен, пробую cookie.'); try { let cookie = document.cookie.match(`${this.#config.themePrefix}\\s*=\\s*(\\d+)`); this.#index = cookie ? parseInt(c[1]) || 0 : 0; @@ -75,21 +76,21 @@ class ThemeManager { catch(e) { console.error(e); } }; } - catch(e) { console.error(this.#config.globalPrefix + `#s-IIFE: All storages are unreachable('localStorage' & 'document.cookie')`); } + catch(e) { console.error(this.#config.globalPrefix + `#s-IIFE: Хранилища не доступны. Настройки темы не сохраняются!`); } } - return s; + return save; })(); - static { + static /* Инициализация тем */ { let addTheme = this.#addTheme; /** @warning Системная версия('auto') должна регистрироваться первой!!! */ addTheme('auto',''); addTheme('light',''); addTheme('dark',''); - /* Темы добавлять здесь */ + /* Новые темы добавлять здесь */ addTheme('old-terminal',''); /* */ if (this.#index >= this.#themes.length || this.#index<0) { - console.error(`${this.#config.globalPrefix}bottom-static: Invalid theme index loaded '${this.#index}'`); + console.error(`${this.#config.globalPrefix}bottom-static: В хранилище сохранён не валидный индекс '${this.#index}'. Сброс на системную тему`); this.#index = 0; } this.#set(this.#index); @@ -97,19 +98,19 @@ class ThemeManager { static set = (theme) => { let i = -1; for(let j=0; j < this.#themes.length; j++) { - if(this.#themes[j].n === name) { + if(this.#themes[j].name === name) { i = j; break; } } if(i == -1) { - throw new Error('Invalid color sсheme.'); + throw new Error(`Темы '${theme}' не существует!.`); } this.#set(i); }; static #_ = () => { window.removeEventListener('DOMContentLoaded',this.#_); - document.body.appendChild(this.#b); + document.body.appendChild(this.#button); }; static { let m = this; diff --git a/js/felexdev-theme-manager.js b/js/felexdev-theme-manager.js index 5011dbc..b0af4d7 100644 --- a/js/felexdev-theme-manager.js +++ b/js/felexdev-theme-manager.js @@ -15,7 +15,7 @@ class ThemeManager { stid:'color-scheme', // Селектор CSS: `gpfx + stdid == 'flxd-tm-color-scheme'` svg:'', /* */ - };static #cl=document.documentElement.classList;static #b=document.createElement('div');static #i=0;static #t=[];static toggle=()=>{this.#s((this.#i+1)%this.#t.length);};static{let m=this;let c=m.#c;c.gpfx+='-';c.stid=c.gpfx+c.stid;m.#b.id=c.btnid=c.gpfx+c.btnid;m.#b.tabIndex=m.#c.btnti;m.#b.setAttribute('aria-label', 'toggle color scheme');m.#b.addEventListener('click',m.toggle)};static #at=(n,i=null)=>{this.#t.push({n:n,i:i||this.#c.svg,s:`${this.stid}-${n}`});};static #s(i){if(i==this.#i)return;let m=this;if(i&&m.#i)m.#cl.replace(m.#t[m.#i].s,m.#t[i].s);else if(m.#i)m.#cl.remove(m.#t[m.#i].s);else m.#cl.add(m.#t[i].s);m.#sv(m.#i=i);m.#b.innerHTML=m.#t[i].i;};static #sv=(()=>{let m=this;let s=(i)=>{console.error(m.#c.gpfx+'#sv: Can`t save theme id, storages are unreachable.');};try{m.#i=parseInt(localStorage.getItem(m.#c.stid))||0;s=(i)=>{try{localStorage.setItem(m.#c.stid,i);}catch(e){console.error(e);}};}catch(e){console.error(m.#c.gpfx+'#sv-IIFE: localStorage is unreachable, trying cookie.');try{let c=document.cookie.match(`${m.#c.stid}\\s*=\\s*(\\d+)`);m.#i=c?parseInt(c[1])||0:0;s=(i)=>{try{document.cookie=`${m.#c.stid}=${i};max-age=${31536000};path=/`;}catch(e){console.error(e);}};}catch(e){console.error(m.#c.gpfx+`#s-IIFE: All storages are unreachable('localStorage' & 'document.cookie')`);}}return s;})();static{let m=this; + };static #cl=document.documentElement.classList;static #b=document.createElement('div');static #i=0;static #t=[];static toggle=()=>{this.#s((this.#i+1)%this.#t.length);};static{let m=this;let c=m.#c;c.gpfx+='-';c.stid=c.gpfx+c.stid;m.#b.id=c.btnid=c.gpfx+c.btnid;m.#b.tabIndex=m.#c.btnti;m.#b.setAttribute('aria-label', 'toggle color scheme');m.#b.addEventListener('click',m.toggle)};static #at=(n,i=null)=>{this.#t.push({n:n,i:i||this.#c.svg,s:`${this.stid}-${n}`});};static #s(i){if(i==this.#i)return;let m=this;if(i&&m.#i)m.#cl.replace(m.#t[m.#i].s,m.#t[i].s);else if(m.#i)m.#cl.remove(m.#t[m.#i].s);else m.#cl.add(m.#t[i].s);m.#sv(m.#i=i);m.#b.innerHTML=m.#t[i].i;m.#b.setAttribute('aria-current',m.#t[i].n);};static #sv=(()=>{let m=this;let s=(i)=>{console.error(m.#c.gpfx+'#sv: Can`t save theme id, storages are unreachable.');};try{m.#i=parseInt(localStorage.getItem(m.#c.stid))||0;s=(i)=>{try{localStorage.setItem(m.#c.stid,i);}catch(e){console.error(e);}};}catch(e){console.error(m.#c.gpfx+'#sv-IIFE: localStorage is unreachable, trying cookie.');try{let c=document.cookie.match(`${m.#c.stid}\\s*=\\s*(\\d+)`);m.#i=c?parseInt(c[1])||0:0;s=(i)=>{try{document.cookie=`${m.#c.stid}=${i};max-age=${31536000};path=/`;}catch(e){console.error(e);}};}catch(e){console.error(m.#c.gpfx+`#s-IIFE: All storages are unreachable('localStorage' & 'document.cookie')`);}}return s;})();static{let m=this; /** * Add new theme * @typedef {function} add @@ -30,5 +30,5 @@ class ThemeManager { add('old-terminal',''); /* */ if(m.#i>=m.#t.length||m.#i<0){console.error(`${m.#c.gpfx}bottom-static: Invalid theme index loaded '${m.#i}'`);m.#i=0;}m.#s(m.#i); - };static set=(theme)=>{let i=-1;for(let j=0;j{window.removeEventListener('DOMContentLoaded',this.#_);document.body.appendChild(this.#b);};static{let m=this;m.#s(m.#i);if(!document.body)window.addEventListener('DOMContentLoaded',this.#_);} + };static set=(name)=>{let i=-1;for(let j=0;j{window.removeEventListener('DOMContentLoaded',this.#_);document.body.appendChild(this.#b);};static{let m=this;m.#s(m.#i);if(!document.body)window.addEventListener('DOMContentLoaded',this.#_);} }; \ No newline at end of file