theme system updated
This commit is contained in:
1 parent
369845f601
commit
da3ea2173a
4 files changed
+126
-62
No files matched your search
@@ -4,35 +4,9 @@ export default function (app) {
|
|||||||
app.component('accessibilityControl', {
|
app.component('accessibilityControl', {
|
||||||
template,
|
template,
|
||||||
controller: ($scope, theme) => {
|
controller: ($scope, theme) => {
|
||||||
const themeSet = {
|
$scope.setColorTheme = theme.setColorTheme.bind(theme)
|
||||||
accessibility: 'accessibility',
|
$scope.setFontTheme = theme.setFontTheme.bind(theme)
|
||||||
color: '',
|
$scope.setFontFamilyTheme = theme.setFontFamilyTheme.bind(theme)
|
||||||
fontSize: '',
|
|
||||||
fontFamily: '',
|
|
||||||
}
|
|
||||||
|
|
||||||
const install = () => {
|
|
||||||
const baked = Object
|
|
||||||
.values(themeSet)
|
|
||||||
.filter(Boolean)
|
|
||||||
.join(' ')
|
|
||||||
theme.setTheme(baked)
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.setColorTheme = (color) => {
|
|
||||||
themeSet.color = color
|
|
||||||
install()
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.setFontTheme = (font) => {
|
|
||||||
themeSet.fontSize = font
|
|
||||||
install()
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.setFontFamilyTheme = (font) => {
|
|
||||||
themeSet.fontFamily = font
|
|
||||||
install()
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@ export default function (app) {
|
|||||||
`,
|
`,
|
||||||
controller: ($scope, theme) => {
|
controller: ($scope, theme) => {
|
||||||
const update = () => {
|
const update = () => {
|
||||||
$scope.isMainTheme = theme.currentTheme === theme.mainTheme
|
$scope.isMainTheme = theme.isDefaultTheme()
|
||||||
}
|
}
|
||||||
|
|
||||||
$scope.toggleTheme = () => {
|
$scope.toggleTheme = () => {
|
||||||
|
|||||||
+116
-26
@@ -1,43 +1,133 @@
|
|||||||
const defaultTheme = 'theme-default'
|
const defaultTheme = 'theme-default'
|
||||||
const defaultAccessibilityTheme = 'accessibility theme-accessibility-black-white'
|
|
||||||
|
|
||||||
const getTheme = () => localStorage.getItem('theme')
|
const themeScheme = {
|
||||||
|
accessibility: 'accessibility',
|
||||||
|
|
||||||
const isDefaultTheme = () => {
|
color: {
|
||||||
const theme = getTheme()
|
wb: 'theme-accessibility-white-black',
|
||||||
if (!theme) return true
|
bw: 'theme-accessibility-black-white',
|
||||||
return theme === defaultTheme
|
},
|
||||||
|
|
||||||
|
font: {
|
||||||
|
sans: 'theme-accessibility-font-sans',
|
||||||
|
serif: 'theme-accessibility-font-serif',
|
||||||
|
},
|
||||||
|
|
||||||
|
fontSize: {
|
||||||
|
normal: 'theme-accessibility-font-normal',
|
||||||
|
big: 'theme-accessibility-font-big',
|
||||||
|
biggest: 'theme-accessibility-font-biggest',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultAccessibilityTheme = () => ({
|
||||||
|
version: '1',
|
||||||
|
theme: {
|
||||||
|
accessibility: 'accessibility',
|
||||||
|
color: 'wb',
|
||||||
|
font: 'sans',
|
||||||
|
fontSize: 'big',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
function loadTheme() {
|
||||||
|
const theme = localStorage.getItem('theme')
|
||||||
|
try {
|
||||||
|
return JSON.parse(theme)
|
||||||
|
} catch (error) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveTheme(theme) {
|
||||||
|
if (!theme) return theme
|
||||||
|
const str = typeof theme === 'string'
|
||||||
|
? theme
|
||||||
|
: JSON.stringify(theme)
|
||||||
|
localStorage.setItem('theme', str)
|
||||||
|
return theme
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetTheme() {
|
||||||
|
localStorage.removeItem('theme')
|
||||||
|
}
|
||||||
|
|
||||||
|
function compileTheme(item) {
|
||||||
|
if (!item) return defaultTheme
|
||||||
|
const {theme, version} = item
|
||||||
|
if (!version) return defaultTheme
|
||||||
|
|
||||||
|
const cssClass = ([scope, name]) => {
|
||||||
|
try {
|
||||||
|
return themeScheme[scope][name]
|
||||||
|
} catch (error) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object
|
||||||
|
.entries(theme)
|
||||||
|
.map(cssClass)
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
class Theme {
|
class Theme {
|
||||||
constructor($rootScope) {
|
constructor($rootScope) {
|
||||||
this.mainTheme = defaultTheme
|
this.mainTheme = defaultTheme
|
||||||
this.currentTheme = defaultAccessibilityTheme
|
this.themeSetup = defaultAccessibilityTheme()
|
||||||
|
this.__scope = $rootScope
|
||||||
|
|
||||||
this.toggle = () => {
|
this.setTheme(loadTheme())
|
||||||
if (isDefaultTheme()) {
|
}
|
||||||
this.setTheme(this.currentTheme)
|
|
||||||
} else {
|
toggle() {
|
||||||
this.setMainTheme()
|
if (this.isDefaultTheme()) {
|
||||||
}
|
this.setTheme(this.themeSetup)
|
||||||
|
} else {
|
||||||
|
this.setMainTheme()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.setTheme = (themeName) => {
|
setMainTheme() {
|
||||||
if (!themeName) {
|
resetTheme()
|
||||||
this.setMainTheme()
|
this.__scope.theme = this.mainTheme
|
||||||
} else {
|
}
|
||||||
localStorage.setItem('theme', themeName)
|
|
||||||
this.currentTheme = themeName
|
setTheme(theme) {
|
||||||
$rootScope.theme = themeName
|
if (!theme) {
|
||||||
}
|
this.setMainTheme()
|
||||||
|
} else {
|
||||||
|
const css = compileTheme(theme)
|
||||||
|
|
||||||
|
saveTheme(theme)
|
||||||
|
this.themeSetup = theme
|
||||||
|
this.__scope.theme = css
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.setMainTheme = () => {
|
setColorTheme(theme) {
|
||||||
localStorage.removeItem('theme')
|
this.themeSetup.theme.color = theme
|
||||||
$rootScope.theme = this.mainTheme
|
this.install()
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setTheme(getTheme())
|
setFontTheme(theme) {
|
||||||
|
this.themeSetup.theme.fontSize = theme
|
||||||
|
this.install()
|
||||||
|
}
|
||||||
|
|
||||||
|
setFontFamilyTheme(theme) {
|
||||||
|
this.themeSetup.theme.font = theme
|
||||||
|
this.install()
|
||||||
|
}
|
||||||
|
|
||||||
|
install() {
|
||||||
|
this.setTheme(this.themeSetup)
|
||||||
|
}
|
||||||
|
|
||||||
|
isDefaultTheme(){
|
||||||
|
const theme = loadTheme()
|
||||||
|
return !theme // default theme cleared up local storage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
<div class="accessibility-control">
|
<div class="accessibility-control">
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<button ng-click="setColorTheme('theme-accessibility-black-white')"
|
<button ng-click="setColorTheme('bw')"
|
||||||
>Белым по черному
|
>Белым по черному
|
||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<button ng-click="setColorTheme('theme-accessibility-white-black')"
|
<button ng-click="setColorTheme('wb')"
|
||||||
>Черным по белому
|
>Черным по белому
|
||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
@@ -32,12 +32,12 @@
|
|||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<button ng-click="setFontTheme('theme-accessibility-font-big')"
|
<button ng-click="setFontTheme('big')"
|
||||||
>Большие
|
>Большие
|
||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<button ng-click="setFontTheme('theme-accessibility-font-biggest')"
|
<button ng-click="setFontTheme('biggest')"
|
||||||
>Огромные
|
>Огромные
|
||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
@@ -45,12 +45,12 @@
|
|||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<button ng-click="setFontFamilyTheme('theme-accessibility-font-sans')"
|
<button ng-click="setFontFamilyTheme('sans')"
|
||||||
>Без засечек
|
>Без засечек
|
||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<button ng-click="setFontFamilyTheme('theme-accessibility-font-serif')"
|
<button ng-click="setFontFamilyTheme('serif')"
|
||||||
>С засечками
|
>С засечками
|
||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
Reference in new issue
Block a user