Lines
0 %
Functions
Branches
100 %
//! The cells a fill wrote ride along with a submit as `filled:<address>`, a
//! copied one with the value `copied`, so a form the server re-renders still
//! lets a later pick move them.
use forms::labels::{COPIED_MARK, FILLED_PREFIX, LABEL_PREFIX};
use wasm_bindgen::prelude::*;
use web_sys::{CustomEvent, Event, HtmlFormElement};
use crate::rows::{each_element, listen_form};
/// Marks a cell a fill wrote: a later fill may move or clear it until the user picks or types it.
pub(crate) const FILLED: &str = "data-filled";
/// A filled cell that came from the split above: a fill may move it, never clear it.
pub(crate) const COPIED: &str = "data-copied";
fn on_request(form: &HtmlFormElement, event: &Event) -> Result<(), JsValue> {
let Some(request) = event.dyn_ref::<CustomEvent>() else {
return Ok(());
};
let parameters = js_sys::Reflect::get(&request.detail(), &"parameters".into())?;
for input in each_element(form, &format!("input[{FILLED}]"))? {
let name = input.get_attribute("name").unwrap_or_default();
let mark = match input.has_attribute(COPIED) {
true => COPIED_MARK,
false => "",
if let Some(address) = name.strip_prefix(LABEL_PREFIX) {
js_sys::Reflect::set(
¶meters,
&format!("{FILLED_PREFIX}{address}").into(),
&mark.into(),
)?;
}
Ok(())
/// Send the filled marks of `form` with every request it makes.
///
/// # Errors
/// Returns the DOM error if the listener cannot be set up.
pub fn init_filled_marks(form: &HtmlFormElement) -> Result<(), JsValue> {
listen_form(form, "htmx:configRequest", on_request)