based/src/ui/htmx/mod.rs
2025-01-15 18:53:55 +01:00

208 lines
8.9 KiB
Rust

use std::collections::HashMap;
mod selector;
mod swap;
mod trigger;
pub use selector::Selector;
use swap::ModifiedSwapStrategy;
pub use swap::SwapStrategy;
pub use trigger::{Event, QueueOption, Trigger};
use super::AttrExtendable;
pub trait HTMXAttributes: AttrExtendable + std::marker::Sized {
/// Issues a `GET` request to the specified URL
#[must_use]
fn hx_get(self, url: &str) -> Self {
self.add_attr("hx-get", url)
}
/// Issues a `POST` request to the specified URL
#[must_use]
fn hx_post(self, url: &str) -> Self {
self.add_attr("hx-post", url)
}
/// Push a URL into the browser location bar to create history
#[must_use]
fn hx_push_url(self) -> Self {
self.add_attr("hx-push-url", "true")
}
/// Select content to swap in from a response
#[must_use]
fn hx_select(self, element: &str) -> Self {
self.add_attr("hx-select", element)
}
/// Select content to swap in from a response, somewhere other than the target (out of band).
/// Select `element` from response and replace `element` in the DOM.
#[must_use]
fn hx_select_oob(self, element: &str) -> Self {
self.add_attr("hx-select-oob", element)
}
/// The hx-boost attribute allows you to “boost” normal anchors and form tags to use AJAX instead.
#[must_use]
fn hx_boost(self) -> Self {
self.add_attr("hx-boost", "true")
}
/// The hx-confirm attribute allows you to confirm an action before issuing a request.
#[must_use]
fn hx_confirm(self, msg: &str) -> Self {
self.add_attr("hx-confirm", msg)
}
/// The hx-delete attribute will cause an element to issue a `DELETE` request to the specified URL and swap the HTML into the DOM using a swap strategy.
#[must_use]
fn hx_delete(self, url: &str) -> Self {
self.add_attr("hx-delete", url)
}
/// The hx-disable attribute will disable htmx processing for a given element and all its children.
#[must_use]
fn hx_disable(self) -> Self {
self.add_attr("hx-disable", "")
}
/// The hx-disabled-elt attribute allows you to specify elements that will have the disabled attribute added to them for the duration of the request.
#[must_use]
fn hx_disabled_elt(self, element: Selector) -> Self {
self.add_attr("hx-disabled-elt", &element.to_value())
}
/// The hx-disinherit attribute allows you to control automatic attribute inheritance.
#[must_use]
fn hx_disinherit(self, attrs: &str) -> Self {
self.add_attr("hx-disinherit", attrs)
}
/// The hx-encoding attribute allows you to switch the request encoding from the usual `application/x-www-form-urlencoded` encoding to `multipart/form-data`, usually to support file uploads in an ajax request.
#[must_use]
fn hx_encoding(self) -> Self {
self.add_attr("hx-encoding", "multipart/form-data")
}
/// The hx-headers attribute allows you to add to the headers that will be submitted with an AJAX request.
#[must_use]
fn hx_headers(self, headers: HashMap<String, String>) -> Self {
let json = serde_json::to_value(headers).unwrap();
let json_str = serde_json::to_string(&json).unwrap();
self.add_attr("hx-headers", &json_str)
}
/// The hx-vals attribute allows you to add to the parameters that will be submitted with an AJAX request.
#[must_use]
fn hx_vals(self, vals: HashMap<String, String>) -> Self {
let json = serde_json::to_value(vals).unwrap();
let json_str = serde_json::to_string(&json).unwrap();
self.add_attr("hx-vals", &json_str)
}
/// Set the hx-history attribute to false on any element in the current document, or any html fragment loaded into the current document by htmx, to prevent sensitive data being saved to the localStorage cache when htmx takes a snapshot of the page state.
///
/// History navigation will work as expected, but on restoration the URL will be requested from the server instead of the history cache.
#[must_use]
fn hx_history(self) -> Self {
self.add_attr("hx-history", "false")
}
/// The hx-history-elt attribute allows you to specify the element that will be used to snapshot and restore page state during navigation. By default, the body tag is used. This is typically good enough for most setups, but you may want to narrow it down to a child element. Just make sure that the element is always visible in your application, or htmx will not be able to restore history navigation properly.
#[must_use]
fn hx_history_elt(self) -> Self {
self.add_attr("hx-history-elt", "")
}
/// The hx-include attribute allows you to include additional element values in an AJAX request.
#[must_use]
fn hx_include(self, element: Selector) -> Self {
self.add_attr("hx-include", &element.to_value())
}
/// The hx-indicator attribute allows you to specify the element that will have the htmx-request class added to it for the duration of the request. This can be used to show spinners or progress indicators while the request is in flight.
///
/// Note: This attribute only supports CSS queries and `closest` match.
#[must_use]
fn hx_indicator(self, indicator: Selector) -> Self {
self.add_attr("hx-indicator", &indicator.to_value())
}
/// The hx-params attribute allows you to filter the parameters that will be submitted with an AJAX request.
///
/// The possible values of this attribute are:
/// `*` - Include all parameters (default)
/// `none` - Include no parameters
/// `not <param-list>` - Include all except the comma separated list of parameter names
/// `<param-list>` - Include all the comma separated list of parameter names
#[must_use]
fn hx_params(self, params: &str) -> Self {
self.add_attr("hx-params", params)
}
/// The hx-patch attribute will cause an element to issue a PATCH to the specified URL and swap the HTML into the DOM using a swap strategy.
#[must_use]
fn hx_patch(self, url: &str) -> Self {
self.add_attr("hx-patch", url)
}
/// The hx-put attribute will cause an element to issue a PUT to the specified URL and swap the HTML into the DOM using a swap strategy
#[must_use]
fn hx_put(self, url: &str) -> Self {
self.add_attr("hx-put", url)
}
/// The hx-replace-url attribute allows you to replace the current url of the browser location history.
///
/// The possible values of this attribute are:
/// `true`, which replaces the fetched URL in the browser navigation bar.
/// `false`, which disables replacing the fetched URL if it would otherwise be replaced due to inheritance.
/// A URL to be replaced into the location bar. This may be relative or absolute, as per `history.replaceState()`.
#[must_use]
fn hx_replace_url(self, value: &str) -> Self {
self.add_attr("hx-replace-url", value)
}
/// The hx-validate attribute will cause an element to validate itself by way of the HTML5 Validation API before it submits a request.
#[must_use]
fn hx_validate(self) -> Self {
self.add_attr("hx-validte", "true")
}
/// The hx-preserve attribute allows you to keep an element unchanged during HTML replacement. Elements with hx-preserve set are preserved by id when htmx updates any ancestor element. You must set an unchanging id on elements for hx-preserve to work. The response requires an element with the same id, but its type and other attributes are ignored.
#[must_use]
fn hx_preserve(self) -> Self {
self.add_attr("hx-preserve", "")
}
/// The hx-prompt attribute allows you to show a prompt before issuing a request. The value of the prompt will be included in the request in the HX-Prompt header.
#[must_use]
fn hx_prompt(self, msg: &str) -> Self {
self.add_attr("hx-prompt", msg)
}
/// The hx-swap attribute allows you to specify how the response will be swapped in relative to the target of an AJAX request.
#[must_use]
fn hx_swap<T: Into<ModifiedSwapStrategy>>(self, swap: T) -> Self {
self.add_attr("hx-swap", &swap.into().to_value())
}
/// The hx-swap-oob attribute allows you to specify that some content in a response should be swapped into the DOM somewhere other than the target, that is “Out of Band”. This allows you to piggy back updates to other element updates on a response.
#[must_use]
fn hx_swap_oob(self) -> Self {
self.add_attr("hx-swap-oob", "true")
}
/// The hx-target attribute allows you to target a different element for swapping than the one issuing the AJAX request.
#[must_use]
fn hx_target(self, element: Selector) -> Self {
self.add_attr("hx-target", &element.to_value())
}
/// The hx-trigger attribute allows you to specify what triggers an AJAX request.
#[must_use]
fn hx_trigger<T: Into<Trigger>>(self, trigger: T) -> Self {
self.add_attr("hx-trigger", &trigger.into().to_value())
}
}