diff --git a/pagetop-admin/src/lib.rs b/pagetop-admin/src/lib.rs index 5ca91531..d05138b4 100644 --- a/pagetop-admin/src/lib.rs +++ b/pagetop-admin/src/lib.rs @@ -1,5 +1,7 @@ use pagetop::prelude::*; +pub const ADMIN_MODULE: &str = "pagetop-admin::module::admin"; + localize!("src/locales"); mod summary; @@ -7,6 +9,10 @@ mod summary; pub struct Admin; impl ModuleTrait for Admin { + fn handler(&self) -> &'static str { + ADMIN_MODULE + } + fn name(&self) -> String { l("module_name") } diff --git a/pagetop-node/src/lib.rs b/pagetop-node/src/lib.rs index 3f8bff18..98af01bd 100644 --- a/pagetop-node/src/lib.rs +++ b/pagetop-node/src/lib.rs @@ -1,5 +1,7 @@ use pagetop::prelude::*; +pub const NODE_MODULE: &str = "pagetop-node::module::node"; + localize!("src/locales"); //mod entity; @@ -8,6 +10,10 @@ mod migration; pub struct Node; impl ModuleTrait for Node { + fn handler(&self) -> &'static str { + NODE_MODULE + } + fn name(&self) -> String { l("module_name") } diff --git a/pagetop-user/src/lib.rs b/pagetop-user/src/lib.rs index 557ecdac..8003e707 100644 --- a/pagetop-user/src/lib.rs +++ b/pagetop-user/src/lib.rs @@ -1,5 +1,7 @@ use pagetop::prelude::*; +pub const USER_MODULE: &str = "pagetop-user::module::user"; + localize!("src/locales"); mod migration; @@ -7,6 +9,10 @@ mod migration; pub struct User; impl ModuleTrait for User { + fn handler(&self) -> &'static str { + USER_MODULE + } + fn name(&self) -> String { l("module_name") } diff --git a/pagetop/src/api/action/all.rs b/pagetop/src/api/action/all.rs index 4e301777..cb9bfb8d 100644 --- a/pagetop/src/api/action/all.rs +++ b/pagetop/src/api/action/all.rs @@ -1,30 +1,26 @@ use crate::Lazy; -use crate::api::TypeId; use super::{ActionItem, ActionsHolder}; use std::sync::RwLock; use std::collections::HashMap; // Registered actions. -static ACTIONS: Lazy>> = Lazy::new(|| { +static ACTIONS: Lazy>> = Lazy::new(|| { RwLock::new(HashMap::new()) }); pub fn add_action(action: ActionItem) { let mut hmap = ACTIONS.write().unwrap(); - let action_id = action.type_id(); - if let Some(actions) = hmap.get_mut(&action_id) { + let action_handler = action.handler(); + if let Some(actions) = hmap.get_mut(action_handler) { actions.add(action); } else { - hmap.insert(action_id, ActionsHolder::new_with(action)); + hmap.insert(action_handler, ActionsHolder::new_with(action)); } } -pub fn run_actions(action_id: TypeId, f: F) -where - F: FnMut(&ActionItem) -> B -{ - if let Some(actions) = ACTIONS.read().unwrap().get(&action_id) { +pub fn run_actions(action_handler: &str, f: F) where F: FnMut(&ActionItem) -> B { + if let Some(actions) = ACTIONS.read().unwrap().get(action_handler) { actions.iter_map(f) } } diff --git a/pagetop/src/api/action/definition.rs b/pagetop/src/api/action/definition.rs index 9acb1258..1c950686 100644 --- a/pagetop/src/api/action/definition.rs +++ b/pagetop/src/api/action/definition.rs @@ -3,6 +3,8 @@ pub use std::any::Any as AnyAction; pub trait ActionTrait: AnyAction + Send + Sync { fn new() -> Self where Self: Sized; + fn handler(&self) -> &'static str; + fn weight(&self) -> isize { 0 } diff --git a/pagetop/src/api/component/action.rs b/pagetop/src/api/component/action.rs index d4e1f460..243646cc 100644 --- a/pagetop/src/api/component/action.rs +++ b/pagetop/src/api/component/action.rs @@ -1,19 +1,25 @@ use crate::api::action::{ActionTrait, AnyAction}; use super::{Assets, ComponentTrait}; -pub struct ActionBeforeRenderComponent { +pub const BEFORE_RENDER_COMPONENT_ACTION: &str = "pagetop::action::before_render_component"; + +pub struct BeforeRenderComponentAction { action: Option, weight: isize, } -impl ActionTrait for ActionBeforeRenderComponent { +impl ActionTrait for BeforeRenderComponentAction { fn new() -> Self { - ActionBeforeRenderComponent { + BeforeRenderComponentAction { action: None, weight: 0, } } + fn handler(&self) -> &'static str { + BEFORE_RENDER_COMPONENT_ACTION + } + fn weight(&self) -> isize { self.weight } @@ -23,7 +29,7 @@ impl ActionTrait for ActionBeforeRenderComponent { } } -impl ActionBeforeRenderComponent { +impl BeforeRenderComponentAction { pub fn with_action(mut self, action: fn(&mut dyn ComponentTrait, &mut Assets)) -> Self { self.action = Some(action); self diff --git a/pagetop/src/api/component/definition.rs b/pagetop/src/api/component/definition.rs index 687b4eae..a807a36f 100644 --- a/pagetop/src/api/component/definition.rs +++ b/pagetop/src/api/component/definition.rs @@ -1,24 +1,18 @@ use crate::html::{Markup, html}; -use crate::api::{TypeId, action::{action_ref, run_actions}}; +use crate::api::action::{action_ref, run_actions}; use crate::util; -use super::{ActionBeforeRenderComponent, Assets}; +use super::{BEFORE_RENDER_COMPONENT_ACTION, BeforeRenderComponentAction}; +use super::Assets; pub use std::any::Any as AnyComponent; -pub trait BaseComponent { - fn type_name(&self) -> &'static str; - - fn single_name(&self) -> &'static str; - - fn qualified_name(&self, last: usize) -> &'static str; -} - -pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync { - +pub trait ComponentTrait: AnyComponent + Send + Sync { fn new() -> Self where Self: Sized; + fn handler(&self) -> &'static str; + fn name(&self) -> String { - self.single_name().to_owned() + util::single_type_name::().to_owned() } fn description(&self) -> Option { @@ -47,20 +41,6 @@ pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync { fn as_mut_any(&mut self) -> &mut dyn AnyComponent; } -impl BaseComponent for C { - fn type_name(&self) -> &'static str { - std::any::type_name::() - } - - fn single_name(&self) -> &'static str { - util::partial_type_name(std::any::type_name::(), 1) - } - - fn qualified_name(&self, last: usize) -> &'static str { - util::partial_type_name(std::any::type_name::(), last) - } -} - pub fn component_ref(component: &dyn ComponentTrait) -> &C { component.as_ref_any().downcast_ref::().unwrap() } @@ -75,8 +55,8 @@ pub fn render_component(component: &mut dyn ComponentTrait, assets: &mut Assets) // Acciones de los módulos antes de renderizar el componente. run_actions( - TypeId::of::(), - |a| action_ref::(&**a).run(component, assets) + BEFORE_RENDER_COMPONENT_ACTION, + |a| action_ref::(&**a).run(component, assets) ); // Acciones del tema antes de renderizar el componente. diff --git a/pagetop/src/api/component/mod.rs b/pagetop/src/api/component/mod.rs index fe21f1ea..95044f52 100644 --- a/pagetop/src/api/component/mod.rs +++ b/pagetop/src/api/component/mod.rs @@ -1,5 +1,8 @@ mod action; -pub use action::ActionBeforeRenderComponent; +pub use action::{ + BEFORE_RENDER_COMPONENT_ACTION, + BeforeRenderComponentAction, +}; mod assets; pub use assets::{ @@ -12,7 +15,6 @@ pub use assets::{ mod definition; pub use definition::{ AnyComponent, - BaseComponent, ComponentTrait, component_ref, component_mut, diff --git a/pagetop/src/api/mod.rs b/pagetop/src/api/mod.rs index 8d84af05..b69ec91b 100644 --- a/pagetop/src/api/mod.rs +++ b/pagetop/src/api/mod.rs @@ -1,5 +1,3 @@ -pub use std::any::TypeId; - pub mod action; // API to define functions that alter the behavior of PageTop core. pub mod component; // API para crear nuevos componentes. pub mod module; // API para añadir módulos con nuevas funcionalidades. diff --git a/pagetop/src/api/module/all.rs b/pagetop/src/api/module/all.rs index 6c7948de..1bb6ea7d 100644 --- a/pagetop/src/api/module/all.rs +++ b/pagetop/src/api/module/all.rs @@ -18,9 +18,9 @@ pub fn register_module(module: &'static dyn ModuleTrait) { } fn add_to(list: &mut Vec<&dyn ModuleTrait>, module: &'static dyn ModuleTrait) { - if !MODULES.read().unwrap().iter().any(|m| m.type_name() == module.type_name()) { - if !list.iter().any(|m| m.type_name() == module.type_name()) { - trace::debug!("Registering \"{}\" module", module.single_name()); + if !MODULES.read().unwrap().iter().any(|m| m.handler() == module.handler()) { + if !list.iter().any(|m| m.handler() == module.handler()) { + trace::debug!("Register module: \"{}\"", module.name()); list.push(module); let mut dependencies = module.dependencies(); diff --git a/pagetop/src/api/module/definition.rs b/pagetop/src/api/module/definition.rs index d1c213e3..7ee26e5d 100644 --- a/pagetop/src/api/module/definition.rs +++ b/pagetop/src/api/module/definition.rs @@ -1,21 +1,16 @@ -use crate::{app, util}; +use crate::app; use crate::api::action::ActionItem; +use crate::util; #[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] use crate::db::MigrationItem; -pub trait BaseModule { - fn type_name(&self) -> &'static str; - - fn single_name(&self) -> &'static str; - - fn qualified_name(&self, last: usize) -> &'static str; -} - /// Los módulos deben implementar este *trait*. -pub trait ModuleTrait: BaseModule + Send + Sync { +pub trait ModuleTrait: Send + Sync { + fn handler(&self) -> &'static str; + fn name(&self) -> String { - self.single_name().to_owned() + util::single_type_name::().to_owned() } fn description(&self) -> Option { @@ -40,17 +35,3 @@ pub trait ModuleTrait: BaseModule + Send + Sync { vec![] } } - -impl BaseModule for M { - fn type_name(&self) -> &'static str { - std::any::type_name::() - } - - fn single_name(&self) -> &'static str { - util::partial_type_name(std::any::type_name::(), 1) - } - - fn qualified_name(&self, last: usize) -> &'static str { - util::partial_type_name(std::any::type_name::(), last) - } -} diff --git a/pagetop/src/api/module/mod.rs b/pagetop/src/api/module/mod.rs index d1addefe..b4a97393 100644 --- a/pagetop/src/api/module/mod.rs +++ b/pagetop/src/api/module/mod.rs @@ -1,10 +1,5 @@ mod definition; -pub use definition::{ - BaseModule, - ModuleTrait, -}; +pub use definition::ModuleTrait; pub(crate) mod all; -pub use all::{ - register_module, -}; +pub use all::register_module; diff --git a/pagetop/src/api/theme/all.rs b/pagetop/src/api/theme/all.rs index 12d96338..b1e47307 100644 --- a/pagetop/src/api/theme/all.rs +++ b/pagetop/src/api/theme/all.rs @@ -12,8 +12,8 @@ static THEMES: Lazy>> = Lazy::new(|| { pub fn register_theme(theme: &'static dyn ThemeTrait) { let mut themes = THEMES.write().unwrap(); - if !themes.iter().any(|t| t.name() == theme.name()) { - trace::debug!("Registering \"{}\" theme", theme.single_name()); + if !themes.iter().any(|t| t.handler() == theme.handler()) { + trace::debug!("Register theme: \"{}\"", theme.name()); themes.push(theme); } } diff --git a/pagetop/src/api/theme/definition.rs b/pagetop/src/api/theme/definition.rs index b5a12243..6bfa63d5 100644 --- a/pagetop/src/api/theme/definition.rs +++ b/pagetop/src/api/theme/definition.rs @@ -6,18 +6,12 @@ use crate::response::page::Page; use crate::base::component::Chunck; use crate::util; -pub trait BaseTheme { - fn type_name(&self) -> &'static str; - - fn single_name(&self) -> &'static str; - - fn qualified_name(&self, last: usize) -> &'static str; -} - /// Los temas deben implementar este "trait". -pub trait ThemeTrait: BaseTheme + Send + Sync { +pub trait ThemeTrait: Send + Sync { + fn handler(&self) -> &'static str; + fn name(&self) -> String { - self.single_name().to_owned() + util::single_type_name::().to_owned() } fn description(&self) -> Option { @@ -93,8 +87,8 @@ pub trait ThemeTrait: BaseTheme + Send + Sync { /* Cómo usarlo: - match component.single_name() { - "Block" => { + match component.handler() { + BLOCK_COMPONENT => { let block = component_mut::(component); block.alter_title("New title"); }, @@ -113,8 +107,8 @@ pub trait ThemeTrait: BaseTheme + Send + Sync { /* Cómo usarlo: - match component.single_name() { - "Block" => { + match component.handler() { + BLOCK_COMPONENT => { let block = component_ref::(component); match block.template() { "default" => Some(block_default(block)), @@ -137,17 +131,3 @@ pub trait ThemeTrait: BaseTheme + Send + Sync { .render() } } - -impl BaseTheme for T { - fn type_name(&self) -> &'static str { - std::any::type_name::() - } - - fn single_name(&self) -> &'static str { - util::partial_type_name(std::any::type_name::(), 1) - } - - fn qualified_name(&self, last: usize) -> &'static str { - util::partial_type_name(std::any::type_name::(), last) - } -} diff --git a/pagetop/src/api/theme/mod.rs b/pagetop/src/api/theme/mod.rs index f76d1fe3..4456189e 100644 --- a/pagetop/src/api/theme/mod.rs +++ b/pagetop/src/api/theme/mod.rs @@ -1,8 +1,5 @@ mod definition; -pub use definition::{ - BaseTheme, - ThemeTrait, -}; +pub use definition::ThemeTrait; pub(crate) mod all; pub use all::{ diff --git a/pagetop/src/base/component/block.rs b/pagetop/src/base/component/block.rs index df209ea9..dafe4497 100644 --- a/pagetop/src/base/component/block.rs +++ b/pagetop/src/base/component/block.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_BLOCK: &str = "pagetop::base::component::block::Block"; +pub const BLOCK_COMPONENT: &str = "pagetop::component::block"; pub struct Block { renderable: fn() -> bool, @@ -25,6 +25,10 @@ impl ComponentTrait for Block { } } + fn handler(&self) -> &'static str { + BLOCK_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } @@ -34,7 +38,7 @@ impl ComponentTrait for Block { } fn default_render(&self, assets: &mut Assets) -> Markup { - let id = assets.serial_id(self.single_name(), self.id()); + let id = assets.serial_id("block", self.id()); html! { div id=(id) class=[self.classes()] { @match self.title() { diff --git a/pagetop/src/base/component/chunck.rs b/pagetop/src/base/component/chunck.rs index a772af1d..c7d8a978 100644 --- a/pagetop/src/base/component/chunck.rs +++ b/pagetop/src/base/component/chunck.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_CHUNCK: &str = "pagetop::base::component::chunck::Chunck"; +pub const CHUNCK_COMPONENT: &str = "pagetop::component::chunck"; pub struct Chunck { renderable: fn() -> bool, @@ -19,6 +19,10 @@ impl ComponentTrait for Chunck { } } + fn handler(&self) -> &'static str { + CHUNCK_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } diff --git a/pagetop/src/base/component/container.rs b/pagetop/src/base/component/container.rs index dd2508ad..e275f255 100644 --- a/pagetop/src/base/component/container.rs +++ b/pagetop/src/base/component/container.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_CONTAINER: &str = "pagetop::base::component::container::Container"; +pub const CONTAINER_COMPONENT: &str = "pagetop::component::container"; pub enum ContainerType { Header, Footer, Main, Section, Wrapper } @@ -29,6 +29,10 @@ impl ComponentTrait for Container { } } + fn handler(&self) -> &'static str { + CONTAINER_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } diff --git a/pagetop/src/base/component/form/button.rs b/pagetop/src/base/component/form/button.rs index 47f2e9ca..fc7fe620 100644 --- a/pagetop/src/base/component/form/button.rs +++ b/pagetop/src/base/component/form/button.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_BUTTON: &str = "pagetop::base::component::form::button::Button"; +pub const BUTTON_COMPONENT: &str = "pagetop::component::form::button"; pub enum ButtonType {Button, Reset, Submit} @@ -32,6 +32,10 @@ impl ComponentTrait for Button { .with_classes("form-button", ClassesOp::AddFirst) } + fn handler(&self) -> &'static str { + BUTTON_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } diff --git a/pagetop/src/base/component/form/date.rs b/pagetop/src/base/component/form/date.rs index e9741120..eeb1d7aa 100644 --- a/pagetop/src/base/component/form/date.rs +++ b/pagetop/src/base/component/form/date.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_DATE: &str = "pagetop::base::component::form::date::Date"; +pub const DATE_COMPONENT: &str = "pagetop::component::form::date"; pub struct Date { renderable : fn() -> bool, @@ -40,6 +40,10 @@ impl ComponentTrait for Date { .with_classes("form-type-date", ClassesOp::AddFirst) } + fn handler(&self) -> &'static str { + DATE_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } diff --git a/pagetop/src/base/component/form/form.rs b/pagetop/src/base/component/form/form.rs index 70a9caf6..dcf209b0 100644 --- a/pagetop/src/base/component/form/form.rs +++ b/pagetop/src/base/component/form/form.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_FORM: &str = "pagetop::base::component::form::form::Form"; +pub const FORM_COMPONENT: &str = "pagetop::component::form"; pub enum FormMethod {Get, Post} @@ -31,6 +31,10 @@ impl ComponentTrait for Form { } } + fn handler(&self) -> &'static str { + FORM_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } diff --git a/pagetop/src/base/component/form/hidden.rs b/pagetop/src/base/component/form/hidden.rs index c837d0a8..c8d86131 100644 --- a/pagetop/src/base/component/form/hidden.rs +++ b/pagetop/src/base/component/form/hidden.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_HIDDEN: &str = "pagetop::base::component::form::hidden::Hidden"; +pub const HIDDEN_COMPONENT: &str = "pagetop::component::form::hidden"; pub struct Hidden { weight: isize, @@ -17,6 +17,10 @@ impl ComponentTrait for Hidden { } } + fn handler(&self) -> &'static str { + HIDDEN_COMPONENT + } + fn weight(&self) -> isize { self.weight } diff --git a/pagetop/src/base/component/form/input.rs b/pagetop/src/base/component/form/input.rs index a7168097..d1614aa3 100644 --- a/pagetop/src/base/component/form/input.rs +++ b/pagetop/src/base/component/form/input.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_INPUT: &str = "pagetop::base::component::form::input::Input"; +pub const INPUT_COMPONENT: &str = "pagetop::component::form::input"; pub enum InputType {Email, Password, Search, Telephone, Textfield, Url} @@ -50,6 +50,10 @@ impl ComponentTrait for Input { .with_classes("form-type-textfield", ClassesOp::AddFirst) } + fn handler(&self) -> &'static str { + INPUT_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } diff --git a/pagetop/src/base/component/form/mod.rs b/pagetop/src/base/component/form/mod.rs index d8df6d2d..4e35a9d4 100644 --- a/pagetop/src/base/component/form/mod.rs +++ b/pagetop/src/base/component/form/mod.rs @@ -1,21 +1,21 @@ mod form; pub use form::{ - TYPENAME_FORM, Form, FormMethod + FORM_COMPONENT, Form, FormMethod }; mod input; pub use input::{ - TYPENAME_INPUT, Input, InputType + INPUT_COMPONENT, Input, InputType }; mod hidden; pub use hidden::{ - TYPENAME_HIDDEN, Hidden + HIDDEN_COMPONENT, Hidden }; mod date; pub use date::{ - TYPENAME_DATE, Date + DATE_COMPONENT, Date }; mod button; pub use button::{ - TYPENAME_BUTTON, Button, ButtonType + BUTTON_COMPONENT, Button, ButtonType }; diff --git a/pagetop/src/base/component/grid/column.rs b/pagetop/src/base/component/grid/column.rs index 8199c986..a69edce7 100644 --- a/pagetop/src/base/component/grid/column.rs +++ b/pagetop/src/base/component/grid/column.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_COLUMN: &str = "pagetop::base::component::grid::column::Column"; +pub const COLUMN_COMPONENT: &str = "pagetop::component::grid::column"; pub struct Column { renderable: fn() -> bool, @@ -23,6 +23,10 @@ impl ComponentTrait for Column { } } + fn handler(&self) -> &'static str { + COLUMN_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } diff --git a/pagetop/src/base/component/grid/mod.rs b/pagetop/src/base/component/grid/mod.rs index e63ea7cd..4fd8936b 100644 --- a/pagetop/src/base/component/grid/mod.rs +++ b/pagetop/src/base/component/grid/mod.rs @@ -1,8 +1,8 @@ mod row; pub use row::{ - TYPENAME_ROW, Row + ROW_COMPONENT, Row }; mod column; pub use column::{ - TYPENAME_COLUMN, Column + COLUMN_COMPONENT, Column }; diff --git a/pagetop/src/base/component/grid/row.rs b/pagetop/src/base/component/grid/row.rs index a366cf7a..05fae1cf 100644 --- a/pagetop/src/base/component/grid/row.rs +++ b/pagetop/src/base/component/grid/row.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_ROW: &str = "pagetop::base::component::grid::row::Row"; +pub const ROW_COMPONENT: &str = "pagetop::component::grid::row"; pub struct Row { renderable: fn() -> bool, @@ -23,6 +23,10 @@ impl ComponentTrait for Row { } } + fn handler(&self) -> &'static str { + ROW_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } diff --git a/pagetop/src/base/component/image.rs b/pagetop/src/base/component/image.rs index efb647c2..300cafc2 100644 --- a/pagetop/src/base/component/image.rs +++ b/pagetop/src/base/component/image.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const TYPENAME_IMAGE: &str = "pagetop::base::component::image::Image"; +pub const IMAGE_COMPONENT: &str = "pagetop::component::image"; pub struct Image { renderable: fn() -> bool, @@ -23,6 +23,10 @@ impl ComponentTrait for Image { } } + fn handler(&self) -> &'static str { + IMAGE_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } diff --git a/pagetop/src/base/component/menu.rs b/pagetop/src/base/component/menu.rs index 9b416daf..0434b90f 100644 --- a/pagetop/src/base/component/menu.rs +++ b/pagetop/src/base/component/menu.rs @@ -1,7 +1,7 @@ use crate::prelude::*; -pub const TYPENAME_MENU: &str = "pagetop::base::component::menu::Menu"; -pub const TYPENAME_MENUITEM: &str = "pagetop::base::component::menu::MenuItem"; +pub const MENU_COMPONENT: &str = "pagetop::component::menu"; +pub const MENUITEM_COMPONENT: &str = "pagetop::component::menu"; pub enum MenuItemType { Label(String), @@ -30,6 +30,10 @@ impl ComponentTrait for MenuItem { } } + fn handler(&self) -> &'static str { + MENUITEM_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } @@ -190,6 +194,10 @@ impl ComponentTrait for Menu { } } + fn handler(&self) -> &'static str { + MENU_COMPONENT + } + fn is_renderable(&self) -> bool { (self.renderable)() } @@ -211,7 +219,7 @@ impl ComponentTrait for Menu { )) .add_jquery(); - let id = assets.serial_id(self.single_name(), self.id()); + let id = assets.serial_id("menu", self.id()); html! { ul id=(id) class=[self.classes()] { (self.items().render(assets)) diff --git a/pagetop/src/base/component/mod.rs b/pagetop/src/base/component/mod.rs index 032fd625..14759dc8 100644 --- a/pagetop/src/base/component/mod.rs +++ b/pagetop/src/base/component/mod.rs @@ -1,28 +1,28 @@ mod container; pub use container::{ - TYPENAME_CONTAINER, Container, ContainerType + CONTAINER_COMPONENT, Container, ContainerType }; pub mod grid; mod chunck; pub use chunck::{ - TYPENAME_CHUNCK, Chunck + CHUNCK_COMPONENT, Chunck }; mod block; pub use block::{ - TYPENAME_BLOCK, Block + BLOCK_COMPONENT, Block }; mod image; pub use image::{ - TYPENAME_IMAGE, Image + IMAGE_COMPONENT, Image }; mod menu; pub use menu::{ - TYPENAME_MENU, TYPENAME_MENUITEM, Menu, MenuItem, MenuItemType + MENU_COMPONENT, MENUITEM_COMPONENT, Menu, MenuItem, MenuItemType }; pub mod form; pub use form::{ - TYPENAME_FORM, Form, FormMethod + FORM_COMPONENT, Form, FormMethod }; diff --git a/pagetop/src/base/module/demopage/mod.rs b/pagetop/src/base/module/demopage/mod.rs index 55ed6d56..343b00a0 100644 --- a/pagetop/src/base/module/demopage/mod.rs +++ b/pagetop/src/base/module/demopage/mod.rs @@ -1,10 +1,16 @@ use crate::prelude::*; +pub const DEMOPAGE_MODULE: &str = "pagetop::module::demopage"; + localize!("src/base/module/demopage/locales"); pub struct Demopage; impl ModuleTrait for Demopage { + fn handler(&self) -> &'static str { + DEMOPAGE_MODULE + } + fn name(&self) -> String { l("module_name") } diff --git a/pagetop/src/base/theme/aliner/mod.rs b/pagetop/src/base/theme/aliner/mod.rs index 6936a019..f3c90f9b 100644 --- a/pagetop/src/base/theme/aliner/mod.rs +++ b/pagetop/src/base/theme/aliner/mod.rs @@ -1,10 +1,16 @@ use crate::prelude::*; +pub const ALINER_THEME: &str = "pagetop::theme::aliner"; + include!(concat!(env!("OUT_DIR"), "/aliner.rs")); pub struct Aliner; impl ThemeTrait for Aliner { + fn handler(&self) -> &'static str { + ALINER_THEME + } + fn configure_theme(&self, cfg: &mut app::web::ServiceConfig) { theme_static_files!(cfg, "/aliner"); } diff --git a/pagetop/src/base/theme/bootsier/mod.rs b/pagetop/src/base/theme/bootsier/mod.rs index 0445356b..a8be7118 100644 --- a/pagetop/src/base/theme/bootsier/mod.rs +++ b/pagetop/src/base/theme/bootsier/mod.rs @@ -1,5 +1,7 @@ use crate::prelude::*; +pub const BOOTSIER_THEME: &str = "pagetop::theme::bootsier"; + include!(concat!(env!("OUT_DIR"), "/bootsier.rs")); localize!("src/base/theme/bootsier/locales"); @@ -7,6 +9,10 @@ localize!("src/base/theme/bootsier/locales"); pub struct Bootsier; impl ThemeTrait for Bootsier { + fn handler(&self) -> &'static str { + BOOTSIER_THEME + } + fn configure_theme(&self, cfg: &mut app::web::ServiceConfig) { theme_static_files!(cfg, "/bootsier"); } diff --git a/pagetop/src/base/theme/bulmix/mod.rs b/pagetop/src/base/theme/bulmix/mod.rs index 1a36fe7d..477dbbf7 100644 --- a/pagetop/src/base/theme/bulmix/mod.rs +++ b/pagetop/src/base/theme/bulmix/mod.rs @@ -1,10 +1,16 @@ use crate::prelude::*; +pub const BULMIX_THEME: &str = "pagetop::theme::bulmix"; + include!(concat!(env!("OUT_DIR"), "/bulmix.rs")); pub struct Bulmix; impl ThemeTrait for Bulmix { + fn handler(&self) -> &'static str { + BULMIX_THEME + } + fn configure_theme(&self, cfg: &mut app::web::ServiceConfig) { theme_static_files!(cfg, "/bulmix"); } @@ -29,12 +35,12 @@ impl ThemeTrait for Bulmix { component: &mut dyn ComponentTrait, _assets: &mut Assets ) { - match component.type_name() { - grid::TYPENAME_ROW => { + match component.handler() { + grid::ROW_COMPONENT => { let row = component_mut::(component); row.alter_classes("columns", ClassesOp::SetDefault); }, - grid::TYPENAME_COLUMN => { + grid::COLUMN_COMPONENT => { let col = component_mut::(component); col.alter_classes("column", ClassesOp::SetDefault); }, diff --git a/pagetop/src/base/theme/minimal/mod.rs b/pagetop/src/base/theme/minimal/mod.rs index d2124fb6..e86e1719 100644 --- a/pagetop/src/base/theme/minimal/mod.rs +++ b/pagetop/src/base/theme/minimal/mod.rs @@ -1,6 +1,11 @@ use crate::prelude::*; +pub const MINIMAL_THEME: &str = "pagetop::theme::minimal"; + pub struct Minimal; impl ThemeTrait for Minimal { + fn handler(&self) -> &'static str { + MINIMAL_THEME + } } diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index a20d1563..357c31cf 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -21,7 +21,6 @@ pub use crate::{ }; pub use crate::{action_item, api::{ - TypeId, action::*, component::*, module::*, diff --git a/pagetop/src/response/page/action.rs b/pagetop/src/response/page/action.rs index 7fe35a7f..7869987c 100644 --- a/pagetop/src/response/page/action.rs +++ b/pagetop/src/response/page/action.rs @@ -1,6 +1,8 @@ use crate::api::action::{ActionTrait, AnyAction}; use super::Page; +pub const BEFORE_RENDER_PAGE_ACTION: &str = "pagetop::action::before_render_page"; + pub struct ActionBeforeRenderPage { action: Option, weight: isize, @@ -14,6 +16,10 @@ impl ActionTrait for ActionBeforeRenderPage { } } + fn handler(&self) -> &'static str { + BEFORE_RENDER_PAGE_ACTION + } + fn weight(&self) -> isize { self.weight } diff --git a/pagetop/src/response/page/mod.rs b/pagetop/src/response/page/mod.rs index 30f95d72..e08e55f0 100644 --- a/pagetop/src/response/page/mod.rs +++ b/pagetop/src/response/page/mod.rs @@ -1,5 +1,8 @@ mod action; -pub use action::ActionBeforeRenderPage; +pub use action::{ + BEFORE_RENDER_PAGE_ACTION, + ActionBeforeRenderPage, +}; mod page; pub use page::Page; diff --git a/pagetop/src/response/page/page.rs b/pagetop/src/response/page/page.rs index 99a81cb3..a4fc741b 100644 --- a/pagetop/src/response/page/page.rs +++ b/pagetop/src/response/page/page.rs @@ -1,9 +1,9 @@ use crate::{Lazy, app, trace}; use crate::config::SETTINGS; use crate::html::*; -use crate::api::{TypeId, action::{action_ref, run_actions}}; +use crate::api::action::{action_ref, run_actions}; use crate::api::component::*; -use super::ActionBeforeRenderPage; +use super::{BEFORE_RENDER_PAGE_ACTION, ActionBeforeRenderPage}; use std::collections::HashMap; @@ -152,7 +152,7 @@ impl<'a> Page<'a> { pub fn render(&mut self) -> app::Result { // Acciones de los módulos antes de renderizar la página. run_actions( - TypeId::of::(), + BEFORE_RENDER_PAGE_ACTION, |a| action_ref::(&**a).run(self) ); diff --git a/pagetop/src/util.rs b/pagetop/src/util.rs index f87b67fc..f4f683bf 100644 --- a/pagetop/src/util.rs +++ b/pagetop/src/util.rs @@ -46,3 +46,7 @@ pub fn partial_type_name(type_name: &'static str, last: usize) -> &'static str { } &type_name[(positions[last - 1].0 + 2)..] } + +pub fn single_type_name() -> &'static str { + partial_type_name(std::any::type_name::(), 1) +}