From aab74546ed857352050cf049f2cdb4d42caa0497 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sun, 15 May 2022 20:53:19 +0200 Subject: [PATCH] =?UTF-8?q?Retoca=20la=20disposici=C3=B3n=20de=20los=20ele?= =?UTF-8?q?mentos=20de=20contexto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/core/component/context.rs | 166 +++++++++++++++++- pagetop/src/core/component/context/favicon.rs | 86 --------- .../src/core/component/context/javascript.rs | 43 ----- .../src/core/component/context/stylesheet.rs | 29 --- 4 files changed, 159 insertions(+), 165 deletions(-) delete mode 100644 pagetop/src/core/component/context/favicon.rs delete mode 100644 pagetop/src/core/component/context/javascript.rs delete mode 100644 pagetop/src/core/component/context/stylesheet.rs diff --git a/pagetop/src/core/component/context.rs b/pagetop/src/core/component/context.rs index a5c7a43a..9b788557 100644 --- a/pagetop/src/core/component/context.rs +++ b/pagetop/src/core/component/context.rs @@ -1,16 +1,168 @@ use crate::{Lazy, base, concat_string, util}; use crate::config::SETTINGS; -use crate::html::{Markup, html}; +use crate::html::{Markup, PreEscaped, html}; use crate::core::theme::*; -mod favicon; -pub use favicon::Favicon; +// Favicon. -mod javascript; -pub use javascript::{JavaScript, JSMode}; +pub struct Favicon(Vec); -mod stylesheet; -pub use stylesheet::StyleSheet; +impl Favicon { + pub fn new() -> Self { + Favicon(Vec::new()) + } + + pub fn with_icon(self, image: &str) -> Self { + self.add_item("icon", image, "", "") + } + + pub fn with_icon_for_sizes(self, image: &str, sizes: &str) -> Self { + self.add_item("icon", image, sizes, "") + } + + pub fn with_apple_touch_icon(self, image: &str, sizes: &str) -> Self { + self.add_item("apple-touch-icon", image, sizes, "") + } + + pub fn with_mask_icon(self, image: &str, color: &str) -> Self { + self.add_item("mask-icon", image, "", color) + } + + pub fn with_manifest(self, file: &str) -> Self { + self.add_item("manifest", file, "", "") + } + + pub fn with_theme_color(mut self, color: &str) -> Self { + self.0.push(format!( + "", color + )); + self + } + + pub fn with_ms_tile_color(mut self, color: &str) -> Self { + self.0.push(format!( + "", color + )); + self + } + + pub fn with_ms_tile_image(mut self, image: &str) -> Self { + self.0.push(format!( + "", image + )); + self + } + + fn add_item( + mut self, + rel : &str, + source: &str, + sizes : &str, + color : &str + ) -> Self { + let mut link: String = format!(" format!("{} type=\"image/gif\"", link), + ".ico" => format!("{} type=\"image/x-icon\"", link), + ".jpg" => format!("{} type=\"image/jpg\"", link), + ".png" => format!("{} type=\"image/png\"", link), + ".svg" => format!("{} type=\"image/svg+xml\"", link), + _ => link + }; + } + if !sizes.is_empty() { + link = format!("{} sizes=\"{}\"", link, sizes); + } + if !color.is_empty() { + link = format!("{} color=\"{}\"", link, color); + } + self.0.push(format!("{} href=\"{}\">", link, source)); + self + } + + fn render(&self) -> Markup { + html! { + @for item in &self.0 { + (PreEscaped(item)) + } + } + } +} + +// JavaScript. + +#[derive(PartialEq)] +pub enum JSMode { Async, Defer, Normal } + +pub struct JavaScript { + source: &'static str, + weight: isize, + mode : JSMode, +} +impl JavaScript { + pub fn source(s: &'static str) -> Self { + JavaScript { + source: s, + weight: 0, + mode : JSMode::Defer, + } + } + + pub fn with_weight(mut self, weight: isize) -> Self { + self.weight = weight; + self + } + + pub fn with_mode(mut self, mode: JSMode) -> Self { + self.mode = mode; + self + } + + pub fn weight(self) -> isize { + self.weight + } + + pub fn render(&self) -> Markup { + html! { + script type="text/javascript" + src=(self.source) + async[self.mode == JSMode::Async] + defer[self.mode == JSMode::Defer] + {}; + } + } +} + +// StyleSheet. + +pub struct StyleSheet { + source: &'static str, + weight: isize, +} +impl StyleSheet { + pub fn source(s: &'static str) -> Self { + StyleSheet { + source: s, + weight: 0, + } + } + + pub fn with_weight(mut self, weight: isize) -> Self { + self.weight = weight; + self + } + + pub fn weight(self) -> isize { + self.weight + } + + fn render(&self) -> Markup { + html! { + link rel="stylesheet" href=(self.source); + } + } +} static DEFAULT_THEME: Lazy<&dyn ThemeTrait> = Lazy::new(|| { match all::theme_by_single_name(&SETTINGS.app.theme) { diff --git a/pagetop/src/core/component/context/favicon.rs b/pagetop/src/core/component/context/favicon.rs deleted file mode 100644 index 1788540e..00000000 --- a/pagetop/src/core/component/context/favicon.rs +++ /dev/null @@ -1,86 +0,0 @@ -use crate::html::{Markup, PreEscaped, html}; - -pub struct Favicon(Vec); - -impl Favicon { - pub fn new() -> Self { - Favicon(Vec::new()) - } - - pub fn with_icon(self, image: &str) -> Self { - self.add_item("icon", image, "", "") - } - - pub fn with_icon_for_sizes(self, image: &str, sizes: &str) -> Self { - self.add_item("icon", image, sizes, "") - } - - pub fn with_apple_touch_icon(self, image: &str, sizes: &str) -> Self { - self.add_item("apple-touch-icon", image, sizes, "") - } - - pub fn with_mask_icon(self, image: &str, color: &str) -> Self { - self.add_item("mask-icon", image, "", color) - } - - pub fn with_manifest(self, file: &str) -> Self { - self.add_item("manifest", file, "", "") - } - - pub fn with_theme_color(mut self, color: &str) -> Self { - self.0.push(format!( - "", color - )); - self - } - - pub fn with_ms_tile_color(mut self, color: &str) -> Self { - self.0.push(format!( - "", color - )); - self - } - - pub fn with_ms_tile_image(mut self, image: &str) -> Self { - self.0.push(format!( - "", image - )); - self - } - - fn add_item( - mut self, - rel : &str, - source: &str, - sizes : &str, - color : &str - ) -> Self { - let mut link: String = format!(" format!("{} type=\"image/gif\"", link), - ".ico" => format!("{} type=\"image/x-icon\"", link), - ".jpg" => format!("{} type=\"image/jpg\"", link), - ".png" => format!("{} type=\"image/png\"", link), - ".svg" => format!("{} type=\"image/svg+xml\"", link), - _ => link - }; - } - if !sizes.is_empty() { - link = format!("{} sizes=\"{}\"", link, sizes); - } - if !color.is_empty() { - link = format!("{} color=\"{}\"", link, color); - } - self.0.push(format!("{} href=\"{}\">", link, source)); - self - } - - pub(super) fn render(&self) -> Markup { - html! { - @for item in &self.0 { - (PreEscaped(item)) - } - } - } -} diff --git a/pagetop/src/core/component/context/javascript.rs b/pagetop/src/core/component/context/javascript.rs deleted file mode 100644 index 2f2d725b..00000000 --- a/pagetop/src/core/component/context/javascript.rs +++ /dev/null @@ -1,43 +0,0 @@ -use crate::html::{Markup, html}; - -#[derive(PartialEq)] -pub enum JSMode { Async, Defer, Normal } - -pub struct JavaScript { - pub(super) source: &'static str, - pub(super) weight: isize, - pub(super) mode : JSMode, -} -impl JavaScript { - pub fn source(s: &'static str) -> Self { - JavaScript { - source: s, - weight: 0, - mode : JSMode::Defer, - } - } - - pub fn with_weight(mut self, weight: isize) -> Self { - self.weight = weight; - self - } - - pub fn with_mode(mut self, mode: JSMode) -> Self { - self.mode = mode; - self - } - - pub fn weight(self) -> isize { - self.weight - } - - pub(super) fn render(&self) -> Markup { - html! { - script type="text/javascript" - src=(self.source) - async[self.mode == JSMode::Async] - defer[self.mode == JSMode::Defer] - {}; - } - } -} diff --git a/pagetop/src/core/component/context/stylesheet.rs b/pagetop/src/core/component/context/stylesheet.rs deleted file mode 100644 index f7803938..00000000 --- a/pagetop/src/core/component/context/stylesheet.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::html::{Markup, html}; - -pub struct StyleSheet { - pub(super) source: &'static str, - pub(super) weight: isize, -} -impl StyleSheet { - pub fn source(s: &'static str) -> Self { - StyleSheet { - source: s, - weight: 0, - } - } - - pub fn with_weight(mut self, weight: isize) -> Self { - self.weight = weight; - self - } - - pub fn weight(self) -> isize { - self.weight - } - - pub(super) fn render(&self) -> Markup { - html! { - link rel="stylesheet" href=(self.source); - } - } -}