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

View file

@ -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();
}

View file

@ -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)
);
));
}
}

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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<StyleSheet>) -> &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<JavaScript>) -> &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

View file

@ -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;

View file

@ -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<T: AssetsTrait> {
Add(T),
Remove(SourceValue),
}
pub struct Assets<T>(Vec<T>);
impl<T: AssetsTrait> Assets<T> {
@ -18,13 +24,22 @@ impl<T: AssetsTrait> Assets<T> {
Assets::<T>(Vec::<T>::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<T>) -> &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
}

View file

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

View file

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