1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#![allow(unused_variables)]
#![allow(dead_code)]

mod drawer_app_content;
mod drawer_header;
mod drawer_subtitle;
mod drawer_title;

pub use drawer_app_content::*;
pub use drawer_header::*;
pub use drawer_subtitle::*;
pub use drawer_title::*;

use super::WeakComponentLink;
use gloo::{
    events::EventListener,
    timers::callback::Timeout,
};

use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{Element, Node};
use yew::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;

/// The `drawer` component
///
/// [Documentation](https://github.com/material-components/material-components-web-components/tree/master/packages/drawer)
pub struct Drawer {
    props: DrawerProps,
    node_ref: NodeRef,
    opened_listener: Option<EventListener>,
    closed_listener: Option<EventListener>,
    animation_frame: i32,
    animation_timer: i32,
}

pub enum DrawerMsg {
    Open,
    Close,
    TransitionEnd,
}

/// Props for [`Drawer`]
///
/// Documentation:
///
/// - [Properties](https://github.com/material-components/material-components-web-components/tree/master/packages/drawer#propertiesattributes)
/// - [Events](https://github.com/material-components/material-components-web-components/tree/master/packages/drawer#events)
#[derive(Clone, PartialEq, Properties)]
pub struct DrawerProps {
    #[prop_or_default]
    pub open: bool,
    #[prop_or_default]
    pub has_header: Option<bool>,
    #[prop_or_default]
    pub drawer_type: String,
    /// Binds to `opened` event on `drawer`
    ///
    /// See events docs to learn more.
    #[prop_or_default]
    pub onopened: Callback<()>,
    /// Binds to `closed` event on `drawer`
    ///
    /// See events docs to learn more.
    #[prop_or_default]
    pub onclosed: Callback<()>,
    #[prop_or_default]
    pub link: WeakComponentLink<Drawer>,
    #[prop_or_default]
    pub children: Children,
}

impl Component for Drawer {
    type Message = DrawerMsg;
    type Properties = DrawerProps;

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        props.link.borrow_mut().replace(link);
        // Drawer::ensure_loaded();
        Self {
            props,
            node_ref: NodeRef::default(),
            opened_listener: None,
            closed_listener: None,
            animation_frame: 0,
            animation_timer: 0,
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Self::Message::Open => {
                self.props.open = true;
                let el = self.node_ref.cast::<Element>().unwrap();
                let class_list = el.class_list();
                let _ = class_list.add_2("mdc-drawer--open", "mdc-drawer--animate");
                
                // just a set timeout
                let timeout = Timeout::new(5, move || {
                    let _ = class_list.add_1("mdc-drawer--opening");
                });

                timeout.forget();

                // // request animation frame
                // let f = Rc::new(RefCell::new(None));
                // let g = f.clone();
                // // let g = f.borrow().as_ref().unwrap();
                // *g.borrow_mut() = Some(Closure::wrap(Box::new(move ||{
                //     info!("OOOUCH");
                //     let _ = f.borrow_mut().take();
                //     let timeout = Timeout::new(0, move || {
                //         info!("GOT TIMEOUT");
                //     });
                //     timeout.forget();
                // }) as Box<dyn FnMut()>));
                // request_animation_frame(g.borrow().as_ref().unwrap());
            },
            Self::Message::Close => {
                // self.onsignal.emit(()); 
                self.props.open = false;
                let el = self.node_ref.cast::<Element>().unwrap();
                let class_list = el.class_list();
                let _ = class_list.add_1("mdc-drawer--closing");
            }
            Self::Message::TransitionEnd => {
                let el = self.node_ref.cast::<Element>().unwrap();
                let class_list = el.class_list();
                let _ = class_list.remove_3("mdc-drawer--animate", "mdc-drawer--opening", "mdc-drawer--closing");
                if !self.props.open {
                    let _ = class_list.remove_1("mdc-drawer--open");
                } 
            }
        }
        false
    }

    fn change(&mut self, props: Self::Properties) -> bool {
        if self.props != props {
            self.props = props;
            true
        } else {
            false
        }
    }

    fn view(&self) -> Html {
        // hasHeader?=self.props.has_header
        let link = self.props.link.borrow().clone().unwrap();
        html! {
            <>
                <aside class="mdc-drawer mdc-drawer--modal" ref=self.node_ref.clone() ontransitionend=link.callback(|_| Self::Message::TransitionEnd)>
                    <div class="mdc-drawer__content">
                        <nav class="mdc-list">
                            { self.props.children.clone() }
                        </nav>
                    </div>
                </aside>
                <div class="mdc-drawer-scrim" onclick=link.callback(|_| Self::Message::Close)></div>
            </>
        }
    }

    fn rendered(&mut self, first_render: bool) {
        // let element = self.node_ref.cast::<Drawer>().unwrap();
        // element.set_type(&JsValue::from(&self.props.drawer_type));
        // element.set_open(self.props.open);

        // if first_render {
        //     let onopen_callback = self.props.onopened.clone();
        //     let onclose_callback = self.props.onclosed.clone();

        //     let element = self.node_ref.cast::<Element>().unwrap();

        //     self.opened_listener = Some(EventListener::new(
        //         &element,
        //         "MDCDrawer:opened",
        //         move |_| {
        //             onopen_callback.emit(());
        //         },
        //     ));

        //     self.closed_listener = Some(EventListener::new(
        //         &element,
        //         "MDCDrawer:closed",
        //         move |_| {
        //             onclose_callback.emit(());
        //         },
        //     ));
        // }
    }
}

impl Drawer {
    fn classes(&self) -> String {
        if self.props.open {
            "mdc-drawer mdc-drawer--modal mdc-drawer--open".into()
        } else {
            "mdc-drawer mdc-drawer--modal".into()
        }
    }


    // Big Unusable Magic
    fn run_next_animation_frame<F>(&self, callback: F) where F: FnOnce() {
        info!("run_next_animation_frame");
        
        // let _ = window().cancel_animation_frame(self.animation_frame);
        let f = Rc::new(RefCell::new(None));
        let g = f.clone();
        // let g = f.borrow().as_ref().unwrap();
        *g.borrow_mut() = Some(Closure::wrap(Box::new(move ||{
            info!("OOOUCH");

            // self.animation_frame = 0;
            let _ = f.borrow_mut().take();
        }) as Box<dyn FnMut()>));
        request_animation_frame(g.borrow().as_ref().unwrap());
    }
}

// move to utils
fn window() -> web_sys::Window {
    web_sys::window().expect("no global `window` exists")
}

fn request_animation_frame(f: &Closure<dyn FnMut()>) {
    window()
        .request_animation_frame(f.as_ref().unchecked_ref())
        .expect("should register `requestAnimationFrame` OK");
}

fn document() -> web_sys::Document {
    window()
        .document()
        .expect("should have a document on window")
}

fn body() -> web_sys::HtmlElement {
    document().body().expect("document should have a body")
}

impl WeakComponentLink<Drawer> {
    /// A convenience method to for `drawer.open = !drawer.open`
    pub fn flip_open_state(&self) {
        // let node_ref = (*self.borrow().as_ref().unwrap().get_component().unwrap())
        //     .node_ref
        //     .clone();
        // let element = node_ref.cast::<Drawer>().unwrap();
        // let open = element.open();
        // element.set_open(!open);
    }
}

pub struct DrawerItem {
    props: DrawerItemProps,
    link: ComponentLink<Self>,
    // onsignal: Callback<()>,
}

pub enum Msg {
    Clicked,
}

/// Props for [`DrawerItem`]
///
/// [Documentation for properties](https://github.com/material-components/material-components-web-components/tree/master/packages/button#propertiesattributes)
#[derive(Debug, Properties, Clone)]
pub struct DrawerItemProps {
    pub label: String,
    #[prop_or_default]
    pub icon: String,
    #[prop_or_default]
    pub raised: bool,
    #[prop_or_default]
    pub unelevated: bool,
    #[prop_or_default]
    pub outlined: bool,
    #[prop_or_default]
    pub dense: bool,
    #[prop_or_default]
    pub disabled: bool,
    #[prop_or_default]
    pub trailing_icon: bool,
    // #[prop_or_default]
    // pub onsignal: Callback<()>,
}

// label=props.label
// icon?=props.icon.as_ref()
// raised?=to_option(props.raised)
// unelevated?=to_option(props.unelevated)
// outlined?=to_option(props.outlined)
// dense?=to_option(props.dense)
// trailingIcon?=to_option(props.trailing_icon)
// disabled=props.disabled

impl Component for DrawerItem {
    type Message = Msg;
    type Properties = DrawerItemProps;

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        Self {
            link,
            props: props
            // onsignal: props.onsignal,
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Clicked => {
                // self.onsignal.emit(());
            }
        }
        false
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        self.props = props;
        // self.onsignal = props.onsignal;
        true
    }

    fn view(&self) -> Html {
        // <a class="mdc-list-item mdc-list-item--activated" href="#" aria-current="page">
        // mdc-list-item--selected
        html! {
            <a class="mdc-list-item" href="#" aria-current="page">
                <span class="mdc-list-item__ripple"></span>
                <i class="material-icons mdc-list-item__graphic" aria-hidden="true">{ &self.props.icon }</i>
                <span class="mdc-list-item__text">{ &self.props.label }</span>
            </a>
        }
    }
}