1
0

aThemeSwitch.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. {{define "component/themeSwitchTemplate"}}
  2. <template>
  3. <a-menu :theme="themeSwitcher.currentTheme" mode="inline" selected-keys="">
  4. <a-sub-menu>
  5. <span slot="title">
  6. <a-icon type="bulb" :theme="themeSwitcher.isDarkTheme ? 'filled' : 'outlined'"></a-icon>
  7. <span>{{ i18n "menu.theme" }}</span>
  8. </span>
  9. <a-menu-item id="change-theme" class="ant-menu-theme-switch" @mousedown="themeSwitcher.animationsOff()">
  10. <span>{{ i18n "menu.dark" }}</span>
  11. <a-switch :style="{ marginLeft: '2px' }" size="small" :default-checked="themeSwitcher.isDarkTheme"
  12. @change="themeSwitcher.toggleTheme()"></a-switch>
  13. </a-menu-item>
  14. <a-menu-item id="change-theme-ultra" v-if="themeSwitcher.isDarkTheme" class="ant-menu-theme-switch"
  15. @mousedown="themeSwitcher.animationsOffUltra()">
  16. <span>{{ i18n "menu.ultraDark" }}</span>
  17. <a-checkbox :style="{ marginLeft: '2px' }" :checked="themeSwitcher.isUltra"
  18. @click="themeSwitcher.toggleUltra()"></a-checkbox>
  19. </a-menu-item>
  20. </a-sub-menu>
  21. </a-menu>
  22. </template>
  23. {{end}}
  24. {{define "component/themeSwitchTemplateLogin"}}
  25. <template>
  26. <a-space @mousedown="themeSwitcher.animationsOff()" id="change-theme" direction="vertical" :size="10"
  27. :style="{ width: '100%' }">
  28. <a-space direction="horizontal" size="small">
  29. <a-switch size="small" :default-checked="themeSwitcher.isDarkTheme"
  30. @change="themeSwitcher.toggleTheme()"></a-switch>
  31. <span>{{ i18n "menu.dark" }}</span>
  32. </a-space>
  33. <a-space v-if="themeSwitcher.isDarkTheme" direction="horizontal" size="small">
  34. <a-checkbox :checked="themeSwitcher.isUltra" @click="themeSwitcher.toggleUltra()"></a-checkbox>
  35. <span>{{ i18n "menu.ultraDark" }}</span>
  36. </a-space>
  37. </a-space>
  38. </template>
  39. {{end}}
  40. {{define "component/aThemeSwitch"}}
  41. <script>
  42. function createThemeSwitcher() {
  43. const darkModePreference = localStorage.getItem('dark-mode');
  44. const isDarkTheme = darkModePreference === null ? true : darkModePreference === 'true';
  45. const isUltra = localStorage.getItem('isUltraDarkThemeEnabled') === 'true';
  46. if (isUltra) {
  47. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  48. }
  49. const theme = isDarkTheme ? 'dark' : 'light';
  50. document.querySelector('body').setAttribute('class', theme);
  51. return {
  52. animationsOff() {
  53. document.documentElement.setAttribute('data-theme-animations', 'off');
  54. const themeAnimations = document.querySelector('#change-theme');
  55. themeAnimations.addEventListener('mouseleave', () => {
  56. document.documentElement.removeAttribute('data-theme-animations');
  57. });
  58. themeAnimations.addEventListener('touchend', () => {
  59. document.documentElement.removeAttribute('data-theme-animations');
  60. });
  61. },
  62. animationsOffUltra() {
  63. document.documentElement.setAttribute('data-theme-animations', 'off');
  64. const themeAnimationsUltra = document.querySelector('#change-theme-ultra');
  65. themeAnimationsUltra.addEventListener('mouseleave', () => {
  66. document.documentElement.removeAttribute('data-theme-animations');
  67. });
  68. themeAnimationsUltra.addEventListener('touchend', () => {
  69. document.documentElement.removeAttribute('data-theme-animations');
  70. });
  71. },
  72. isDarkTheme,
  73. isUltra,
  74. get currentTheme() {
  75. return this.isDarkTheme ? 'dark' : 'light';
  76. },
  77. toggleTheme() {
  78. this.isDarkTheme = !this.isDarkTheme;
  79. localStorage.setItem('dark-mode', this.isDarkTheme);
  80. document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light');
  81. document.getElementById('message').className = themeSwitcher.currentTheme;
  82. },
  83. toggleUltra() {
  84. this.isUltra = !this.isUltra;
  85. if (this.isUltra) {
  86. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  87. } else {
  88. document.documentElement.removeAttribute('data-theme');
  89. }
  90. localStorage.setItem('isUltraDarkThemeEnabled', this.isUltra.toString());
  91. }
  92. };
  93. }
  94. const themeSwitcher = createThemeSwitcher();
  95. Vue.component('a-theme-switch', {
  96. template: `{{template "component/themeSwitchTemplate"}}`,
  97. data: () => ({
  98. themeSwitcher
  99. }),
  100. mounted() {
  101. this.$message.config({
  102. getContainer: () => document.getElementById('message')
  103. });
  104. document.getElementById('message').className = themeSwitcher.currentTheme;
  105. }
  106. });
  107. Vue.component('a-theme-switch-login', {
  108. template: `{{template "component/themeSwitchTemplateLogin"}}`,
  109. data: () => ({
  110. themeSwitcher
  111. }),
  112. mounted() {
  113. this.$message.config({
  114. getContainer: () => document.getElementById('message')
  115. });
  116. document.getElementById('message').className = themeSwitcher.currentTheme;
  117. }
  118. });
  119. </script>
  120. {{end}}