Añade componente para crear menús

También introduce el nuevo módulo Admin para porporcionar un entorno
común de administración para los demás módulos de PageTop.
This commit is contained in:
Manuel Cillero 2022-02-25 20:42:06 +01:00
parent eddb397bc7
commit edf5ddf81b
28 changed files with 1849 additions and 187 deletions

105
src/base/component/block.rs Normal file
View file

@ -0,0 +1,105 @@
use crate::prelude::*;
pub struct Block {
renderable: fn() -> bool,
weight : i8,
id : Option<String>,
title : String,
markup : Vec<Markup>,
template : String,
}
impl PageComponent for Block {
fn prepare() -> Self {
Block {
renderable: always,
weight : 0,
id : None,
title : "".to_string(),
markup : Vec::new(),
template : "default".to_string(),
}
}
fn is_renderable(&self) -> bool {
(self.renderable)()
}
fn weight(&self) -> i8 {
self.weight
}
fn default_render(&self, assets: &mut PageAssets) -> Markup {
let id = assets.required_id(self.name(), self.id());
html! {
div id=(id) class="block" {
@if !self.title.is_empty() {
h2 class="block-title" { (self.title) }
}
div class="block-body" {
@for markup in self.markup.iter() {
(*markup)
}
}
}
}
}
}
impl Block {
pub fn markup(markup: Markup) -> Self {
Block::prepare().add_markup(markup)
}
// Block BUILDER.
pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self {
self.renderable = renderable;
self
}
pub fn with_weight(mut self, weight: i8) -> Self {
self.weight = weight;
self
}
pub fn with_id(mut self, id: &str) -> Self {
self.id = util::valid_id(id);
self
}
pub fn with_title(mut self, title: &str) -> Self {
self.title = title.to_string();
self
}
pub fn add_markup(mut self, markup: Markup) -> Self {
self.markup.push(markup);
self
}
pub fn using_template(mut self, template: &str) -> Self {
self.template = template.to_string();
self
}
// Block GETTERS.
pub fn id(&self) -> &str {
util::assigned_value(&self.id)
}
pub fn title(&self) -> &str {
self.title.as_str()
}
pub fn template(&self) -> &str {
self.template.as_str()
}
}
fn always() -> bool {
true
}

View file

@ -5,7 +5,7 @@ enum ContainerType { Column, Row, Wrapper }
pub struct Container {
renderable: fn() -> bool,
weight : i8,
id : String,
id : Option<String>,
container : ContainerType,
components: PageContainer,
template : String,
@ -17,7 +17,7 @@ impl PageComponent for Container {
Container {
renderable: always,
weight : 0,
id : "".to_string(),
id : None,
container : ContainerType::Wrapper,
components: PageContainer::new(),
template : "default".to_string(),
@ -39,7 +39,7 @@ impl PageComponent for Container {
ContainerType::Column => "col",
};
html! {
div class=(classes) {
div id=[&self.id] class=(classes) {
(self.components.render(assets))
}
}
@ -73,7 +73,7 @@ impl Container {
}
pub fn with_id(mut self, id: &str) -> Self {
self.id = id.to_string();
self.id = util::valid_id(id);
self
}
@ -87,10 +87,10 @@ impl Container {
self
}
// Grid GETTERS.
// Container GETTERS.
pub fn id(&self) -> &str {
self.id.as_str()
util::assigned_value(&self.id)
}
pub fn template(&self) -> &str {

View file

@ -23,7 +23,7 @@ impl PageComponent for Button {
name : None,
value : None,
autofocus : None,
disabled : None,
disabled : None,
template : "default".to_string(),
}
}
@ -96,20 +96,12 @@ impl Button {
}
pub fn with_name(mut self, name: &str) -> Self {
self.name = if name.is_empty() {
None
} else {
Some(name.replace(" ", "_"))
};
self.name = util::valid_id(name);
self
}
pub fn with_value(mut self, value: &str) -> Self {
self.value = if value.is_empty() {
None
} else {
Some(value.to_string())
};
self.value = util::optional_value(value);
self
}
@ -137,17 +129,11 @@ impl Button {
// Button GETTERS.
pub fn name(&self) -> &str {
match &self.name {
Some(name) => name.as_str(),
_ => ""
}
util::assigned_value(&self.name)
}
pub fn value(&self) -> &str {
match &self.value {
Some(value) => value.as_str(),
_ => ""
}
util::assigned_value(&self.value)
}
pub fn has_autofocus(&self) -> bool {

View file

@ -107,20 +107,12 @@ impl Date {
}
pub fn with_name(mut self, name: &str) -> Self {
self.name = if name.is_empty() {
None
} else {
Some(name.replace(" ", "_"))
};
self.name = util::valid_id(name);
self
}
pub fn with_value(mut self, value: &str) -> Self {
self.value = if value.is_empty() {
None
} else {
Some(value.to_string())
};
self.value = util::optional_value(value);
self
}
@ -130,11 +122,7 @@ impl Date {
}
pub fn with_placeholder(mut self, placeholder: &str) -> Self {
self.placeholder = if placeholder.is_empty() {
None
} else {
Some(placeholder.to_string())
};
self.placeholder = util::optional_value(placeholder);
self
}
@ -191,17 +179,11 @@ impl Date {
// Date GETTERS.
pub fn name(&self) -> &str {
match &self.name {
Some(name) => name.as_str(),
_ => ""
}
util::assigned_value(&self.name)
}
pub fn value(&self) -> &str {
match &self.value {
Some(value) => value.as_str(),
_ => ""
}
util::assigned_value(&self.value)
}
pub fn label(&self) -> &str {
@ -209,10 +191,7 @@ impl Date {
}
pub fn placeholder(&self) -> &str {
match &self.placeholder {
Some(placeholder) => placeholder.as_str(),
_ => ""
}
util::assigned_value(&self.placeholder)
}
pub fn has_autofocus(&self) -> bool {

View file

@ -71,20 +71,12 @@ impl Form {
}
pub fn with_id(mut self, id: &str) -> Self {
self.id = if id.is_empty() {
None
} else {
Some(id.replace(" ", "_"))
};
self.id = util::valid_id(id);
self
}
pub fn with_action(mut self, action: &str) -> Self {
self.action = if action.is_empty() {
None
} else {
Some(action.to_string())
};
self.action = util::optional_value(action);
self
}
@ -94,11 +86,7 @@ impl Form {
}
pub fn with_charset(mut self, charset: &str) -> Self {
self.charset = if charset.is_empty() {
None
} else {
Some(charset.to_string())
};
self.charset = util::optional_value(charset);
self
}
@ -115,17 +103,11 @@ impl Form {
// Form GETTERS.
pub fn id(&self) -> &str {
match &self.id {
Some(id) => id.as_str(),
_ => ""
}
util::assigned_value(&self.id)
}
pub fn action(&self) -> &str {
match &self.action {
Some(action) => action.as_str(),
_ => ""
}
util::assigned_value(&self.action)
}
pub fn method(&self) -> &str {
@ -136,10 +118,7 @@ impl Form {
}
pub fn charset(&self) -> &str {
match &self.charset {
Some(charset) => charset.as_str(),
_ => ""
}
util::assigned_value(&self.charset)
}
pub fn template(&self) -> &str {

View file

@ -1,29 +1,21 @@
use crate::prelude::*;
pub struct Hidden {
renderable : fn() -> bool,
weight : i8,
name : Option<String>,
value : Option<String>,
template : String,
weight : i8,
name : Option<String>,
value : Option<String>,
}
impl PageComponent for Hidden {
fn prepare() -> Self {
Hidden {
renderable : always,
weight : 0,
name : None,
value : None,
template : "default".to_string(),
weight : 0,
name : None,
value : None,
}
}
fn is_renderable(&self) -> bool {
(self.renderable)()
}
fn weight(&self) -> i8 {
self.weight
}
@ -51,60 +43,28 @@ impl Hidden {
// Hidden BUILDER.
pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self {
self.renderable = renderable;
self
}
pub fn with_weight(mut self, weight: i8) -> Self {
self.weight = weight;
self
}
pub fn with_name(mut self, name: &str) -> Self {
self.name = if name.is_empty() {
None
} else {
Some(name.replace(" ", "_"))
};
self.name = util::valid_id(name);
self
}
pub fn with_value(mut self, value: &str) -> Self {
self.value = if value.is_empty() {
None
} else {
Some(value.to_string())
};
self
}
pub fn using_template(mut self, template: &str) -> Self {
self.template = template.to_string();
self.value = util::optional_value(value);
self
}
// Hidden GETTERS.
pub fn name(&self) -> &str {
match &self.name {
Some(name) => name.as_str(),
_ => ""
}
util::assigned_value(&self.name)
}
pub fn value(&self) -> &str {
match &self.value {
Some(value) => value.as_str(),
_ => ""
}
}
pub fn template(&self) -> &str {
self.template.as_str()
util::assigned_value(&self.value)
}
}
fn always() -> bool {
true
}

View file

@ -162,20 +162,12 @@ impl Input {
}
pub fn with_name(mut self, name: &str) -> Self {
self.name = if name.is_empty() {
None
} else {
Some(name.replace(" ", "_"))
};
self.name = util::valid_id(name);
self
}
pub fn with_value(mut self, value: &str) -> Self {
self.value = if value.is_empty() {
None
} else {
Some(value.to_string())
};
self.value = util::optional_value(value);
self
}
@ -200,11 +192,7 @@ impl Input {
}
pub fn with_placeholder(mut self, placeholder: &str) -> Self {
self.placeholder = if placeholder.is_empty() {
None
} else {
Some(placeholder.to_string())
};
self.placeholder = util::optional_value(placeholder);
self
}
@ -261,17 +249,11 @@ impl Input {
// Input GETTERS.
pub fn name(&self) -> &str {
match &self.name {
Some(name) => name.as_str(),
_ => ""
}
util::assigned_value(&self.name)
}
pub fn value(&self) -> &str {
match &self.value {
Some(value) => value.as_str(),
_ => ""
}
util::assigned_value(&self.value)
}
pub fn label(&self) -> &str {
@ -291,10 +273,7 @@ impl Input {
}
pub fn placeholder(&self) -> &str {
match &self.placeholder {
Some(placeholder) => placeholder.as_str(),
_ => ""
}
util::assigned_value(&self.placeholder)
}
pub fn has_autofocus(&self) -> bool {

252
src/base/component/menu.rs Normal file
View file

@ -0,0 +1,252 @@
use crate::prelude::*;
enum MenuItemType {
Label(String),
Link(String, String),
LinkBlank(String, String),
Markup(Markup),
Separator,
Submenu(String, Menu),
}
// -----------------------------------------------------------------------------
// MenuItem.
// -----------------------------------------------------------------------------
pub struct MenuItem {
renderable: fn() -> bool,
weight : i8,
item_type : Option<MenuItemType>,
}
impl PageComponent for MenuItem {
fn prepare() -> Self {
MenuItem {
renderable: always,
weight : 0,
item_type : None,
}
}
fn is_renderable(&self) -> bool {
(self.renderable)()
}
fn weight(&self) -> i8 {
self.weight
}
fn default_render(&self, assets: &mut PageAssets) -> Markup {
match &self.item_type {
Some(MenuItemType::Label(label)) => html! {
li class="label" { a href="#" { (label) } }
},
Some(MenuItemType::Link(label, path)) => html! {
li class="link" { a href=(path) { (label) } }
},
Some(MenuItemType::LinkBlank(label, path)) => html! {
li class="link_blank" {
a href=(path) target="_blank" { (label) }
}
},
Some(MenuItemType::Markup(markup)) => html! {
li class="markup" { (*markup) }
},
Some(MenuItemType::Submenu(label, menu)) => html! {
li class="submenu" {
a href="#" { (label) }
ul {
(menu.render_items(assets))
}
}
},
Some(MenuItemType::Separator) => html! {
li class="separator" { }
},
None => html! {}
}
}
}
impl MenuItem {
pub fn label(label: &str) -> Self {
MenuItem {
renderable: always,
weight : 0,
item_type : Some(MenuItemType::Label(label.to_string())),
}
}
pub fn link(label: &str, path: &str) -> Self {
MenuItem {
renderable: always,
weight : 0,
item_type : Some(MenuItemType::Link(
label.to_string(),
path.to_string(),
)),
}
}
pub fn link_blank(label: &str, path: &str) -> Self {
MenuItem {
renderable: always,
weight : 0,
item_type : Some(MenuItemType::LinkBlank(
label.to_string(),
path.to_string(),
)),
}
}
pub fn markup(markup: Markup) -> Self {
MenuItem {
renderable: always,
weight : 0,
item_type : Some(MenuItemType::Markup(markup)),
}
}
pub fn separator() -> Self {
MenuItem {
renderable: always,
weight : 0,
item_type : Some(MenuItemType::Separator),
}
}
pub fn submenu(label: &str, menu: Menu) -> Self {
MenuItem {
renderable: always,
weight : 0,
item_type : Some(MenuItemType::Submenu(
label.to_string(),
menu
)),
}
}
// MenuItem BUILDER.
pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self {
self.renderable = renderable;
self
}
pub fn with_weight(mut self, weight: i8) -> Self {
self.weight = weight;
self
}
}
// -----------------------------------------------------------------------------
// Menu.
// -----------------------------------------------------------------------------
pub struct Menu {
renderable: fn() -> bool,
weight : i8,
id : Option<String>,
items : PageContainer,
template : String,
}
impl PageComponent for Menu {
fn prepare() -> Self {
Menu {
renderable: always,
weight : 0,
id : None,
items : PageContainer::new(),
template : "default".to_string(),
}
}
fn is_renderable(&self) -> bool {
(self.renderable)()
}
fn weight(&self) -> i8 {
self.weight
}
fn default_render(&self, assets: &mut PageAssets) -> Markup {
assets
.add_stylesheet(StyleSheet::source(
"/theme/menu/css/menu.css?ver=1.1.1"
))
.add_stylesheet(StyleSheet::source(
"/theme/menu/css/menu-clean.css?ver=1.1.1"
))
.add_javascript(JavaScript::source(
"/theme/menu/js/menu.min.js?ver=1.1.1"
))
.add_jquery();
let id = assets.required_id(self.name(), self.id());
html! {
ul id=(id) class="sm sm-clean" {
(self.render_items(assets))
}
script type="text/javascript" defer {
"jQuery(function(){jQuery('#" (id) "').smartmenus({"
"hideTimeout: 0,"
"showTimeout: 80,"
"});});"
}
}
}
}
impl Menu {
// Menu BUILDER.
pub fn with_renderable(mut self, renderable: fn() -> bool) -> Self {
self.renderable = renderable;
self
}
pub fn with_weight(mut self, weight: i8) -> Self {
self.weight = weight;
self
}
pub fn with_id(mut self, id: &str) -> Self {
self.id = util::valid_id(id);
self
}
pub fn add(mut self, item: MenuItem) -> Self {
self.items.add(item);
self
}
pub fn using_template(mut self, template: &str) -> Self {
self.template = template.to_string();
self
}
// Menu GETTERS.
pub fn id(&self) -> &str {
util::assigned_value(&self.id)
}
pub fn template(&self) -> &str {
self.template.as_str()
}
// Menu EXTRAS.
pub fn render_items(&self, assets: &mut PageAssets) -> Markup {
html! { (self.items.render(assets)) }
}
}
fn always() -> bool {
true
}

View file

@ -2,6 +2,10 @@ mod container;
pub use container::Container;
mod chunck;
pub use chunck::Chunck;
mod block;
pub use block::Block;
mod menu;
pub use menu::{Menu, MenuItem};
pub mod form;
pub use form::{Form, FormMethod};

View file

@ -0,0 +1,17 @@
use crate::prelude::*;
use crate::base::module::admin::summary::summary;
pub struct AdminModule;
impl Module for AdminModule {
fn name(&self) -> String {
"PageTop Admin".to_string()
}
fn configure_module(&self, cfg: &mut server::web::ServiceConfig) {
cfg.service(
server::web::scope("/admin")
.route("", server::web::get().to(summary))
);
}
}

View file

@ -0,0 +1,4 @@
mod configure;
pub use configure::AdminModule;
mod summary;

View file

@ -0,0 +1,54 @@
use crate::prelude::*;
pub async fn summary() -> server::Result<Markup> {
let top_menu = Menu::prepare()
.add(MenuItem::label("Opción 1"))
.add(MenuItem::link("Opción 2", "https://www.google.es"))
.add(MenuItem::link_blank("Opción 3", "https://www.google.es"))
.add(MenuItem::submenu("Submenú 1", Menu::prepare()
.add(MenuItem::label("Opción 1"))
.add(MenuItem::label("Opción 2"))
))
.add(MenuItem::separator())
.add(MenuItem::submenu("Submenú 2", Menu::prepare()
.add(MenuItem::label("Opción 1"))
.add(MenuItem::label("Opción 2"))
))
.add(MenuItem::label("Opción 4"));
let side_menu = Menu::prepare()
.add(MenuItem::label("Opción 1"))
.add(MenuItem::link("Opción 2", "https://www.google.es"))
.add(MenuItem::link_blank("Opción 3", "https://www.google.es"))
.add(MenuItem::submenu("Submenú 1", Menu::prepare()
.add(MenuItem::label("Opción 1"))
.add(MenuItem::label("Opción 2"))
))
.add(MenuItem::separator())
.add(MenuItem::submenu("Submenú 2", Menu::prepare()
.add(MenuItem::label("Opción 1"))
.add(MenuItem::label("Opción 2"))
))
.add(MenuItem::label("Opción 4"));
Page::prepare()
.with_title("Admin")
.add_to("top-menu", top_menu)
.add_to("content", Container::row()
.add(Container::column()
.add(side_menu)
)
.add(Container::column()
.add(Chunck::markup(html! {
p { "Columna 2"}
}))
)
)
.using_template("admin")
.render()
}

View file

@ -1,2 +1,3 @@
pub mod admin;
pub mod homepage;
pub mod user;

View file

@ -26,13 +26,13 @@ impl Theme for BootsierTheme {
)
.add_stylesheet(
StyleSheet::source(
"/bootsier/css/bootstrap.min.css?v=5.1.3"
"/bootsier/css/bootstrap.min.css?ver=5.1.3"
)
.with_weight(-99)
)
.add_javascript(
JavaScript::source(
"/bootsier/js/bootstrap.bundle.min.js?v=5.1.3"
"/bootsier/js/bootstrap.bundle.min.js?ver=5.1.3"
)
.with_weight(-99)
)

View file

@ -177,7 +177,7 @@ pub struct Assets {
stylesheets: Vec<StyleSheet>,
javascripts: Vec<JavaScript>,
with_jquery: bool,
seqid_count: u16,
id_counter : u32,
}
impl Assets {
@ -188,7 +188,7 @@ impl Assets {
stylesheets: Vec::new(),
javascripts: Vec::new(),
with_jquery: false,
seqid_count: 0,
id_counter : 0,
}
}
@ -228,7 +228,7 @@ impl Assets {
if !self.with_jquery {
self.add_javascript(
JavaScript::source(
"/theme/js/jquery.min.js?v=3.6.0"
"/theme/js/jquery.min.js?ver=3.6.0"
)
.with_weight(i8::MIN)
.with_mode(JSMode::Normal)
@ -262,13 +262,20 @@ impl Assets {
}
}
// Assets GETTERS.
// Assets EXTRAS.
pub fn seqid(&mut self, id: &str) -> String {
pub fn required_id(&mut self, prefix: &str, id: &str) -> String {
if id.is_empty() {
self.seqid_count += 1;
return format!("seqid-{}", self.seqid_count);
let prefix = prefix.trim().replace(" ", "_").to_lowercase();
let prefix = if prefix.is_empty() {
"prefix".to_string()
} else {
prefix
};
self.id_counter += 1;
[prefix, self.id_counter.to_string()].join("-")
} else {
id.to_string()
}
id.to_string()
}
}

View file

@ -9,6 +9,14 @@ pub trait Component: Downcast + Send + Sync {
fn prepare() -> Self where Self: Sized;
fn name(&self) -> &str {
let name = type_name::<Self>();
match name.rfind("::") {
Some(position) => &name[(position + 2)..],
None => name
}
}
fn qualified_name(&self) -> &str {
type_name::<Self>()
}

View file

@ -39,6 +39,7 @@ pub fn register_theme(t: &'static (dyn Theme + 'static)) {
pub static MODULES: Lazy<RwLock<Vec<&dyn Module>>> = Lazy::new(|| {
RwLock::new(vec![
&base::module::admin::AdminModule,
&base::module::user::UserModule,
])
});

View file

@ -7,11 +7,11 @@ pub use once_cell::sync::Lazy;
// APIs públicas.
// -----------------------------------------------------------------------------
pub mod macros; // Macros útiles.
pub mod config; // Gestión de la configuración.
pub mod trace; // Traza de ejecución.
pub mod locale; // Localización.
pub mod core; // Servidor web y sistemas para Temas, Módulos y Respuestas.
pub mod base; // Temas, Módulos y Componentes base.
pub mod util; // Macros y funciones útiles.
pub mod prelude; // Re-exporta recursos comunes.

View file

@ -1,19 +0,0 @@
#[macro_export]
/// Macro para construir grupos de pares clave-valor.
///
/// ```
/// let args = args![
/// "userName" => "Roberto",
/// "photoCount" => 3,
/// "userGender" => "male"
/// ];
/// ```
macro_rules! args {
( $($key:expr => $value:expr),* ) => {{
let mut a = std::collections::HashMap::new();
$(
a.insert(String::from($key), $value.into());
)*
a
}};
}

View file

@ -1,6 +1,8 @@
//! Re-exporta recursos comunes.
pub use crate::args;
pub use crate::util;
pub use crate::config::SETTINGS;
pub use crate::trace;
pub use crate::localize;

42
src/util.rs Normal file
View file

@ -0,0 +1,42 @@
#[macro_export]
/// Macro para construir grupos de pares clave-valor.
///
/// ```
/// let args = args![
/// "userName" => "Roberto",
/// "photoCount" => 3,
/// "userGender" => "male"
/// ];
/// ```
macro_rules! args {
( $($key:expr => $value:expr),* ) => {{
let mut a = std::collections::HashMap::new();
$(
a.insert(String::from($key), $value.into());
)*
a
}};
}
pub fn valid_id(id: &str) -> Option<String> {
let id = id.trim().replace(" ", "_").to_lowercase();
match id.is_empty() {
true => None,
false => Some(id),
}
}
pub fn optional_value(value: &str) -> Option<String> {
let value = value.to_string();
match value.is_empty() {
true => None,
false => Some(value),
}
}
pub fn assigned_value(value: &Option<String>) -> &str {
match value {
Some(value) => value.as_str(),
_ => "",
}
}