summaryrefslogtreecommitdiff
path: root/src/model/mod.rs
blob: b8dcfff3c4b49c172124e8a1fbc21d35555dae43 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use crate::model::utility::File;
use crate::view::property_group::*;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsCast;
use web_sys::FileReader;
use yew::prelude::*;
use yew::services::ConsoleService;

pub mod address;
pub mod name;
pub mod organizational;
pub mod other_identification;
pub mod telephone;
pub mod utility;
pub mod vcard;

/// Trait for types that represent the data of a vcard property used inside of a `VCardPropertyInputComponent`.
pub trait VCardPropertyInputGroupObject<M: 'static + PartialEq + Clone>: Clone + PartialEq
where
    Self: Sized,
{
    /// Function for creating a new (and empty) `VCardPropertyInputGroupObject`.
    fn new() -> Self;
    /// Getter function for the title of the component
    fn get_title(&self) -> String;
    /// Converts each field of the `VCardPropertyInputGroupObject` to a VCardPropertyInputField and returns them as a vector.
    fn get_input_fields(
        &self,
        link: &ComponentLink<PropertyGroupInputComponent<Self, M>>,
    ) -> Vec<VCardPropertyInputField>;
    fn update(
        &mut self,
        props: InputProps<Self, M>,
        msg: <PropertyGroupInputComponent<Self, M> as yew::Component>::Message,
    ) -> bool;
    /// Returns a `Html` representation of the `VCardPropertyInputGroupObject`.
    fn render(&self, link: &ComponentLink<PropertyGroupInputComponent<Self, M>>) -> Html {
        html! {
            <div class="columns is-mobile is-multiline">
                {
                    for self.get_input_fields(link).iter().map(|field|
                        field.render()
                    )
                }
            </div>
        }
    }
    /// Convenience function for checking if the `VCardPropertyInputGroupObject` is empty.
    fn is_empty(&self) -> bool;
}

/// Type for saving error messages.
///
/// More of a placeholder for something better later on.
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
    pub msg: String,
}

/// Type that represents the visiual appearance of an input field.
pub enum VCardPropertyInputField {
    Text {
        label: String,
        id: Option<String>,
        placeholder: Option<String>,
        oninput: Callback<InputData>,
        value: String,
        typ: String,
    },
    File {
        label: String,
        name: String,
        callback: Callback<Option<File>>,
        value: Option<File>,
    },
    Select {
        label: String,
        id: Option<String>,
        options: Vec<(String, String)>,
        onchange: Callback<ChangeData>,
        value: String,
    },
    CheckBox {
        label: String,
        id: Option<String>,
        onclick: Callback<MouseEvent>,
        value: bool,
    },
}

impl VCardPropertyInputField {
    /// Returns a `Html` representation of the `VCardPropertyInputField`.
    pub fn render(&self) -> Html {
        match self {
            Self::Text {
                label,
                id,
                placeholder,
                oninput,
                value: _,
                typ,
            } => Self::text_field_input(label, id, placeholder, oninput, typ),
            Self::File {
                label,
                name,
                callback,
                value,
            } => Self::file_field_input(label, name, callback, value),
            Self::Select {
                label,
                id,
                options,
                onchange,
                value,
            } => Self::select_field_input(label, id, options, onchange, value),
            Self::CheckBox {
                label,
                id,
                onclick,
                value,
            } => Self::checkbox_field_input(label, id, value, onclick),
        }
    }
    /// Returns an `Html` representation of a text input field with the given parameters.
    fn text_field_input(
        label: &str,
        id: &Option<String>,
        placeholder: &Option<String>,
        oninput: &Callback<InputData>,
        typ: &str,
    ) -> Html {
        html! {
            <div class="field column
                        is-one-fifth-widescreen
                        is-one-quarter-desktop
                        is-one-third-tablet
                        is-half-mobile" >
                <label class="label">{ label }</label>
                <div class="control">
                    <input  id=id.as_ref().unwrap_or(&"".to_string())
                            type=typ
                            placeholder=placeholder.as_ref().unwrap_or(&"".to_string())
                            oninput=oninput
                    />
                </div>
            </div>
        }
    }
    /// Returns an `Html` representation of a file input field with the given parameters.
    fn file_field_input(
        label: &str,
        name: &str,
        callback: &Callback<Option<File>>,
        file: &Option<File>,
    ) -> Html {
        let callback = callback.clone();
        let onchange = Callback::<()>::default();
        let onchange = onchange.reform(move |c: ChangeData| {
            if let ChangeData::Files(files) = c {
                match files.item(0) {
                    Some(file) => {
                        let file_reader = FileReader::new().unwrap();
                        match file_reader.read_as_data_url(&file) {
                            Ok(_) => (),
                            Err(_) => ConsoleService::warn("Error: Couldn't get file as data url."),
                        };

                        let callback = callback.clone();
                        let onload = Closure::wrap(Box::new(move |event: Event| {
                            let file_reader: FileReader =
                                event.target().unwrap().dyn_into().unwrap();
                            let data_url: Option<String> =
                                file_reader.result().unwrap().as_string();
                            match data_url {
                                Some(content) => callback.emit(Some(File {
                                    name: file.name(),
                                    content,
                                })),
                                None => {
                                    ConsoleService::warn("Couldn't get data url as string.");
                                    callback.emit(None);
                                }
                            };
                        }) as Box<dyn FnMut(_)>);

                        file_reader.set_onload(Some(onload.as_ref().unchecked_ref()));
                        onload.forget();
                    }
                    None => callback.emit(None),
                }
            } else {
                callback.emit(None);
            }
        });
        html! {
            <div class="field column
                        is-one-third-widescreen
                        is-two-quarters-desktop
                        is-two-thirds-tablet
                        is-full-mobile" >
                <label class="label">{ label }</label>
                <div class="file has-name control">
                    <label class="file-label">
                        <input  class="file-input"
                                type="file"
                                name=name
                                onchange=onchange
                        />
                        <span class="file-cta">
                        <span class="file-icon">
                            <i class="fas fa-upload"></i>
                        </span>
                        <span class="file-label">
                            { "Choose file..." }
                        </span>
                        </span>
                        <span class="file-name">
                            {
                                match file {
                                    Some(file) => file.name.clone(),
                                    None => String::from("No file selected"),
                                }
                            }
                        </span>
                    </label>
                </div>
            </div>
        }
    }
    fn select_field_input(
        label: &str,
        id: &Option<String>,
        options: &Vec<(String, String)>,
        onchange: &Callback<ChangeData>,
        value: &str,
    ) -> Html {
        html! {
            <div class="field column
                        is-one-fifth-widescreen 
                        is-one-quarter-desktop 
                        is-one-third-tablet 
                        is-half-mobile" >
                <label class="label">{ label }</label>
                <div class="select">
                    <select id=id.as_ref().unwrap_or(&"".to_string())
                            value=value
                            onchange=onchange >
                        {
                            for options.iter().map(|option|
                                html! {
                                    <option value=option.0>{ option.1.clone() }</option>
                                }
                            )
                        }
                    </select>
                </div>

            </div>
        }
    }
    /// Returns an `Html` representation of a checkbox input field with the given parameters.
    fn checkbox_field_input(
        label: &str,
        id: &Option<String>,
        checked: &bool,
        onclick: &Callback<MouseEvent>,
    ) -> Html {
        html! {
            <div class="field column
                        is-one-fifth-widescreen 
                        is-one-quarter-desktop 
                        is-one-third-tablet 
                        is-half-mobile" >
                <label class="checkbox">
                    <input  id=id.as_ref().unwrap_or(&"".to_string())
                            type="checkbox"
                            checked=*checked
                            onclick=onclick
                    />
                    { label }
                </label>
            </div>
        }
    }
}