Reimplement search() in web-sys

This commit is contained in:
Kitaiti Makoto 2021-02-11 01:19:42 +09:00
parent d86bd898cb
commit d0ada7fc53
2 changed files with 39 additions and 0 deletions

View file

@ -25,7 +25,11 @@ features = [
'DomTokenList', 'DomTokenList',
'Element', 'Element',
'EventTarget', 'EventTarget',
'HtmlFormElement',
'HtmlInputElement',
'Navigator', 'Navigator',
'Node',
'NodeList',
'TouchEvent', 'TouchEvent',
'Window' 'Window'
] ]

View file

@ -77,6 +77,7 @@ lazy_static! {
#[wasm_bindgen(start)] #[wasm_bindgen(start)]
pub fn main() -> Result<(), JsValue> { pub fn main() -> Result<(), JsValue> {
menu(); menu();
search();
Ok(()) Ok(())
} }
@ -119,3 +120,37 @@ fn menu() {
} }
} }
} }
/// Clear the URL of the search page before submitting request
fn search() {
if let Some(form) = window()
.unwrap()
.document()
.unwrap()
.get_element_by_id("form")
{
let normalize_query = Closure::wrap(Box::new(|_: web_sys::Event| {
window()
.unwrap()
.document()
.unwrap()
.query_selector_all("#form input")
.map(|inputs| {
for i in 0..inputs.length() {
let input = inputs.get(i).unwrap();
let input = input.dyn_ref::<web_sys::HtmlInputElement>().unwrap();
if input.name().is_empty() {
input.set_name(&input.dyn_ref::<web_sys::Element>().unwrap().id());
}
if !input.name().is_empty() && input.value().is_empty() {
input.set_name("");
}
}
})
.unwrap();
}) as Box<dyn FnMut(web_sys::Event)>);
form.add_event_listener_with_callback("submit", normalize_query.as_ref().unchecked_ref())
.unwrap();
normalize_query.forget();
}
}