From f5314e9ca4caa126db5729521fac99c99ca51750 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Mon, 16 May 2022 20:44:17 +0200 Subject: [PATCH] =?UTF-8?q?Modifica=20la=20localizaci=C3=B3n=20de=20activo?= =?UTF-8?q?s=20en=20el=20contexto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/base/component/form/input.rs | 20 ++- pagetop/src/base/theme/aliner.rs | 7 +- pagetop/src/base/theme/bootsier.rs | 7 +- pagetop/src/base/theme/bulmix.rs | 7 +- pagetop/src/core/component.rs | 5 - pagetop/src/core/component/context.rs | 204 ++--------------------- pagetop/src/core/theme/definition.rs | 11 +- pagetop/src/html.rs | 10 ++ pagetop/src/html/assets.rs | 41 +++++ pagetop/src/html/assets/javascript.rs | 51 ++++++ pagetop/src/html/assets/stylesheet.rs | 37 ++++ pagetop/src/html/favicon.rs | 86 ++++++++++ 12 files changed, 269 insertions(+), 217 deletions(-) create mode 100644 pagetop/src/html/assets.rs create mode 100644 pagetop/src/html/assets/javascript.rs create mode 100644 pagetop/src/html/assets/stylesheet.rs create mode 100644 pagetop/src/html/favicon.rs diff --git a/pagetop/src/base/component/form/input.rs b/pagetop/src/base/component/form/input.rs index 18a980e3..0c615de2 100644 --- a/pagetop/src/base/component/form/input.rs +++ b/pagetop/src/base/component/form/input.rs @@ -47,6 +47,7 @@ impl ComponentTrait for Input { classes : Classes::new_with_default("form-item"), template : "default".to_owned(), } + .with_classes("form-type-textfield", ClassesOp::AddFirst) } fn handler(&self) -> &'static str { @@ -126,31 +127,36 @@ impl Input { } pub fn password() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-password", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Password; input } pub fn search() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-search", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Search; input } pub fn email() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-email", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Email; input } pub fn telephone() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-telephone", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Telephone; input } pub fn url() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-url", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Url; input } @@ -256,6 +262,10 @@ impl Input { pub fn alter_name(&mut self, name: &str) -> &mut Self { self.name.with_value(name); + self.alter_classes( + concat_string!("form-item form-item-", name).as_str(), + ClassesOp::SetDefault + ); self } diff --git a/pagetop/src/base/theme/aliner.rs b/pagetop/src/base/theme/aliner.rs index e4180303..02ed19d3 100644 --- a/pagetop/src/base/theme/aliner.rs +++ b/pagetop/src/base/theme/aliner.rs @@ -17,10 +17,9 @@ impl ThemeTrait for Aliner { fn before_render_page(&self, page: &mut Page) { page.context() - .with_favicon( - Favicon::new() - .with_icon("/theme/favicon.png") - ) + .with_favicon(Some(Favicon::new() + .with_icon("/theme/favicon.png") + )) .add_stylesheet( StyleSheet::with_source( "/aliner/css/styles.css" diff --git a/pagetop/src/base/theme/bootsier.rs b/pagetop/src/base/theme/bootsier.rs index ec294457..8512234f 100644 --- a/pagetop/src/base/theme/bootsier.rs +++ b/pagetop/src/base/theme/bootsier.rs @@ -19,10 +19,9 @@ impl ThemeTrait for Bootsier { fn before_render_page(&self, page: &mut Page) { page.context() - .with_favicon( - Favicon::new() - .with_icon("/theme/favicon.png") - ) + .with_favicon(Some(Favicon::new() + .with_icon("/theme/favicon.png") + )) .add_stylesheet( StyleSheet::with_source( "/bootsier/css/bootstrap.min.css?ver=5.1.3" diff --git a/pagetop/src/base/theme/bulmix.rs b/pagetop/src/base/theme/bulmix.rs index 9dfde724..a1b98275 100644 --- a/pagetop/src/base/theme/bulmix.rs +++ b/pagetop/src/base/theme/bulmix.rs @@ -17,10 +17,9 @@ impl ThemeTrait for Bulmix { fn before_render_page(&self, page: &mut Page) { page.context() - .with_favicon( - Favicon::new() - .with_icon("/theme/favicon.png") - ) + .with_favicon(Some(Favicon::new() + .with_icon("/theme/favicon.png") + )) .add_stylesheet( StyleSheet::with_source( "/bulmix/css/bulma.min.css?ver=0.9.3" diff --git a/pagetop/src/core/component.rs b/pagetop/src/core/component.rs index 038ee93d..ef3e33d5 100644 --- a/pagetop/src/core/component.rs +++ b/pagetop/src/core/component.rs @@ -6,11 +6,6 @@ pub use hook::{ mod context; pub use context::InContext; -pub use context::{ - Favicon, - JavaScript, JSMode, - StyleSheet, -}; mod definition; pub use definition::{ diff --git a/pagetop/src/core/component/context.rs b/pagetop/src/core/component/context.rs index 6fcf7be8..916d0d96 100644 --- a/pagetop/src/core/component/context.rs +++ b/pagetop/src/core/component/context.rs @@ -1,163 +1,11 @@ use crate::{Lazy, base, concat_string, util}; use crate::config::SETTINGS; -use crate::html::{Markup, PreEscaped, html}; -use crate::core::theme::*; - -// Favicon. - -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 - } - - 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 with_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 - } - - 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 with_source(s: &'static str) -> Self { - StyleSheet { - source: s, - weight: 0, - } - } - - pub fn with_weight(mut self, weight: isize) -> Self { - self.weight = weight; - self - } - - fn render(&self) -> Markup { - html! { - link rel="stylesheet" href=(self.source); - } - } -} +use crate::html::*; +use crate::core::theme::ThemeTrait; +use crate::core::theme::all::theme_by_single_name; static DEFAULT_THEME: Lazy<&dyn ThemeTrait> = Lazy::new(|| { - match all::theme_by_single_name(&SETTINGS.app.theme) { + match theme_by_single_name(&SETTINGS.app.theme) { Some(theme) => theme, None => &base::theme::bootsier::Bootsier, } @@ -167,8 +15,8 @@ pub struct InContext { theme : &'static dyn ThemeTrait, favicon : Option, metadata : Vec<(String, String)>, - stylesheets: Vec, - javascripts: Vec, + stylesheets: Assets, + javascripts: Assets, with_jquery: bool, id_counter : usize, } @@ -179,20 +27,20 @@ impl InContext { theme : *DEFAULT_THEME, favicon : None, metadata : Vec::new(), - stylesheets: Vec::new(), - javascripts: Vec::new(), + stylesheets: Assets::::new(), + javascripts: Assets::::new(), with_jquery: false, id_counter : 0, } } pub fn using_theme(&mut self, theme_name: &str) -> &mut Self { - self.theme = all::theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME); + self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME); self } - pub fn with_favicon(&mut self, favicon: Favicon) -> &mut Self { - self.favicon = Some(favicon); + pub fn with_favicon(&mut self, favicon: Option) -> &mut Self { + self.favicon = favicon; self } @@ -202,24 +50,12 @@ impl InContext { } pub fn add_stylesheet(&mut self, css: StyleSheet) -> &mut Self { - match self.stylesheets.iter().position(|x| x.source == css.source) { - Some(index) => if self.stylesheets[index].weight > css.weight { - self.stylesheets.remove(index); - self.stylesheets.push(css); - }, - _ => self.stylesheets.push(css) - } + self.stylesheets.add(css); self } pub fn add_javascript(&mut self, js: JavaScript) -> &mut Self { - match self.javascripts.iter().position(|x| x.source == js.source) { - Some(index) => if self.javascripts[index].weight > js.weight { - self.javascripts.remove(index); - self.javascripts.push(js); - }, - _ => self.javascripts.push(js) - } + self.javascripts.add(js); self } @@ -246,12 +82,6 @@ impl InContext { /// InContext RENDER. pub fn render(&mut self) -> Markup { - let ordered_css = &mut self.stylesheets; - ordered_css.sort_by_key(|css| css.weight); - - let ordered_js = &mut self.javascripts; - ordered_js.sort_by_key(|js| js.weight); - html! { @match &self.favicon { Some(favicon) => (favicon.render()), @@ -260,12 +90,8 @@ impl InContext { @for (name, content) in &self.metadata { meta name=(name) content=(content) {} } - @for css in ordered_css { - (css.render()) - } - @for js in ordered_js { - (js.render()) - } + (self.stylesheets.render()) + (self.javascripts.render()) } } diff --git a/pagetop/src/core/theme/definition.rs b/pagetop/src/core/theme/definition.rs index 951c7a50..520eadc4 100644 --- a/pagetop/src/core/theme/definition.rs +++ b/pagetop/src/core/theme/definition.rs @@ -1,7 +1,7 @@ use crate::{app, concat_string, util}; use crate::config::SETTINGS; -use crate::html::{Markup, html}; -use crate::core::component::{ComponentTrait, Favicon, InContext}; +use crate::html::{Favicon, Markup, html}; +use crate::core::component::{ComponentTrait, InContext}; use crate::response::page::Page; use crate::base::component::Chunck; @@ -28,10 +28,9 @@ pub trait ThemeTrait: BaseTheme + Send + Sync { #[allow(unused_variables)] fn before_render_page(&self, page: &mut Page) { page.context() - .with_favicon( - Favicon::new() - .with_icon("/theme/favicon.png") - ); + .with_favicon(Some(Favicon::new() + .with_icon("/theme/favicon.png") + )); } fn render_page_head(&self, page: &mut Page) -> Markup { diff --git a/pagetop/src/html.rs b/pagetop/src/html.rs index 56e5c6d2..0afcdadd 100644 --- a/pagetop/src/html.rs +++ b/pagetop/src/html.rs @@ -1,8 +1,18 @@ pub use maud::{DOCTYPE, Markup, PreEscaped, html}; +mod assets; +pub use assets::Assets; +pub use assets::javascript::{JavaScript, JSMode}; +pub use assets::stylesheet::StyleSheet; + +mod favicon; +pub use favicon::Favicon; + mod optiden; pub use optiden::OptIden; + mod optattr; pub use optattr::OptAttr; + mod classes; pub use classes::{Classes, ClassesOp}; diff --git a/pagetop/src/html/assets.rs b/pagetop/src/html/assets.rs new file mode 100644 index 00000000..921d6816 --- /dev/null +++ b/pagetop/src/html/assets.rs @@ -0,0 +1,41 @@ +pub mod javascript; +pub mod stylesheet; + +use crate::html::{Markup, html}; + +pub trait AssetsTrait { + fn source(&self) -> &'static str; + + fn weight(&self) -> isize; + + fn render(&self) -> Markup; +} + +pub struct Assets(Vec); + +impl Assets { + pub fn new() -> Self { + Assets::(Vec::::new()) + } + + pub fn add(&mut self, assets: T) -> &mut Self { + match self.0.iter().position(|x| x.source() == assets.source()) { + Some(index) => if self.0[index].weight() > assets.weight() { + self.0.remove(index); + self.0.push(assets); + }, + _ => self.0.push(assets) + } + self + } + + pub fn render(&mut self) -> Markup { + let assets = &mut self.0; + assets.sort_by_key(|a| a.weight()); + html! { + @for a in assets { + (a.render()) + } + } + } +} \ No newline at end of file diff --git a/pagetop/src/html/assets/javascript.rs b/pagetop/src/html/assets/javascript.rs new file mode 100644 index 00000000..40cdfb07 --- /dev/null +++ b/pagetop/src/html/assets/javascript.rs @@ -0,0 +1,51 @@ +use crate::html::{Markup, html}; +use super::AssetsTrait; + +#[derive(PartialEq)] +pub enum JSMode { Async, Defer, Normal } + +pub struct JavaScript { + source: &'static str, + weight: isize, + mode : JSMode, +} + +impl AssetsTrait for JavaScript { + fn source(&self) -> &'static str { + self.source + } + + fn weight(&self) -> isize { + self.weight + } + + fn render(&self) -> Markup { + html! { + script type="text/javascript" + src=(self.source) + async[self.mode == JSMode::Async] + defer[self.mode == JSMode::Defer] + {}; + } + } +} + +impl JavaScript { + pub fn with_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 + } +} diff --git a/pagetop/src/html/assets/stylesheet.rs b/pagetop/src/html/assets/stylesheet.rs new file mode 100644 index 00000000..ff7da045 --- /dev/null +++ b/pagetop/src/html/assets/stylesheet.rs @@ -0,0 +1,37 @@ +use crate::html::{Markup, html}; +use super::AssetsTrait; + +pub struct StyleSheet { + source: &'static str, + weight: isize, +} + +impl AssetsTrait for StyleSheet { + fn source(&self) -> &'static str { + self.source + } + + fn weight(&self) -> isize { + self.weight + } + + fn render(&self) -> Markup { + html! { + link rel="stylesheet" href=(self.source); + } + } +} + +impl StyleSheet { + pub fn with_source(s: &'static str) -> Self { + StyleSheet { + source: s, + weight: 0, + } + } + + pub fn with_weight(mut self, weight: isize) -> Self { + self.weight = weight; + self + } +} diff --git a/pagetop/src/html/favicon.rs b/pagetop/src/html/favicon.rs new file mode 100644 index 00000000..d8d6fc18 --- /dev/null +++ b/pagetop/src/html/favicon.rs @@ -0,0 +1,86 @@ +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(crate) fn render(&self) -> Markup { + html! { + @for item in &self.0 { + (PreEscaped(item)) + } + } + } +}