1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use super::{MJAccordionTitle, NAME};
use crate::helper::condition::negation_conditional_tag;
use crate::helper::tag::Tag;
use crate::prelude::hash::Map;
use crate::prelude::render::{Error, Header, Options, Render, Renderable};
use std::cell::{Ref, RefCell};
use std::rc::Rc;
struct MJAccordionTitleRender<'e, 'h> {
header: Rc<RefCell<Header<'h>>>,
element: &'e MJAccordionTitle,
extra: Map<String, String>,
}
impl<'e, 'h> MJAccordionTitleRender<'e, 'h> {
fn set_style_img(&self, tag: Tag) -> Tag {
tag.add_style("display", "none")
.maybe_add_style("width", self.attribute("icon-width"))
.maybe_add_style("height", self.attribute("icon-height"))
}
fn render_title(&self, opts: &Options) -> Result<String, Error> {
let content = self
.element
.children
.iter()
.try_fold(String::default(), |res, child| {
let renderer = child.renderer(Rc::clone(&self.header));
Ok(res + &renderer.render(opts)?)
})?;
Ok(Tag::td()
.add_style("width", "100%")
.maybe_add_style("background-color", self.attribute("background-color"))
.maybe_add_style("color", self.attribute("color"))
.maybe_add_style("font-size", self.attribute("font-size"))
.maybe_add_style("font-family", self.attribute("font-family"))
.maybe_add_style("padding-top", self.attribute("padding-top"))
.maybe_add_style("padding-right", self.attribute("padding-right"))
.maybe_add_style("padding-bottom", self.attribute("padding-bottom"))
.maybe_add_style("padding-left", self.attribute("padding-left"))
.maybe_add_style("padding", self.attribute("padding"))
.maybe_add_class(self.attribute("css-class"))
.render(content))
}
fn render_icons(&self) -> String {
let img_more = self
.set_style_img(Tag::new("img"))
.maybe_add_attribute("src", self.attribute("icon-wrapped-url"))
.maybe_add_attribute("alt", self.attribute("icon-wrapped-alt"))
.add_class("mj-accordion-more")
.closed();
let img_less = self
.set_style_img(Tag::new("img"))
.maybe_add_attribute("src", self.attribute("icon-unwrapped-url"))
.maybe_add_attribute("alt", self.attribute("icon-unwrapped-alt"))
.add_class("mj-accordion-less")
.closed();
let td = Tag::td()
.add_style("padding", "16px")
.maybe_add_style("background", self.attribute("background-color"))
.maybe_add_style("vertical-align", self.attribute("icon-align"))
.add_class("mj-accordion-ico")
.render(img_more + &img_less);
negation_conditional_tag(td)
}
}
impl<'e, 'h> Render<'h> for MJAccordionTitleRender<'e, 'h> {
fn add_extra_attribute(&mut self, key: &str, value: &str) {
self.extra.insert(key.to_string(), value.to_string());
}
fn extra_attributes(&self) -> Option<&Map<String, String>> {
Some(&self.extra)
}
fn default_attribute(&self, name: &str) -> Option<&str> {
match name {
"font-size" => Some("13px"),
"padding" => Some("16px"),
_ => None,
}
}
fn attributes(&self) -> Option<&Map<String, String>> {
Some(&self.element.attributes)
}
fn tag(&self) -> Option<&str> {
Some(NAME)
}
fn header(&self) -> Ref<Header<'h>> {
self.header.borrow()
}
fn render(&self, opts: &Options) -> Result<String, Error> {
let font_families = self.attribute("font-family");
self.header
.borrow_mut()
.maybe_add_font_families(font_families);
let mut content = vec![self.render_title(opts)?, self.render_icons()];
if !self.attribute_equals("icon-position", "right") {
content.reverse();
}
let content = content.join("");
let tr = Tag::tr().render(content);
let tbody = Tag::tbody().render(tr);
let table = Tag::table()
.add_attribute("cell-spacing", 0)
.add_attribute("cell-padding", 0)
.add_style("width", "100%")
.maybe_add_style("border-bottom", self.attribute("border"))
.render(tbody);
Ok(Tag::div().add_class("mj-accordion-title").render(table))
}
}
impl<'r, 'e: 'r, 'h: 'r> Renderable<'r, 'e, 'h> for MJAccordionTitle {
fn renderer(&'e self, header: Rc<RefCell<Header<'h>>>) -> Box<dyn Render<'h> + 'r> {
Box::new(MJAccordionTitleRender::<'e, 'h> {
element: self,
header,
extra: Map::new(),
})
}
}