use super::prelude::*;
use crate::tree::{AttributeMap, Element};
#[derive(Debug, Copy, Clone)]
pub struct Collapsible<'a> {
elements: &'a [Element<'a>],
attributes: &'a AttributeMap<'a>,
start_open: bool,
show_text: Option<&'a str>,
hide_text: Option<&'a str>,
show_top: bool,
show_bottom: bool,
}
impl<'a> Collapsible<'a> {
#[inline]
pub fn new(
elements: &'a [Element<'a>],
attributes: &'a AttributeMap<'a>,
start_open: bool,
show_text: Option<&'a str>,
hide_text: Option<&'a str>,
show_top: bool,
show_bottom: bool,
) -> Self {
Collapsible {
elements,
attributes,
start_open,
show_text,
hide_text,
show_top,
show_bottom,
}
}
}
pub fn render_collapsible(ctx: &mut HtmlContext, collapsible: Collapsible) {
let Collapsible {
elements,
attributes,
start_open,
show_text,
hide_text,
show_top,
show_bottom,
} = collapsible;
debug!(
"Rendering collapsible (elements length {}, start-open {}, show-text {}, hide-text {}, show-top {}, show-bottom {})",
elements.len(),
start_open,
show_text.unwrap_or("<default>"),
hide_text.unwrap_or("<default>"),
show_top,
show_bottom,
);
let show_text = show_text
.unwrap_or_else(|| ctx.handle().get_message(ctx.language(), "collapsible-open"));
let hide_text = hide_text
.unwrap_or_else(|| ctx.handle().get_message(ctx.language(), "collapsible-hide"));
ctx.html()
.details()
.attr(attr!(
"class" => "wj-collapsible",
"open"; if start_open,
"data-show-top"; if show_top,
"data-show-bottom"; if show_bottom;;
attributes,
))
.inner(|ctx| {
ctx.html()
.summary()
.attr(attr!(
"class" => "wj-collapsible-button wj-collapsible-button-top",
))
.inner(|ctx| {
ctx.html()
.span()
.attr(attr!("class" => "wj-collapsible-show-text"))
.contents(show_text);
ctx.html()
.span()
.attr(attr!("class" => "wj-collapsible-hide-text"))
.contents(hide_text);
});
ctx.html()
.div()
.attr(attr!("class" => "wj-collapsible-content"))
.contents(elements);
if show_bottom {
ctx.html()
.element("wj-collapsible-button-bottom")
.attr(attr!(
"class" => "wj-collapsible-button wj-collapsible-button-bottom",
))
.inner(|ctx| {
ctx.html()
.span()
.attr(attr!("class" => "wj-collapsible-hide-text"))
.contents(hide_text);
});
}
});
}