1
0
mirror of https://github.com/kognise/water.css.git synced 2025-02-23 13:23:54 +01:00
css-water.css/script.js

118 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-10-02 11:09:20 -07:00
/* global matchMedia, faviconModeSwitcher */
(function () {
2019-06-02 04:46:52 +02:00
const iconModeSwitcher = window.faviconModeSwitcher && faviconModeSwitcher.default
if (!iconModeSwitcher) return
iconModeSwitcher([
{
element: 'link[rel="shortcut icon"]',
2019-09-21 16:49:01 +04:00
href: { dark: 'icons/light-favicon.ico' }
2019-06-02 04:46:52 +02:00
},
{
element: 'link[rel="icon"][sizes="16x16"]',
2019-09-21 16:49:01 +04:00
href: { dark: 'icons/light-favicon-16x16.png' }
2019-06-02 04:46:52 +02:00
},
{
element: 'link[rel="icon"][sizes="32x32"]',
2019-09-21 16:49:01 +04:00
href: { dark: 'icons/light-favicon-32x32.png' }
2019-06-02 04:46:52 +02:00
}
])
})()
2019-10-02 11:09:20 -07:00
2019-06-02 04:46:52 +02:00
;(function (ThemeSwitcher) {
2019-10-02 11:09:20 -07:00
const themeSwitcher = new ThemeSwitcher('stylesheet')
const themeSwitchBtn = document.getElementById('switch')
2019-05-29 15:40:45 +09:00
const themes = {
2019-05-29 22:33:08 +09:00
dark: 'dark',
darkStandalone: 'dark.standalone',
light: 'light',
lightStandalone: 'light.standalone'
2019-10-02 11:09:20 -07:00
}
2019-05-29 22:45:30 +09:00
const getSwitchThemeName = function () {
2019-05-29 22:33:08 +09:00
// Case: switch to "light.standalone.css"
if (
2019-10-02 11:09:20 -07:00
((themeSwitcher.current === themes.dark) && themeSwitcher.isDark) ||
((themeSwitcher.current === themes.light) && themeSwitcher.isDark) ||
2019-05-29 22:33:08 +09:00
themeSwitcher.current === themes.darkStandalone
) {
return themes.lightStandalone
2019-05-29 15:40:45 +09:00
2019-05-29 22:33:08 +09:00
// Case: switch to "dark.standalone.css"
} else if (
2019-10-02 11:09:20 -07:00
((themeSwitcher.current === themes.dark) && themeSwitcher.isLight) ||
((themeSwitcher.current === themes.light) && themeSwitcher.isLight) ||
2019-05-29 22:33:08 +09:00
themeSwitcher.current === themes.lightStandalone
) {
2019-10-02 11:09:20 -07:00
return themes.darkStandalone
2019-05-29 15:40:45 +09:00
2019-05-29 22:33:08 +09:00
// Case: switch to "light.css"
} else if (themeSwitcher.current === themes.dark) {
2019-10-02 11:09:20 -07:00
return themes.light
2019-05-29 22:33:08 +09:00
// Case: switch to "dark.css"
} else if (themeSwitcher.current === themes.light) {
2019-10-02 11:09:20 -07:00
return themes.dark
2019-05-29 22:33:08 +09:00
// Case: switch destination is unknown
2019-05-29 15:40:45 +09:00
} else {
2019-10-02 11:09:20 -07:00
return themeSwitcher.current
2019-05-29 15:40:45 +09:00
}
2019-10-02 11:09:20 -07:00
}
2019-05-29 22:45:30 +09:00
const getGeneralThemeName = function () {
2019-10-02 11:09:20 -07:00
return themeSwitcher.current.replace(/\.standalone/g, '')
}
2019-05-29 22:33:08 +09:00
2019-10-02 11:09:20 -07:00
themeSwitchBtn.addEventListener('click', function () {
themeSwitcher.switch(getSwitchThemeName())
})
2019-05-29 22:45:30 +09:00
themeSwitcher.onChangeDark = function () {
2019-10-02 11:09:20 -07:00
themeSwitcher.switch(getGeneralThemeName())
}
2019-05-29 22:45:30 +09:00
themeSwitcher.onChangeLight = function () {
2019-10-02 11:09:20 -07:00
themeSwitcher.switch(getGeneralThemeName())
}
2019-05-29 22:33:08 +09:00
})(
(function () {
2019-10-02 11:09:20 -07:00
const ThemeSwitcher = function (stylesheet) {
const darkSchemeMql = matchMedia('(prefers-color-scheme: dark)')
const lightSchemeMql = matchMedia('(prefers-color-scheme: light)')
const that = this
2019-05-29 22:33:08 +09:00
2019-10-02 11:09:20 -07:00
this.themeDir = 'dist/'
this.stylesheet = document.getElementById(stylesheet)
this.current = this.getThemeName(this.stylesheet.href)
this.isDark = darkSchemeMql.matches
this.isLight = lightSchemeMql.matches
2019-05-29 22:45:30 +09:00
darkSchemeMql.addListener(function (mql) {
if (mql.matches && typeof that.onChangeDark === 'function') {
that.onChangeDark()
}
2019-10-02 11:09:20 -07:00
})
2019-05-29 22:45:30 +09:00
lightSchemeMql.addListener(function (mql) {
if (mql.matches && typeof that.onChangeLight === 'function') {
that.onChangeLight()
}
2019-10-02 11:09:20 -07:00
})
}
2019-05-29 22:33:08 +09:00
ThemeSwitcher.prototype = {
switch: function (themeName) {
2019-10-02 11:09:20 -07:00
this.stylesheet.href = this.themeDir + themeName + '.css'
this.current = themeName
2019-05-29 22:33:08 +09:00
},
getThemeName: function () {
2019-10-02 11:09:20 -07:00
const reg = new RegExp(this.themeDir + '(|.+?).css')
return this.stylesheet.getAttribute('href').replace(reg, '$1')
2019-05-29 22:45:30 +09:00
},
onChangeDark: null,
onChangeLight: null
2019-10-02 11:09:20 -07:00
}
2019-05-29 22:33:08 +09:00
2019-10-02 11:09:20 -07:00
return ThemeSwitcher
2019-05-29 22:33:08 +09:00
})()
2019-10-02 11:09:20 -07:00
)