|
| 1 | +use crate::components::component; |
| 2 | +use crate::components::component::Component; |
| 3 | +use sailfish::TemplateOnce; |
| 4 | + |
| 5 | +use crate::components::StaticNavLink; |
| 6 | + |
| 7 | +pub enum DropdownValue { |
| 8 | + Icon(Component), |
| 9 | + Text(Component), |
| 10 | +} |
| 11 | + |
| 12 | +impl Default for DropdownValue { |
| 13 | + fn default() -> Self { |
| 14 | + DropdownValue::Text("Menu".into()) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(TemplateOnce, Default)] |
| 19 | +#[template(path = "dropdown/template.html")] |
| 20 | +pub struct Dropdown { |
| 21 | + /// The currently selected value. |
| 22 | + value: DropdownValue, |
| 23 | + |
| 24 | + /// The list of dropdown links to render. |
| 25 | + links: Vec<StaticNavLink>, |
| 26 | + |
| 27 | + /// Position of the dropdown menu. |
| 28 | + offset: String, |
| 29 | + |
| 30 | + /// Whether or not the dropdown is collapsble. |
| 31 | + collapsable: bool, |
| 32 | + offset_collapsed: String, |
| 33 | + |
| 34 | + /// Where the dropdown menu should appear |
| 35 | + menu_position: String, |
| 36 | + expandable: bool, |
| 37 | +} |
| 38 | + |
| 39 | +impl Dropdown { |
| 40 | + pub fn new(links: Vec<StaticNavLink>) -> Self { |
| 41 | + let binding = links |
| 42 | + .iter() |
| 43 | + .filter(|link| link.active) |
| 44 | + .collect::<Vec<&StaticNavLink>>(); |
| 45 | + let active = binding.first(); |
| 46 | + let value = if let Some(active) = active { |
| 47 | + active.name.to_owned() |
| 48 | + } else { |
| 49 | + "Menu".to_owned() |
| 50 | + }; |
| 51 | + Dropdown { |
| 52 | + links, |
| 53 | + value: DropdownValue::Text(value.into()), |
| 54 | + offset: "0, 10".to_owned(), |
| 55 | + offset_collapsed: "68, -44".to_owned(), |
| 56 | + menu_position: "".to_owned(), |
| 57 | + ..Default::default() |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + pub fn text(mut self, value: Component) -> Self { |
| 62 | + self.value = DropdownValue::Text(value); |
| 63 | + self |
| 64 | + } |
| 65 | + |
| 66 | + pub fn icon(mut self, icon: Component) -> Self { |
| 67 | + self.value = DropdownValue::Icon(icon); |
| 68 | + self |
| 69 | + } |
| 70 | + |
| 71 | + pub fn collapsable(mut self) -> Self { |
| 72 | + self.collapsable = true; |
| 73 | + self |
| 74 | + } |
| 75 | + |
| 76 | + pub fn menu_end(mut self) -> Self { |
| 77 | + self.menu_position = "dropdown-menu-end".to_owned(); |
| 78 | + self |
| 79 | + } |
| 80 | + |
| 81 | + pub fn menu_start(mut self) -> Self { |
| 82 | + self.menu_position = "dropdown-menu-start".to_owned(); |
| 83 | + self |
| 84 | + } |
| 85 | + |
| 86 | + pub fn offset(mut self, offset: &str) -> Self { |
| 87 | + self.offset = offset.to_owned(); |
| 88 | + self |
| 89 | + } |
| 90 | + |
| 91 | + pub fn offset_collapsed(mut self, offset: &str) -> Self { |
| 92 | + self.offset_collapsed = offset.to_owned(); |
| 93 | + self |
| 94 | + } |
| 95 | + |
| 96 | + pub fn expandable(mut self) -> Self { |
| 97 | + self.expandable = true; |
| 98 | + self |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +component!(Dropdown); |
0 commit comments