♻️ Add Favicon in assets and let context manage it
This commit is contained in:
parent
5fb0a1332e
commit
ce28bf3e2f
9 changed files with 82 additions and 69 deletions
|
|
@ -14,17 +14,19 @@ impl PackageTrait for Basic {
|
|||
|
||||
impl ThemeTrait for Basic {
|
||||
fn after_prepare_body(&self, page: &mut Page) {
|
||||
page.set_favicon(Some(Favicon::new().with_icon("/base/favicon.ico")))
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/normalize.min.css")
|
||||
.with_version("8.0.1")
|
||||
.with_weight(-90),
|
||||
))
|
||||
.set_assets(AssetsOp::AddBaseAssets)
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/basic.css")
|
||||
.with_version("0.0.1")
|
||||
.with_weight(-90),
|
||||
));
|
||||
page.set_assets(AssetsOp::SetFavicon(
|
||||
Some(Favicon::new().with_icon("/base/favicon.ico")),
|
||||
))
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/normalize.min.css")
|
||||
.with_version("8.0.1")
|
||||
.with_weight(-90),
|
||||
))
|
||||
.set_assets(AssetsOp::AddBaseAssets)
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/basic.css")
|
||||
.with_version("0.0.1")
|
||||
.with_weight(-90),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,17 +14,19 @@ impl PackageTrait for Chassis {
|
|||
|
||||
impl ThemeTrait for Chassis {
|
||||
fn after_prepare_body(&self, page: &mut Page) {
|
||||
page.set_favicon(Some(Favicon::new().with_icon("/base/favicon.ico")))
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/normalize.min.css")
|
||||
.with_version("8.0.1")
|
||||
.with_weight(-90),
|
||||
))
|
||||
.set_assets(AssetsOp::AddBaseAssets)
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/chassis.css")
|
||||
.with_version("0.0.1")
|
||||
.with_weight(-90),
|
||||
));
|
||||
page.set_assets(AssetsOp::SetFavicon(
|
||||
Some(Favicon::new().with_icon("/base/favicon.ico")),
|
||||
))
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/normalize.min.css")
|
||||
.with_version("8.0.1")
|
||||
.with_weight(-90),
|
||||
))
|
||||
.set_assets(AssetsOp::AddBaseAssets)
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/chassis.css")
|
||||
.with_version("0.0.1")
|
||||
.with_weight(-90),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,17 +14,19 @@ impl PackageTrait for Inception {
|
|||
|
||||
impl ThemeTrait for Inception {
|
||||
fn after_prepare_body(&self, page: &mut Page) {
|
||||
page.set_favicon(Some(Favicon::new().with_icon("/base/favicon.ico")))
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/normalize.min.css")
|
||||
.with_version("8.0.1")
|
||||
.with_weight(-90),
|
||||
))
|
||||
.set_assets(AssetsOp::AddBaseAssets)
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/inception.css")
|
||||
.with_version("0.0.1")
|
||||
.with_weight(-90),
|
||||
));
|
||||
page.set_assets(AssetsOp::SetFavicon(
|
||||
Some(Favicon::new().with_icon("/base/favicon.ico")),
|
||||
))
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/normalize.min.css")
|
||||
.with_version("8.0.1")
|
||||
.with_weight(-90),
|
||||
))
|
||||
.set_assets(AssetsOp::AddBaseAssets)
|
||||
.set_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::from("/base/css/inception.css")
|
||||
.with_version("0.0.1")
|
||||
.with_weight(-90),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use crate::core::component::AnyOp;
|
|||
use crate::core::theme::all::{theme_by_short_name, THEME_DEFAULT};
|
||||
use crate::core::theme::{ComponentsInRegions, ThemeRef};
|
||||
use crate::html::{html, Markup};
|
||||
use crate::html::{Assets, JavaScript, StyleSheet};
|
||||
use crate::html::{Assets, Favicon, JavaScript, StyleSheet};
|
||||
use crate::locale::{LanguageIdentifier, LANGID_DEFAULT};
|
||||
use crate::service::HttpRequest;
|
||||
use crate::util::TypeInfo;
|
||||
|
|
@ -19,6 +19,9 @@ pub enum AssetsOp {
|
|||
LangId(&'static LanguageIdentifier),
|
||||
Theme(&'static str),
|
||||
Layout(&'static str),
|
||||
// Favicon.
|
||||
SetFavicon(Option<Favicon>),
|
||||
SetFaviconIfNone(Favicon),
|
||||
// Stylesheets.
|
||||
AddStyleSheet(StyleSheet),
|
||||
RemoveStyleSheet(&'static str),
|
||||
|
|
@ -52,6 +55,7 @@ pub struct Context {
|
|||
langid : &'static LanguageIdentifier,
|
||||
theme : ThemeRef,
|
||||
layout : &'static str,
|
||||
favicon : Option<Favicon>,
|
||||
stylesheet: Assets<StyleSheet>,
|
||||
javascript: Assets<JavaScript>,
|
||||
regions : ComponentsInRegions,
|
||||
|
|
@ -67,6 +71,7 @@ impl Context {
|
|||
langid : &LANGID_DEFAULT,
|
||||
theme : *THEME_DEFAULT,
|
||||
layout : "default",
|
||||
favicon : None,
|
||||
stylesheet: Assets::<StyleSheet>::new(),
|
||||
javascript: Assets::<JavaScript>::new(),
|
||||
regions : ComponentsInRegions::default(),
|
||||
|
|
@ -75,7 +80,6 @@ impl Context {
|
|||
}
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub fn set_assets(&mut self, op: AssetsOp) -> &mut Self {
|
||||
match op {
|
||||
AssetsOp::LangId(langid) => {
|
||||
|
|
@ -87,16 +91,33 @@ impl Context {
|
|||
AssetsOp::Layout(layout) => {
|
||||
self.layout = layout;
|
||||
}
|
||||
|
||||
// Favicon.
|
||||
AssetsOp::SetFavicon(favicon) => {
|
||||
self.favicon = favicon;
|
||||
}
|
||||
AssetsOp::SetFaviconIfNone(icon) => {
|
||||
if self.favicon.is_none() {
|
||||
self.favicon = Some(icon);
|
||||
}
|
||||
}
|
||||
// Stylesheets.
|
||||
AssetsOp::AddStyleSheet(css) => { self.stylesheet.add(css); }
|
||||
AssetsOp::RemoveStyleSheet(path) => { self.stylesheet.remove(path); }
|
||||
AssetsOp::AddStyleSheet(css) => {
|
||||
self.stylesheet.add(css);
|
||||
}
|
||||
AssetsOp::RemoveStyleSheet(path) => {
|
||||
self.stylesheet.remove(path);
|
||||
}
|
||||
// JavaScripts.
|
||||
AssetsOp::AddJavaScript(js) => { self.javascript.add(js); }
|
||||
AssetsOp::RemoveJavaScript(path) => { self.javascript.remove(path); }
|
||||
|
||||
AssetsOp::AddJavaScript(js) => {
|
||||
self.javascript.add(js);
|
||||
}
|
||||
AssetsOp::RemoveJavaScript(path) => {
|
||||
self.javascript.remove(path);
|
||||
}
|
||||
// Add assets to properly use base components.
|
||||
AssetsOp::AddBaseAssets => { add_base_assets(self); }
|
||||
AssetsOp::AddBaseAssets => {
|
||||
add_base_assets(self);
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
|
@ -144,6 +165,9 @@ impl Context {
|
|||
|
||||
pub(crate) fn prepare_assets(&mut self) -> Markup {
|
||||
html! {
|
||||
@if let Some(favicon) = &self.favicon {
|
||||
(favicon.prepare())
|
||||
}
|
||||
(self.stylesheet.prepare())
|
||||
(self.javascript.prepare())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::base::component::*;
|
||||
use crate::core::component::{ComponentBase, ComponentTrait};
|
||||
use crate::core::component::{AssetsOp, ComponentBase, ComponentTrait};
|
||||
use crate::core::package::PackageTrait;
|
||||
use crate::html::{html, Favicon, PrepareMarkup};
|
||||
use crate::locale::L10n;
|
||||
|
|
@ -70,9 +70,9 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
|
|||
}
|
||||
|
||||
fn after_prepare_body(&self, page: &mut Page) {
|
||||
if page.favicon().is_none() {
|
||||
page.set_favicon(Some(Favicon::new().with_icon("/base/favicon.ico")));
|
||||
}
|
||||
page.set_assets(AssetsOp::SetFaviconIfNone(
|
||||
Favicon::new().with_icon("/base/favicon.ico"),
|
||||
));
|
||||
}
|
||||
|
||||
fn prepare_head(&self, page: &mut Page) -> PrepareMarkup {
|
||||
|
|
@ -101,10 +101,6 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
|
|||
meta property=(property) content=(content) {}
|
||||
}
|
||||
|
||||
@if let Some(favicon) = page.favicon() {
|
||||
(favicon.prepare())
|
||||
}
|
||||
|
||||
(page.context().prepare_assets())
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -4,13 +4,11 @@ mod maud;
|
|||
pub use maud::{html, html_private, Markup, PreEscaped, DOCTYPE};
|
||||
|
||||
mod assets;
|
||||
pub use assets::favicon::Favicon;
|
||||
pub use assets::javascript::JavaScript;
|
||||
pub use assets::stylesheet::{StyleSheet, TargetMedia};
|
||||
pub(crate) use assets::Assets;
|
||||
|
||||
mod favicon;
|
||||
pub use favicon::Favicon;
|
||||
|
||||
mod opt_id;
|
||||
pub use opt_id::OptionId;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod favicon;
|
||||
pub mod javascript;
|
||||
pub mod stylesheet;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use crate::base::action;
|
|||
use crate::core::component::{AnyComponent, AnyOp, ComponentTrait};
|
||||
use crate::core::component::{AssetsOp, Context};
|
||||
use crate::fn_builder;
|
||||
use crate::html::{html, Favicon, Markup, DOCTYPE};
|
||||
use crate::html::{html, Markup, DOCTYPE};
|
||||
use crate::html::{ClassesOp, OptionClasses, OptionId, OptionTranslated};
|
||||
use crate::locale::L10n;
|
||||
use crate::service::HttpRequest;
|
||||
|
|
@ -20,7 +20,6 @@ pub struct Page {
|
|||
description : OptionTranslated,
|
||||
metadata : Vec<(&'static str, &'static str)>,
|
||||
properties : Vec<(&'static str, &'static str)>,
|
||||
favicon : Option<Favicon>,
|
||||
context : Context,
|
||||
body_id : OptionId,
|
||||
body_classes: OptionClasses,
|
||||
|
|
@ -35,7 +34,6 @@ impl Page {
|
|||
description : OptionTranslated::default(),
|
||||
metadata : Vec::default(),
|
||||
properties : Vec::default(),
|
||||
favicon : None,
|
||||
context : Context::new(request),
|
||||
body_id : OptionId::default(),
|
||||
body_classes: OptionClasses::default(),
|
||||
|
|
@ -69,12 +67,6 @@ impl Page {
|
|||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_favicon(&mut self, favicon: Option<Favicon>) -> &mut Self {
|
||||
self.favicon = favicon;
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_assets(&mut self, op: AssetsOp) -> &mut Self {
|
||||
self.context.set_assets(op);
|
||||
|
|
@ -145,10 +137,6 @@ impl Page {
|
|||
&self.properties
|
||||
}
|
||||
|
||||
pub fn favicon(&self) -> &Option<Favicon> {
|
||||
&self.favicon
|
||||
}
|
||||
|
||||
pub fn context(&mut self) -> &mut Context {
|
||||
&mut self.context
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue