🚧 [megamenu] Improve linking with dynamic urls

This commit is contained in:
Manuel Cillero 2023-07-31 11:46:28 +02:00
parent c054081390
commit 7b821da411
3 changed files with 45 additions and 46 deletions

View file

@ -2,18 +2,20 @@ use pagetop::prelude::*;
use crate::component::MegaMenu; use crate::component::MegaMenu;
new_handle!(COMPONENT_MEGAMENUITEM); new_handle!(COMPONENT_MEGAITEM);
type Label = OneComponent<L10n>; type Label = OneComponent<L10n>;
type Content = OneComponent<L10n>; type Content = OneComponent<Html>;
pub type MegaItemPath = fn(cx: &Context) -> &str;
#[derive(Default)] #[derive(Default)]
pub enum MegaMenuItemType { pub enum MegaItemType {
#[default] #[default]
Void, Void,
Label(Label), Label(Label),
Link(Label, String), Link(Label, MegaItemPath),
LinkBlank(Label, String), LinkBlank(Label, MegaItemPath),
Html(Content), Html(Content),
Submenu(Label, MegaMenu), Submenu(Label, MegaMenu),
Separator, Separator,
@ -23,19 +25,19 @@ pub enum MegaMenuItemType {
#[rustfmt::skip] #[rustfmt::skip]
#[derive(Default)] #[derive(Default)]
pub struct MegaMenuItem { pub struct MegaItem {
weight : Weight, weight : Weight,
renderable: Renderable, renderable: Renderable,
item_type : MegaMenuItemType, item_type : MegaItemType,
} }
impl ComponentTrait for MegaMenuItem { impl ComponentTrait for MegaItem {
fn new() -> Self { fn new() -> Self {
MegaMenuItem::default() MegaItem::default()
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
COMPONENT_MEGAMENUITEM COMPONENT_MEGAITEM
} }
fn weight(&self) -> Weight { fn weight(&self) -> Weight {
@ -48,23 +50,20 @@ impl ComponentTrait for MegaMenuItem {
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
match self.item_type() { match self.item_type() {
MegaMenuItemType::Void => PrepareMarkup::None, MegaItemType::Void => PrepareMarkup::None,
MegaItemType::Label(label) => PrepareMarkup::With(html! {
MegaMenuItemType::Label(label) => PrepareMarkup::With(html! { li class="link" { a href="#" { (label.prepare(cx)) } }
li class="label" { a href="#" { (label.prepare(cx)) } }
}), }),
MegaMenuItemType::Link(label, path) => PrepareMarkup::With(html! { MegaItemType::Link(label, path) => PrepareMarkup::With(html! {
li class="link" { a href=(path) { (label.prepare(cx)) } } li class="link" { a href=(path(cx)) { (label.prepare(cx)) } }
}), }),
MegaMenuItemType::LinkBlank(label, path) => PrepareMarkup::With(html! { MegaItemType::LinkBlank(label, path) => PrepareMarkup::With(html! {
li class="link_blank" { li class="link" { a href=(path(cx)) target="_blank" { (label.prepare(cx)) } }
a href=(path) target="_blank" { (label.prepare(cx)) }
}
}), }),
MegaMenuItemType::Html(content) => PrepareMarkup::With(html! { MegaItemType::Html(content) => PrepareMarkup::With(html! {
li class="html" { (content.prepare(cx)) } li class="html" { (content.prepare(cx)) }
}), }),
MegaMenuItemType::Submenu(label, menu) => PrepareMarkup::With(html! { MegaItemType::Submenu(label, menu) => PrepareMarkup::With(html! {
li class="submenu" { li class="submenu" {
a href="#" { (label.prepare(cx)) } a href="#" { (label.prepare(cx)) }
ul { ul {
@ -72,57 +71,57 @@ impl ComponentTrait for MegaMenuItem {
} }
} }
}), }),
MegaMenuItemType::Separator => PrepareMarkup::With(html! { MegaItemType::Separator => PrepareMarkup::With(html! {
li class="separator" { } li class="separator" { }
}), }),
} }
} }
} }
impl MegaMenuItem { impl MegaItem {
pub fn label(label: L10n) -> Self { pub fn label(label: L10n) -> Self {
MegaMenuItem { MegaItem {
item_type: MegaMenuItemType::Label(OneComponent::new_with(label)), item_type: MegaItemType::Label(OneComponent::new_with(label)),
..Default::default() ..Default::default()
} }
} }
pub fn link(label: L10n, path: &str) -> Self { pub fn link(label: L10n, path: MegaItemPath) -> Self {
MegaMenuItem { MegaItem {
item_type: MegaMenuItemType::Link(OneComponent::new_with(label), path.to_owned()), item_type: MegaItemType::Link(OneComponent::new_with(label), path),
..Default::default() ..Default::default()
} }
} }
pub fn link_blank(label: L10n, path: &str) -> Self { pub fn link_blank(label: L10n, path: MegaItemPath) -> Self {
MegaMenuItem { MegaItem {
item_type: MegaMenuItemType::LinkBlank(OneComponent::new_with(label), path.to_owned()), item_type: MegaItemType::LinkBlank(OneComponent::new_with(label), path),
..Default::default() ..Default::default()
} }
} }
pub fn html(content: L10n) -> Self { pub fn html(content: Html) -> Self {
MegaMenuItem { MegaItem {
item_type: MegaMenuItemType::Html(OneComponent::new_with(content)), item_type: MegaItemType::Html(OneComponent::new_with(content)),
..Default::default() ..Default::default()
} }
} }
pub fn submenu(label: L10n, menu: MegaMenu) -> Self { pub fn submenu(label: L10n, menu: MegaMenu) -> Self {
MegaMenuItem { MegaItem {
item_type: MegaMenuItemType::Submenu(OneComponent::new_with(label), menu), item_type: MegaItemType::Submenu(OneComponent::new_with(label), menu),
..Default::default() ..Default::default()
} }
} }
pub fn separator() -> Self { pub fn separator() -> Self {
MegaMenuItem { MegaItem {
item_type: MegaMenuItemType::Separator, item_type: MegaItemType::Separator,
..Default::default() ..Default::default()
} }
} }
// MegaMenuItem BUILDER. // MegaItem BUILDER.
#[fn_builder] #[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self { pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
@ -136,9 +135,9 @@ impl MegaMenuItem {
self self
} }
// MegaMenuItem GETTERS. // MegaItem GETTERS.
pub fn item_type(&self) -> &MegaMenuItemType { pub fn item_type(&self) -> &MegaItemType {
&self.item_type &self.item_type
} }
} }

View file

@ -1,7 +1,7 @@
use pagetop::prelude::*; use pagetop::prelude::*;
use pagetop_jquery::JQuery; use pagetop_jquery::JQuery;
use crate::component::MegaMenuItem; use crate::component::MegaItem;
use crate::LOCALES_MEGAMENU; use crate::LOCALES_MEGAMENU;
new_handle!(COMPONENT_MEGAMENU); new_handle!(COMPONENT_MEGAMENU);
@ -138,12 +138,12 @@ impl MegaMenu {
self self
} }
pub fn with_item(mut self, item: MegaMenuItem) -> Self { pub fn with_item(mut self, item: MegaItem) -> Self {
self.items.alter(PackOp::Add, ComponentRef::to(item)); self.items.alter(PackOp::Add, ComponentRef::to(item));
self self
} }
pub fn alter_items(&mut self, op: PackOp, item: MegaMenuItem) -> &mut Self { pub fn alter_items(&mut self, op: PackOp, item: MegaItem) -> &mut Self {
self.items.alter(op, ComponentRef::to(item)); self.items.alter(op, ComponentRef::to(item));
self self
} }

View file

@ -2,7 +2,7 @@ use pagetop::prelude::*;
pub mod component { pub mod component {
mod item; mod item;
pub use item::{MegaMenuItem, MegaMenuItemType, COMPONENT_MEGAMENUITEM}; pub use item::{MegaItem, MegaItemPath, MegaItemType, COMPONENT_MEGAITEM};
mod menu; mod menu;
pub use menu::{MegaMenu, COMPONENT_MEGAMENU}; pub use menu::{MegaMenu, COMPONENT_MEGAMENU};
} }