Files
trihuy-russian/web/html/component/aThemeSwitch.html
T

122 lines
4.8 KiB
HTML
Raw Normal View History

2023-05-08 18:15:08 +04:30
{{define "component/themeSwitchTemplate"}}
<template>
2024-04-20 22:15:36 +03:30
<a-menu :theme="themeSwitcher.currentTheme" mode="inline" selected-keys="">
<a-sub-menu>
<span slot="title">
<a-icon type="bulb" :theme="themeSwitcher.isDarkTheme ? 'filled' : 'outlined'"></a-icon>
2025-03-17 18:26:07 +07:00
<span>{{ i18n "menu.theme" }}</span>
2024-04-20 22:15:36 +03:30
</span>
2025-04-01 19:24:03 +07:00
<a-menu-item id="change-theme" class="ant-menu-theme-switch" @mousedown="themeSwitcher.animationsOff()">
2025-03-17 18:26:07 +07:00
<span>{{ i18n "menu.dark" }}</span>
2025-04-06 16:40:33 +07:00
<a-switch :style="{ marginLeft: '2px' }" size="small" :default-checked="themeSwitcher.isDarkTheme"
2025-04-01 19:24:03 +07:00
@change="themeSwitcher.toggleTheme()"></a-switch>
2024-04-20 22:15:36 +03:30
</a-menu-item>
2025-04-01 19:24:03 +07:00
<a-menu-item id="change-theme-ultra" v-if="themeSwitcher.isDarkTheme" class="ant-menu-theme-switch"
@mousedown="themeSwitcher.animationsOffUltra()">
2025-03-17 18:26:07 +07:00
<span>{{ i18n "menu.ultraDark" }}</span>
2025-04-06 16:40:33 +07:00
<a-checkbox :style="{ marginLeft: '2px' }" :checked="themeSwitcher.isUltra"
2025-04-01 19:24:03 +07:00
@click="themeSwitcher.toggleUltra()"></a-checkbox>
2024-04-20 22:15:36 +03:30
</a-menu-item>
</a-sub-menu>
</a-menu>
</template>
{{end}}
{{define "component/themeSwitchTemplateLogin"}}
<template>
2026-05-04 13:20:24 +02:00
<a-space @mousedown="themeSwitcher.animationsOff()" id="change-theme" direction="vertical" :size="10"
:style="{ width: '100%' }">
2025-04-01 19:24:03 +07:00
<a-space direction="horizontal" size="small">
2026-05-04 13:20:24 +02:00
<a-switch size="small" :default-checked="themeSwitcher.isDarkTheme"
@change="themeSwitcher.toggleTheme()"></a-switch>
2025-04-01 19:24:03 +07:00
<span>{{ i18n "menu.dark" }}</span>
</a-space>
<a-space v-if="themeSwitcher.isDarkTheme" direction="horizontal" size="small">
<a-checkbox :checked="themeSwitcher.isUltra" @click="themeSwitcher.toggleUltra()"></a-checkbox>
<span>{{ i18n "menu.ultraDark" }}</span>
</a-space>
</a-space>
2023-05-08 18:15:08 +04:30
</template>
{{end}}
2025-03-17 18:26:07 +07:00
{{define "component/aThemeSwitch"}}
2023-05-08 18:15:08 +04:30
<script>
function createThemeSwitcher() {
2026-04-26 20:16:27 +02:00
const darkModePreference = localStorage.getItem('dark-mode');
const isDarkTheme = darkModePreference === null ? true : darkModePreference === 'true';
2024-02-28 14:35:01 +03:30
const isUltra = localStorage.getItem('isUltraDarkThemeEnabled') === 'true';
if (isUltra) {
document.documentElement.setAttribute('data-theme', 'ultra-dark');
}
2023-05-08 18:15:08 +04:30
const theme = isDarkTheme ? 'dark' : 'light';
2024-02-28 14:35:01 +03:30
document.querySelector('body').setAttribute('class', theme);
2023-05-08 18:15:08 +04:30
return {
2024-04-20 22:15:36 +03:30
animationsOff() {
document.documentElement.setAttribute('data-theme-animations', 'off');
const themeAnimations = document.querySelector('#change-theme');
themeAnimations.addEventListener('mouseleave', () => {
document.documentElement.removeAttribute('data-theme-animations');
});
themeAnimations.addEventListener('touchend', () => {
document.documentElement.removeAttribute('data-theme-animations');
});
},
animationsOffUltra() {
document.documentElement.setAttribute('data-theme-animations', 'off');
const themeAnimationsUltra = document.querySelector('#change-theme-ultra');
themeAnimationsUltra.addEventListener('mouseleave', () => {
document.documentElement.removeAttribute('data-theme-animations');
});
themeAnimationsUltra.addEventListener('touchend', () => {
document.documentElement.removeAttribute('data-theme-animations');
});
},
2023-05-08 18:15:08 +04:30
isDarkTheme,
2024-02-28 14:35:01 +03:30
isUltra,
2023-05-08 18:15:08 +04:30
get currentTheme() {
return this.isDarkTheme ? 'dark' : 'light';
},
toggleTheme() {
this.isDarkTheme = !this.isDarkTheme;
localStorage.setItem('dark-mode', this.isDarkTheme);
2024-02-28 14:35:01 +03:30
document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light');
2024-02-21 15:32:18 +03:30
document.getElementById('message').className = themeSwitcher.currentTheme;
2023-05-08 18:15:08 +04:30
},
2024-02-28 14:35:01 +03:30
toggleUltra() {
this.isUltra = !this.isUltra;
if (this.isUltra) {
document.documentElement.setAttribute('data-theme', 'ultra-dark');
} else {
document.documentElement.removeAttribute('data-theme');
}
localStorage.setItem('isUltraDarkThemeEnabled', this.isUltra.toString());
}
2023-05-08 18:15:08 +04:30
};
}
const themeSwitcher = createThemeSwitcher();
2025-03-08 22:41:27 +07:00
Vue.component('a-theme-switch', {
template: `{{template "component/themeSwitchTemplate" .}}`,
2024-02-28 14:35:01 +03:30
data: () => ({
themeSwitcher
}),
2024-02-21 15:32:18 +03:30
mounted() {
2024-02-28 14:35:01 +03:30
this.$message.config({
getContainer: () => document.getElementById('message')
});
2024-02-21 15:32:18 +03:30
document.getElementById('message').className = themeSwitcher.currentTheme;
2024-04-20 22:15:36 +03:30
}
});
2025-03-08 22:41:27 +07:00
Vue.component('a-theme-switch-login', {
template: `{{template "component/themeSwitchTemplateLogin" .}}`,
2024-04-20 22:15:36 +03:30
data: () => ({
themeSwitcher
}),
mounted() {
this.$message.config({
getContainer: () => document.getElementById('message')
2024-04-01 10:38:22 +03:30
});
2024-04-20 22:15:36 +03:30
document.getElementById('message').className = themeSwitcher.currentTheme;
2024-02-21 15:32:18 +03:30
}
2023-05-08 18:15:08 +04:30
});
</script>
2025-03-08 22:41:27 +07:00
{{end}}