Añade funcionalidades en bundles de componentes

This commit is contained in:
Manuel Cillero 2023-06-11 03:29:25 +02:00
parent a455488044
commit 4ae6580357
11 changed files with 114 additions and 15 deletions

View file

@ -260,6 +260,12 @@ impl MegaMenu {
self
}
#[fn_builder]
pub fn alter_bundle(&mut self, op: BundleOp, item: MegaMenuItem) -> &mut Self {
self.items.alter_bundle(op, item);
self
}
#[fn_builder]
pub fn alter_template(&mut self, template: &str) -> &mut Self {
self.template = template.to_owned();

View file

@ -164,6 +164,12 @@ impl Container {
self
}
#[fn_builder]
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_bundle(op, component);
self
}
#[fn_builder]
pub fn alter_template(&mut self, template: &str) -> &mut Self {
self.template = template.to_owned();

View file

@ -130,6 +130,12 @@ impl Form {
self
}
#[fn_builder]
pub fn alter_bundle(&mut self, op: BundleOp, element: impl ComponentTrait) -> &mut Self {
self.elements.alter_bundle(op, element);
self
}
#[fn_builder]
pub fn alter_template(&mut self, template: &str) -> &mut Self {
self.template = template.to_owned();

View file

@ -145,6 +145,12 @@ impl Column {
self
}
#[fn_builder]
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_bundle(op, component);
self
}
#[fn_builder]
pub fn alter_template(&mut self, template: &str) -> &mut Self {
self.template = template.to_owned();

View file

@ -92,6 +92,12 @@ impl Row {
self
}
#[fn_builder]
pub fn alter_bundle(&mut self, op: BundleOp, column: grid::Column) -> &mut Self {
self.columns.alter_bundle(op, column);
self
}
#[fn_builder]
pub fn alter_template(&mut self, template: &str) -> &mut Self {
self.template = template.to_owned();

View file

@ -103,6 +103,12 @@ impl Paragraph {
self
}
#[fn_builder]
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_bundle(op, component);
self
}
#[rustfmt::skip]
#[fn_builder]
pub fn alter_display(&mut self, display: ParagraphDisplay) -> &mut Self {

View file

@ -67,6 +67,12 @@ impl ComponentTrait for Block {
impl Block {
// Block BUILDER.
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
self.id.alter_value(id);
self
}
#[fn_builder]
pub fn alter_weight(&mut self, weight: isize) -> &mut Self {
self.weight = weight;
@ -79,12 +85,6 @@ impl Block {
self
}
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
self.id.alter_value(id);
self
}
#[fn_builder]
pub fn alter_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
self.classes.alter_value(op, classes);
@ -103,6 +103,12 @@ impl Block {
self
}
#[fn_builder]
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_bundle(op, component);
self
}
#[fn_builder]
pub fn alter_template(&mut self, template: &str) -> &mut Self {
self.template = template.to_owned();

View file

@ -1,8 +1,4 @@
use crate::core::module::ModuleTrait;
use crate::core::theme::{ThemeStaticRef, ThemeTrait};
use crate::html::Favicon;
use crate::response::page::Page;
use crate::{define_handle, serve_static_files, service, Handle};
use crate::prelude::*;
define_handle!(THEME_BASIC);

View file

@ -8,7 +8,7 @@ mod one;
pub use one::OneComponent;
mod bundle;
pub use bundle::ComponentsBundle;
pub use bundle::{BundleOp, ComponentsBundle};
mod renderable;
pub use renderable::{IsRenderable, Renderable};

View file

@ -3,6 +3,16 @@ use crate::html::{html, Markup};
use std::sync::{Arc, RwLock};
pub enum BundleOp {
Add,
AddAfterId(&'static str),
AddBeforeId(&'static str),
AddFirst,
RemoveById(&'static str),
ReplaceById(&'static str),
Reset,
}
#[derive(Clone, Default)]
pub struct ComponentsBundle(Vec<Arc<RwLock<dyn ComponentTrait>>>);
@ -31,14 +41,62 @@ impl ComponentsBundle {
}
}
pub fn add(&mut self, component: impl ComponentTrait) {
// ComponentsBundle BUILDER.
pub fn add(&mut self, component: impl ComponentTrait) -> &mut Self {
self.0.push(Arc::new(RwLock::new(component)));
self
}
pub fn clear(&mut self) {
self.0.clear();
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
let arc = Arc::new(RwLock::new(component));
match op {
BundleOp::Add => self.0.push(arc),
BundleOp::AddAfterId(id) => {
match self
.0
.iter()
.position(|c| c.read().unwrap().id().as_deref() == Some(id))
{
Some(index) => self.0.insert(index + 1, arc),
_ => self.0.push(arc),
}
}
BundleOp::AddBeforeId(id) => {
match self
.0
.iter()
.position(|c| c.read().unwrap().id().as_deref() == Some(id))
{
Some(index) => self.0.insert(index, arc),
_ => self.0.insert(0, arc),
}
}
BundleOp::AddFirst => self.0.insert(0, arc),
BundleOp::RemoveById(id) => {
if let Some(index) = self
.0
.iter()
.position(|c| c.read().unwrap().id().as_deref() == Some(id))
{
self.0.remove(index);
}
}
BundleOp::ReplaceById(id) => {
for c in self.0.iter_mut() {
if c.read().unwrap().id().as_deref() == Some(id) {
*c = arc;
break;
}
}
}
BundleOp::Reset => self.0.clear(),
}
self
}
// ComponentsBundle RENDER.
pub fn render(&self, rcx: &mut RenderContext) -> Markup {
let mut components = self.0.clone();
components.sort_by_key(|c| c.read().unwrap().weight());

View file

@ -9,6 +9,9 @@ pub use crate::{
// Macros declarativas globales.
pub use crate::{args, define_config, define_handle, define_locale, serve_static_files};
// Traducciones globales.
pub use crate::LOCALE_PAGETOP;
// Funciones útiles.
pub use crate::util;