web/_includes/mode-toggle.html

152 lines
3.5 KiB
HTML
Raw Normal View History

<!--
Switch the mode between dark and light.
-->
2020-12-04 20:40:01 +03:00
<i class="mode-toggle fas fa-adjust"></i>
<script type="text/javascript">
class ModeToggle {
static get MODE_KEY() { return "mode"; }
static get DARK_MODE() { return "dark"; }
static get LIGHT_MODE() { return "light"; }
constructor() {
2020-12-10 21:20:18 +03:00
if (this.hasMode) {
if (this.isDarkMode) {
2020-04-01 19:30:00 +03:00
if (!this.isSysDarkPrefer) {
2020-02-14 18:36:38 +03:00
this.setDark();
}
} else {
2020-04-01 19:30:00 +03:00
if (this.isSysDarkPrefer) {
2020-02-14 18:36:38 +03:00
this.setLight();
}
}
}
var self = this;
2020-02-14 18:36:38 +03:00
/* always follow the system prefers */
this.sysDarkPrefers.addListener(function() {
2020-12-09 21:42:46 +03:00
if (self.hasMode) {
if (self.isDarkMode) {
2020-04-01 19:30:00 +03:00
if (!self.isSysDarkPrefer) {
2020-02-14 18:36:38 +03:00
self.setDark();
}
} else {
2020-04-01 19:30:00 +03:00
if (self.isSysDarkPrefer) {
2020-02-14 18:36:38 +03:00
self.setLight();
}
}
self.clearMode();
}
2020-12-09 21:42:46 +03:00
self.updateMermaid();
});
} /* constructor() */
2020-02-14 18:36:38 +03:00
setDark() {
$('html').attr(ModeToggle.MODE_KEY, ModeToggle.DARK_MODE);
sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.DARK_MODE);
}
2020-02-14 18:36:38 +03:00
setLight() {
$('html').attr(ModeToggle.MODE_KEY, ModeToggle.LIGHT_MODE);
sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.LIGHT_MODE);
}
2020-02-14 18:36:38 +03:00
clearMode() {
$('html').removeAttr(ModeToggle.MODE_KEY);
sessionStorage.removeItem(ModeToggle.MODE_KEY);
}
2020-04-01 19:30:00 +03:00
get sysDarkPrefers() { return window.matchMedia("(prefers-color-scheme: dark)"); }
2020-04-01 19:30:00 +03:00
get isSysDarkPrefer() { return this.sysDarkPrefers.matches; }
2020-04-01 19:30:00 +03:00
get isDarkMode() { return this.mode == ModeToggle.DARK_MODE; }
2020-10-23 10:42:43 +03:00
get isLightMode() { return this.mode == ModeToggle.LIGHT_MODE; }
2020-04-01 19:30:00 +03:00
get hasMode() { return this.mode != null; }
get mode() { return sessionStorage.getItem(ModeToggle.MODE_KEY); }
2020-12-09 21:42:46 +03:00
/* get the current mode on screen */
get modeStatus() {
if (this.isDarkMode
|| (!this.hasMode && this.isSysDarkPrefer) ) {
return ModeToggle.DARK_MODE;
} else {
return ModeToggle.LIGHT_MODE;
}
}
updateMermaid() {
2020-12-10 21:20:18 +03:00
if (typeof mermaid !== "undefined") {
2020-12-09 21:42:46 +03:00
let expectedTheme = (this.modeStatus === ModeToggle.DARK_MODE? "dark" : "default");
let config = { theme: expectedTheme };
/* re-render the SVG <https://github.com/mermaid-js/mermaid/issues/311#issuecomment-332557344> */
$(".mermaid").each(function() {
let svgCode = $(this).prev().children().html();
$(this).removeAttr("data-processed");
$(this).html(svgCode);
});
mermaid.initialize(config);
mermaid.init(undefined, ".mermaid");
}
}
flipMode() {
2020-04-01 19:30:00 +03:00
if (this.hasMode) {
if (this.isSysDarkPrefer) {
2020-10-23 10:42:43 +03:00
if (this.isLightMode) {
2020-04-01 19:30:00 +03:00
this.clearMode();
} else {
this.setLight();
}
2020-02-14 18:36:38 +03:00
2020-04-01 19:30:00 +03:00
} else {
if (this.isDarkMode) {
this.clearMode();
} else {
this.setDark();
}
}
} else {
if (this.isSysDarkPrefer) {
this.setLight();
} else {
this.setDark();
}
}
2020-04-01 19:30:00 +03:00
2020-12-09 21:42:46 +03:00
this.updateMermaid();
2020-04-01 19:30:00 +03:00
} /* flipMode() */
} /* ModeToggle */
let toggle = new ModeToggle();
2020-02-14 18:36:38 +03:00
$(".mode-toggle").click(function() {
2020-12-09 21:42:46 +03:00
toggle.flipMode();
2020-12-09 21:42:46 +03:00
});
$("#mode-toggle-wrapper").keyup(function(e){
if(e.keyCode == 13) {
toggle.flipMode();
}
});
2020-10-23 10:42:43 +03:00
</script>