From d0ada7fc532a8fcff1f1690b685d7abb8de96d05 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Thu, 11 Feb 2021 01:19:42 +0900 Subject: [PATCH] Reimplement search() in web-sys --- plume-front/Cargo.toml | 4 ++++ plume-front/src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/plume-front/Cargo.toml b/plume-front/Cargo.toml index 23c0f0ff..9e913892 100644 --- a/plume-front/Cargo.toml +++ b/plume-front/Cargo.toml @@ -25,7 +25,11 @@ features = [ 'DomTokenList', 'Element', 'EventTarget', + 'HtmlFormElement', + 'HtmlInputElement', 'Navigator', + 'Node', + 'NodeList', 'TouchEvent', 'Window' ] diff --git a/plume-front/src/lib.rs b/plume-front/src/lib.rs index 110c04ff..33824ba6 100755 --- a/plume-front/src/lib.rs +++ b/plume-front/src/lib.rs @@ -77,6 +77,7 @@ lazy_static! { #[wasm_bindgen(start)] pub fn main() -> Result<(), JsValue> { menu(); + search(); 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::().unwrap(); + if input.name().is_empty() { + input.set_name(&input.dyn_ref::().unwrap().id()); + } + if !input.name().is_empty() && input.value().is_empty() { + input.set_name(""); + } + } + }) + .unwrap(); + }) as Box); + form.add_event_listener_with_callback("submit", normalize_query.as_ref().unchecked_ref()) + .unwrap(); + normalize_query.forget(); + } +}