Struct respo::RespoEffectArg
source · pub struct RespoEffectArg(_);Expand description
(internal) abstraction on effect argument
Implementations§
source§impl RespoEffectArg
impl RespoEffectArg
sourcepub fn new(v: Value) -> Self
pub fn new(v: Value) -> Self
Examples found in repository?
src/respo/primes.rs (line 558)
550 551 552 553 554 555 556 557 558 559 560 561 562
pub fn new<U, V>(args: Vec<V>, handler: U) -> Self
where
U: Fn(Vec<RespoEffectArg>, RespoEffectType, &Node) -> Result<(), String> + 'static,
V: Serialize,
{
Self {
args: args
.iter()
.map(|v| RespoEffectArg::new(serde_json::to_value(v).expect("to json")))
.collect(),
handler: Rc::new(handler),
}
}sourcepub fn cast_into<U>(&self) -> Result<U, String>where
U: DeserializeOwned,
pub fn cast_into<U>(&self) -> Result<U, String>where
U: DeserializeOwned,
Examples found in repository?
src/dialog.rs (line 26)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
pub(crate) fn effect_focus(args: Vec<RespoEffectArg>, effect_type: RespoEffectType, el: &Node) -> Result<(), String> {
let show: bool = args[0].cast_into()?;
if effect_type == RespoEffectType::Updated && show {
focus_element(el, BUTTON_NAME)?;
}
Ok(())
}
fn focus_element(el: &Node, name: &str) -> Result<(), String> {
match el.dyn_ref::<Element>().unwrap().query_selector(&format!(".{}", name)) {
Ok(Some(element)) => {
match element.dyn_ref::<HtmlElement>() {
Some(el) => el.focus().unwrap(),
None => {
respo::util::log!("focus_element: {} is not an HTML element", name);
}
};
}
Ok(None) => {
respo::util::log!("focus_element: {} not found", name);
}
Err(e) => {
respo::util::log!("focus_element: {} not found: {:?}", name, e);
}
}
Ok(())
}
pub(crate) fn effect_modal_fade(args: Vec<RespoEffectArg>, effect_type: RespoEffectType, el: &Node) -> Result<(), String> {
let show: bool = args[0].cast_into()?;
match effect_type {
RespoEffectType::BeforeUpdate => {
if !show {
// when closing, fade out the cloned element
match el.first_child() {
Some(target) => {
let d = target.clone_node_with_deep(true).unwrap();
let cloned = Rc::new(d.dyn_ref::<HtmlElement>().unwrap().to_owned()); // outlive
let cloned2 = cloned.clone();
let document = el.owner_document().unwrap();
document.body().unwrap().append_child(&cloned).unwrap();
// setTimeout
let window = web_sys::window().unwrap();
let immediate_call: Closure<dyn FnMut()> = Closure::once(move || {
let style = cloned.style();
style.set_property("opacity", "0").unwrap();
let card = cloned.first_child().unwrap();
let card_style = card.dyn_ref::<HtmlElement>().unwrap().style();
card_style.set_property("transition-duration", "240ms").unwrap();
card_style.set_property("transform", "scale(0.94) translate(0px,-20px)").unwrap();
});
window
.set_timeout_with_callback_and_timeout_and_arguments_0(immediate_call.as_ref().unchecked_ref(), 10)
.unwrap();
immediate_call.forget();
let delay_call: Closure<dyn FnMut()> = Closure::once(move || {
cloned2.remove();
});
window
.set_timeout_with_callback_and_timeout_and_arguments_0(delay_call.as_ref().unchecked_ref(), 250)
.unwrap();
delay_call.forget();
}
None => {
respo::util::log!("content not found");
}
}
}
}
RespoEffectType::Updated => {
if show {
// when opening, fade in the cloned element
let target = el.first_child().unwrap();
let style = target.dyn_ref::<HtmlElement>().unwrap().style();
let card_style = target.first_child().unwrap().dyn_ref::<HtmlElement>().unwrap().style();
style.set_property("opacity", "0").unwrap();
card_style.set_property("transform", "scale(0.94) translate(0px,-12px)").unwrap();
let call = Closure::once(move || {
style.set_property("transition-duration", "240ms").unwrap();
card_style.set_property("transition-duration", "240ms").unwrap();
style.set_property("opacity", "1").unwrap();
card_style.set_property("transform", "scale(1) translate(0px,0px)").unwrap();
});
let window = web_sys::window().unwrap();
window
.set_timeout_with_callback_and_timeout_and_arguments_0(call.as_ref().unchecked_ref(), 10)
.unwrap();
call.forget();
}
}
_ => {}
}
Ok(())
}
pub(crate) fn effect_drawer_fade(args: Vec<RespoEffectArg>, effect_type: RespoEffectType, el: &Node) -> Result<(), String> {
let show: bool = args[0].cast_into()?;
match effect_type {
RespoEffectType::BeforeUpdate => {
if !show {
// when closing, fade out the cloned element
match el.first_child() {
Some(target) => {
let d = target.clone_node_with_deep(true).unwrap();
let cloned = Rc::new(d.dyn_ref::<HtmlElement>().unwrap().to_owned()); // outlive
let cloned2 = cloned.clone();
let document = el.owner_document().unwrap();
document.body().unwrap().append_child(&cloned).unwrap();
// setTimeout
let window = web_sys::window().unwrap();
let immediate_call: Closure<dyn FnMut()> = Closure::once(move || {
let style = cloned.style();
style.set_property("opacity", "0").unwrap();
let card = cloned.first_child().unwrap();
let card_style = card.dyn_ref::<HtmlElement>().unwrap().style();
card_style.set_property("transition-duration", "240ms").unwrap();
card_style.set_property("transform", "translate(100%,0px)").unwrap();
});
window
.set_timeout_with_callback_and_timeout_and_arguments_0(immediate_call.as_ref().unchecked_ref(), 10)
.unwrap();
immediate_call.forget();
let delay_call: Closure<dyn FnMut()> = Closure::once(move || {
cloned2.remove();
});
window
.set_timeout_with_callback_and_timeout_and_arguments_0(delay_call.as_ref().unchecked_ref(), 250)
.unwrap();
delay_call.forget();
}
None => {
respo::util::log!("content not found");
}
}
}
}
RespoEffectType::Updated => {
if show {
// when opening, fade in the cloned element
let target = el.first_child().unwrap();
let style = target.dyn_ref::<HtmlElement>().unwrap().style();
let card_style = target.first_child().unwrap().dyn_ref::<HtmlElement>().unwrap().style();
style.set_property("opacity", "0").unwrap();
card_style.set_property("transform", "translate(100%, 0px)").unwrap();
let call = Closure::once(move || {
style.set_property("transition-duration", "240ms").unwrap();
card_style.set_property("transition-duration", "240ms").unwrap();
style.set_property("opacity", "1").unwrap();
card_style.set_property("transform", "translate(0px,0px)").unwrap();
});
let window = web_sys::window().unwrap();
window
.set_timeout_with_callback_and_timeout_and_arguments_0(call.as_ref().unchecked_ref(), 10)
.unwrap();
call.forget();
}
}
_ => {}
}
Ok(())
}Trait Implementations§
source§impl Clone for RespoEffectArg
impl Clone for RespoEffectArg
source§fn clone(&self) -> RespoEffectArg
fn clone(&self) -> RespoEffectArg
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl Debug for RespoEffectArg
impl Debug for RespoEffectArg
source§impl<'de> Deserialize<'de> for RespoEffectArg
impl<'de> Deserialize<'de> for RespoEffectArg
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl PartialEq<RespoEffectArg> for RespoEffectArg
impl PartialEq<RespoEffectArg> for RespoEffectArg
source§fn eq(&self, other: &RespoEffectArg) -> bool
fn eq(&self, other: &RespoEffectArg) -> bool
This method tests for
self and other values to be equal, and is used
by ==.