Modifica InContext por PageContext y PageOp

This commit is contained in:
Manuel Cillero 2022-07-30 06:11:30 +02:00
parent de0785c29f
commit 31daf11faa
30 changed files with 149 additions and 133 deletions

View file

@ -41,7 +41,7 @@ pub async fn summary() -> ResultPage<Markup, FatalError> {
.with_item(MenuItem::label("Opción 4")); .with_item(MenuItem::label("Opción 4"));
Page::new() Page::new()
.with_context(InContextOp::SetTheme("Bootsier")) .with_context(PageOp::SetTheme("Bootsier"))
.with_title("Admin") .with_title("Admin")
.add_to("top-menu", top_menu) .add_to("top-menu", top_menu)
.add_to( .add_to(

View file

@ -57,11 +57,11 @@ impl ComponentTrait for Anchor {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut PageContext) -> Markup {
let target = match &self.target() { let target = match &self.target() {
AnchorTarget::Blank => Some("_blank"), AnchorTarget::Blank => Some("_blank"),
AnchorTarget::Context(name) => Some(name.as_str()), AnchorTarget::Context(name) => Some(name.as_str()),

View file

@ -35,15 +35,15 @@ impl ComponentTrait for Block {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn before_render(&mut self, context: &mut InContext) { fn before_render(&mut self, context: &mut PageContext) {
before_render_inline(self, context); before_render_inline(self, context);
} }
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut PageContext) -> Markup {
let id = context.required_id::<Block>(self.id()); let id = context.required_id::<Block>(self.id());
html! { html! {
div id=(id) class=[self.classes().get()] { div id=(id) class=[self.classes().get()] {

View file

@ -45,15 +45,15 @@ impl ComponentTrait for Container {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn before_render(&mut self, context: &mut InContext) { fn before_render(&mut self, context: &mut PageContext) {
before_render_inline(self, context); before_render_inline(self, context);
} }
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut PageContext) -> Markup {
match self.container_type() { match self.container_type() {
ContainerType::Header => html! { ContainerType::Header => html! {
header id=[self.id().get()] class=[self.classes().get()] { header id=[self.id().get()] class=[self.classes().get()] {

View file

@ -44,11 +44,11 @@ impl ComponentTrait for Button {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn default_render(&self, _: &mut InContext) -> Markup { fn default_render(&self, _: &mut PageContext) -> Markup {
let button_type = match self.button_type() { let button_type = match self.button_type() {
ButtonType::Button => "button", ButtonType::Button => "button",
ButtonType::Reset => "reset", ButtonType::Reset => "reset",

View file

@ -48,11 +48,11 @@ impl ComponentTrait for Date {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn default_render(&self, _: &mut InContext) -> Markup { fn default_render(&self, _: &mut PageContext) -> Markup {
let id = self.name().get().map(|name| concat_string!("edit-", name)); let id = self.name().get().map(|name| concat_string!("edit-", name));
html! { html! {
div class=[self.classes().get()] { div class=[self.classes().get()] {

View file

@ -44,15 +44,15 @@ impl ComponentTrait for Form {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn before_render(&mut self, context: &mut InContext) { fn before_render(&mut self, context: &mut PageContext) {
before_render_inline(self, context); before_render_inline(self, context);
} }
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut PageContext) -> Markup {
let method = match self.method() { let method = match self.method() {
FormMethod::Get => None, FormMethod::Get => None,
FormMethod::Post => Some("post".to_owned()), FormMethod::Post => Some("post".to_owned()),

View file

@ -25,7 +25,7 @@ impl ComponentTrait for Hidden {
self.weight self.weight
} }
fn default_render(&self, _: &mut InContext) -> Markup { fn default_render(&self, _: &mut PageContext) -> Markup {
let id = self.name().get().map(|name| concat_string!("value-", name)); let id = self.name().get().map(|name| concat_string!("value-", name));
html! { html! {
input type="hidden" id=[id] name=[self.name().get()] value=[self.value().get()]; input type="hidden" id=[id] name=[self.name().get()] value=[self.value().get()];

View file

@ -65,11 +65,11 @@ impl ComponentTrait for Input {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn default_render(&self, _: &mut InContext) -> Markup { fn default_render(&self, _: &mut PageContext) -> Markup {
let type_input = match self.input_type() { let type_input = match self.input_type() {
InputType::Email => "email", InputType::Email => "email",
InputType::Password => "password", InputType::Password => "password",

View file

@ -64,15 +64,15 @@ impl ComponentTrait for Column {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn before_render(&mut self, context: &mut InContext) { fn before_render(&mut self, context: &mut PageContext) {
before_render_inline(self, context); before_render_inline(self, context);
} }
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut PageContext) -> Markup {
html! { html! {
div id=[self.id().get()] class=[self.classes().get()] { div id=[self.id().get()] class=[self.classes().get()] {
(self.components().render(context)) (self.components().render(context))

View file

@ -33,15 +33,15 @@ impl ComponentTrait for Row {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn before_render(&mut self, context: &mut InContext) { fn before_render(&mut self, context: &mut PageContext) {
before_render_inline(self, context); before_render_inline(self, context);
} }
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut PageContext) -> Markup {
html! { html! {
div id=[self.id().get()] class=[self.classes().get()] { div id=[self.id().get()] class=[self.classes().get()] {
(self.columns().render(context)) (self.columns().render(context))

View file

@ -54,11 +54,11 @@ impl ComponentTrait for Heading {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn default_render(&self, _: &mut InContext) -> Markup { fn default_render(&self, _: &mut PageContext) -> Markup {
let id = self.id().get(); let id = self.id().get();
let classes = self.classes().get(); let classes = self.classes().get();
html! { @match &self.heading_type() { html! { @match &self.heading_type() {

View file

@ -27,11 +27,11 @@ impl ComponentTrait for Html {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn default_render(&self, _: &mut InContext) -> Markup { fn default_render(&self, _: &mut PageContext) -> Markup {
html! { (*self.html()) } html! { (*self.html()) }
} }

View file

@ -27,17 +27,17 @@ impl ComponentTrait for Icon {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn before_render(&mut self, context: &mut InContext) { fn before_render(&mut self, context: &mut PageContext) {
context.alter(InContextOp::AddStyleSheet( context.alter(PageOp::AddStyleSheet(
StyleSheet::located("/theme/icons/bootstrap-icons.css").with_version("1.8.2"), StyleSheet::located("/theme/icons/bootstrap-icons.css").with_version("1.8.2"),
)); ));
} }
fn default_render(&self, _: &mut InContext) -> Markup { fn default_render(&self, _: &mut PageContext) -> Markup {
html! { i class=[self.classes().get()] {}; } html! { i class=[self.classes().get()] {}; }
} }

View file

@ -31,11 +31,11 @@ impl ComponentTrait for Image {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn default_render(&self, _: &mut InContext) -> Markup { fn default_render(&self, _: &mut PageContext) -> Markup {
html! { html! {
img img
src=[self.source().get()] src=[self.source().get()]

View file

@ -37,11 +37,11 @@ impl ComponentTrait for MenuItem {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut PageContext) -> Markup {
match self.item_type() { match self.item_type() {
MenuItemType::Label(label) => html! { MenuItemType::Label(label) => html! {
li class="label" { a href="#" { (label) } } li class="label" { a href="#" { (label) } }
@ -196,26 +196,26 @@ impl ComponentTrait for Menu {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn before_render(&mut self, context: &mut InContext) { fn before_render(&mut self, context: &mut PageContext) {
before_render_inline(self, context); before_render_inline(self, context);
} }
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut PageContext) -> Markup {
context context
.alter(InContextOp::AddStyleSheet( .alter(PageOp::AddStyleSheet(
StyleSheet::located("/theme/menu/css/menu.css").with_version("1.1.1"), StyleSheet::located("/theme/menu/css/menu.css").with_version("1.1.1"),
)) ))
.alter(InContextOp::AddStyleSheet( .alter(PageOp::AddStyleSheet(
StyleSheet::located("/theme/menu/css/menu-clean.css").with_version("1.1.1"), StyleSheet::located("/theme/menu/css/menu-clean.css").with_version("1.1.1"),
)) ))
.alter(InContextOp::AddJavaScript( .alter(PageOp::AddJavaScript(
JavaScript::located("/theme/menu/js/menu.min.js").with_version("1.1.1"), JavaScript::located("/theme/menu/js/menu.min.js").with_version("1.1.1"),
)) ))
.alter(InContextOp::AddJQuery); .alter(PageOp::AddJQuery);
let id = context.required_id::<Menu>(self.id()); let id = context.required_id::<Menu>(self.id());

View file

@ -42,11 +42,11 @@ impl ComponentTrait for Paragraph {
self.weight self.weight
} }
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
(self.renderable)(context) (self.renderable)(context)
} }
fn default_render(&self, _: &mut InContext) -> Markup { fn default_render(&self, _: &mut PageContext) -> Markup {
html! { html! {
p id=[self.id().get()] class=[self.classes().get()] { (*self.html()) } p id=[self.id().get()] class=[self.classes().get()] { (*self.html()) }
} }

View file

@ -27,7 +27,7 @@ impl ModuleTrait for DefaultHomePage {
async fn demo() -> ResultPage<Markup, FatalError> { async fn demo() -> ResultPage<Markup, FatalError> {
Page::new() Page::new()
.with_title(l("page_title").as_str()) .with_title(l("page_title").as_str())
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/theme/module/homepage/styles.css" StyleSheet::located("/theme/module/homepage/styles.css"
))) )))
.add_to("region-content", hello_world()) .add_to("region-content", hello_world())

View file

@ -17,10 +17,10 @@ impl ThemeTrait for Aliner {
fn before_render_page(&self, page: &mut Page) { fn before_render_page(&self, page: &mut Page) {
page page
.alter_context(InContextOp::AddFavicon( .alter_context(PageOp::AddFavicon(
Favicon::new().with_icon("/theme/favicon.png"), Favicon::new().with_icon("/theme/favicon.png"),
)) ))
.alter_context(InContextOp::AddStyleSheet( .alter_context(PageOp::AddStyleSheet(
StyleSheet::located("/aliner/css/styles.css").with_weight(-99), StyleSheet::located("/aliner/css/styles.css").with_weight(-99),
)); ));
} }

View file

@ -19,20 +19,20 @@ impl ThemeTrait for Bootsier {
fn before_render_page(&self, page: &mut Page) { fn before_render_page(&self, page: &mut Page) {
page page
.alter_context(InContextOp::AddFavicon( .alter_context(PageOp::AddFavicon(
Favicon::new().with_icon("/theme/favicon.png"), Favicon::new().with_icon("/theme/favicon.png"),
)) ))
.alter_context(InContextOp::AddStyleSheet( .alter_context(PageOp::AddStyleSheet(
StyleSheet::located("/bootsier/css/bootstrap.min.css") StyleSheet::located("/bootsier/css/bootstrap.min.css")
.with_version("5.1.3") .with_version("5.1.3")
.with_weight(-99), .with_weight(-99),
)) ))
.alter_context(InContextOp::AddJavaScript( .alter_context(PageOp::AddJavaScript(
JavaScript::located("/bootsier/js/bootstrap.bundle.min.js") JavaScript::located("/bootsier/js/bootstrap.bundle.min.js")
.with_version("5.1.3") .with_version("5.1.3")
.with_weight(-99), .with_weight(-99),
)) ))
.alter_context(InContextOp::AddJQuery); .alter_context(PageOp::AddJQuery);
} }
fn error_404_not_found(&self) -> Container { fn error_404_not_found(&self) -> Container {

View file

@ -17,21 +17,21 @@ impl ThemeTrait for Bulmix {
fn before_render_page(&self, page: &mut Page) { fn before_render_page(&self, page: &mut Page) {
page page
.alter_context(InContextOp::AddFavicon( .alter_context(PageOp::AddFavicon(
Favicon::new().with_icon("/theme/favicon.png"), Favicon::new().with_icon("/theme/favicon.png"),
)) ))
.alter_context(InContextOp::AddStyleSheet( .alter_context(PageOp::AddStyleSheet(
StyleSheet::located("/bulmix/css/bulma.min.css") StyleSheet::located("/bulmix/css/bulma.min.css")
.with_version("0.9.4") .with_version("0.9.4")
.with_weight(-99), .with_weight(-99),
)) ))
.alter_context(InContextOp::AddJQuery); .alter_context(PageOp::AddJQuery);
} }
fn before_render_component( fn before_render_component(
&self, &self,
component: &mut dyn ComponentTrait, component: &mut dyn ComponentTrait,
_context: &mut InContext, _context: &mut PageContext,
) { ) {
match component.handler() { match component.handler() {
COMPONENT_ANCHOR => { COMPONENT_ANCHOR => {
@ -110,7 +110,7 @@ impl ThemeTrait for Bulmix {
fn render_component( fn render_component(
&self, &self,
component: &dyn ComponentTrait, component: &dyn ComponentTrait,
_context: &mut InContext, _context: &mut PageContext,
) -> Option<Markup> { ) -> Option<Markup> {
match component.handler() { match component.handler() {
COMPONENT_ICON => { COMPONENT_ICON => {

View file

@ -1,6 +1,3 @@
mod context;
pub use context::{InContext, InContextOp};
mod definition; mod definition;
pub use definition::{component_mut, component_ref, AnyComponent, BaseComponent, ComponentTrait}; pub use definition::{component_mut, component_ref, AnyComponent, BaseComponent, ComponentTrait};
@ -11,12 +8,14 @@ mod all;
pub use all::add_component_to; pub use all::add_component_to;
pub(crate) use all::common_components; pub(crate) use all::common_components;
pub type Renderable = fn(_: &InContext) -> bool; use crate::response::page::PageContext;
pub fn render_always(_: &InContext) -> bool { pub type Renderable = fn(_: &PageContext) -> bool;
pub fn render_always(_: &PageContext) -> bool {
true true
} }
pub fn render_never(_: &InContext) -> bool { pub fn render_never(_: &PageContext) -> bool {
false false
} }

View file

@ -1,5 +1,6 @@
use super::{ComponentTrait, InContext}; use super::ComponentTrait;
use crate::html::{html, Markup}; use crate::html::{html, Markup};
use crate::response::page::PageContext;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -25,7 +26,7 @@ impl ComponentsBundle {
self.0.clear(); self.0.clear();
} }
pub fn render(&self, context: &mut InContext) -> Markup { pub fn render(&self, context: &mut PageContext) -> Markup {
let mut components = self.0.clone(); let mut components = self.0.clone();
components.sort_by_key(|c| c.read().unwrap().weight()); components.sort_by_key(|c| c.read().unwrap().weight());
html! { html! {

View file

@ -1,11 +1,11 @@
use super::InContext;
use crate::html::{html, Markup}; use crate::html::{html, Markup};
use crate::response::page::PageContext;
use crate::util::{single_type_name, Handler}; use crate::util::{single_type_name, Handler};
pub use std::any::Any as AnyComponent; pub use std::any::Any as AnyComponent;
pub trait BaseComponent { pub trait BaseComponent {
fn render(&mut self, context: &mut InContext) -> Markup; fn render(&mut self, context: &mut PageContext) -> Markup;
} }
pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync { pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
@ -28,15 +28,15 @@ pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
} }
#[allow(unused_variables)] #[allow(unused_variables)]
fn is_renderable(&self, context: &InContext) -> bool { fn is_renderable(&self, context: &PageContext) -> bool {
true true
} }
#[allow(unused_variables)] #[allow(unused_variables)]
fn before_render(&mut self, context: &mut InContext) {} fn before_render(&mut self, context: &mut PageContext) {}
#[allow(unused_variables)] #[allow(unused_variables)]
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut PageContext) -> Markup {
html! {} html! {}
} }
@ -46,7 +46,7 @@ pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
} }
impl<C: ComponentTrait> BaseComponent for C { impl<C: ComponentTrait> BaseComponent for C {
fn render(&mut self, context: &mut InContext) -> Markup { fn render(&mut self, context: &mut PageContext) -> Markup {
// Acciones del componente antes de renderizar. // Acciones del componente antes de renderizar.
self.before_render(context); self.before_render(context);
@ -77,7 +77,7 @@ macro_rules! hook_before_render_component {
paste::paste! { paste::paste! {
$crate::pub_const_handler!($ACTION_HANDLER); $crate::pub_const_handler!($ACTION_HANDLER);
type Action = fn(&$Component, &mut InContext); type Action = fn(&$Component, &mut PageContext);
pub struct [< BeforeRender $Component >] { pub struct [< BeforeRender $Component >] {
action: Option<Action>, action: Option<Action>,
@ -118,7 +118,7 @@ macro_rules! hook_before_render_component {
self self
} }
pub fn run(&self, component: &mut $Component, context: &mut InContext) { pub fn run(&self, component: &mut $Component, context: &mut PageContext) {
if let Some(action) = self.action { if let Some(action) = self.action {
action(component, context) action(component, context)
} }
@ -126,7 +126,7 @@ macro_rules! hook_before_render_component {
} }
#[inline(always)] #[inline(always)]
fn before_render_inline(component: &mut $Component, context: &mut InContext) { fn before_render_inline(component: &mut $Component, context: &mut PageContext) {
run_actions( run_actions(
$ACTION_HANDLER, $ACTION_HANDLER,
|action| action_ref::<[< BeforeRender $Component >]>(&**action).run(component, context) |action| action_ref::<[< BeforeRender $Component >]>(&**action).run(component, context)

View file

@ -2,9 +2,9 @@ use crate::app;
use crate::base::component::{Container, Html}; use crate::base::component::{Container, Html};
use crate::concat_string; use crate::concat_string;
use crate::config::SETTINGS; use crate::config::SETTINGS;
use crate::core::component::{ComponentTrait, InContext, InContextOp}; use crate::core::component::ComponentTrait;
use crate::html::{html, Favicon, Markup}; use crate::html::{html, Favicon, Markup};
use crate::response::page::Page; use crate::response::page::{Page, PageContext, PageOp};
use crate::util::{single_type_name, Handler}; use crate::util::{single_type_name, Handler};
pub trait BaseTheme { pub trait BaseTheme {
@ -28,7 +28,7 @@ pub trait ThemeTrait: BaseTheme + Send + Sync {
#[allow(unused_variables)] #[allow(unused_variables)]
fn before_render_page(&self, page: &mut Page) { fn before_render_page(&self, page: &mut Page) {
page.alter_context(InContextOp::AddFavicon( page.alter_context(PageOp::AddFavicon(
Favicon::new().with_icon("/theme/favicon.png"), Favicon::new().with_icon("/theme/favicon.png"),
)); ));
} }
@ -83,7 +83,11 @@ pub trait ThemeTrait: BaseTheme + Send + Sync {
} }
#[allow(unused_variables)] #[allow(unused_variables)]
fn before_render_component(&self, component: &mut dyn ComponentTrait, context: &mut InContext) { fn before_render_component(
&self,
component: &mut dyn ComponentTrait,
context: &mut PageContext,
) {
/* /*
Cómo usarlo: Cómo usarlo:
@ -101,7 +105,7 @@ pub trait ThemeTrait: BaseTheme + Send + Sync {
fn render_component( fn render_component(
&self, &self,
component: &dyn ComponentTrait, component: &dyn ComponentTrait,
context: &mut InContext, context: &mut PageContext,
) -> Option<Markup> { ) -> Option<Markup> {
None None
/* /*

View file

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

View file

@ -1,5 +1,27 @@
use crate::html::{Favicon, StyleSheet, JavaScript};
pub use actix_web::Result as ResultPage; pub use actix_web::Result as ResultPage;
pub enum PageOp {
SetTheme(&'static str),
AddFavicon(Favicon),
RemoveFavicon,
AddMetadata(&'static str, &'static str),
AddProperty(&'static str, &'static str),
AddStyleSheet(StyleSheet),
RemoveStyleSheet(&'static str),
AddJavaScript(JavaScript),
RemoveJavaScript(&'static str),
AddJQuery,
}
mod context;
pub use context::PageContext;
mod hook; mod hook;
pub use hook::{BeforeRenderPageHook, HOOK_BEFORE_RENDER_PAGE}; pub use hook::{BeforeRenderPageHook, HOOK_BEFORE_RENDER_PAGE};

View file

@ -1,7 +1,8 @@
use super::PageOp;
use crate::config::SETTINGS; use crate::config::SETTINGS;
use crate::core::theme::all::theme_by_single_name; use crate::core::theme::all::theme_by_single_name;
use crate::core::theme::ThemeTrait; use crate::core::theme::ThemeTrait;
use crate::html::*; use crate::html::{html, Assets, Favicon, StyleSheet, JavaScript, JSMode, Markup, IdentifierValue};
use crate::{base, concat_string, util, LazyStatic}; use crate::{base, concat_string, util, LazyStatic};
static DEFAULT_THEME: LazyStatic<&dyn ThemeTrait> = LazyStatic::new(|| static DEFAULT_THEME: LazyStatic<&dyn ThemeTrait> = LazyStatic::new(||
@ -11,24 +12,7 @@ static DEFAULT_THEME: LazyStatic<&dyn ThemeTrait> = LazyStatic::new(||
} }
); );
pub enum InContextOp { pub struct PageContext {
SetTheme(&'static str),
AddFavicon(Favicon),
RemoveFavicon,
AddMetadata(&'static str, &'static str),
AddProperty(&'static str, &'static str),
AddStyleSheet(StyleSheet),
RemoveStyleSheet(&'static str),
AddJavaScript(JavaScript),
RemoveJavaScript(&'static str),
AddJQuery,
}
pub struct InContext {
theme : &'static dyn ThemeTrait, theme : &'static dyn ThemeTrait,
favicon : Option<Favicon>, favicon : Option<Favicon>,
metadata : Vec<(&'static str, &'static str)>, metadata : Vec<(&'static str, &'static str)>,
@ -39,9 +23,9 @@ pub struct InContext {
id_counter : usize, id_counter : usize,
} }
impl InContext { impl PageContext {
pub fn new() -> Self { pub fn new() -> Self {
InContext { PageContext {
theme : *DEFAULT_THEME, theme : *DEFAULT_THEME,
favicon : None, favicon : None,
metadata : Vec::new(), metadata : Vec::new(),
@ -53,40 +37,40 @@ impl InContext {
} }
} }
pub fn alter(&mut self, op: InContextOp) -> &mut Self { pub fn alter(&mut self, op: PageOp) -> &mut Self {
match op { match op {
InContextOp::SetTheme(theme_name) => { PageOp::SetTheme(theme_name) => {
self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME); self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME);
} }
InContextOp::AddFavicon(favicon) => { PageOp::AddFavicon(favicon) => {
self.favicon = Some(favicon); self.favicon = Some(favicon);
} }
InContextOp::RemoveFavicon => { PageOp::RemoveFavicon => {
self.favicon = None; self.favicon = None;
} }
InContextOp::AddMetadata(name, content) => { PageOp::AddMetadata(name, content) => {
self.metadata.push((name, content)); self.metadata.push((name, content));
} }
InContextOp::AddProperty(property, content) => { PageOp::AddProperty(property, content) => {
self.properties.push((property, content)); self.properties.push((property, content));
} }
InContextOp::AddStyleSheet(css) => { PageOp::AddStyleSheet(css) => {
self.stylesheets.add(css); self.stylesheets.add(css);
} }
InContextOp::RemoveStyleSheet(source) => { PageOp::RemoveStyleSheet(source) => {
self.stylesheets.remove(source); self.stylesheets.remove(source);
} }
InContextOp::AddJavaScript(js) => { PageOp::AddJavaScript(js) => {
self.javascripts.add(js); self.javascripts.add(js);
} }
InContextOp::RemoveJavaScript(source) => { PageOp::RemoveJavaScript(source) => {
self.javascripts.remove(source); self.javascripts.remove(source);
} }
InContextOp::AddJQuery => { PageOp::AddJQuery => {
if !self.with_jquery { if !self.with_jquery {
self.javascripts.add( self.javascripts.add(
JavaScript::located("/theme/js/jquery.min.js") JavaScript::located("/theme/js/jquery.min.js")
@ -101,13 +85,13 @@ impl InContext {
self self
} }
/// InContext GETTERS. /// PageContext GETTERS.
pub(crate) fn theme(&mut self) -> &'static dyn ThemeTrait { pub(crate) fn theme(&mut self) -> &'static dyn ThemeTrait {
self.theme self.theme
} }
/// InContext RENDER. /// PageContext RENDER.
pub fn render(&mut self) -> Markup { pub fn render(&mut self) -> Markup {
html! { html! {
@ -126,7 +110,7 @@ impl InContext {
} }
} }
// InContext EXTRAS. // PageContext EXTRAS.
pub fn required_id<T>(&mut self, id: &IdentifierValue) -> String { pub fn required_id<T>(&mut self, id: &IdentifierValue) -> String {
match id.get() { match id.get() {

View file

@ -1,9 +1,15 @@
use super::{BeforeRenderPageHook, ResultPage, HOOK_BEFORE_RENDER_PAGE}; use super::{
BeforeRenderPageHook,
PageContext,
PageOp,
ResultPage,
HOOK_BEFORE_RENDER_PAGE,
};
use crate::app::fatal_error::FatalError; use crate::app::fatal_error::FatalError;
use crate::config::SETTINGS; use crate::config::SETTINGS;
use crate::core::component::*; use crate::core::component::*;
use crate::core::hook::{action_ref, run_actions}; use crate::core::hook::{action_ref, run_actions};
use crate::html::*; use crate::html::{html, AttributeValue, Classes, ClassesOp, Markup, DOCTYPE};
use crate::{trace, LazyStatic}; use crate::{trace, LazyStatic};
use std::collections::HashMap; use std::collections::HashMap;
@ -42,7 +48,7 @@ pub enum TextDirection {
} }
pub struct Page { pub struct Page {
context : InContext, context : PageContext,
language : AttributeValue, language : AttributeValue,
direction : AttributeValue, direction : AttributeValue,
title : AttributeValue, title : AttributeValue,
@ -55,7 +61,7 @@ pub struct Page {
impl Page { impl Page {
pub fn new() -> Self { pub fn new() -> Self {
Page { Page {
context : InContext::new(), context : PageContext::new(),
language : match &*DEFAULT_LANGUAGE { language : match &*DEFAULT_LANGUAGE {
Some(language) => AttributeValue::new_with_value(language), Some(language) => AttributeValue::new_with_value(language),
_ => AttributeValue::new(), _ => AttributeValue::new(),
@ -74,7 +80,7 @@ impl Page {
// Page BUILDER. // Page BUILDER.
pub fn with_context(mut self, op: InContextOp) -> Self { pub fn with_context(mut self, op: PageOp) -> Self {
self.alter_context(op); self.alter_context(op);
self self
} }
@ -121,7 +127,7 @@ impl Page {
// Page ALTER. // Page ALTER.
pub fn alter_context(&mut self, op: InContextOp) -> &mut Self { pub fn alter_context(&mut self, op: PageOp) -> &mut Self {
self.context.alter(op); self.context.alter(op);
self self
} }
@ -162,7 +168,7 @@ impl Page {
// Page GETTERS. // Page GETTERS.
pub fn context(&mut self) -> &mut InContext { pub fn context(&mut self) -> &mut PageContext {
&mut self.context &mut self.context
} }

View file

@ -59,32 +59,32 @@ async fn mdbook_page(request: app::HttpRequest) -> ResultPage<Markup, FatalError
Page::new() Page::new()
.with_title(title) .with_title(title)
.with_context(InContextOp::AddMetadata("theme-color", "#ffffff")) .with_context(PageOp::AddMetadata("theme-color", "#ffffff"))
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/doc/css/variables.css") StyleSheet::located("/doc/css/variables.css")
)) ))
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/doc/css/general.css") StyleSheet::located("/doc/css/general.css")
)) ))
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/doc/css/chrome.css") StyleSheet::located("/doc/css/chrome.css")
)) ))
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/doc/css/print.css").for_media(TargetMedia::Print) StyleSheet::located("/doc/css/print.css").for_media(TargetMedia::Print)
)) ))
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/doc/FontAwesome/css/font-awesome.css") StyleSheet::located("/doc/FontAwesome/css/font-awesome.css")
)) ))
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/doc/fonts/fonts.css") StyleSheet::located("/doc/fonts/fonts.css")
)) ))
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/doc/highlight.css") StyleSheet::located("/doc/highlight.css")
)) ))
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/doc/tomorrow-night.css") StyleSheet::located("/doc/tomorrow-night.css")
)) ))
.with_context(InContextOp::AddStyleSheet( .with_context(PageOp::AddStyleSheet(
StyleSheet::located("/doc/ayu-highlight.css") StyleSheet::located("/doc/ayu-highlight.css")
)) ))
.add_to( .add_to(