♻️ Major code restructuring

This commit is contained in:
Manuel Cillero 2024-02-09 14:05:38 +01:00
parent a96e203bb3
commit fa66d628a0
221 changed files with 228 additions and 315 deletions

View file

@ -0,0 +1,87 @@
use crate::prelude::*;
use crate::BaseHandle;
use super::Submenu;
type Content = ArcTypedComponent<Html>;
type SubmenuItems = ArcTypedComponent<Submenu>;
#[derive(SmartDefault)]
pub enum ElementType {
#[default]
Void,
Html(Content),
Submenu(SubmenuItems),
}
// Element.
#[rustfmt::skip]
#[derive(BaseHandle, SmartDefault)]
pub struct Element {
weight : Weight,
renderable : Renderable,
element_type: ElementType,
}
impl ComponentTrait for Element {
fn new() -> Self {
Element::default()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
match self.element_type() {
ElementType::Void => PrepareMarkup::None,
ElementType::Html(content) => PrepareMarkup::With(html! {
(content.render(cx))
}),
ElementType::Submenu(submenu) => PrepareMarkup::With(html! {
(submenu.render(cx))
}),
}
}
}
impl Element {
pub fn html(content: Html) -> Self {
Element {
element_type: ElementType::Html(Content::new(content)),
..Default::default()
}
}
pub fn submenu(submenu: Submenu) -> Self {
Element {
element_type: ElementType::Submenu(SubmenuItems::new(submenu)),
..Default::default()
}
}
// Element BUILDER.
#[fn_with]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_with]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;
self
}
// Element GETTERS.
pub fn element_type(&self) -> &ElementType {
&self.element_type
}
}

View file

@ -0,0 +1,79 @@
use crate::prelude::*;
use crate::BaseHandle;
use super::Element;
#[rustfmt::skip]
#[derive(BaseHandle, SmartDefault)]
pub struct Group {
id : OptionId,
weight : Weight,
renderable: Renderable,
elements : TypedComponents<Element>,
}
impl ComponentTrait for Group {
fn new() -> Self {
Group::default()
}
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
PrepareMarkup::With(html! {
div id=[self.id()] class="menu-group" {
(self.elements().render(cx))
}
})
}
}
impl Group {
// Group BUILDER.
#[fn_with]
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
#[fn_with]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_with]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;
self
}
#[rustfmt::skip]
pub fn add_element(mut self, element: Element) -> Self {
self.elements.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(element)));
self
}
#[fn_with]
pub fn alter_elements(&mut self, op: ArcTypedOp<Element>) -> &mut Self {
self.elements.alter_value(op);
self
}
// Group GETTERS.
pub fn elements(&self) -> &TypedComponents<Element> {
&self.elements
}
}

View file

@ -0,0 +1,207 @@
use crate::prelude::*;
use crate::BaseHandle;
use super::{Megamenu, Submenu};
type Label = L10n;
type Content = ArcTypedComponent<Html>;
type SubmenuItems = ArcTypedComponent<Submenu>;
type MegamenuGroups = ArcTypedComponent<Megamenu>;
#[derive(SmartDefault)]
pub enum ItemType {
#[default]
Void,
Label(Label),
Link(Label, FnContextualPath),
LinkBlank(Label, FnContextualPath),
Html(Content),
Submenu(Label, SubmenuItems),
Megamenu(Label, MegamenuGroups),
}
// Item.
#[rustfmt::skip]
#[derive(BaseHandle, SmartDefault)]
pub struct Item {
weight : Weight,
renderable : Renderable,
item_type : ItemType,
description: OptionTranslated,
left_icon : OptionComponent<Icon>,
right_icon : OptionComponent<Icon>,
}
impl ComponentTrait for Item {
fn new() -> Self {
Item::default()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
let description = self.description.using(cx.langid());
let left_icon = self.left_icon().render(cx);
let right_icon = self.right_icon().render(cx);
match self.item_type() {
ItemType::Void => PrepareMarkup::None,
ItemType::Label(label) => PrepareMarkup::With(html! {
li class="pt-menu__label" {
span title=[description] {
(left_icon)
(label.escaped(cx.langid()))
(right_icon)
}
}
}),
ItemType::Link(label, path) => PrepareMarkup::With(html! {
li class="pt-menu__link" {
a href=(path(cx)) title=[description] {
(left_icon)
(label.escaped(cx.langid()))
(right_icon)
}
}
}),
ItemType::LinkBlank(label, path) => PrepareMarkup::With(html! {
li class="pt-menu__link" {
a href=(path(cx)) title=[description] target="_blank" {
(left_icon)
(label.escaped(cx.langid()))
(right_icon)
}
}
}),
ItemType::Html(content) => PrepareMarkup::With(html! {
li class="pt-menu__html" {
(content.render(cx))
}
}),
ItemType::Submenu(label, submenu) => PrepareMarkup::With(html! {
li class="pt-menu__children" {
a href="#" title=[description] {
(left_icon)
(label.escaped(cx.langid())) i class="pt-menu__icon bi-chevron-down" {}
}
div class="pt-menu__subs" {
(submenu.render(cx))
}
}
}),
ItemType::Megamenu(label, megamenu) => PrepareMarkup::With(html! {
li class="pt-menu__children" {
a href="#" title=[description] {
(left_icon)
(label.escaped(cx.langid())) i class="pt-menu__icon bi-chevron-down" {}
}
div class="pt-menu__subs pt-menu__mega" {
(megamenu.render(cx))
}
}
}),
}
}
}
impl Item {
pub fn label(label: L10n) -> Self {
Item {
item_type: ItemType::Label(label),
..Default::default()
}
}
pub fn link(label: L10n, path: FnContextualPath) -> Self {
Item {
item_type: ItemType::Link(label, path),
..Default::default()
}
}
pub fn link_blank(label: L10n, path: FnContextualPath) -> Self {
Item {
item_type: ItemType::LinkBlank(label, path),
..Default::default()
}
}
pub fn html(content: Html) -> Self {
Item {
item_type: ItemType::Html(Content::new(content)),
..Default::default()
}
}
pub fn submenu(label: L10n, submenu: Submenu) -> Self {
Item {
item_type: ItemType::Submenu(label, SubmenuItems::new(submenu)),
..Default::default()
}
}
pub fn megamenu(label: L10n, megamenu: Megamenu) -> Self {
Item {
item_type: ItemType::Megamenu(label, MegamenuGroups::new(megamenu)),
..Default::default()
}
}
// Item BUILDER.
#[fn_with]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_with]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;
self
}
#[fn_with]
pub fn alter_description(&mut self, text: L10n) -> &mut Self {
self.description.alter_value(text);
self
}
#[fn_with]
pub fn alter_left_icon(&mut self, icon: Option<Icon>) -> &mut Self {
self.left_icon.alter_value(icon);
self
}
#[fn_with]
pub fn alter_right_icon(&mut self, icon: Option<Icon>) -> &mut Self {
self.right_icon.alter_value(icon);
self
}
// Item GETTERS.
pub fn item_type(&self) -> &ItemType {
&self.item_type
}
pub fn description(&self) -> &OptionTranslated {
&self.description
}
pub fn left_icon(&self) -> &OptionComponent<Icon> {
&self.left_icon
}
pub fn right_icon(&self) -> &OptionComponent<Icon> {
&self.right_icon
}
}

View file

@ -0,0 +1,79 @@
use crate::prelude::*;
use crate::BaseHandle;
use super::Group;
#[rustfmt::skip]
#[derive(BaseHandle, SmartDefault)]
pub struct Megamenu {
id : OptionId,
weight : Weight,
renderable: Renderable,
groups : TypedComponents<Group>,
}
impl ComponentTrait for Megamenu {
fn new() -> Self {
Megamenu::default()
}
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
PrepareMarkup::With(html! {
div id=[self.id()] class="pt-menu__groups" {
(self.groups().render(cx))
}
})
}
}
impl Megamenu {
// Megamenu BUILDER.
#[fn_with]
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
#[fn_with]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_with]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;
self
}
#[rustfmt::skip]
pub fn add_group(mut self, group: Group) -> Self {
self.groups.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(group)));
self
}
#[fn_with]
pub fn alter_groups(&mut self, op: ArcTypedOp<Group>) -> &mut Self {
self.groups.alter_value(op);
self
}
// Megamenu GETTERS.
pub fn groups(&self) -> &TypedComponents<Group> {
&self.groups
}
}

View file

@ -0,0 +1,107 @@
use crate::prelude::*;
use crate::BaseHandle;
use super::Item;
#[rustfmt::skip]
#[derive(BaseHandle, SmartDefault)]
pub struct Menu {
id : OptionId,
weight : Weight,
renderable: Renderable,
items : TypedComponents<Item>,
}
impl ComponentTrait for Menu {
fn new() -> Self {
Menu::default()
}
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
cx.set_param::<bool>(PARAM_BASE_INCLUDE_MENU_ASSETS, true);
cx.set_param::<bool>(PARAM_BASE_INCLUDE_ICONS, true);
PrepareMarkup::With(html! {
div id=[self.id()] class="pt-menu__container" {
div class="pt-menu__wrapper" {
div class="pt-menu__main" {
div class="pt-menu__overlay" {}
nav class="pt-menu__nav" {
div class="pt-menu__header" {
button type="button" class="pt-menu__arrow" {
i class="bi-chevron-left" {}
}
div class="pt-menu__title" {}
button type="button" class="pt-menu__close" {
i class="bi-x" {}
}
}
ul class="pt-menu__section" {
(self.items().render(cx))
}
}
}
button
type="button"
class="pt-menu__trigger"
title=[L10n::l("menu_toggle").using(cx.langid())]
{
span {} span {} span {}
}
}
}
})
}
}
impl Menu {
// Menu BUILDER.
#[fn_with]
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
#[fn_with]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_with]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;
self
}
#[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(item)));
self
}
#[fn_with]
pub fn alter_items(&mut self, op: ArcTypedOp<Item>) -> &mut Self {
self.items.alter_value(op);
self
}
// Menu GETTERS.
pub fn items(&self) -> &TypedComponents<Item> {
&self.items
}
}

View file

@ -0,0 +1,95 @@
use crate::prelude::*;
use crate::BaseHandle;
use super::Item;
#[rustfmt::skip]
#[derive(BaseHandle, SmartDefault)]
pub struct Submenu {
id : OptionId,
weight : Weight,
renderable: Renderable,
title : OptionTranslated,
items : TypedComponents<Item>,
}
impl ComponentTrait for Submenu {
fn new() -> Self {
Submenu::default()
}
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
PrepareMarkup::With(html! {
div id=[self.id()] class="pt-menu__items" {
@if let Some(title) = self.title().using(cx.langid()) {
h4 class="pt-menu__title" { (title) }
}
ul {
(self.items().render(cx))
}
}
})
}
}
impl Submenu {
// Submenu BUILDER.
#[fn_with]
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
#[fn_with]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_with]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;
self
}
#[fn_with]
pub fn alter_title(&mut self, title: L10n) -> &mut Self {
self.title.alter_value(title);
self
}
#[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(item)));
self
}
#[fn_with]
pub fn alter_items(&mut self, op: ArcTypedOp<Item>) -> &mut Self {
self.items.alter_value(op);
self
}
// Submenu GETTERS.
pub fn title(&self) -> &OptionTranslated {
&self.title
}
pub fn items(&self) -> &TypedComponents<Item> {
&self.items
}
}