🏗️ Nuevo componente Html para simplificar L10n

This commit is contained in:
Manuel Cillero 2023-06-09 12:37:58 +02:00
parent a48c575198
commit 52baa4f671
8 changed files with 83 additions and 38 deletions

View file

@ -1,4 +1,5 @@
use super::LOCALE_ADMIN; use crate::LOCALE_ADMIN;
use pagetop::prelude::*; use pagetop::prelude::*;
use pagetop_megamenu::component::{MegaMenu, MegaMenuItem}; use pagetop_megamenu::component::{MegaMenu, MegaMenuItem};
use pagetop_minimal::component::*; use pagetop_minimal::component::*;
@ -7,62 +8,62 @@ pub async fn summary(request: server::HttpRequest) -> ResultPage<Markup, FatalEr
let top_menu = MegaMenu::new() let top_menu = MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::t("module_name", &LOCALE_ADMIN))) .with_item(MegaMenuItem::label(L10n::t("module_name", &LOCALE_ADMIN)))
.with_item(MegaMenuItem::link( .with_item(MegaMenuItem::link(
L10n::text("Opción 2"), L10n::n("Opción 2"),
"https://www.google.es", "https://www.google.es",
)) ))
.with_item(MegaMenuItem::link_blank( .with_item(MegaMenuItem::link_blank(
L10n::text("Opción 3"), L10n::n("Opción 3"),
"https://www.google.es", "https://www.google.es",
)) ))
.with_item(MegaMenuItem::submenu( .with_item(MegaMenuItem::submenu(
L10n::text("Submenú 1"), L10n::n("Submenú 1"),
MegaMenu::new() MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::text("Opción 1"))) .with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::label(L10n::text("Opción 2"))), .with_item(MegaMenuItem::label(L10n::n("Opción 2"))),
)) ))
.with_item(MegaMenuItem::separator()) .with_item(MegaMenuItem::separator())
.with_item(MegaMenuItem::submenu( .with_item(MegaMenuItem::submenu(
L10n::text("Submenú 2"), L10n::n("Submenú 2"),
MegaMenu::new() MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::text("Opción 1"))) .with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::label(L10n::text("Opción 2"))), .with_item(MegaMenuItem::label(L10n::n("Opción 2"))),
)) ))
.with_item(MegaMenuItem::label(L10n::text("Opción 4"))); .with_item(MegaMenuItem::label(L10n::n("Opción 4")));
let side_menu = MegaMenu::new() let side_menu = MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::text("Opción 1"))) .with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::link( .with_item(MegaMenuItem::link(
L10n::text("Opción 2"), L10n::n("Opción 2"),
"https://www.google.es", "https://www.google.es",
)) ))
.with_item(MegaMenuItem::link_blank( .with_item(MegaMenuItem::link_blank(
L10n::text("Opción 3"), L10n::n("Opción 3"),
"https://www.google.es", "https://www.google.es",
)) ))
.with_item(MegaMenuItem::submenu( .with_item(MegaMenuItem::submenu(
L10n::text("Submenú 1"), L10n::n("Submenú 1"),
MegaMenu::new() MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::text("Opción 1"))) .with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::label(L10n::text("Opción 2"))), .with_item(MegaMenuItem::label(L10n::n("Opción 2"))),
)) ))
.with_item(MegaMenuItem::separator()) .with_item(MegaMenuItem::separator())
.with_item(MegaMenuItem::submenu( .with_item(MegaMenuItem::submenu(
L10n::text("Submenú 2"), L10n::n("Submenú 2"),
MegaMenu::new() MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::text("Opción 1"))) .with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::label(L10n::text("Opción 2"))), .with_item(MegaMenuItem::label(L10n::n("Opción 2"))),
)) ))
.with_item(MegaMenuItem::label(L10n::text("Opción 4"))); .with_item(MegaMenuItem::label(L10n::n("Opción 4")));
Page::new(request) Page::new(request)
.with_context(ContextOp::Theme("Bootsier")) .with_context(ContextOp::Theme("Bootsier"))
.with_title(L10n::text("Admin")) .with_title(L10n::n("Admin"))
.with_in("top-menu", top_menu) .with_in("top-menu", top_menu)
.with_in( .with_in(
"content", "content",
grid::Row::new() grid::Row::new()
.with_column(grid::Column::new().with_component(side_menu)) .with_column(grid::Column::new().with_component(side_menu))
.with_column(grid::Column::new().with_component(L10n::html(html! { .with_column(grid::Column::new().with_component(Html::with(html! {
p { "Columna 2"} p { "Columna 2"}
}))), }))),
) )

View file

@ -41,7 +41,7 @@ impl ModuleTrait for Node {
} }
async fn node(request: server::HttpRequest) -> ResultPage<Markup, FatalError> { async fn node(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
Page::new(request).with_title(L10n::text("Nodo")).render() Page::new(request).with_title(L10n::n("Nodo")).render()
} }
fn before_render_page(page: &mut Page) { fn before_render_page(page: &mut Page) {

View file

@ -42,7 +42,7 @@ impl ModuleTrait for User {
async fn login(request: server::HttpRequest) -> ResultPage<Markup, FatalError> { async fn login(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
Page::new(request) Page::new(request)
.with_title(L10n::text("Identificación del usuario")) .with_title(L10n::n("Identificación del usuario"))
.with_in( .with_in(
"content", "content",
Container::new() Container::new()

View file

@ -1,3 +1,6 @@
mod html;
pub use html::{Html, COMPONENT_HTML};
mod l10n; mod l10n;
pub use l10n::{L10n, COMPONENT_L10N}; pub use l10n::{L10n, COMPONENT_L10N};

View file

@ -0,0 +1,48 @@
use crate::prelude::*;
define_handle!(COMPONENT_HTML);
#[derive(Default)]
pub struct Html(Markup);
impl ComponentTrait for Html {
fn new() -> Self {
Html::default()
}
fn handle(&self) -> Handle {
COMPONENT_HTML
}
fn default_render(&self, _: &mut RenderContext) -> Markup {
html! { (self.html()) }
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Html {
pub fn with(html: Markup) -> Self {
Html(html)
}
// Html BUILDER.
#[fn_builder]
pub fn alter_html(&mut self, html: Markup) -> &mut Self {
self.0 = html;
self
}
// Html GETTERS.
pub fn html(&self) -> &Markup {
&self.0
}
}

View file

@ -8,7 +8,7 @@ define_handle!(COMPONENT_L10N);
pub enum L10nOp { pub enum L10nOp {
#[default] #[default]
None, None,
Value(Markup), Text(&'static str),
Translated(&'static str, &'static Locales), Translated(&'static str, &'static Locales),
Escaped(&'static str, &'static Locales), Escaped(&'static str, &'static Locales),
} }
@ -31,7 +31,7 @@ impl ComponentTrait for L10n {
fn default_render(&self, rcx: &mut RenderContext) -> Markup { fn default_render(&self, rcx: &mut RenderContext) -> Markup {
match self.op() { match self.op() {
L10nOp::None => html! {}, L10nOp::None => html! {},
L10nOp::Value(value) => html! { (value) }, L10nOp::Text(text) => html! { (text) },
L10nOp::Translated(key, locales) => html! { L10nOp::Translated(key, locales) => html! {
(locales (locales
.lookup_with_args( .lookup_with_args(
@ -71,16 +71,9 @@ impl ComponentTrait for L10n {
} }
impl L10n { impl L10n {
pub fn text(text: &'static str) -> Self { pub fn n(text: &'static str) -> Self {
L10n { L10n {
op: L10nOp::Value(html! { (text) }), op: L10nOp::Text(text),
..Default::default()
}
}
pub fn html(html: Markup) -> Self {
L10n {
op: L10nOp::Value(html),
..Default::default() ..Default::default()
} }
} }

View file

@ -17,7 +17,7 @@ pub trait ModuleTrait: BaseModule + Send + Sync {
fn handle(&self) -> Handle; fn handle(&self) -> Handle;
fn name(&self) -> L10n { fn name(&self) -> L10n {
L10n::text(self.single_name()) L10n::n(self.single_name())
} }
fn description(&self) -> L10n { fn description(&self) -> L10n {

View file

@ -32,7 +32,7 @@ impl fmt::Display for FatalError {
FatalError::AccessDenied(request) => { FatalError::AccessDenied(request) => {
let error_page = Page::new(request.clone()); let error_page = Page::new(request.clone());
if let Ok(page) = error_page if let Ok(page) = error_page
.with_title(L10n::text("Error FORBIDDEN")) .with_title(L10n::n("Error FORBIDDEN"))
.with_in("content", error403::Error403) .with_in("content", error403::Error403)
.with_template("error") .with_template("error")
.render() .render()
@ -46,7 +46,7 @@ impl fmt::Display for FatalError {
FatalError::NotFound(request) => { FatalError::NotFound(request) => {
let error_page = Page::new(request.clone()); let error_page = Page::new(request.clone());
if let Ok(page) = error_page if let Ok(page) = error_page
.with_title(L10n::text("Error RESOURCE NOT FOUND")) .with_title(L10n::n("Error RESOURCE NOT FOUND"))
.with_in("content", error404::Error404) .with_in("content", error404::Error404)
.with_template("error") .with_template("error")
.render() .render()