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
use crate::utils::create_style;
use yew::prelude::*;

#[derive(Clone)]
pub enum ItemLayout {
    ItXs(i8),
    ItS(i8),
    ItM(i8),
    ItL(i8),
    ItXl(i8),
}

#[derive(Clone)]
pub enum AlignSelf {
    Auto,
    FlexStart,
    FlexEnd,
    Center,
    Baseline,
    Stretch,
}

pub enum Msg {
    Clicked,
}

pub struct Item {
    link: ComponentLink<Self>,
    props: Props,
}

#[derive(Clone)]
struct ItemProps {
    layouts_classes: String,
    name: String,
    index: i16,
    class_name: String,
}

#[derive(Clone, Copy)]
struct ItemModel;

#[derive(Clone, Properties)]
pub struct Props {
    pub layouts: Vec<ItemLayout>,
    pub align_self: AlignSelf,
    pub name: String,
    pub class_name: String,
    pub onsignal: DefaultCallback<Callback<()>>,
    pub children: Children,
    pub index: i16,
}

#[derive(Clone)]
pub struct DefaultCallback<T> {
    callback: T,
}

impl Default for DefaultCallback<Callback<()>> {
    fn default() -> Self {
        let callback = DefaultCallback {
            callback: Callback::noop(),
        };

        callback
    }
}

impl Default for AlignSelf {
    fn default() -> Self {
        AlignSelf::Auto
    }
}

impl Component for Item {
    type Message = Msg;
    type Properties = Props;

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        Item { link, props }
    }

    fn mounted(&mut self) -> ShouldRender {
        ItemModel.init(self.props.clone());

        true
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Clicked => {
                self.props.onsignal.callback.emit(());
            }
        };

        false
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        ItemModel.init(props.clone());

        self.props = props;
        true
    }

    fn view(&self) -> Html {
        let item_props = ItemProps::from(self.props.clone());

        html! {
            <div
                class=if item_props.name == "" {
                    format!("item item-{} {} {}", item_props.index, item_props.layouts_classes, item_props.class_name)
                } else {
                    format!("item item-{}-{} {} {}", item_props.name, item_props.index, item_props.layouts_classes, item_props.class_name)
                }

                onclick=self.link.callback(|_| Msg::Clicked)
            >
                {self.props.children.render()}
            </div>
        }
    }
}

impl From<Props> for ItemProps {
    fn from(props: Props) -> Self {
        ItemProps {
            layouts_classes: ItemModel.get_layout_classes(props.layouts),
            name: props.name,
            index: props.index,
            class_name: props.class_name,
        }
    }
}

impl ItemModel {
    fn init(self, props: Props) {
        self.get_item_align(props.align_self, props.index, props.name);
    }

    fn get_layout_classes(self, layouts_prop: Vec<ItemLayout>) -> String {
        let mut layouts = layouts_prop
            .into_iter()
            .map(|layout| self.get_layout(layout))
            .collect::<String>();

        layouts.truncate(layouts.len() - 1);
        layouts
    }

    fn get_layout(self, item_layout: ItemLayout) -> String {
        match item_layout {
            ItemLayout::ItXs(size) => format!("it-xs-{} ", size),
            ItemLayout::ItS(size) => format!("it-s-{} ", size),
            ItemLayout::ItM(size) => format!("it-m-{} ", size),
            ItemLayout::ItL(size) => format!("it-l-{} ", size),
            ItemLayout::ItXl(size) => format!("it-xl-{} ", size),
        }
    }

    fn get_item_align(self, align: AlignSelf, index: i16, name: String) {
        let value = match align {
            AlignSelf::Auto => format!("auto"),
            AlignSelf::Baseline => format!("baseline"),
            AlignSelf::Center => format!("center"),
            AlignSelf::FlexStart => format!("flex-start"),
            AlignSelf::FlexEnd => format!("flex-end"),
            AlignSelf::Stretch => format!("stretch"),
        };

        create_style(
            String::from("alignSelf"),
            value,
            if name == "" {
                format!("item-{}", index)
            } else {
                format!("item-{}-{}", name, index)
            },
        );
    }
}