mrml/mj_navbar_link/
render.rs1use super::{MjNavbarLink, NAME};
2use crate::helper::size::Pixel;
3use crate::prelude::hash::Map;
4use crate::prelude::render::*;
5
6struct MjNavbarLinkExtra<'a> {
7 attributes: Map<&'a str, &'a str>,
8}
9
10impl Default for MjNavbarLinkExtra<'_> {
11 fn default() -> Self {
12 Self {
13 attributes: Map::new(),
14 }
15 }
16}
17
18impl<'root> Renderer<'root, MjNavbarLink, MjNavbarLinkExtra<'root>> {
19 fn set_style_a<'a, 't>(&'a self, tag: Tag<'t>) -> Tag<'t>
20 where
21 'root: 'a,
22 'a: 't,
23 {
24 tag.add_style("display", "inline-block")
25 .maybe_add_style("color", self.attribute("color"))
26 .maybe_add_style("font-family", self.attribute("font-family"))
27 .maybe_add_style("font-size", self.attribute("font-size"))
28 .maybe_add_style("font-style", self.attribute("font-style"))
29 .maybe_add_style("font-weight", self.attribute("font-weight"))
30 .maybe_add_style("letter-spacing", self.attribute("letter-spacing"))
31 .maybe_add_style("line-height", self.attribute("line-height"))
32 .maybe_add_style("text-decoration", self.attribute("text-decoration"))
33 .maybe_add_style("text-transform", self.attribute("text-transform"))
34 .maybe_add_style("padding", self.attribute("padding"))
35 .maybe_add_style("padding-top", self.attribute("padding-top"))
36 .maybe_add_style("padding-right", self.attribute("padding-right"))
37 .maybe_add_style("padding-bottom", self.attribute("padding-bottom"))
38 .maybe_add_style("padding-left", self.attribute("padding-left"))
39 }
40
41 fn set_style_td<'a, 't>(&'a self, tag: Tag<'t>) -> Tag<'t>
42 where
43 'root: 'a,
44 'a: 't,
45 {
46 tag.maybe_add_style("padding", self.attribute("padding"))
47 .maybe_add_style("padding-top", self.attribute("padding-top"))
48 .maybe_add_style("padding-right", self.attribute("padding-right"))
49 .maybe_add_style("padding-bottom", self.attribute("padding-bottom"))
50 .maybe_add_style("padding-left", self.attribute("padding-left"))
51 }
52
53 fn get_link(&self) -> Option<String> {
54 self.attribute("href").as_ref().and_then(|href| {
55 self.attribute("navbar-base-url")
56 .map(move |base| format!("{base}{href}"))
57 .or_else(|| Some(href.to_string()))
58 })
59 }
60
61 fn render_content(&self, cursor: &mut RenderCursor) -> Result<(), Error> {
62 let link = self
63 .set_style_a(Tag::new("a"))
64 .add_class("mj-link")
65 .maybe_add_class(self.attribute("css-class"))
66 .maybe_add_attribute("href", self.get_link())
67 .maybe_add_attribute("rel", self.attribute("rel"))
68 .maybe_add_attribute("target", self.attribute("target"))
69 .maybe_add_attribute("name", self.attribute("name"));
70
71 link.render_open(&mut cursor.buffer)?;
72 for child in self.element.children.iter() {
73 let renderer = child.renderer(self.context());
74 renderer.render(cursor)?;
75 }
76 link.render_close(&mut cursor.buffer);
77
78 Ok(())
79 }
80}
81
82impl<'root> Render<'root> for Renderer<'root, MjNavbarLink, MjNavbarLinkExtra<'root>> {
83 fn default_attribute(&self, key: &str) -> Option<&'static str> {
84 match key {
85 "color" => Some("#000000"),
86 "font-family" => Some("Ubuntu, Helvetica, Arial, sans-serif"),
87 "font-size" => Some("13px"),
88 "font-weight" => Some("normal"),
89 "line-height" => Some("22px"),
90 "padding" => Some("15px 10px"),
91 "target" => Some("_blank"),
92 "text-decoration" => Some("none"),
93 "text-transform" => Some("uppercase"),
94 _ => None,
95 }
96 }
97
98 fn add_extra_attribute(&mut self, key: &'root str, value: &'root str) {
99 self.extra.attributes.insert(key, value);
100 }
101
102 fn raw_extra_attribute(&self, key: &str) -> Option<&'root str> {
103 self.extra.attributes.get(key).copied()
104 }
105
106 fn raw_attribute(&self, key: &str) -> Option<&'root str> {
107 match self.element.attributes.get(key) {
108 Some(Some(inner)) => Some(inner),
109 _ => None,
110 }
111 }
112
113 fn tag(&self) -> Option<&str> {
114 Some(NAME)
115 }
116
117 fn set_container_width(&mut self, width: Option<Pixel>) {
118 self.container_width = width;
119 }
120
121 fn context(&self) -> &'root RenderContext<'root> {
122 self.context
123 }
124
125 fn render(&self, cursor: &mut RenderCursor) -> Result<(), Error> {
126 let font_families = self.attribute("font-family");
127 cursor.header.maybe_add_font_families(font_families);
128
129 let td = self
130 .set_style_td(Tag::td())
131 .maybe_add_suffixed_class(self.attribute("css-class"), "outlook");
132
133 cursor.buffer.start_conditional_tag();
134 td.render_open(&mut cursor.buffer)?;
135 cursor.buffer.end_conditional_tag();
136 self.render_content(cursor)?;
137 cursor.buffer.start_conditional_tag();
138 td.render_close(&mut cursor.buffer);
139 cursor.buffer.end_conditional_tag();
140
141 Ok(())
142 }
143}
144
145impl<'render, 'root: 'render> Renderable<'render, 'root> for MjNavbarLink {
146 fn renderer(
147 &'root self,
148 context: &'root RenderContext<'root>,
149 ) -> Box<dyn Render<'root> + 'render> {
150 Box::new(Renderer::new(context, self, MjNavbarLinkExtra::default()))
151 }
152}