Select random theme for new articles client side

This commit is contained in:
Magnus Hovland Hoff 2018-10-05 11:19:47 +02:00
parent 65ad262bd8
commit 7373af0417
2 changed files with 18 additions and 4 deletions

View file

@ -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) {

View file

@ -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::<Vec<_>>(),
},
}.to_string()))