use super::traits::Renderable
pub enum NavbarPosition {
Top,
Bottom,
}
pub struct NavbarItem {
label: string,
href: string,
}
impl NavbarItem {
pub fn new(label: string, href: string) -> NavbarItem {
NavbarItem {
label: label,
href: href,
}
}
}
pub struct Navbar {
brand: string,
items: Vec<NavbarItem>,
position: NavbarPosition,
sticky: bool,
}
impl Navbar {
pub fn new() -> Navbar {
Navbar {
brand: String::from(""),
items: Vec::new(),
position: NavbarPosition::Top,
sticky: false,
}
}
pub fn brand(self, brand: string) -> Navbar {
self.brand = brand
self
}
pub fn item(self, item: NavbarItem) -> Navbar {
self.items.push(item)
self
}
pub fn position(self, pos: NavbarPosition) -> Navbar {
self.position = pos
self
}
pub fn sticky(self, sticky: bool) -> Navbar {
self.sticky = sticky
self
}
}
impl Renderable for Navbar {
fn render(self) -> string {
let mut items_html = Vec::new()
for item in self.items {
items_html.push(format!("<a href='{}' class='wj-navbar-item'>{}</a>", item.href, item.label))
}
let position_class = match self.position {
NavbarPosition::Top => "wj-navbar-top",
NavbarPosition::Bottom => "wj-navbar-bottom",
}
let sticky_class = if self.sticky { " wj-navbar-sticky" } else { "" }
let brand_html = if self.brand.len() > 0 {
format!("<div class='wj-navbar-brand'>{}</div>", self.brand)
} else {
String::from("")
}
format!(
"<nav class='wj-navbar {} {}'>{}<div class='wj-navbar-items'>{}</div></nav>",
position_class,
sticky_class,
brand_html,
items_html.join("")
)
}
}