166 lines
5.9 KiB
Rust
166 lines
5.9 KiB
Rust
#[allow(non_camel_case_types)]
|
|
pub enum SwapStrategy {
|
|
/// Replace the inner html of the target element
|
|
innerHTML,
|
|
/// Replace the entire target element with the response
|
|
outerHTML,
|
|
/// Replace the text content of the target element, without parsing the response as HTML
|
|
textContent,
|
|
/// Insert the response before the target element
|
|
beforebegin,
|
|
/// Insert the response before the first child of the target element
|
|
afterbegin,
|
|
/// Insert the response after the last child of the target element
|
|
beforeend,
|
|
/// Insert the response after the target element
|
|
afterend,
|
|
/// Deletes the target element regardless of the response
|
|
delete,
|
|
/// Does not append content from response (out of band items will still be processed).
|
|
none,
|
|
}
|
|
|
|
impl Default for SwapStrategy {
|
|
fn default() -> Self {
|
|
Self::innerHTML
|
|
}
|
|
}
|
|
|
|
impl SwapStrategy {
|
|
#[must_use]
|
|
pub const fn to_value(&self) -> &str {
|
|
match self {
|
|
Self::innerHTML => "innerHTML",
|
|
Self::outerHTML => "outerHTML",
|
|
Self::textContent => "textContent",
|
|
Self::beforebegin => "beforebegin",
|
|
Self::afterbegin => "afterbegin",
|
|
Self::beforeend => "beforeend",
|
|
Self::afterend => "afterend",
|
|
Self::delete => "delete",
|
|
Self::none => "none",
|
|
}
|
|
}
|
|
|
|
/// If you want to use the new View Transitions API when a swap occurs, you can use the transition:true option for your swap.
|
|
#[must_use]
|
|
pub fn transition(self) -> ModifiedSwapStrategy {
|
|
let modifier = "transition".to_owned();
|
|
ModifiedSwapStrategy::new(self, modifier)
|
|
}
|
|
|
|
/// You can modify the amount of time that htmx will wait after receiving a response to swap the content by including a swap modifier.
|
|
#[must_use]
|
|
pub fn swap(self, duration: &str) -> ModifiedSwapStrategy {
|
|
let modifier = format!("swap:{duration}");
|
|
ModifiedSwapStrategy::new(self, modifier)
|
|
}
|
|
|
|
/// You can modify the time between the swap and the settle logic by including a settle modifier.
|
|
#[must_use]
|
|
pub fn settle(self, duration: &str) -> ModifiedSwapStrategy {
|
|
let modifier = format!("settle:{duration}");
|
|
ModifiedSwapStrategy::new(self, modifier)
|
|
}
|
|
|
|
/// By default, htmx will update the title of the page if it finds a `<title>` tag in the response content. You can turn off this behavior.
|
|
#[must_use]
|
|
pub fn ignore_title(self) -> ModifiedSwapStrategy {
|
|
let modifier = "ignoreTitle:true";
|
|
ModifiedSwapStrategy::new(self, modifier.to_owned())
|
|
}
|
|
|
|
/// htmx preserves focus between requests for inputs that have a defined id attribute. By default htmx prevents auto-scrolling to focused inputs between requests which can be unwanted behavior on longer requests when the user has already scrolled away.
|
|
#[must_use]
|
|
pub fn focus_scroll(self, enable: bool) -> ModifiedSwapStrategy {
|
|
let modifier = format!("focus-scroll:{enable}");
|
|
ModifiedSwapStrategy::new(self, modifier)
|
|
}
|
|
|
|
/// Ensure visibility
|
|
#[must_use]
|
|
pub fn show(self, e: &str) -> ModifiedSwapStrategy {
|
|
let modifier = format!("show:{e}");
|
|
ModifiedSwapStrategy::new(self, modifier)
|
|
}
|
|
|
|
/// Scroll to this location after load
|
|
#[must_use]
|
|
pub fn scroll(self, e: &str) -> ModifiedSwapStrategy {
|
|
let modifier = format!("scroll:{e}");
|
|
ModifiedSwapStrategy::new(self, modifier)
|
|
}
|
|
}
|
|
|
|
pub struct ModifiedSwapStrategy {
|
|
pub strategy: SwapStrategy,
|
|
pub modifiers: Vec<String>,
|
|
}
|
|
|
|
impl From<SwapStrategy> for ModifiedSwapStrategy {
|
|
fn from(value: SwapStrategy) -> Self {
|
|
Self::new(value, String::new())
|
|
}
|
|
}
|
|
|
|
impl ModifiedSwapStrategy {
|
|
fn new(strategy: SwapStrategy, modifier: String) -> Self {
|
|
Self {
|
|
strategy,
|
|
modifiers: vec![modifier],
|
|
}
|
|
}
|
|
|
|
/// If you want to use the new View Transitions API when a swap occurs, you can use the transition:true option for your swap.
|
|
pub fn transition(mut self) -> Self {
|
|
let modifier = "transition:true".to_owned();
|
|
self.modifiers.push(modifier);
|
|
self
|
|
}
|
|
|
|
/// You can modify the amount of time that htmx will wait after receiving a response to swap the content by including a swap modifier.
|
|
pub fn swap(mut self, duration: &str) -> Self {
|
|
let modifier = format!("swap:{duration}");
|
|
self.modifiers.push(modifier);
|
|
self
|
|
}
|
|
|
|
/// You can modify the time between the swap and the settle logic by including a settle modifier.
|
|
pub fn settle(mut self, duration: &str) -> Self {
|
|
let modifier = format!("settle:{duration}");
|
|
self.modifiers.push(modifier);
|
|
self
|
|
}
|
|
|
|
/// By default, htmx will update the title of the page if it finds a `<title>` tag in the response content. You can turn off this behavior.
|
|
pub fn ignore_title(mut self) -> Self {
|
|
let modifier = "ignoreTitle:true";
|
|
self.modifiers.push(modifier.to_owned());
|
|
self
|
|
}
|
|
|
|
/// htmx preserves focus between requests for inputs that have a defined id attribute. By default htmx prevents auto-scrolling to focused inputs between requests which can be unwanted behavior on longer requests when the user has already scrolled away.
|
|
pub fn focus_scroll(mut self, enable: bool) -> Self {
|
|
let modifier = format!("focus-scroll:{enable}");
|
|
self.modifiers.push(modifier);
|
|
self
|
|
}
|
|
|
|
/// Ensure visibility
|
|
pub fn show(mut self, e: &str) -> Self {
|
|
let modifier = format!("show:{e}");
|
|
self.modifiers.push(modifier);
|
|
self
|
|
}
|
|
|
|
/// Scroll to this location after load
|
|
pub fn scroll(mut self, e: &str) -> Self {
|
|
let modifier = format!("scroll:{e}");
|
|
self.modifiers.push(modifier);
|
|
self
|
|
}
|
|
|
|
pub fn to_value(&self) -> String {
|
|
format!("{} {}", self.strategy.to_value(), self.modifiers.join(" "))
|
|
}
|
|
}
|