From 7373af041766d24fc74c201009e700a423cbe99b Mon Sep 17 00:00:00 2001 From: Magnus Hovland Hoff Date: Fri, 5 Oct 2018 11:19:47 +0200 Subject: [PATCH] Select random theme for new articles client side --- assets/script.js | 16 ++++++++++++++++ src/resources/new_article_resource.rs | 6 ++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/assets/script.js b/assets/script.js index 7932394..aa9f0db 100644 --- a/assets/script.js +++ b/assets/script.js @@ -259,6 +259,22 @@ function openEditor() { } } +function initializeTheme() { + const form = document.getElementById('article-editor'); + + let preSelectedTheme = form.querySelector(`.theme-picker--option[checked]`); + if (preSelectedTheme) return; + + let themes = form.querySelectorAll(`.theme-picker--option`); + let randomThemeId = (Math.random() * themes.length) | 0; + + let theme = themes[randomThemeId]; + theme.defaultChecked = theme.checked = true; + document.querySelector("body").className = `theme-${theme.value}`; +} + +initializeTheme(); + document .getElementById("openEditor") .addEventListener("click", function (ev) { diff --git a/src/resources/new_article_resource.rs b/src/resources/new_article_resource.rs index 1e96923..40f2fb0 100644 --- a/src/resources/new_article_resource.rs +++ b/src/resources/new_article_resource.rs @@ -87,15 +87,13 @@ impl Resource for NewArticleResource { let title = self.slug.as_ref() .map_or("".to_owned(), |x| title_from_slug(x)); - let theme = theme::theme_from_str_hash(&title); - Box::new(self.head() .and_then(move |head| { Ok(head .with_body(Layout { base: None, // Hmm, should perhaps accept `base` as argument title: &title, - theme, + theme: theme::Theme::Gray, body: &Template { revision: NEW, last_updated: None, @@ -106,7 +104,7 @@ impl Resource for NewArticleResource { rendered: EMPTY_ARTICLE_MESSAGE, themes: &theme::THEMES.iter().map(|&x| SelectableTheme { theme: x, - selected: x == theme, + selected: false, }).collect::>(), }, }.to_string()))