Añade opción de borrar stylesheets y javascripts

This commit is contained in:
Manuel Cillero 2022-07-13 20:07:05 +02:00
parent e8c904fac7
commit 4ff9cfec0f
10 changed files with 109 additions and 66 deletions

View file

@ -35,8 +35,9 @@ impl ComponentTrait for Icon {
fn before_render(&mut self, context: &mut InContext) { fn before_render(&mut self, context: &mut InContext) {
context context
.add_stylesheet(StyleSheet::with_source( .with_stylesheet(AssetsOp::Add(
"/theme/icons/bootstrap-icons.css?ver=1.8.2" 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); self.alter_classes(concat_string!("bi-", self.icon_name()).as_str(), ClassesOp::SetDefault);

View file

@ -208,14 +208,17 @@ impl ComponentTrait for Menu {
fn before_render(&mut self, context: &mut InContext) { fn before_render(&mut self, context: &mut InContext) {
context context
.add_stylesheet(StyleSheet::with_source( .with_stylesheet(AssetsOp::Add(
"/theme/menu/css/menu.css?ver=1.1.1" StyleSheet::located("/theme/menu/css/menu.css")
.with_version("1.1.1")
)) ))
.add_stylesheet(StyleSheet::with_source( .with_stylesheet(AssetsOp::Add(
"/theme/menu/css/menu-clean.css?ver=1.1.1" StyleSheet::located("/theme/menu/css/menu-clean.css")
.with_version("1.1.1")
)) ))
.add_javascript(JavaScript::with_source( .with_javascript(AssetsOp::Add(
"/theme/menu/js/menu.min.js?ver=1.1.1" JavaScript::located("/theme/menu/js/menu.min.js")
.with_version("1.1.1")
)) ))
.add_jquery(); .add_jquery();
} }

View file

@ -20,11 +20,11 @@ impl ThemeTrait for Aliner {
.with_favicon(Some(Favicon::new() .with_favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png") .with_icon("/theme/favicon.png")
)) ))
.add_stylesheet( .with_stylesheet(AssetsOp::Add(
StyleSheet::with_source( StyleSheet::located(
"/aliner/css/styles.css" "/aliner/css/styles.css"
) )
.with_weight(-99) .with_weight(-99)
); ));
} }
} }

View file

@ -22,18 +22,16 @@ impl ThemeTrait for Bootsier {
.with_favicon(Some(Favicon::new() .with_favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png") .with_icon("/theme/favicon.png")
)) ))
.add_stylesheet( .with_stylesheet(AssetsOp::Add(
StyleSheet::with_source( StyleSheet::located("/bootsier/css/bootstrap.min.css")
"/bootsier/css/bootstrap.min.css?ver=5.1.3" .with_version("5.1.3")
) .with_weight(-99)
.with_weight(-99) ))
) .with_javascript(AssetsOp::Add(
.add_javascript( JavaScript::located("/bootsier/js/bootstrap.bundle.min.js")
JavaScript::with_source( .with_version("5.1.3")
"/bootsier/js/bootstrap.bundle.min.js?ver=5.1.3" .with_weight(-99)
) ))
.with_weight(-99)
)
.add_jquery(); .add_jquery();
} }

View file

@ -20,12 +20,11 @@ impl ThemeTrait for Bulmix {
.with_favicon(Some(Favicon::new() .with_favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png") .with_icon("/theme/favicon.png")
)) ))
.add_stylesheet( .with_stylesheet(AssetsOp::Add(
StyleSheet::with_source( StyleSheet::located("/bulmix/css/bulma.min.css")
"/bulmix/css/bulma.min.css?ver=0.9.4" .with_version("0.9.4")
) .with_weight(-99)
.with_weight(-99) ))
)
.add_jquery(); .add_jquery();
} }

View file

@ -49,25 +49,24 @@ impl InContext {
self self
} }
pub fn add_stylesheet(&mut self, css: StyleSheet) -> &mut Self { pub fn with_stylesheet(&mut self, css: AssetsOp<StyleSheet>) -> &mut Self {
self.stylesheets.add(css); self.stylesheets.alter(css);
self self
} }
pub fn add_javascript(&mut self, js: JavaScript) -> &mut Self { pub fn with_javascript(&mut self, js: AssetsOp<JavaScript>) -> &mut Self {
self.javascripts.add(js); self.javascripts.alter(js);
self self
} }
pub fn add_jquery(&mut self) -> &mut Self { pub fn add_jquery(&mut self) -> &mut Self {
if !self.with_jquery { if !self.with_jquery {
self.add_javascript( self.with_javascript(AssetsOp::Add(
JavaScript::with_source( JavaScript::located("/theme/js/jquery.min.js")
"/theme/js/jquery.min.js?ver=3.6.0" .with_version("3.6.0")
) .with_weight(isize::MIN)
.with_weight(isize::MIN) .with_mode(JSMode::Normal)
.with_mode(JSMode::Normal) ));
);
self.with_jquery = true; self.with_jquery = true;
} }
self self

View file

@ -1,7 +1,7 @@
pub use maud::{DOCTYPE, Markup, PreEscaped, html}; pub use maud::{DOCTYPE, Markup, PreEscaped, html};
mod assets; mod assets;
pub use assets::Assets; pub use assets::{Assets, AssetsOp, SourceValue};
pub use assets::javascript::{JavaScript, JSMode}; pub use assets::javascript::{JavaScript, JSMode};
pub use assets::stylesheet::StyleSheet; pub use assets::stylesheet::StyleSheet;

View file

@ -3,14 +3,20 @@ pub mod stylesheet;
use crate::html::{Markup, html}; use crate::html::{Markup, html};
pub type SourceValue = &'static str;
pub trait AssetsTrait { pub trait AssetsTrait {
fn source(&self) -> &'static str; fn source(&self) -> SourceValue;
fn weight(&self) -> isize; fn weight(&self) -> isize;
fn render(&self) -> Markup; fn render(&self) -> Markup;
} }
pub enum AssetsOp<T: AssetsTrait> {
Add(T),
Remove(SourceValue),
}
pub struct Assets<T>(Vec<T>); pub struct Assets<T>(Vec<T>);
impl<T: AssetsTrait> Assets<T> { impl<T: AssetsTrait> Assets<T> {
@ -18,13 +24,22 @@ impl<T: AssetsTrait> Assets<T> {
Assets::<T>(Vec::<T>::new()) Assets::<T>(Vec::<T>::new())
} }
pub fn add(&mut self, assets: T) -> &mut Self { pub fn alter(&mut self, op: AssetsOp<T>) -> &mut Self {
match self.0.iter().position(|x| x.source() == assets.source()) { match op {
Some(index) => if self.0[index].weight() > assets.weight() { 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.remove(index);
self.0.push(assets); }
},
_ => self.0.push(assets)
} }
self self
} }

View file

@ -1,17 +1,19 @@
use crate::html::{Markup, html}; use crate::html::{Markup, html};
use super::AssetsTrait; use super::{AssetsTrait, SourceValue};
#[derive(PartialEq)] #[derive(PartialEq)]
pub enum JSMode { Async, Defer, Normal } pub enum JSMode { Async, Defer, Normal }
pub struct JavaScript { pub struct JavaScript {
source: &'static str, source : SourceValue,
weight: isize, prefix : &'static str,
mode : JSMode, version: &'static str,
weight : isize,
mode : JSMode,
} }
impl AssetsTrait for JavaScript { impl AssetsTrait for JavaScript {
fn source(&self) -> &'static str { fn source(&self) -> SourceValue {
self.source self.source
} }
@ -22,7 +24,7 @@ impl AssetsTrait for JavaScript {
fn render(&self) -> Markup { fn render(&self) -> Markup {
html! { html! {
script type="text/javascript" script type="text/javascript"
src=(self.source) src=(crate::concat_string!(self.source, self.prefix, self.version))
async[self.mode == JSMode::Async] async[self.mode == JSMode::Async]
defer[self.mode == JSMode::Defer] defer[self.mode == JSMode::Defer]
{}; {};
@ -31,14 +33,25 @@ impl AssetsTrait for JavaScript {
} }
impl JavaScript { impl JavaScript {
pub fn with_source(s: &'static str) -> Self { pub fn located(source: SourceValue) -> Self {
JavaScript { JavaScript {
source: s, source,
weight: 0, prefix : "",
mode : JSMode::Defer, 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 { pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight; self.weight = weight;
self self

View file

@ -1,13 +1,15 @@
use crate::html::{Markup, html}; use crate::html::{Markup, html};
use super::AssetsTrait; use super::{AssetsTrait, SourceValue};
pub struct StyleSheet { pub struct StyleSheet {
source: &'static str, source : SourceValue,
weight: isize, prefix : &'static str,
version: &'static str,
weight : isize,
} }
impl AssetsTrait for StyleSheet { impl AssetsTrait for StyleSheet {
fn source(&self) -> &'static str { fn source(&self) -> SourceValue {
self.source self.source
} }
@ -17,19 +19,32 @@ impl AssetsTrait for StyleSheet {
fn render(&self) -> Markup { fn render(&self) -> Markup {
html! { html! {
link rel="stylesheet" href=(self.source); link
rel="stylesheet"
href=(crate::concat_string!(self.source, self.prefix, self.version));
} }
} }
} }
impl StyleSheet { impl StyleSheet {
pub fn with_source(s: &'static str) -> Self { pub fn located(source: SourceValue) -> Self {
StyleSheet { StyleSheet {
source: s, source,
weight: 0, 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 { pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight; self.weight = weight;
self self