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};