use gloo::console; use gloo_net::http::Request; use wasm_bindgen::prelude::*; use wasm_bindgen::{JsCast, UnwrapThrowExt}; pub use web_sys::InputEvent; use web_sys::{ Document, Event, HtmlElement, HtmlInputElement, RequestCredentials, RequestMode, Window, }; use yew::virtual_dom::VNode; use yew::{html, Html}; pub fn window() -> Window { web_sys::window().expect_throw("Unable to retrieve window") } pub fn document() -> Document { window() .document() .expect_throw("Unable to retrieve document") } pub fn body() -> HtmlElement { document().body().expect_throw("Unable to retrieve body") } pub fn autofocus(target: &str) { // If an element with an id attribute matching 'target' exists, focus it. let doc = document(); if let Some(element) = doc.get_element_by_id(target) { if let Ok(htmlelement) = element.dyn_into::() { if htmlelement.focus().is_err() { console::warn!( "unable to autofocus element, couldn't find target with id '{}'", target ); } } } } pub fn get_value_from_input_event(e: InputEvent) -> String { let event: Event = e.dyn_into().unwrap_throw(); let event_target = event.target().unwrap_throw(); let target: HtmlInputElement = event_target.dyn_into().unwrap_throw(); target.value() } // pub fn get_element_by_id(id: &str) -> Option { // document() // .get_element_by_id(id) // .and_then(|element| element.dyn_into::().ok()) // } // pub fn get_buttonelement_by_id(id: &str) -> Option { // document() // .get_element_by_id(id) // .and_then(|element| element.dyn_into::().ok()) // } // pub fn get_inputelement_by_id(id: &str) -> Option { // document() // .get_element_by_id(id) // .and_then(|element| element.dyn_into::().ok()) // } pub fn get_value_from_element_id(id: &str) -> Option { document() .get_element_by_id(id) .and_then(|element| element.dyn_into::().ok()) .map(|element| element.value()) } #[wasm_bindgen(raw_module = "/pkg/wasmloader.js")] extern "C" { pub fn modal_hide_by_id(m: &str); } /// Returns the footer node for the UI pub fn do_footer() -> VNode { html! { } } /// Builds a request object to a server-local endpoint with the usual requirements pub fn init_request(endpoint: &str) -> gloo_net::http::Request { Request::new(endpoint) .mode(RequestMode::SameOrigin) .credentials(RequestCredentials::SameOrigin) .header("content-type", "application/json") } pub fn do_alert_error(alert_title: &str, alert_message: Option<&str>) -> Html { html! {
} } pub fn do_page_header(page_title: &str) -> Html { html! {

{ page_title }

} }