diff options
author | jelemux <jeremias.weber@protonmail.com> | 2021-02-17 17:06:48 +0100 |
---|---|---|
committer | jelemux <jeremias.weber@protonmail.com> | 2021-02-17 17:06:48 +0100 |
commit | cb310a66e94db4e0c4f7d0373a670156b012412a (patch) | |
tree | 501fd4ed17d892d693d4123fffaca8d942144e3b /src/viewmodel/mod.rs | |
parent | be3367dd2921eb30ae7970d233b83ac3af861952 (diff) | |
parent | 0660151a8b641fa0a23dde2598132029970f7ae4 (diff) | |
download | wasm-card-cb310a66e94db4e0c4f7d0373a670156b012412a.tar.gz wasm-card-cb310a66e94db4e0c4f7d0373a670156b012412a.tar.bz2 |
Merge branch 'main' of codeberg.org:jelemux/wasm-card
Diffstat (limited to 'src/viewmodel/mod.rs')
-rw-r--r-- | src/viewmodel/mod.rs | 231 |
1 files changed, 0 insertions, 231 deletions
diff --git a/src/viewmodel/mod.rs b/src/viewmodel/mod.rs deleted file mode 100644 index 0385c61..0000000 --- a/src/viewmodel/mod.rs +++ /dev/null @@ -1,231 +0,0 @@ -use crate::view::VCardPropertyInputComponent; -use crate::viewmodel::utility::File; -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 dates; -pub mod name; -pub mod organizational; -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 VCardPropertyInputObject<C: VCardPropertyInputComponent<Self>>: - Clone + PartialEq -where - Self: Sized, -{ - /// Function for creating a new (and empty) `VCardPropertyInputObject`. - fn new() -> Self; - /// Converts each field of the `VCardPropertyInputObject` to a VCardPropertyInputField and returns them as a vector. - fn get_input_fields(&self, link: &ComponentLink<C>) -> Vec<VCardPropertyInputField>; - /// Returns a `Html` representation of the `VCardPropertyInputObject`. - fn render(&self, link: &ComponentLink<C>) -> 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 `VCardPropertyInputObject` 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>, - }, - 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::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-fifth-widescreen - is-one-quarter-desktop - is-one-third-tablet - is-half-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> - } - } - /// 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> - } - } -} |