From 4ff9cfec0fe0d40ad92b77842c5350b1aa67535a Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Wed, 13 Jul 2022 20:07:05 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1ade=20opci=C3=B3n=20de=20borrar=20styles?= =?UTF-8?q?heets=20y=20javascripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/base/component/icon.rs | 5 ++-- pagetop/src/base/component/menu.rs | 15 +++++++----- pagetop/src/base/theme/aliner.rs | 6 ++--- pagetop/src/base/theme/bootsier.rs | 22 ++++++++---------- pagetop/src/base/theme/bulmix.rs | 11 ++++----- pagetop/src/core/component/context.rs | 21 ++++++++--------- pagetop/src/html.rs | 2 +- pagetop/src/html/assets.rs | 29 +++++++++++++++++------ pagetop/src/html/assets/javascript.rs | 33 +++++++++++++++++++-------- pagetop/src/html/assets/stylesheet.rs | 31 ++++++++++++++++++------- 10 files changed, 109 insertions(+), 66 deletions(-) diff --git a/pagetop/src/base/component/icon.rs b/pagetop/src/base/component/icon.rs index d731940d..071c0b6e 100644 --- a/pagetop/src/base/component/icon.rs +++ b/pagetop/src/base/component/icon.rs @@ -35,8 +35,9 @@ impl ComponentTrait for Icon { fn before_render(&mut self, context: &mut InContext) { context - .add_stylesheet(StyleSheet::with_source( - "/theme/icons/bootstrap-icons.css?ver=1.8.2" + .with_stylesheet(AssetsOp::Add( + StyleSheet::located("/theme/icons/bootstrap-icons.css") + .with_version("1.8.2") )); self.alter_classes(concat_string!("bi-", self.icon_name()).as_str(), ClassesOp::SetDefault); diff --git a/pagetop/src/base/component/menu.rs b/pagetop/src/base/component/menu.rs index 0531219d..8b8b1b9f 100644 --- a/pagetop/src/base/component/menu.rs +++ b/pagetop/src/base/component/menu.rs @@ -208,14 +208,17 @@ impl ComponentTrait for Menu { fn before_render(&mut self, context: &mut InContext) { context - .add_stylesheet(StyleSheet::with_source( - "/theme/menu/css/menu.css?ver=1.1.1" + .with_stylesheet(AssetsOp::Add( + StyleSheet::located("/theme/menu/css/menu.css") + .with_version("1.1.1") )) - .add_stylesheet(StyleSheet::with_source( - "/theme/menu/css/menu-clean.css?ver=1.1.1" + .with_stylesheet(AssetsOp::Add( + StyleSheet::located("/theme/menu/css/menu-clean.css") + .with_version("1.1.1") )) - .add_javascript(JavaScript::with_source( - "/theme/menu/js/menu.min.js?ver=1.1.1" + .with_javascript(AssetsOp::Add( + JavaScript::located("/theme/menu/js/menu.min.js") + .with_version("1.1.1") )) .add_jquery(); } diff --git a/pagetop/src/base/theme/aliner.rs b/pagetop/src/base/theme/aliner.rs index 02ed19d3..10ff3379 100644 --- a/pagetop/src/base/theme/aliner.rs +++ b/pagetop/src/base/theme/aliner.rs @@ -20,11 +20,11 @@ impl ThemeTrait for Aliner { .with_favicon(Some(Favicon::new() .with_icon("/theme/favicon.png") )) - .add_stylesheet( - StyleSheet::with_source( + .with_stylesheet(AssetsOp::Add( + StyleSheet::located( "/aliner/css/styles.css" ) .with_weight(-99) - ); + )); } } diff --git a/pagetop/src/base/theme/bootsier.rs b/pagetop/src/base/theme/bootsier.rs index 3e875f30..ed9d431e 100644 --- a/pagetop/src/base/theme/bootsier.rs +++ b/pagetop/src/base/theme/bootsier.rs @@ -22,18 +22,16 @@ impl ThemeTrait for Bootsier { .with_favicon(Some(Favicon::new() .with_icon("/theme/favicon.png") )) - .add_stylesheet( - StyleSheet::with_source( - "/bootsier/css/bootstrap.min.css?ver=5.1.3" - ) - .with_weight(-99) - ) - .add_javascript( - JavaScript::with_source( - "/bootsier/js/bootstrap.bundle.min.js?ver=5.1.3" - ) - .with_weight(-99) - ) + .with_stylesheet(AssetsOp::Add( + StyleSheet::located("/bootsier/css/bootstrap.min.css") + .with_version("5.1.3") + .with_weight(-99) + )) + .with_javascript(AssetsOp::Add( + JavaScript::located("/bootsier/js/bootstrap.bundle.min.js") + .with_version("5.1.3") + .with_weight(-99) + )) .add_jquery(); } diff --git a/pagetop/src/base/theme/bulmix.rs b/pagetop/src/base/theme/bulmix.rs index 77e0ce2b..72340600 100644 --- a/pagetop/src/base/theme/bulmix.rs +++ b/pagetop/src/base/theme/bulmix.rs @@ -20,12 +20,11 @@ impl ThemeTrait for Bulmix { .with_favicon(Some(Favicon::new() .with_icon("/theme/favicon.png") )) - .add_stylesheet( - StyleSheet::with_source( - "/bulmix/css/bulma.min.css?ver=0.9.4" - ) - .with_weight(-99) - ) + .with_stylesheet(AssetsOp::Add( + StyleSheet::located("/bulmix/css/bulma.min.css") + .with_version("0.9.4") + .with_weight(-99) + )) .add_jquery(); } diff --git a/pagetop/src/core/component/context.rs b/pagetop/src/core/component/context.rs index b0296e56..ef309f01 100644 --- a/pagetop/src/core/component/context.rs +++ b/pagetop/src/core/component/context.rs @@ -49,25 +49,24 @@ impl InContext { self } - pub fn add_stylesheet(&mut self, css: StyleSheet) -> &mut Self { - self.stylesheets.add(css); + pub fn with_stylesheet(&mut self, css: AssetsOp) -> &mut Self { + self.stylesheets.alter(css); self } - pub fn add_javascript(&mut self, js: JavaScript) -> &mut Self { - self.javascripts.add(js); + pub fn with_javascript(&mut self, js: AssetsOp) -> &mut Self { + self.javascripts.alter(js); self } pub fn add_jquery(&mut self) -> &mut Self { if !self.with_jquery { - self.add_javascript( - JavaScript::with_source( - "/theme/js/jquery.min.js?ver=3.6.0" - ) - .with_weight(isize::MIN) - .with_mode(JSMode::Normal) - ); + self.with_javascript(AssetsOp::Add( + JavaScript::located("/theme/js/jquery.min.js") + .with_version("3.6.0") + .with_weight(isize::MIN) + .with_mode(JSMode::Normal) + )); self.with_jquery = true; } self diff --git a/pagetop/src/html.rs b/pagetop/src/html.rs index 17d271a2..3d71db75 100644 --- a/pagetop/src/html.rs +++ b/pagetop/src/html.rs @@ -1,7 +1,7 @@ pub use maud::{DOCTYPE, Markup, PreEscaped, html}; mod assets; -pub use assets::Assets; +pub use assets::{Assets, AssetsOp, SourceValue}; pub use assets::javascript::{JavaScript, JSMode}; pub use assets::stylesheet::StyleSheet; diff --git a/pagetop/src/html/assets.rs b/pagetop/src/html/assets.rs index 921d6816..96e52d2c 100644 --- a/pagetop/src/html/assets.rs +++ b/pagetop/src/html/assets.rs @@ -3,14 +3,20 @@ pub mod stylesheet; use crate::html::{Markup, html}; +pub type SourceValue = &'static str; + pub trait AssetsTrait { - fn source(&self) -> &'static str; + fn source(&self) -> SourceValue; fn weight(&self) -> isize; fn render(&self) -> Markup; } +pub enum AssetsOp { + Add(T), + Remove(SourceValue), +} pub struct Assets(Vec); impl Assets { @@ -18,13 +24,22 @@ impl Assets { 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() { + pub fn alter(&mut self, op: AssetsOp) -> &mut Self { + match op { + AssetsOp::Add(asset) => match self.0.iter().position( + |x| x.source() == asset.source() + ) { + Some(index) => if self.0[index].weight() > asset.weight() { + self.0.remove(index); + self.0.push(asset); + }, + _ => self.0.push(asset) + } + AssetsOp::Remove(source) => if let Some(index) = self.0.iter().position( + |x| x.source() == source + ) { self.0.remove(index); - self.0.push(assets); - }, - _ => self.0.push(assets) + } } self } diff --git a/pagetop/src/html/assets/javascript.rs b/pagetop/src/html/assets/javascript.rs index 40cdfb07..08d7705a 100644 --- a/pagetop/src/html/assets/javascript.rs +++ b/pagetop/src/html/assets/javascript.rs @@ -1,17 +1,19 @@ use crate::html::{Markup, html}; -use super::AssetsTrait; +use super::{AssetsTrait, SourceValue}; #[derive(PartialEq)] pub enum JSMode { Async, Defer, Normal } pub struct JavaScript { - source: &'static str, - weight: isize, - mode : JSMode, + source : SourceValue, + prefix : &'static str, + version: &'static str, + weight : isize, + mode : JSMode, } impl AssetsTrait for JavaScript { - fn source(&self) -> &'static str { + fn source(&self) -> SourceValue { self.source } @@ -22,7 +24,7 @@ impl AssetsTrait for JavaScript { fn render(&self) -> Markup { html! { script type="text/javascript" - src=(self.source) + src=(crate::concat_string!(self.source, self.prefix, self.version)) async[self.mode == JSMode::Async] defer[self.mode == JSMode::Defer] {}; @@ -31,14 +33,25 @@ impl AssetsTrait for JavaScript { } impl JavaScript { - pub fn with_source(s: &'static str) -> Self { + pub fn located(source: SourceValue) -> Self { JavaScript { - source: s, - weight: 0, - mode : JSMode::Defer, + source, + prefix : "", + version: "", + weight : 0, + mode : JSMode::Defer, } } + pub fn with_version(mut self, version: &'static str) -> Self { + (self.prefix, self.version) = if version.is_empty() { + ("", "") + } else { + ("?ver=", version) + }; + self + } + pub fn with_weight(mut self, weight: isize) -> Self { self.weight = weight; self diff --git a/pagetop/src/html/assets/stylesheet.rs b/pagetop/src/html/assets/stylesheet.rs index ff7da045..65f477fd 100644 --- a/pagetop/src/html/assets/stylesheet.rs +++ b/pagetop/src/html/assets/stylesheet.rs @@ -1,13 +1,15 @@ use crate::html::{Markup, html}; -use super::AssetsTrait; +use super::{AssetsTrait, SourceValue}; pub struct StyleSheet { - source: &'static str, - weight: isize, + source : SourceValue, + prefix : &'static str, + version: &'static str, + weight : isize, } impl AssetsTrait for StyleSheet { - fn source(&self) -> &'static str { + fn source(&self) -> SourceValue { self.source } @@ -17,19 +19,32 @@ impl AssetsTrait for StyleSheet { fn render(&self) -> Markup { html! { - link rel="stylesheet" href=(self.source); + link + rel="stylesheet" + href=(crate::concat_string!(self.source, self.prefix, self.version)); } } } impl StyleSheet { - pub fn with_source(s: &'static str) -> Self { + pub fn located(source: SourceValue) -> Self { StyleSheet { - source: s, - weight: 0, + source, + prefix : "", + version: "", + weight : 0, } } + pub fn with_version(mut self, version: &'static str) -> Self { + (self.prefix, self.version) = if version.is_empty() { + ("", "") + } else { + ("?ver=", version) + }; + self + } + pub fn with_weight(mut self, weight: isize) -> Self { self.weight = weight; self