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