Actualiza y sanea piezas de código sensible

This commit is contained in:
Manuel Cillero 2022-03-01 22:15:41 +01:00
parent db3efa9ef9
commit cac4c2f102
9 changed files with 29 additions and 29 deletions

View file

@ -31,7 +31,7 @@ impl PageComponent for Block {
}
fn default_render(&self, assets: &mut PageAssets) -> Markup {
let id = assets.required_id(self.name(), self.id());
let id = assets.serial_id(self.name(), self.id());
html! {
div id=(id) class="block" {
@if !self.title.is_empty() {

View file

@ -186,7 +186,7 @@ impl PageComponent for Menu {
))
.add_jquery();
let id = assets.required_id(self.name(), self.id());
let id = assets.serial_id(self.name(), self.id());
html! {
ul id=(id) class="sm sm-clean" {
(self.render_items(assets))

View file

@ -3,7 +3,7 @@ use crate::config::SETTINGS;
use crate::core::all;
use crate::core::theme::{Markup, PreEscaped, Theme, find_theme, html};
pub static DEFAULT_THEME: Lazy<&dyn Theme> = Lazy::new(|| {
static DEFAULT_THEME: Lazy<&dyn Theme> = Lazy::new(|| {
for t in all::THEMES.read().unwrap().iter() {
if t.name().to_lowercase() == SETTINGS.app.theme.to_lowercase() {
return *t;
@ -183,7 +183,7 @@ impl JavaScript {
// Page assets.
// -----------------------------------------------------------------------------
pub struct Assets {
pub struct PageAssets {
theme : &'static dyn Theme,
favicon : Option<Favicon>,
metadata : Vec<(String, String)>,
@ -193,9 +193,9 @@ pub struct Assets {
id_counter : u32,
}
impl Assets {
impl PageAssets {
pub fn new() -> Self {
Assets {
PageAssets {
theme : *DEFAULT_THEME,
favicon : None,
metadata : Vec::new(),
@ -206,8 +206,8 @@ impl Assets {
}
}
pub fn using_theme(&mut self, theme_id: &str) -> &mut Self {
self.theme = find_theme(theme_id).unwrap_or(*DEFAULT_THEME);
pub fn using_theme(&mut self, theme_name: &str) -> &mut Self {
self.theme = find_theme(theme_name).unwrap_or(*DEFAULT_THEME);
self
}
@ -291,7 +291,7 @@ impl Assets {
// Assets EXTRAS.
pub fn required_id(&mut self, prefix: &str, id: &str) -> String {
pub fn serial_id(&mut self, prefix: &str, id: &str) -> String {
if id.is_empty() {
let prefix = prefix.trim().replace(" ", "_").to_lowercase();
let prefix = if prefix.is_empty() {

View file

@ -5,7 +5,7 @@ use downcast_rs::{Downcast, impl_downcast};
use std::any::type_name;
pub trait Component: Downcast + Send + Sync {
pub trait PageComponent: Downcast + Send + Sync {
fn prepare() -> Self where Self: Sized;
@ -39,4 +39,4 @@ pub trait Component: Downcast + Send + Sync {
}
}
impl_downcast!(Component);
impl_downcast!(PageComponent);

View file

@ -4,15 +4,15 @@ use crate::core::response::page::{PageAssets, PageComponent, render_component};
use std::sync::Arc;
#[derive(Clone)]
pub struct Container(Vec<Arc<dyn PageComponent>>);
pub struct PageContainer(Vec<Arc<dyn PageComponent>>);
impl Container {
impl PageContainer {
pub fn new() -> Self {
Container(Vec::new())
PageContainer(Vec::new())
}
pub fn new_with(component: impl PageComponent) -> Self {
let mut container = Container::new();
let mut container = PageContainer::new();
container.add(component);
container
}

View file

@ -1,11 +1,16 @@
pub mod assets;
pub use assets::Assets as PageAssets;
mod assets;
pub use assets::{
Favicon,
StyleSheet,
JavaScript, JSMode,
PageAssets,
};
mod component;
pub use component::Component as PageComponent;
pub use component::PageComponent;
mod container;
pub use container::Container as PageContainer;
pub use container::PageContainer;
mod page;
pub use page::Page;

View file

@ -7,7 +7,7 @@ use crate::core::response::page::{PageAssets, PageComponent, PageContainer};
use std::borrow::Cow;
use std::collections::HashMap;
pub static DEFAULT_LANGUAGE: Lazy<Option<String>> = Lazy::new(|| {
static DEFAULT_LANGUAGE: Lazy<Option<String>> = Lazy::new(|| {
let language = SETTINGS.app.language[..2].to_lowercase();
if !language.is_empty() {
Some(language)
@ -16,7 +16,7 @@ pub static DEFAULT_LANGUAGE: Lazy<Option<String>> = Lazy::new(|| {
}
});
pub static DEFAULT_DIRECTION: Lazy<Option<String>> = Lazy::new(|| {
static DEFAULT_DIRECTION: Lazy<Option<String>> = Lazy::new(|| {
let direction = SETTINGS.app.direction.to_lowercase();
match direction.as_str() {
"auto" => Some("auto".to_string()),
@ -188,8 +188,8 @@ impl<'a> Page<'a> {
// Page EXTRAS.
pub fn using_theme(&mut self, theme_id: &str) -> &mut Self {
self.assets.using_theme(theme_id);
pub fn using_theme(&mut self, theme_name: &str) -> &mut Self {
self.assets.using_theme(theme_name);
self
}
}

View file

@ -80,7 +80,7 @@ pub trait Theme: Send + Sync {
/*
Cómo usarlo:
match component.handle() {
match component.name() {
"block" => {
let block = component.downcast_mut::<Block>().unwrap();
match block.template() {

View file

@ -8,13 +8,8 @@ pub use crate::trace;
pub use crate::localize;
pub use crate::core::theme::*;
pub use crate::core::module::*;
pub use crate::core::response::page::*;
pub use crate::core::response::page::assets::*;
pub use crate::core::server;
pub use crate::base::theme::*;
pub use crate::base::component::*;