2021-12-31 00:11:20 +01:00
|
|
|
use gloo::console;
|
2022-09-20 06:23:54 +02:00
|
|
|
use gloo_net::http::Request;
|
2022-06-05 08:30:08 +02:00
|
|
|
use wasm_bindgen::prelude::*;
|
2021-12-31 00:11:20 +01:00
|
|
|
use wasm_bindgen::{JsCast, UnwrapThrowExt};
|
|
|
|
pub use web_sys::InputEvent;
|
2022-10-31 01:50:04 +01:00
|
|
|
use web_sys::{
|
|
|
|
Document, Event, HtmlElement, HtmlInputElement, RequestCredentials, RequestMode, Window,
|
|
|
|
};
|
2022-08-01 07:52:01 +02:00
|
|
|
use yew::virtual_dom::VNode;
|
2022-09-20 06:23:54 +02:00
|
|
|
use yew::{html, Html};
|
2021-12-31 00:11:20 +01:00
|
|
|
|
|
|
|
pub fn window() -> Window {
|
2022-02-20 03:43:38 +01:00
|
|
|
web_sys::window().expect_throw("Unable to retrieve window")
|
2021-12-31 00:11:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn document() -> Document {
|
2022-02-20 03:43:38 +01:00
|
|
|
window()
|
|
|
|
.document()
|
|
|
|
.expect_throw("Unable to retrieve document")
|
2021-12-31 00:11:20 +01:00
|
|
|
}
|
2021-12-29 02:36:56 +01:00
|
|
|
|
2022-01-09 01:47:21 +01:00
|
|
|
pub fn body() -> HtmlElement {
|
2022-02-20 03:43:38 +01:00
|
|
|
document().body().expect_throw("Unable to retrieve body")
|
2022-01-09 01:47:21 +01:00
|
|
|
}
|
|
|
|
|
2022-08-01 07:52:01 +02:00
|
|
|
pub fn autofocus(target: &str) {
|
|
|
|
// If an element with an id attribute matching 'target' exists, focus it.
|
2021-12-31 00:11:20 +01:00
|
|
|
let doc = document();
|
2022-08-01 07:52:01 +02:00
|
|
|
if let Some(element) = doc.get_element_by_id(target) {
|
2021-12-29 02:36:56 +01:00
|
|
|
if let Ok(htmlelement) = element.dyn_into::<web_sys::HtmlElement>() {
|
|
|
|
if htmlelement.focus().is_err() {
|
2022-08-01 07:52:01 +02:00
|
|
|
console::warn!(
|
|
|
|
"unable to autofocus element, couldn't find target with id '{}'",
|
|
|
|
target
|
|
|
|
);
|
2021-12-29 02:36:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 00:11:20 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2022-06-05 08:30:08 +02:00
|
|
|
|
2022-06-28 01:22:31 +02:00
|
|
|
// pub fn get_element_by_id(id: &str) -> Option<HtmlElement> {
|
|
|
|
// document()
|
|
|
|
// .get_element_by_id(id)
|
|
|
|
// .and_then(|element| element.dyn_into::<web_sys::HtmlElement>().ok())
|
|
|
|
// }
|
2022-06-05 08:30:08 +02:00
|
|
|
|
2022-06-28 01:22:31 +02:00
|
|
|
// pub fn get_buttonelement_by_id(id: &str) -> Option<HtmlButtonElement> {
|
|
|
|
// document()
|
|
|
|
// .get_element_by_id(id)
|
|
|
|
// .and_then(|element| element.dyn_into::<web_sys::HtmlButtonElement>().ok())
|
|
|
|
// }
|
2022-06-05 08:30:08 +02:00
|
|
|
|
2022-06-28 01:22:31 +02:00
|
|
|
// pub fn get_inputelement_by_id(id: &str) -> Option<HtmlInputElement> {
|
|
|
|
// document()
|
|
|
|
// .get_element_by_id(id)
|
|
|
|
// .and_then(|element| element.dyn_into::<web_sys::HtmlInputElement>().ok())
|
|
|
|
// }
|
2022-06-05 08:30:08 +02:00
|
|
|
|
|
|
|
pub fn get_value_from_element_id(id: &str) -> Option<String> {
|
|
|
|
document()
|
|
|
|
.get_element_by_id(id)
|
|
|
|
.and_then(|element| element.dyn_into::<web_sys::HtmlInputElement>().ok())
|
|
|
|
.map(|element| element.value())
|
|
|
|
}
|
|
|
|
|
2022-07-11 08:33:18 +02:00
|
|
|
#[wasm_bindgen(raw_module = "/pkg/wasmloader.js")]
|
2022-06-05 08:30:08 +02:00
|
|
|
extern "C" {
|
2022-07-11 08:33:18 +02:00
|
|
|
pub fn modal_hide_by_id(m: &str);
|
2022-06-05 08:30:08 +02:00
|
|
|
}
|
2022-08-01 07:52:01 +02:00
|
|
|
|
|
|
|
/// Returns the footer node for the UI
|
|
|
|
pub fn do_footer() -> VNode {
|
|
|
|
html! {
|
|
|
|
<footer class="footer mt-auto py-3 bg-light text-end">
|
|
|
|
<div class="container">
|
|
|
|
<span class="text-muted">{ "Powered by " }<a href="https://kanidm.com">{ "Kanidm" }</a></span>
|
|
|
|
</div>
|
|
|
|
</footer>
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 06:23:54 +02:00
|
|
|
|
|
|
|
/// Builds a request object to a server-local endpoint with the usual requirements
|
2022-10-31 01:50:04 +01:00
|
|
|
pub fn init_request(endpoint: &str) -> gloo_net::http::Request {
|
2022-09-20 06:23:54 +02:00
|
|
|
Request::new(endpoint)
|
|
|
|
.mode(RequestMode::SameOrigin)
|
2022-10-31 01:50:04 +01:00
|
|
|
.credentials(RequestCredentials::SameOrigin)
|
2022-09-20 06:23:54 +02:00
|
|
|
.header("content-type", "application/json")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn do_alert_error(alert_title: &str, alert_message: Option<&str>) -> Html {
|
|
|
|
html! {
|
|
|
|
<div class="container">
|
|
|
|
<div class="row justify-content-md-center">
|
|
|
|
<div class="alert alert-danger" role="alert">
|
|
|
|
<p><strong>{ alert_title }</strong></p>
|
|
|
|
if let Some(value) = alert_message {
|
|
|
|
<p>{ value }</p>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn do_page_header(page_title: &str) -> Html {
|
|
|
|
html! {
|
|
|
|
<div class={crate::constants::CSS_PAGE_HEADER}>
|
|
|
|
<h2>{ page_title }</h2>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|