🧑‍💻 Replace "prepare" with "render" for better

This commit is contained in:
Manuel Cillero 2023-11-03 17:18:41 +01:00
parent 80139d9153
commit 306cf5dc43
18 changed files with 40 additions and 70 deletions

View file

@ -49,7 +49,7 @@ impl ComponentTrait for Block {
h2 class="block-title" { (title) } h2 class="block-title" { (title) }
} }
div class="block-body" { div class="block-body" {
(self.components().prepare(cx)) (self.components().render(cx))
} }
} }
}) })

View file

@ -55,7 +55,7 @@ impl ComponentTrait for Branding {
div id=[self.id()] { div id=[self.id()] {
div class="pt-branding__wrapper" { div class="pt-branding__wrapper" {
div class="pt-branding__logo" { div class="pt-branding__logo" {
(self.logo().prepare(cx)) (self.logo().render(cx))
} }
div class="pt-branding__text" { div class="pt-branding__text" {
div class="pt-branding__name" { div class="pt-branding__name" {

View file

@ -54,7 +54,7 @@ impl ComponentTrait for Container {
PrepareMarkup::With(html! { PrepareMarkup::With(html! {
div id=[self.id()] class=[self.classes().get()] style=[gap] { div id=[self.id()] class=[self.classes().get()] style=[gap] {
(self.items().prepare(cx)) (self.items().render(cx))
} }
}) })
} }

View file

@ -55,7 +55,7 @@ impl ComponentTrait for Item {
PrepareMarkup::With(html! { PrepareMarkup::With(html! {
div id=[self.id()] class=[self.item_classes().get()] style=[order] { div id=[self.id()] class=[self.item_classes().get()] style=[order] {
div class=[self.inner_classes().get()] { div class=[self.inner_classes().get()] {
(self.components().prepare(cx)) (self.components().render(cx))
} }
} }
}) })

View file

@ -65,7 +65,7 @@ impl ComponentTrait for Form {
method=[method] method=[method]
accept-charset=[self.charset().get()] accept-charset=[self.charset().get()]
{ {
div { (self.elements().prepare(cx)) } div { (self.elements().render(cx)) }
} }
}) })
} }

View file

@ -46,10 +46,10 @@ impl ComponentTrait for Element {
match self.element_type() { match self.element_type() {
ElementType::Void => PrepareMarkup::None, ElementType::Void => PrepareMarkup::None,
ElementType::Html(content) => PrepareMarkup::With(html! { ElementType::Html(content) => PrepareMarkup::With(html! {
(content.prepare(cx)) (content.render(cx))
}), }),
ElementType::Submenu(submenu) => PrepareMarkup::With(html! { ElementType::Submenu(submenu) => PrepareMarkup::With(html! {
(submenu.prepare(cx)) (submenu.render(cx))
}), }),
} }
} }

View file

@ -39,7 +39,7 @@ impl ComponentTrait for Group {
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
PrepareMarkup::With(html! { PrepareMarkup::With(html! {
div id=[self.id()] class="menu-group" { div id=[self.id()] class="menu-group" {
(self.elements().prepare(cx)) (self.elements().render(cx))
} }
}) })
} }

View file

@ -76,7 +76,7 @@ impl ComponentTrait for Item {
}), }),
ItemType::Html(content) => PrepareMarkup::With(html! { ItemType::Html(content) => PrepareMarkup::With(html! {
li class="pt-menu__html" { li class="pt-menu__html" {
(content.prepare(cx)) (content.render(cx))
} }
}), }),
ItemType::Submenu(label, submenu) => PrepareMarkup::With(html! { ItemType::Submenu(label, submenu) => PrepareMarkup::With(html! {
@ -85,7 +85,7 @@ impl ComponentTrait for Item {
(label.escaped(cx.langid())) i class="pt-menu__icon bi-chevron-down" {} (label.escaped(cx.langid())) i class="pt-menu__icon bi-chevron-down" {}
} }
div class="pt-menu__subs" { div class="pt-menu__subs" {
(submenu.prepare(cx)) (submenu.render(cx))
} }
} }
}), }),
@ -95,7 +95,7 @@ impl ComponentTrait for Item {
(label.escaped(cx.langid())) i class="pt-menu__icon bi-chevron-down" {} (label.escaped(cx.langid())) i class="pt-menu__icon bi-chevron-down" {}
} }
div class="pt-menu__subs pt-menu__mega" { div class="pt-menu__subs pt-menu__mega" {
(megamenu.prepare(cx)) (megamenu.render(cx))
} }
} }
}), }),

View file

@ -39,7 +39,7 @@ impl ComponentTrait for Megamenu {
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
PrepareMarkup::With(html! { PrepareMarkup::With(html! {
div id=[self.id()] class="pt-menu__groups" { div id=[self.id()] class="pt-menu__groups" {
(self.groups().prepare(cx)) (self.groups().render(cx))
} }
}) })
} }

View file

@ -60,7 +60,7 @@ impl ComponentTrait for Menu {
} }
} }
ul class="pt-menu__section" { ul class="pt-menu__section" {
(self.items().prepare(cx)) (self.items().render(cx))
} }
} }
} }

View file

@ -44,7 +44,7 @@ impl ComponentTrait for Submenu {
h4 class="pt-menu__title" { (title) } h4 class="pt-menu__title" { (title) }
} }
ul { ul {
(self.items().prepare(cx)) (self.items().render(cx))
} }
} }
}) })

View file

@ -40,7 +40,7 @@ impl ComponentTrait for Paragraph {
id=[self.id()] id=[self.id()]
class=[self.classes().get()] class=[self.classes().get()]
{ {
(self.components().prepare(cx)) (self.components().render(cx))
} }
}) })
} }

View file

@ -59,34 +59,34 @@ impl ComponentTrait for Wrapper {
WrapperType::Header => PrepareMarkup::With(html! { WrapperType::Header => PrepareMarkup::With(html! {
header id=[self.id()] class=[self.classes().get()] { header id=[self.id()] class=[self.classes().get()] {
div class=[self.inner_classes().get()] { div class=[self.inner_classes().get()] {
(self.components().prepare(cx)) (self.components().render(cx))
} }
} }
}), }),
WrapperType::Footer => PrepareMarkup::With(html! { WrapperType::Footer => PrepareMarkup::With(html! {
footer id=[self.id()] class=[self.classes().get()] { footer id=[self.id()] class=[self.classes().get()] {
div class=[self.inner_classes().get()] { div class=[self.inner_classes().get()] {
(self.components().prepare(cx)) (self.components().render(cx))
} }
} }
}), }),
WrapperType::Main => PrepareMarkup::With(html! { WrapperType::Main => PrepareMarkup::With(html! {
main id=[self.id()] class=[self.classes().get()] { main id=[self.id()] class=[self.classes().get()] {
div class=[self.inner_classes().get()] { div class=[self.inner_classes().get()] {
(self.components().prepare(cx)) (self.components().render(cx))
} }
} }
}), }),
WrapperType::Section => PrepareMarkup::With(html! { WrapperType::Section => PrepareMarkup::With(html! {
section id=[self.id()] class=[self.classes().get()] { section id=[self.id()] class=[self.classes().get()] {
div class=[self.inner_classes().get()] { div class=[self.inner_classes().get()] {
(self.components().prepare(cx)) (self.components().render(cx))
} }
} }
}), }),
_ => PrepareMarkup::With(html! { _ => PrepareMarkup::With(html! {
div id=[self.id()] class=[self.classes().get()] { div id=[self.id()] class=[self.classes().get()] {
(self.components().prepare(cx)) (self.components().render(cx))
} }
}), }),
} }

View file

@ -61,10 +61,10 @@ impl ArcComponent {
self.0.read().unwrap().weight() self.0.read().unwrap().weight()
} }
// ArcComponent PREPARE. // ArcComponent RENDER.
pub fn prepare(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().prepare(cx) self.0.write().unwrap().render(cx)
} }
} }
@ -150,14 +150,14 @@ impl ArcComponents {
self.0.iter().filter(move |&c| c.handle() == handle) self.0.iter().filter(move |&c| c.handle() == handle)
} }
// ArcComponents PREPARE. // ArcComponents RENDER.
pub fn prepare(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
let mut components = self.0.clone(); let mut components = self.0.clone();
components.sort_by_key(|c| c.weight()); components.sort_by_key(|c| c.weight());
html! { html! {
@for c in components.iter() { @for c in components.iter() {
" " (c.prepare(cx)) " " " " (c.render(cx)) " "
} }
} }
} }

View file

@ -5,7 +5,7 @@ use crate::{util, Handle, Weight};
use std::any::Any; use std::any::Any;
pub trait ComponentBase: Any { pub trait ComponentBase: Any {
fn prepare(&mut self, cx: &mut Context) -> Markup; fn render(&mut self, cx: &mut Context) -> Markup;
fn as_ref_any(&self) -> &dyn Any; fn as_ref_any(&self) -> &dyn Any;
@ -53,7 +53,7 @@ pub trait ComponentTrait: ComponentBase + Send + Sync {
} }
impl<C: ComponentTrait> ComponentBase for C { impl<C: ComponentTrait> ComponentBase for C {
fn prepare(&mut self, cx: &mut Context) -> Markup { fn render(&mut self, cx: &mut Context) -> Markup {
if self.is_renderable(cx) { if self.is_renderable(cx) {
// Acciones antes de preparar el componente. // Acciones antes de preparar el componente.
self.before_prepare_component(cx); self.before_prepare_component(cx);
@ -63,7 +63,11 @@ impl<C: ComponentTrait> ComponentBase for C {
let markup = match cx.theme().render_component(self, cx) { let markup = match cx.theme().render_component(self, cx) {
Some(html) => html, Some(html) => html,
None => self.prepare_component(cx).html(), None => match self.prepare_component(cx) {
PrepareMarkup::None => html! {},
PrepareMarkup::Text(text) => html! { (text) },
PrepareMarkup::With(html) => html,
},
}; };
// Acciones después de preparar el componente. // Acciones después de preparar el componente.

View file

@ -46,10 +46,10 @@ impl<T: ComponentTrait + Default> TypedComponent<T> {
self.0.read().unwrap().weight() self.0.read().unwrap().weight()
} }
// TypedComponent PREPARE. // TypedComponent RENDER.
pub fn prepare(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().prepare(cx) self.0.write().unwrap().render(cx)
} }
} }
@ -127,14 +127,14 @@ impl<T: ComponentTrait + Default> TypedComponents<T> {
self.0.iter().filter(move |&c| c.handle() == handle) self.0.iter().filter(move |&c| c.handle() == handle)
} }
// TypedComponents PREPARE. // TypedComponents RENDER.
pub fn prepare(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
let mut components = self.0.clone(); let mut components = self.0.clone();
components.sort_by_key(|c| c.weight()); components.sort_by_key(|c| c.weight());
html! { html! {
@for c in components.iter() { @for c in components.iter() {
" " (c.prepare(cx)) " " " " (c.render(cx)) " "
} }
} }
} }

View file

@ -21,7 +21,7 @@ pub trait ThemeTrait: ModuleTrait + Send + Sync {
} }
fn prepare_region(&self, page: &mut Page, region: &str) -> Markup { fn prepare_region(&self, page: &mut Page, region: &str) -> Markup {
let render_region = page.components_in(region).prepare(page.context()); let render_region = page.components_in(region).render(page.context());
if render_region.is_empty() { if render_region.is_empty() {
html! {} html! {}
} else { } else {

View file

@ -35,37 +35,3 @@ pub enum PrepareMarkup {
Text(&'static str), Text(&'static str),
With(Markup), With(Markup),
} }
impl PrepareMarkup {
pub fn html(self) -> Markup {
match self {
PrepareMarkup::None => html! {},
PrepareMarkup::Text(text) => html! { (text) },
PrepareMarkup::With(markup) => markup,
}
}
pub fn into_string(self) -> Option<String> {
match self {
PrepareMarkup::None => None,
PrepareMarkup::Text(text) => {
if text.is_empty() {
None
} else {
Some(text.to_string())
}
}
PrepareMarkup::With(markup) => Some(markup.into_string()),
}
}
}
impl ToString for PrepareMarkup {
fn to_string(&self) -> String {
match self {
PrepareMarkup::None => "".to_owned(),
PrepareMarkup::Text(text) => text.to_string(),
PrepareMarkup::With(markup) => markup.to_owned().into_string(),
}
}
}