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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use super::{MJCarouselImage, NAME};
use crate::helper::size::Pixel;
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 MJCarouselImageRender<'e, 'h> {
    header: Rc<RefCell<Header<'h>>>,
    element: &'e MJCarouselImage,
    extra: Map<String, String>,
    container_width: Option<Pixel>,
    index: usize,
}

impl<'e, 'h> MJCarouselImageRender<'e, 'h> {
    fn set_style_images_img(&self, tag: Tag) -> Tag {
        tag.maybe_add_style("border-radius", self.attribute("border-radius"))
            .add_style("display", "block")
            .maybe_add_style(
                "width",
                self.container_width.as_ref().map(|value| value.to_string()),
            )
            .add_style("max-width", "100%")
            .add_style("height", "auto")
    }

    fn set_style_radio_input(&self, tag: Tag) -> Tag {
        tag.add_style("display", "none")
            .add_style("mso-hide", "all")
    }

    fn set_style_thumbnails_a(&self, tag: Tag) -> Tag {
        tag.maybe_add_style("border", self.attribute("tb-border"))
            .maybe_add_style("border-radius", self.attribute("tb-border-radius"))
            .add_style("display", "inline-block")
            .add_style("overflow", "hidden")
            .maybe_add_style("width", self.attribute("tb-width"))
    }

    fn set_style_thumbnails_img(&self, tag: Tag) -> Tag {
        tag.add_style("display", "block")
            .add_style("width", "100%")
            .add_style("height", "auto")
    }

    fn render_radio(&self) -> String {
        self.set_style_radio_input(Tag::new("input"))
            .add_class("mj-carousel-radio")
            .maybe_add_class(
                self.extra
                    .get("carousel-id")
                    .map(|id| format!("mj-carousel-{}-radio", id)),
            )
            .maybe_add_class(
                self.extra
                    .get("carousel-id")
                    .map(|id| format!("mj-carousel-{}-radio-{}", id, self.index + 1)),
            )
            .maybe_add_attribute(
                "checked",
                if self.index == 0 {
                    Some("checked")
                } else {
                    None
                },
            )
            .add_attribute("type", "radio")
            .maybe_add_attribute(
                "name",
                self.extra
                    .get("carousel-id")
                    .map(|id| format!("mj-carousel-radio-{}", id)),
            )
            .maybe_add_attribute(
                "id",
                self.extra
                    .get("carousel-id")
                    .map(|id| format!("mj-carousel-{}-radio-{}", id, self.index + 1)),
            )
            .closed()
    }

    pub fn render_thumbnail(&self) -> Result<String, Error> {
        let img = self
            .set_style_thumbnails_img(Tag::new("img"))
            .maybe_add_attribute(
                "src",
                self.attribute("thumbnails-src")
                    .or_else(|| self.attribute("src")),
            )
            .maybe_add_attribute("alt", self.attribute("alt"))
            .maybe_add_attribute(
                "width",
                self.container_width.as_ref().map(|item| item.value()),
            )
            .closed();
        let label = Tag::new("label")
            .maybe_add_attribute(
                "for",
                self.extra
                    .get("carousel-id")
                    .map(|id| format!("mj-carousel-{}-radio-{}", id, self.index + 1)),
            )
            .render(img);
        Ok(self
            .set_style_thumbnails_a(Tag::new("a"))
            .add_attribute("href", format!("#{}", self.index + 1))
            .maybe_add_attribute("target", self.attribute("target"))
            .add_class("mj-carousel-thumbnail")
            .maybe_add_class(
                self.extra
                    .get("carousel-id")
                    .map(|id| format!("mj-carousel-{}-thumbnail", id)),
            )
            .maybe_add_class(
                self.extra
                    .get("carousel-id")
                    .map(|id| format!("mj-carousel-{}-thumbnail-{}", id, self.index + 1)),
            )
            .maybe_add_suffixed_class(self.attribute("css-class"), "thumbnail")
            .maybe_add_style(
                "width",
                self.container_width.as_ref().map(|item| item.to_string()),
            )
            .render(label))
    }
}

impl<'e, 'h> Render<'h> for MJCarouselImageRender<'e, 'h> {
    fn default_attribute(&self, key: &str) -> Option<&str> {
        match key {
            "target" => Some("_blank"),
            _ => None,
        }
    }

    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 attributes(&self) -> Option<&Map<String, String>> {
        Some(&self.element.attributes)
    }

    fn tag(&self) -> Option<&str> {
        Some(NAME)
    }

    fn set_container_width(&mut self, width: Option<Pixel>) {
        self.container_width = width;
    }

    fn set_index(&mut self, index: usize) {
        self.index = index;
    }

    fn header(&self) -> Ref<Header<'h>> {
        self.header.borrow()
    }

    fn render_fragment(&self, name: &str, opts: &Options) -> Result<String, Error> {
        match name {
            "main" => self.render(opts),
            "radio" => Ok(self.render_radio()),
            "thumbnail" => self.render_thumbnail(),
            _ => Err(Error::UnknownFragment(name.to_string())),
        }
    }

    fn render(&self, _opts: &Options) -> Result<String, Error> {
        let img = self
            .set_style_images_img(Tag::new("img"))
            .add_attribute("border", 0)
            .maybe_add_attribute("alt", self.attribute("alt"))
            .maybe_add_attribute("src", self.attribute("src"))
            .maybe_add_attribute("title", self.attribute("title"))
            .maybe_add_attribute(
                "width",
                self.container_width.as_ref().map(|width| width.value()),
            )
            .closed();
        let link = match self.attribute("href") {
            None => img,
            Some(href) => Tag::new("a")
                .add_attribute("href", href)
                .maybe_add_attribute("rel", self.attribute("rel"))
                .add_attribute("target", "_blank")
                .render(img),
        };
        let div = if self.index == 0 {
            Tag::div()
        } else {
            Tag::div()
                .add_style("display", "none")
                .add_style("mso-hide", "all")
        };
        let div = div
            .add_class("mj-carousel-image")
            .add_class(format!("mj-carousel-image-{}", self.index + 1))
            .maybe_add_class(self.attribute("css-class"))
            .render(link);
        Ok(div)
    }
}

impl<'r, 'e: 'r, 'h: 'r> Renderable<'r, 'e, 'h> for MJCarouselImage {
    fn renderer(&'e self, header: Rc<RefCell<Header<'h>>>) -> Box<dyn Render<'h> + 'r> {
        Box::new(MJCarouselImageRender::<'e, 'h> {
            element: self,
            header,
            extra: Map::new(),
            container_width: None,
            index: 0,
        })
    }
}