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
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) {
let doc = document();
if let Some(element) = doc.get_element_by_id(target) {
if let Ok(htmlelement) = element.dyn_into::<web_sys::HtmlElement>() {
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_inputelement_by_id(id: &str) -> Option<HtmlInputElement> {
document()
.get_element_by_id(id)
.and_then(|element| element.dyn_into::<web_sys::HtmlInputElement>().ok())
}
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())
}
#[wasm_bindgen(raw_module = "/pkg/wasmloader.js")]
extern "C" {
pub fn modal_hide_by_id(m: &str);
}
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>
}
}
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! {
<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>
}
}