diff --git a/pagetop/src/base/component/block.rs b/pagetop/src/base/component/block.rs index 99950286..86370ff4 100644 --- a/pagetop/src/base/component/block.rs +++ b/pagetop/src/base/component/block.rs @@ -13,12 +13,12 @@ pub struct Block { impl PageComponent for Block { fn new() -> Self { Block { - renderable: always, + renderable: render_always, weight : 0, components: PageContainer::new(), - title : OptAttr::none(), - id : OptIden::none(), - classes : Classes::none(), + title : OptAttr::new(), + id : OptIden::new(), + classes : Classes::new_with_default("block"), template : "default".to_owned(), } } @@ -34,7 +34,7 @@ impl PageComponent for Block { fn default_render(&self, assets: &mut PageAssets) -> Markup { let id = assets.serial_id(self.name(), self.id()); html! { - div id=(id) class=[self.classes("block")] { + div id=(id) class=[self.classes()] { @match self.title() { Some(title) => h2 class="block-title" { (title) }, None => {} @@ -63,36 +63,63 @@ impl Block { // Block BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_title(mut self, title: &str) -> Self { - self.title.with_value(title); + self.alter_title(title); self } pub fn with_id(mut self, id: &str) -> Self { - self.id.with_value(id); + self.alter_id(id); self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); - self - } - - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); self } pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Block ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_title(&mut self, title: &str) -> &mut Self { + self.title.with_value(title); + self + } + + pub fn alter_id(&mut self, id: &str) -> &mut Self { + self.id.with_value(id); + self + } + + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); + self + } + + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -107,15 +134,11 @@ impl Block { self.id.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/chunck.rs b/pagetop/src/base/component/chunck.rs index 0e46c779..6dbc0ff1 100644 --- a/pagetop/src/base/component/chunck.rs +++ b/pagetop/src/base/component/chunck.rs @@ -10,7 +10,7 @@ pub struct Chunck { impl PageComponent for Chunck { fn new() -> Self { Chunck { - renderable: always, + renderable: render_always, weight : 0, html : html! {}, template : "default".to_owned(), @@ -32,24 +32,49 @@ impl PageComponent for Chunck { impl Chunck { pub fn with(html: Markup) -> Self { - let mut chunck = Chunck::new(); - chunck.html = html; - chunck + Chunck::new().with_html(html) } // Chunck BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); + self + } + + pub fn with_html(mut self, html: Markup) -> Self { + self.alter_html(html); self } pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Chunck ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_html(&mut self, html: Markup) -> &mut Self { + self.html = html; + self + } + + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -64,7 +89,3 @@ impl Chunck { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/container.rs b/pagetop/src/base/component/container.rs index 70162bb9..fab8225a 100644 --- a/pagetop/src/base/component/container.rs +++ b/pagetop/src/base/component/container.rs @@ -3,25 +3,27 @@ use crate::prelude::*; pub enum ContainerType { Header, Footer, Main, Section, Wrapper } pub struct Container { - renderable: fn() -> bool, - weight : i8, - components: PageContainer, - container : ContainerType, - id : OptIden, - classes : Classes, - template : String, + renderable : fn() -> bool, + weight : i8, + components : PageContainer, + container : ContainerType, + id : OptIden, + classes : Classes, + inner_classes: Classes, + template : String, } impl PageComponent for Container { fn new() -> Self { Container { - renderable: always, - weight : 0, - components: PageContainer::new(), - container : ContainerType::Wrapper, - id : OptIden::none(), - classes : Classes::none(), - template : "default".to_owned(), + renderable : render_always, + weight : 0, + components : PageContainer::new(), + container : ContainerType::Wrapper, + id : OptIden::new(), + classes : Classes::new_with_default("container"), + inner_classes: Classes::new_with_default("container"), + template : "default".to_owned(), } } @@ -36,35 +38,35 @@ impl PageComponent for Container { fn default_render(&self, assets: &mut PageAssets) -> Markup { match self.container_type() { ContainerType::Header => html! { - header id=[self.id()] class=[self.classes("header")] { - div class="container" { + header id=[self.id()] class=[self.classes()] { + div class=[self.inner_classes()] { (self.components().render(assets)) } } }, ContainerType::Footer => html! { - footer id=[self.id()] class=[self.classes("footer")] { - div class="container" { + footer id=[self.id()] class=[self.classes()] { + div class=[self.inner_classes()] { (self.components().render(assets)) } } }, ContainerType::Main => html! { - main id=[self.id()] class=[self.classes("main")] { - div class="container" { + main id=[self.id()] class=[self.classes()] { + div class=[self.inner_classes()] { (self.components().render(assets)) } } }, ContainerType::Section => html! { - section id=[self.id()] class=[self.classes("section")] { - div class="container" { + section id=[self.id()] class=[self.classes()] { + div class=[self.inner_classes()] { (self.components().render(assets)) } } }, _ => html! { - div id=[self.id()] class=[self.classes("container")] { + div id=[self.id()] class=[self.classes()] { (self.components().render(assets)) } } @@ -74,25 +76,25 @@ impl PageComponent for Container { impl Container { pub fn header() -> Self { - let mut c = Container::new(); + let mut c = Container::new().with_classes("header", ClassesOp::SetDefault); c.container = ContainerType::Header; c } pub fn footer() -> Self { - let mut c = Container::new(); + let mut c = Container::new().with_classes("footer", ClassesOp::SetDefault); c.container = ContainerType::Footer; c } pub fn main() -> Self { - let mut c = Container::new(); + let mut c = Container::new().with_classes("main", ClassesOp::SetDefault); c.container = ContainerType::Main; c } pub fn section() -> Self { - let mut c = Container::new(); + let mut c = Container::new().with_classes("section", ClassesOp::SetDefault); c.container = ContainerType::Section; c } @@ -111,31 +113,63 @@ impl Container { // Container BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_id(mut self, id: &str) -> Self { - self.id.with_value(id); + self.alter_id(id); self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); self } - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); + pub fn with_inner_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_inner_classes(classes, op); self } pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Container ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_id(&mut self, id: &str) -> &mut Self { + self.id.with_value(id); + self + } + + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); + self + } + + pub fn alter_inner_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.inner_classes.alter(classes, op); + self + } + + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -150,15 +184,15 @@ impl Container { self.id.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() + } + + pub fn inner_classes(&self) -> &Option { + self.inner_classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/form/button.rs b/pagetop/src/base/component/form/button.rs index f99f06d3..4c75dc62 100644 --- a/pagetop/src/base/component/form/button.rs +++ b/pagetop/src/base/component/form/button.rs @@ -17,16 +17,17 @@ pub struct Button { impl PageComponent for Button { fn new() -> Self { Button { - renderable : always, + renderable : render_always, weight : 0, button_type: ButtonType::Button, - name : OptAttr::none(), - value : OptAttr::none(), - autofocus : OptAttr::none(), - disabled : OptAttr::none(), - classes : Classes::none(), + name : OptAttr::new(), + value : OptAttr::new(), + autofocus : OptAttr::new(), + disabled : OptAttr::new(), + classes : Classes::new_with_default("btn btn-primary"), template : "default".to_owned(), } + .with_classes("form-button", ClassesOp::AddFirst) } fn is_renderable(&self) -> bool { @@ -38,10 +39,10 @@ impl PageComponent for Button { } fn default_render(&self, _: &mut PageAssets) -> Markup { - let (button_type, button_class) = match self.button_type() { - ButtonType::Button => ("button", "btn btn-primary form-button"), - ButtonType::Reset => ("reset", "btn btn-primary form-reset" ), - ButtonType::Submit => ("submit", "btn btn-primary form-submit"), + let button_type = match self.button_type() { + ButtonType::Button => "button", + ButtonType::Reset => "reset", + ButtonType::Submit => "submit", }; let id = match self.name() { Some(name) => Some(concat_string!("edit-", name)), @@ -51,7 +52,7 @@ impl PageComponent for Button { button type=(button_type) id=[id] - class=[self.classes(button_class)] + class=[self.classes()] name=[self.name()] value=[self.value()] autofocus=[self.autofocus()] @@ -72,13 +73,17 @@ impl Button { } pub fn reset(value: &str) -> Self { - let mut button = Button::new().with_value(value); + let mut button = Button::new() + .with_classes("form-reset", ClassesOp::Replace("form-button")) + .with_value(value); button.button_type = ButtonType::Reset; button } pub fn submit(value: &str) -> Self { - let mut button = Button::new().with_value(value); + let mut button = Button::new() + .with_classes("form-submit", ClassesOp::Replace("form-button")) + .with_value(value); button.button_type = ButtonType::Submit; button } @@ -86,26 +91,68 @@ impl Button { // Button BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_name(mut self, name: &str) -> Self { - self.name.with_value(name); + self.alter_name(name); self } pub fn with_value(mut self, value: &str) -> Self { - self.value.with_value(value); + self.alter_value(value); self } pub fn with_autofocus(mut self, toggle: bool) -> Self { + self.alter_autofocus(toggle); + self + } + + pub fn with_disabled(mut self, toggle: bool) -> Self { + self.alter_disabled(toggle); + self + } + + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); + self + } + + pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Button ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_name(&mut self, name: &str) -> &mut Self { + self.name.with_value(name); + self + } + + pub fn alter_value(&mut self, value: &str) -> &mut Self { + self.value.with_value(value); + self + } + + pub fn alter_autofocus(&mut self, toggle: bool) -> &mut Self { self.autofocus.with_value(match toggle { true => "autofocus", false => "", @@ -113,7 +160,7 @@ impl Button { self } - pub fn with_disabled(mut self, toggle: bool) -> Self { + pub fn alter_disabled(&mut self, toggle: bool) -> &mut Self { self.disabled.with_value(match toggle { true => "disabled", false => "", @@ -121,17 +168,12 @@ impl Button { self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); self } - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); - self - } - - pub fn using_template(mut self, template: &str) -> Self { + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -158,15 +200,11 @@ impl Button { self.disabled.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/form/date.rs b/pagetop/src/base/component/form/date.rs index 40c2f784..10e795cb 100644 --- a/pagetop/src/base/component/form/date.rs +++ b/pagetop/src/base/component/form/date.rs @@ -20,21 +20,22 @@ pub struct Date { impl PageComponent for Date { fn new() -> Self { Date { - renderable : always, + renderable : render_always, weight : 0, - name : OptAttr::none(), - value : OptAttr::none(), - label : OptAttr::none(), - placeholder : OptAttr::none(), - autofocus : OptAttr::none(), - autocomplete: OptAttr::none(), - disabled : OptAttr::none(), - readonly : OptAttr::none(), - required : OptAttr::none(), - help_text : OptAttr::none(), - classes : Classes::none(), + name : OptAttr::new(), + value : OptAttr::new(), + label : OptAttr::new(), + placeholder : OptAttr::new(), + autofocus : OptAttr::new(), + autocomplete: OptAttr::new(), + disabled : OptAttr::new(), + readonly : OptAttr::new(), + required : OptAttr::new(), + help_text : OptAttr::new(), + classes : Classes::new_with_default("form-item"), template : "default".to_owned(), } + .with_classes("form-type-date", ClassesOp::AddFirst) } fn is_renderable(&self) -> bool { @@ -46,18 +47,12 @@ impl PageComponent for Date { } fn default_render(&self, _: &mut PageAssets) -> Markup { - let (classes, id) = match self.name() { - Some(name) => ( - concat_string!("form-item form-item-", name, " form-type-date"), - Some(concat_string!("edit-", name)) - ), - None => ( - "form-item form-type-date".to_owned(), - None - ) + let id = match self.name() { + Some(name) => Some(concat_string!("edit-", name)), + None => None, }; html! { - div class=[self.classes(classes.as_str())] { + div class=[self.classes()] { @match self.label() { Some(label) => label class="form-label" for=[&id] { (label) " " @@ -96,36 +91,108 @@ impl Date { // Date BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_name(mut self, name: &str) -> Self { - self.name.with_value(name); + self.alter_name(name); self } pub fn with_value(mut self, value: &str) -> Self { - self.value.with_value(value); + self.alter_value(value); self } pub fn with_label(mut self, label: &str) -> Self { - self.label.with_value(label); + self.alter_label(label); self } pub fn with_placeholder(mut self, placeholder: &str) -> Self { - self.placeholder.with_value(placeholder); + self.alter_placeholder(placeholder); self } pub fn with_autofocus(mut self, toggle: bool) -> Self { + self.alter_autofocus(toggle); + self + } + + pub fn with_autocomplete(mut self, toggle: bool) -> Self { + self.alter_autocomplete(toggle); + self + } + + pub fn with_disabled(mut self, toggle: bool) -> Self { + self.alter_disabled(toggle); + self + } + + pub fn with_readonly(mut self, toggle: bool) -> Self { + self.alter_readonly(toggle); + self + } + + pub fn with_required(mut self, toggle: bool) -> Self { + self.alter_required(toggle); + self + } + + pub fn with_help_text(mut self, help_text: &str) -> Self { + self.alter_help_text(help_text); + self + } + + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); + self + } + + pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Date ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_name(&mut self, name: &str) -> &mut Self { + self.name.with_value(name); + self + } + + pub fn alter_value(&mut self, value: &str) -> &mut Self { + self.value.with_value(value); + self + } + + pub fn alter_label(&mut self, label: &str) -> &mut Self { + self.label.with_value(label); + self + } + + pub fn alter_placeholder(&mut self, placeholder: &str) -> &mut Self { + self.placeholder.with_value(placeholder); + self + } + + pub fn alter_autofocus(&mut self, toggle: bool) -> &mut Self { self.autofocus.with_value(match toggle { true => "autofocus", false => "", @@ -133,7 +200,7 @@ impl Date { self } - pub fn with_autocomplete(mut self, toggle: bool) -> Self { + pub fn alter_autocomplete(&mut self, toggle: bool) -> &mut Self { self.autocomplete.with_value(match toggle { true => "", false => "off", @@ -141,7 +208,7 @@ impl Date { self } - pub fn with_disabled(mut self, toggle: bool) -> Self { + pub fn alter_disabled(&mut self, toggle: bool) -> &mut Self { self.disabled.with_value(match toggle { true => "disabled", false => "", @@ -149,7 +216,7 @@ impl Date { self } - pub fn with_readonly(mut self, toggle: bool) -> Self { + pub fn alter_readonly(&mut self, toggle: bool) -> &mut Self { self.readonly.with_value(match toggle { true => "readonly", false => "", @@ -157,7 +224,7 @@ impl Date { self } - pub fn with_required(mut self, toggle: bool) -> Self { + pub fn alter_required(&mut self, toggle: bool) -> &mut Self { self.required.with_value(match toggle { true => "required", false => "", @@ -165,22 +232,17 @@ impl Date { self } - pub fn with_help_text(mut self, help_text: &str) -> Self { + pub fn alter_help_text(&mut self, help_text: &str) -> &mut Self { self.help_text.with_value(help_text); self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); self } - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); - self - } - - pub fn using_template(mut self, template: &str) -> Self { + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -227,15 +289,11 @@ impl Date { self.help_text.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/form/form.rs b/pagetop/src/base/component/form/form.rs index 07cb6e17..73581ca2 100644 --- a/pagetop/src/base/component/form/form.rs +++ b/pagetop/src/base/component/form/form.rs @@ -17,14 +17,14 @@ pub struct Form { impl PageComponent for Form { fn new() -> Self { Form { - renderable: always, + renderable: render_always, weight : 0, elements : PageContainer::new(), - action : OptAttr::none(), - charset : OptAttr::some("UTF-8"), + action : OptAttr::new(), + charset : OptAttr::new_with_value("UTF-8"), method : FormMethod::Post, - id : OptIden::none(), - classes : Classes::none(), + id : OptIden::new(), + classes : Classes::new_with_default("form"), template : "default".to_owned(), } } @@ -45,7 +45,7 @@ impl PageComponent for Form { html! { form id=[self.id()] - class=[self.classes("form")] + class=[self.classes()] action=[self.action()] method=[method] accept-charset=[self.charset()] @@ -72,46 +72,83 @@ impl Form { // Form BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_action(mut self, action: &str) -> Self { - self.action.with_value(action); + self.alter_action(action); self } pub fn with_charset(mut self, charset: &str) -> Self { - self.charset.with_value(charset); + self.alter_charset(charset); self } pub fn with_method(mut self, method: FormMethod) -> Self { - self.method = method; + self.alter_method(method); self } pub fn with_id(mut self, id: &str) -> Self { - self.id.with_value(id); + self.alter_id(id); self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); - self - } - - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); self } pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Form ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_action(&mut self, action: &str) -> &mut Self { + self.action.with_value(action); + self + } + + pub fn alter_charset(&mut self, charset: &str) -> &mut Self { + self.charset.with_value(charset); + self + } + + pub fn alter_method(&mut self, method: FormMethod) -> &mut Self { + self.method = method; + self + } + + pub fn alter_id(&mut self, id: &str) -> &mut Self { + self.id.with_value(id); + self + } + + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); + self + } + + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -134,15 +171,11 @@ impl Form { self.id.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/form/hidden.rs b/pagetop/src/base/component/form/hidden.rs index 8f2cc4b3..736aa4ce 100644 --- a/pagetop/src/base/component/form/hidden.rs +++ b/pagetop/src/base/component/form/hidden.rs @@ -10,8 +10,8 @@ impl PageComponent for Hidden { fn new() -> Self { Hidden { weight: 0, - name : OptIden::none(), - value : OptAttr::none(), + name : OptIden::new(), + value : OptAttr::new(), } } @@ -32,24 +32,39 @@ impl PageComponent for Hidden { impl Hidden { pub fn set(name: &str, value: &str) -> Self { - Hidden::new() - .with_name(name) - .with_value(value) + Hidden::new().with_name(name).with_value(value) } // Hidden BUILDER. pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_name(mut self, name: &str) -> Self { - self.name.with_value(name); + self.alter_name(name); self } pub fn with_value(mut self, value: &str) -> Self { + self.alter_value(value); + self + } + + // Hidden ALTER. + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_name(&mut self, name: &str) -> &mut Self { + self.name.with_value(name); + self + } + + pub fn alter_value(&mut self, value: &str) -> &mut Self { self.value.with_value(value); self } diff --git a/pagetop/src/base/component/form/input.rs b/pagetop/src/base/component/form/input.rs index 9d6e1afd..71d801b7 100644 --- a/pagetop/src/base/component/form/input.rs +++ b/pagetop/src/base/component/form/input.rs @@ -26,25 +26,26 @@ pub struct Input { impl PageComponent for Input { fn new() -> Self { Input { - renderable : always, + renderable : render_always, weight : 0, input_type : InputType::Textfield, - name : OptIden::none(), - value : OptAttr::none(), - label : OptAttr::none(), + name : OptIden::new(), + value : OptAttr::new(), + label : OptAttr::new(), size : Some(60), minlength : None, maxlength : Some(128), - placeholder : OptAttr::none(), - autofocus : OptAttr::none(), - autocomplete: OptAttr::none(), - disabled : OptAttr::none(), - readonly : OptAttr::none(), - required : OptAttr::none(), - help_text : OptAttr::none(), - classes : Classes::none(), + placeholder : OptAttr::new(), + autofocus : OptAttr::new(), + autocomplete: OptAttr::new(), + disabled : OptAttr::new(), + readonly : OptAttr::new(), + required : OptAttr::new(), + help_text : OptAttr::new(), + classes : Classes::new_with_default("form-item"), template : "default".to_owned(), } + .with_classes("form-type-textfield", ClassesOp::AddFirst) } fn is_renderable(&self) -> bool { @@ -55,27 +56,28 @@ impl PageComponent for Input { self.weight } + fn before_render(&mut self, _: &mut PageAssets) { + if let Some(name) = self.name() { + let class = concat_string!("form-item-", name); + self.alter_classes(class.as_str(), ClassesOp::AddFirst); + } + } + fn default_render(&self, _: &mut PageAssets) -> Markup { - let (type_input, type_class) = match self.input_type() { - InputType::Email => ("email", "form-type-email"), - InputType::Password => ("password", "form-type-password"), - InputType::Search => ("search", "form-type-search"), - InputType::Telephone => ("tel", "form-type-telephone"), - InputType::Textfield => ("text", "form-type-textfield"), - InputType::Url => ("url", "form-type-url") + let type_input = match self.input_type() { + InputType::Email => "email", + InputType::Password => "password", + InputType::Search => "search", + InputType::Telephone => "tel", + InputType::Textfield => "text", + InputType::Url => "url", }; - let (class, id) = match self.name() { - Some(name) => ( - concat_string!("form-item form-item-", name, " ", type_class), - Some(concat_string!("edit-", name)) - ), - None => ( - concat_string!("form-item ", type_class), - None - ) + let id = match self.name() { + Some(name) => Some(concat_string!("edit-", name)), + None => None, }; html! { - div class=(class) { + div class=[self.classes()] { @match self.label() { Some(label) => label class="form-label" for=[&id] { (label) " " @@ -118,31 +120,36 @@ impl Input { } pub fn password() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-password", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Password; input } pub fn search() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-search", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Search; input } pub fn email() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-email", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Email; input } pub fn telephone() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-telephone", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Telephone; input } pub fn url() -> Self { - let mut input = Input::new(); + let mut input = Input::new() + .with_classes("form-type-url", ClassesOp::Replace("form-type-textfield")); input.input_type = InputType::Url; input } @@ -150,51 +157,138 @@ impl Input { // Input BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_name(mut self, name: &str) -> Self { - self.name.with_value(name); + self.alter_name(name); self } pub fn with_value(mut self, value: &str) -> Self { - self.value.with_value(value); + self.alter_value(value); self } pub fn with_label(mut self, label: &str) -> Self { - self.label.with_value(label); + self.alter_label(label); self } pub fn with_size(mut self, size: Option) -> Self { - self.size = size; + self.alter_size(size); self } pub fn with_minlength(mut self, minlength: Option) -> Self { - self.minlength = minlength; + self.alter_minlength(minlength); self } pub fn with_maxlength(mut self, maxlength: Option) -> Self { - self.maxlength = maxlength; + self.alter_maxlength(maxlength); self } pub fn with_placeholder(mut self, placeholder: &str) -> Self { - self.placeholder.with_value(placeholder); + self.alter_placeholder(placeholder); self } pub fn with_autofocus(mut self, toggle: bool) -> Self { + self.alter_autofocus(toggle); + self + } + + pub fn with_autocomplete(mut self, toggle: bool) -> Self { + self.alter_autocomplete(toggle); + self + } + + pub fn with_disabled(mut self, toggle: bool) -> Self { + self.alter_disabled(toggle); + self + } + + pub fn with_readonly(mut self, toggle: bool) -> Self { + self.alter_readonly(toggle); + self + } + + pub fn with_required(mut self, toggle: bool) -> Self { + self.alter_required(toggle); + self + } + + pub fn with_help_text(mut self, help_text: &str) -> Self { + self.alter_help_text(help_text); + self + } + + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); + self + } + + pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Input ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_name(&mut self, name: &str) -> &mut Self { + self.name.with_value(name); + self + } + + pub fn alter_value(&mut self, value: &str) -> &mut Self { + self.value.with_value(value); + self + } + + pub fn alter_label(&mut self, label: &str) -> &mut Self { + self.label.with_value(label); + self + } + + pub fn alter_size(&mut self, size: Option) -> &mut Self { + self.size = size; + self + } + + pub fn alter_minlength(&mut self, minlength: Option) -> &mut Self { + self.minlength = minlength; + self + } + + pub fn alter_maxlength(&mut self, maxlength: Option) -> &mut Self { + self.maxlength = maxlength; + self + } + + pub fn alter_placeholder(&mut self, placeholder: &str) -> &mut Self { + self.placeholder.with_value(placeholder); + self + } + + pub fn alter_autofocus(&mut self, toggle: bool) -> &mut Self { self.autofocus.with_value(match toggle { true => "autofocus", false => "", @@ -202,7 +296,7 @@ impl Input { self } - pub fn with_autocomplete(mut self, toggle: bool) -> Self { + pub fn alter_autocomplete(&mut self, toggle: bool) -> &mut Self { self.autocomplete.with_value(match toggle { true => "", false => "off", @@ -210,7 +304,7 @@ impl Input { self } - pub fn with_disabled(mut self, toggle: bool) -> Self { + pub fn alter_disabled(&mut self, toggle: bool) -> &mut Self { self.disabled.with_value(match toggle { true => "disabled", false => "", @@ -218,7 +312,7 @@ impl Input { self } - pub fn with_readonly(mut self, toggle: bool) -> Self { + pub fn alter_readonly(&mut self, toggle: bool) -> &mut Self { self.readonly.with_value(match toggle { true => "readonly", false => "", @@ -226,7 +320,7 @@ impl Input { self } - pub fn with_required(mut self, toggle: bool) -> Self { + pub fn alter_required(&mut self, toggle: bool) -> &mut Self { self.required.with_value(match toggle { true => "required", false => "", @@ -234,22 +328,17 @@ impl Input { self } - pub fn with_help_text(mut self, help_text: &str) -> Self { + pub fn alter_help_text(&mut self, help_text: &str) -> &mut Self { self.help_text.with_value(help_text); self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); self } - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); - self - } - - pub fn using_template(mut self, template: &str) -> Self { + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -312,15 +401,11 @@ impl Input { self.help_text.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/grid/column.rs b/pagetop/src/base/component/grid/column.rs index f29544ca..77512ea2 100644 --- a/pagetop/src/base/component/grid/column.rs +++ b/pagetop/src/base/component/grid/column.rs @@ -12,11 +12,11 @@ pub struct Column { impl PageComponent for Column { fn new() -> Self { Column { - renderable: always, + renderable: render_always, weight : 0, components: PageContainer::new(), - id : OptIden::none(), - classes : Classes::none(), + id : OptIden::new(), + classes : Classes::new_with_default("col"), template : "default".to_owned(), } } @@ -35,7 +35,7 @@ impl PageComponent for Column { fn default_render(&self, assets: &mut PageAssets) -> Markup { html! { - div id=[self.id()] class=[self.classes("col")] { + div id=[self.id()] class=[self.classes()] { (self.components().render(assets)) } } @@ -58,31 +58,53 @@ impl Column { // Column BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_id(mut self, id: &str) -> Self { - self.id.with_value(id); + self.alter_id(id); self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); - self - } - - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); self } pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Column ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_id(&mut self, id: &str) -> &mut Self { + self.id.with_value(id); + self + } + + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); + self + } + + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -93,15 +115,11 @@ impl Column { self.id.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/grid/row.rs b/pagetop/src/base/component/grid/row.rs index 96d74afc..4574b090 100644 --- a/pagetop/src/base/component/grid/row.rs +++ b/pagetop/src/base/component/grid/row.rs @@ -12,11 +12,11 @@ pub struct Row { impl PageComponent for Row { fn new() -> Self { Row { - renderable: always, + renderable: render_always, weight : 0, columns : PageContainer::new(), - id : OptIden::none(), - classes : Classes::none(), + id : OptIden::new(), + classes : Classes::new_with_default("row"), template : "default".to_owned(), } } @@ -35,7 +35,7 @@ impl PageComponent for Row { fn default_render(&self, assets: &mut PageAssets) -> Markup { html! { - div id=[self.id()] class=[self.classes("row")] { + div id=[self.id()] class=[self.classes()] { (self.columns().render(assets)) } } @@ -58,36 +58,53 @@ impl Row { // Row BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_id(mut self, id: &str) -> Self { - self.id.with_value(id); + self.alter_id(id); self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); - self - } - - pub fn set_classes_ref(&mut self, classes: &str) -> &Self { - self.classes.set_classes(classes); - self - } - - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); self } pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Row ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_id(&mut self, id: &str) -> &mut Self { + self.id.with_value(id); + self + } + + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); + self + } + + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -98,15 +115,11 @@ impl Row { self.id.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/image.rs b/pagetop/src/base/component/image.rs index 1384f4df..62690679 100644 --- a/pagetop/src/base/component/image.rs +++ b/pagetop/src/base/component/image.rs @@ -12,11 +12,11 @@ pub struct Image { impl PageComponent for Image { fn new() -> Self { Image { - renderable: always, + renderable: render_always, weight : 0, - source : OptAttr::none(), - id : OptIden::none(), - classes : Classes::none(), + source : OptAttr::new(), + id : OptIden::new(), + classes : Classes::new_with_default("img-fluid"), template : "default".to_owned(), } } @@ -34,7 +34,7 @@ impl PageComponent for Image { img src=[self.source()] id=[self.id()] - class=[self.classes("img-fluid")]; + class=[self.classes()]; } } } @@ -47,36 +47,63 @@ impl Image { // Image BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_source(mut self, source: &str) -> Self { - self.source.with_value(source); + self.alter_source(source); self } pub fn with_id(mut self, id: &str) -> Self { - self.id.with_value(id); + self.alter_id(id); self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); - self - } - - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); self } pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Image ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_source(&mut self, source: &str) -> &mut Self { + self.source.with_value(source); + self + } + + pub fn alter_id(&mut self, id: &str) -> &mut Self { + self.id.with_value(id); + self + } + + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); + self + } + + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -91,15 +118,11 @@ impl Image { self.id.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/component/menu.rs b/pagetop/src/base/component/menu.rs index 01a60129..68c5c97e 100644 --- a/pagetop/src/base/component/menu.rs +++ b/pagetop/src/base/component/menu.rs @@ -23,7 +23,7 @@ pub struct MenuItem { impl PageComponent for MenuItem { fn new() -> Self { MenuItem { - renderable: always, + renderable: render_always, weight : 0, item_type : MenuItemType::Void, } @@ -72,7 +72,7 @@ impl PageComponent for MenuItem { impl MenuItem { pub fn label(label: &str) -> Self { MenuItem { - renderable: always, + renderable: render_always, weight : 0, item_type : MenuItemType::Label(label.to_owned()), } @@ -80,7 +80,7 @@ impl MenuItem { pub fn link(label: &str, path: &str) -> Self { MenuItem { - renderable: always, + renderable: render_always, weight : 0, item_type : MenuItemType::Link( label.to_owned(), @@ -91,7 +91,7 @@ impl MenuItem { pub fn link_blank(label: &str, path: &str) -> Self { MenuItem { - renderable: always, + renderable: render_always, weight : 0, item_type : MenuItemType::LinkBlank( label.to_owned(), @@ -102,7 +102,7 @@ impl MenuItem { pub fn html(html: Markup) -> Self { MenuItem { - renderable: always, + renderable: render_always, weight : 0, item_type : MenuItemType::Html(html), } @@ -110,7 +110,7 @@ impl MenuItem { pub fn separator() -> Self { MenuItem { - renderable: always, + renderable: render_always, weight : 0, item_type : MenuItemType::Separator, } @@ -118,7 +118,7 @@ impl MenuItem { pub fn submenu(label: &str, menu: Menu) -> Self { MenuItem { - renderable: always, + renderable: render_always, weight : 0, item_type : MenuItemType::Submenu( label.to_owned(), @@ -130,11 +130,23 @@ impl MenuItem { // MenuItem BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { + self.alter_weight(weight); + self + } + + // MenuItem ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { self.weight = weight; self } @@ -162,11 +174,11 @@ pub struct Menu { impl PageComponent for Menu { fn new() -> Self { Menu { - renderable: always, + renderable: render_always, weight : 0, items : PageContainer::new(), - id : OptIden::none(), - classes : Classes::none(), + id : OptIden::new(), + classes : Classes::new_with_default("sm sm-clean"), template : "default".to_owned(), } } @@ -194,7 +206,7 @@ impl PageComponent for Menu { let id = assets.serial_id(self.name(), self.id()); html! { - ul id=(id) class=[self.classes("sm sm-clean")] { + ul id=(id) class=[self.classes()] { (self.items().render(assets)) } script type="text/javascript" defer { @@ -223,31 +235,53 @@ impl Menu { // Menu BUILDER. pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self { - self.renderable = renderable; + self.alter_renderable(renderable); self } pub fn with_weight(mut self, weight: i8) -> Self { - self.weight = weight; + self.alter_weight(weight); self } pub fn with_id(mut self, id: &str) -> Self { - self.id.with_value(id); + self.alter_id(id); self } - pub fn set_classes(mut self, classes: &str) -> Self { - self.classes.set_classes(classes); - self - } - - pub fn add_classes(mut self, classes: &str) -> Self { - self.classes.add_classes(classes); + pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self { + self.alter_classes(classes, op); self } pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Menu ALTER. + + pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { + self.renderable = renderable; + self + } + + pub fn alter_weight(&mut self, weight: i8) -> &mut Self { + self.weight = weight; + self + } + + pub fn alter_id(&mut self, id: &str) -> &mut Self { + self.id.with_value(id); + self + } + + pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.classes.alter(classes, op); + self + } + + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } @@ -258,15 +292,11 @@ impl Menu { self.id.option() } - pub fn classes(&self, default: &str) -> Option { - self.classes.option(default) + pub fn classes(&self) -> &Option { + self.classes.option() } pub fn template(&self) -> &str { self.template.as_str() } } - -fn always() -> bool { - true -} diff --git a/pagetop/src/base/theme/bulmix/mod.rs b/pagetop/src/base/theme/bulmix/mod.rs index cf83a8f6..c7aa3cb2 100644 --- a/pagetop/src/base/theme/bulmix/mod.rs +++ b/pagetop/src/base/theme/bulmix/mod.rs @@ -32,33 +32,21 @@ impl ThemeTrait for BulmixTheme { .add_jquery(); } - fn render_component( + fn before_render_component( &self, component: &mut dyn PageComponent, - assets: &mut PageAssets - ) -> Option { + _assets: &mut PageAssets + ) { match component.name() { "GridRow" => { let row = component.downcast_mut::().unwrap(); - row.set_classes_ref("Prueba"); - None - /* - Some(html! { - div id=[row.id()] class=[row.classes("columns")] { - (row.columns().render(assets)) - } - }) - */ + row.alter_classes("columns", ClassesOp::SetDefault); }, "GridColumn" => { - let col = component.downcast_ref::().unwrap(); - Some(html! { - div id=[col.id()] class=[col.classes("column")] { - (col.components().render(assets)) - } - }) + let col = component.downcast_mut::().unwrap(); + col.alter_classes("column", ClassesOp::SetDefault); }, - _ => None + _ => {}, } } } diff --git a/pagetop/src/html/classes.rs b/pagetop/src/html/classes.rs index 6c41aafb..225d6537 100644 --- a/pagetop/src/html/classes.rs +++ b/pagetop/src/html/classes.rs @@ -1,23 +1,82 @@ use crate::concat_string; -pub struct Classes(String); +pub enum ClassesOp { + Add, + AddAfter(&'static str), + AddBefore(&'static str), + AddFirst, + Replace(&'static str), + Reset, + SetDefault, +} + +pub struct Classes { + default: String, + added : String, + option : Option, +} impl Classes { - pub fn none() -> Self { - Classes("".to_owned()) + pub fn new() -> Self { + Classes { + default: "".to_owned(), + added : "".to_owned(), + option : None, + } } - pub fn set_classes(&mut self, classes: &str) -> &mut Self { - self.0 = classes.to_owned(); + pub fn new_with_default(default: &str) -> Self { + let mut classes = Self::new(); + classes.alter(default, ClassesOp::SetDefault); + classes + } + + pub fn alter(&mut self, classes: &str, op: ClassesOp) -> &Self { + let classes = classes.trim(); + match op { + ClassesOp::Add => self.added.push_str(concat_string!(" ", classes).as_str()), + + ClassesOp::AddAfter(class) => { + let mut v_added: Vec<&str> = self.added.split_ascii_whitespace().collect(); + match v_added.iter().position(|c| c.eq(&class)) { + Some(pos) => v_added.insert(pos + 1, classes), + _ => v_added.push(classes), + } + self.added = v_added.join(" "); + }, + + ClassesOp::AddBefore(class) => { + let mut v_added: Vec<&str> = self.added.split_ascii_whitespace().collect(); + match v_added.iter().position(|c| c.eq(&class)) { + Some(pos) => v_added.insert(pos, classes), + _ => v_added.insert(0, classes), + } + self.added = v_added.join(" "); + }, + + ClassesOp::AddFirst => self.added = concat_string!(classes, " ", self.added), + + ClassesOp::Replace(class) => { + let mut v_added: Vec<&str> = self.added.split_ascii_whitespace().collect(); + match v_added.iter().position(|c| c.eq(&class)) { + Some(pos) => { + v_added.remove(pos); + v_added.insert(pos, classes); + }, + _ => v_added.push(classes), + } + self.added = v_added.join(" "); + }, + + ClassesOp::Reset => self.added = classes.to_owned(), + + ClassesOp::SetDefault => self.default = classes.to_owned(), + } + self.option = Some(concat_string!(self.default, " ", self.added).trim().to_owned()); self } - pub fn add_classes(&mut self, classes: &str) -> &mut Self { - self.0 = concat_string!(self.0, " ", classes).trim().to_owned(); - self - } - - pub fn option(&self, default: &str) -> Option { - Some(concat_string!(default.to_owned(), " ", self.0).trim().to_owned()) + pub fn option(&self) -> &Option { + &self.option } } diff --git a/pagetop/src/html/mod.rs b/pagetop/src/html/mod.rs index 52f735fe..56e5c6d2 100644 --- a/pagetop/src/html/mod.rs +++ b/pagetop/src/html/mod.rs @@ -5,4 +5,4 @@ pub use optiden::OptIden; mod optattr; pub use optattr::OptAttr; mod classes; -pub use classes::Classes; +pub use classes::{Classes, ClassesOp}; diff --git a/pagetop/src/html/optattr.rs b/pagetop/src/html/optattr.rs index d009e32e..86c21e8c 100644 --- a/pagetop/src/html/optattr.rs +++ b/pagetop/src/html/optattr.rs @@ -1,14 +1,14 @@ pub struct OptAttr(Option); impl OptAttr { - pub fn none() -> Self { + pub fn new() -> Self { OptAttr(None) } - pub fn some(value: &str) -> Self { - let mut o = OptAttr(None); - o.with_value(value); - o + pub fn new_with_value(value: &str) -> Self { + let mut option = Self::new(); + option.with_value(value); + option } pub fn with_value(&mut self, value: &str) -> &mut Self { diff --git a/pagetop/src/html/optiden.rs b/pagetop/src/html/optiden.rs index 52a65c10..450867d9 100644 --- a/pagetop/src/html/optiden.rs +++ b/pagetop/src/html/optiden.rs @@ -1,17 +1,17 @@ pub struct OptIden(Option); impl OptIden { - pub fn none() -> Self { + pub fn new() -> Self { OptIden(None) } - pub fn some(id: &str) -> Self { - let mut o = OptIden(None); - o.with_value(id); - o + pub fn new_with_value(id: &str) -> Self { + let mut option = Self::new(); + option.with_value(id); + option } - pub fn with_value(&mut self, id: &str) -> &mut Self { + pub fn with_value(&mut self, id: &str) -> &Self { let id = id.trim(); self.0 = match id.is_empty() { true => None, diff --git a/pagetop/src/response/page/assets.rs b/pagetop/src/response/page/assets.rs index 3d42a06f..8ab4d2eb 100644 --- a/pagetop/src/response/page/assets.rs +++ b/pagetop/src/response/page/assets.rs @@ -257,7 +257,7 @@ impl PageAssets { /// Assets GETTERS. - pub fn theme(&mut self) -> &'static dyn ThemeTrait { + pub(crate) fn theme(&mut self) -> &'static dyn ThemeTrait { self.theme } diff --git a/pagetop/src/response/page/component.rs b/pagetop/src/response/page/component.rs index 630b8a9b..05528fc8 100644 --- a/pagetop/src/response/page/component.rs +++ b/pagetop/src/response/page/component.rs @@ -33,6 +33,10 @@ pub trait PageComponent: Downcast + Send + Sync { 0 } + #[allow(unused_variables)] + fn before_render(&mut self, assets: &mut PageAssets) { + } + #[allow(unused_variables)] fn default_render(&self, assets: &mut PageAssets) -> Markup { html! {} diff --git a/pagetop/src/response/page/mod.rs b/pagetop/src/response/page/mod.rs index f34070e2..527f74f9 100644 --- a/pagetop/src/response/page/mod.rs +++ b/pagetop/src/response/page/mod.rs @@ -17,3 +17,6 @@ pub use page::Page; pub use page::render_component; pub use page::add_component_to; +pub fn render_always() -> bool { true } + +pub fn render_never() -> bool { false } \ No newline at end of file diff --git a/pagetop/src/response/page/page.rs b/pagetop/src/response/page/page.rs index 56db5ecc..f7bee609 100644 --- a/pagetop/src/response/page/page.rs +++ b/pagetop/src/response/page/page.rs @@ -1,6 +1,6 @@ use crate::{Lazy, app, trace}; use crate::config::SETTINGS; -use crate::html::{Classes, DOCTYPE, Markup, OptAttr, html}; +use crate::html::*; use crate::response::page::*; use std::sync::RwLock; @@ -55,18 +55,18 @@ impl<'a> Page<'a> { pub fn new() -> Self { Page { language : match &*DEFAULT_LANGUAGE { - Some(language) => OptAttr::some(language), - _ => OptAttr::none(), + Some(language) => OptAttr::new_with_value(language), + _ => OptAttr::new(), }, direction : match &*DEFAULT_DIRECTION { - Some(direction) => OptAttr::some(direction), - _ => OptAttr::none(), + Some(direction) => OptAttr::new_with_value(direction), + _ => OptAttr::new(), }, - title : OptAttr::none(), - description : OptAttr::none(), + title : OptAttr::new(), + description : OptAttr::new(), assets : PageAssets::new(), regions : COMPONENTS.read().unwrap().clone(), - body_classes: Classes::none(), + body_classes: Classes::new_with_default("body"), template : "default".to_owned(), } } @@ -110,13 +110,8 @@ impl<'a> Page<'a> { self } - pub fn set_body_classes(&mut self, classes: &str) -> &mut Self { - self.body_classes.set_classes(classes); - self - } - - pub fn add_body_classes(&mut self, classes: &str) -> &mut Self { - self.body_classes.add_classes(classes); + pub fn alter_body_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self { + self.body_classes.alter(classes, op); self } @@ -147,8 +142,8 @@ impl<'a> Page<'a> { &mut self.assets } - pub fn body_classes(&self, default: &str) -> Option { - self.body_classes.option(default) + pub fn body_classes(&self) -> &Option { + self.body_classes.option() } pub fn template(&self) -> &str { @@ -192,14 +187,15 @@ impl<'a> Page<'a> { } } -pub fn render_component( - component: &mut dyn PageComponent, - assets: &mut PageAssets -) -> Markup { +pub fn render_component(component: &mut dyn PageComponent, assets: &mut PageAssets) -> Markup { + component.before_render(assets); + assets.theme().before_render_component(component, assets); match component.is_renderable() { - true => match assets.theme().render_component(component, assets) { - Some(html) => html, - None => component.default_render(assets) + true => { + match assets.theme().render_component(component, assets) { + Some(html) => html, + None => component.default_render(assets) + } }, false => html! {} } diff --git a/pagetop/src/theme/definition.rs b/pagetop/src/theme/definition.rs index 457860c7..983bf0a6 100644 --- a/pagetop/src/theme/definition.rs +++ b/pagetop/src/theme/definition.rs @@ -55,7 +55,7 @@ pub trait ThemeTrait: Send + Sync { fn render_page_body(&self, page: &mut Page) -> Markup { html! { - body class=[page.body_classes("body")] { + body class=[page.body_classes()] { @match page.template() { "admin" => { @for region in &["top-menu", "side-menu", "content"] { @@ -75,10 +75,29 @@ pub trait ThemeTrait: Send + Sync { } #[allow(unused_variables)] - fn render_component( + fn before_render_component( &self, component: &mut dyn PageComponent, assets: &mut PageAssets + ) { + /* + Cómo usarlo: + + match component.name() { + "Block" => { + let block = component.downcast_mut::().unwrap(); + block.alter_title("New title"); + }, + _ => {}, + } + */ + } + + #[allow(unused_variables)] + fn render_component( + &self, + component: &dyn PageComponent, + assets: &mut PageAssets ) -> Option { None /* @@ -89,10 +108,10 @@ pub trait ThemeTrait: Send + Sync { let block = component.downcast_ref::().unwrap(); match block.template() { "default" => Some(block_default(block)), - _ => None + _ => None, } }, - _ => None + _ => None, } */ }