aboutsummaryrefslogtreecommitdiffstats
path: root/src/renderer.rs
diff options
context:
space:
mode:
authorBao Trinh <qubidt@gmail.com>2023-07-26 00:03:05 +0000
committerBao Trinh <qubidt@gmail.com>2023-07-26 00:52:18 +0000
commita2211fa4726336ab480cf0af7d32853edc40daa6 (patch)
treee1cac395d0925f948d670dfc641172ca4e781697 /src/renderer.rs
parentMerge remote-tracking branch 'origin/master' into css (diff)
downloadminiserve-a2211fa4726336ab480cf0af7d32853edc40daa6.tar.gz
miniserve-a2211fa4726336ab480cf0af7d32853edc40daa6.zip
Consolidate javascript and add comments
Diffstat (limited to '')
-rw-r--r--src/renderer.rs64
1 files changed, 29 insertions, 35 deletions
diff --git a/src/renderer.rs b/src/renderer.rs
index 946089d..699a01f 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -57,28 +57,6 @@ pub fn page(
body #drop-container
{
-
- (PreEscaped(r#"
- <script>
- // read theme from local storage and apply it to body
- const body = document.body;
- var theme = localStorage.getItem('theme');
- updateColorScheme(theme)
-
- // updates the color scheme by replacing the appropriate class
- // on body and saving the new theme to local storage
- function updateColorScheme(name) {
- if (name && name != "default") {
- body.setAttribute("data-theme", name)
- } else {
- body.removeAttribute("data-theme")
- }
-
- localStorage.setItem('theme', name);
- }
- </script>
- "#))
-
div.toolbar_box_group {
@if conf.file_upload {
div.form {
@@ -604,6 +582,33 @@ fn page_header(title: &str, file_upload: bool, favicon_route: &str, css_route: &
title { (title) }
+ (PreEscaped(r#"
+ <script>
+ // updates the color scheme by setting the theme data attribute
+ // on body and saving the new theme to local storage
+ function updateColorScheme(name) {
+ if (name && name != "default") {
+ localStorage.setItem('theme', name);
+ document.body.setAttribute("data-theme", name)
+ } else {
+ localStorage.removeItem('theme');
+ document.body.removeAttribute("data-theme")
+ }
+ }
+
+ // read theme from local storage and apply it to body
+ function loadColorScheme() {
+ var name = localStorage.getItem('theme');
+ updateColorScheme(name);
+ }
+
+ // load saved theme on page load
+ addEventListener("load", loadColorScheme);
+ // load saved theme when local storage is changed (synchronize between tabs)
+ addEventListener("storage", loadColorScheme);
+ </script>
+ "#))
+
@if file_upload {
(PreEscaped(r#"
<script>
@@ -672,19 +677,8 @@ pub fn render_error(
html {
(page_header(&error_code.to_string(), false, &conf.favicon_route, &conf.css_route))
- body.(format!("default_theme_{}", conf.default_color_scheme))
- .(format!("default_theme_dark_{}", conf.default_color_scheme_dark)) {
-
- (PreEscaped(r#"
- <script>
- // read theme from local storage and apply it to body
- var theme = localStorage.getItem('theme');
- if (theme && theme != "default") {
- body.setAttribute("data-theme", name)
- }
- </script>
- "#))
-
+ body
+ {
div.error {
p { (error_code.to_string()) }
@for error in error_description.lines() {