Struct respo::RespoEffectArg

source ·
pub struct RespoEffectArg(_);
Expand description

(internal) abstraction on effect argument

Implementations§

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),
    }
  }
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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.