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
339
340
341
342
343
344
345
346
347
use std::fmt::{self, Debug};
use std::convert::From;
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::collections::*;
use std::cell::{self, Cell, RefCell};
use std::rc::Rc;
use std::any::Any;
use serde::{self, Serialize, Deserialize, de::DeserializeOwned};
use either::Either;
use wasm_bindgen::JsValue;

use ss_web_utils::js;
use ss_web_utils::dom;


///////////////////////////////////////////////////////////////////////////////
// DOM EVENT-HANDLERS
///////////////////////////////////////////////////////////////////////////////

pub fn on_click<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_mouse_down<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_mouse_up<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_mouse_enter<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_mouse_leave<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_mouse_over<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_mouse_out<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_input<Msg>(cb: fn(String)->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_check<Msg>(cb: fn(bool)->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_submit<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_blur<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}
pub fn on_focus<Msg>(cb: fn()->Msg) -> EventHandler<Msg> {
    unimplemented!()
}


///////////////////////////////////////////////////////////////////////////////
// INTERNAL DATA TYPES
///////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
pub struct EventHandlers<Msg>(pub(crate) BTreeMap<EventType, EventHandler<Msg>>);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct EventHandler<Msg>(pub(crate) IEventHandler<Msg>);

impl<Msg> EventHandler<Msg> {
    pub fn event_name(&self) -> EventType {
        match self {
            EventHandler(IEventHandler::OnClick(_)) => 
                EventType::OnClick,
            EventHandler(IEventHandler::OnMouseDown(_)) => 
                EventType::OnMouseDown,
            EventHandler(IEventHandler::OnMouseUp(_)) => 
                EventType::OnMouseUp,
            EventHandler(IEventHandler::OnMouseEnter(_)) => 
                EventType::OnMouseEnter,
            EventHandler(IEventHandler::OnMouseLeave(_)) => 
                EventType::OnMouseLeave,
            EventHandler(IEventHandler::OnMouseOver(_)) => 
                EventType::OnMouseOver,
            EventHandler(IEventHandler::OnMouseOut(_)) => 
                EventType::OnMouseOut,
            EventHandler(IEventHandler::OnInput(_)) => 
                EventType::OnInput,
            EventHandler(IEventHandler::OnCheck(_)) => 
                EventType::OnCheck,
            EventHandler(IEventHandler::OnSubmit(_)) => 
                EventType::OnSubmit,
            EventHandler(IEventHandler::OnBlur(_)) => 
                EventType::OnBlur,
            EventHandler(IEventHandler::OnFocus(_)) => 
                EventType::OnFocus,
        }
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
/// DOM events enum.
pub enum EventType {
    OnClick,
    OnMouseDown,
    OnMouseUp,
    OnMouseEnter,
    OnMouseLeave,
    OnMouseOver,
    OnMouseOut,
    OnInput,
    OnCheck,
    OnSubmit,
    OnBlur,
    OnFocus,
}

impl EventType {
    /// Gets the event name as a string.
    pub fn as_str(&self) -> &str {
        match self {
            EventType::OnClick => "click",
            EventType::OnMouseDown => "mousedown",
            EventType::OnMouseUp => "mouseup",
            EventType::OnMouseEnter => "mouseenter",
            EventType::OnMouseLeave => "mouseenter",
            EventType::OnMouseOver => "mouseover",
            EventType::OnMouseOut => "mouseout",
            EventType::OnInput => "change",
            EventType::OnCheck => "click",
            EventType::OnSubmit => "submit",
            EventType::OnBlur => "blur",
            EventType::OnFocus => "focus",
        }
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum IEventHandler<Msg> {
    OnClick(OnClick<Msg>),
    OnMouseDown(OnMouseDown<Msg>),
    OnMouseUp(OnMouseUp<Msg>),
    OnMouseEnter(OnMouseEnter<Msg>),
    OnMouseLeave(OnMouseLeave<Msg>),
    OnMouseOver(OnMouseOver<Msg>),
    OnMouseOut(OnMouseOut<Msg>),
    OnInput(OnInput<Msg>),
    OnCheck(OnCheck<Msg>),
    OnSubmit(OnSubmit<Msg>),
    OnBlur(OnBlur<Msg>),
    OnFocus(OnFocus<Msg>),
}

// MOUSE
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnClick<Msg>(pub fn()->Msg);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnMouseDown<Msg>(pub fn()->Msg);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnMouseUp<Msg>(pub fn()->Msg);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnMouseEnter<Msg>(pub fn()->Msg);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnMouseLeave<Msg>(pub fn()->Msg);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnMouseOver<Msg>(pub fn()->Msg);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnMouseOut<Msg>(pub fn()->Msg);


// FORMS
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnInput<Msg>(pub fn(String)->Msg);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnCheck<Msg>(pub fn(bool)->Msg);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnSubmit<Msg>(pub fn()->Msg);


// FOCUS
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnBlur<Msg>(pub fn()->Msg);

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct OnFocus<Msg>(pub fn()->Msg);


///////////////////////////////////////////////////////////////////////////////
// INTERNAL - NODE EVENT-HANDLERS
///////////////////////////////////////////////////////////////////////////////

impl<Msg: Ord> EventHandlers<Msg> {
    pub fn new() -> Self {
        EventHandlers(BTreeMap::new())
    }
    pub fn into_inner(self) -> Vec<EventHandler<Msg>> {
        let mut results = Vec::new();
        for (_, v) in self.0 {
            results.push(v);
        }
        results
    }
    pub fn add(&mut self, handler: EventHandler<Msg>) {
        let key = match &handler {
            EventHandler(IEventHandler::OnClick(_)) =>
                EventType::OnClick,
            EventHandler(IEventHandler::OnMouseDown(_)) =>
                EventType::OnMouseDown,
            EventHandler(IEventHandler::OnMouseUp(_)) =>
                EventType::OnMouseUp,
            EventHandler(IEventHandler::OnMouseEnter(_)) =>
                EventType::OnMouseEnter,
            EventHandler(IEventHandler::OnMouseLeave(_)) =>
                EventType::OnMouseLeave,
            EventHandler(IEventHandler::OnMouseOver(_)) =>
                EventType::OnMouseOver,
            EventHandler(IEventHandler::OnMouseOut(_)) =>
                EventType::OnMouseOut,
            EventHandler(IEventHandler::OnInput(_)) =>
                EventType::OnInput,
            EventHandler(IEventHandler::OnCheck(_)) =>
                EventType::OnCheck,
            EventHandler(IEventHandler::OnSubmit(_)) =>
                EventType::OnSubmit,
            EventHandler(IEventHandler::OnBlur(_)) =>
                EventType::OnBlur,
            EventHandler(IEventHandler::OnFocus(_)) =>
                EventType::OnFocus,
        };
        self.0.insert(key, handler);
    }
}


///////////////////////////////////////////////////////////////////////////////
// INTERNAL - INSTANCES
///////////////////////////////////////////////////////////////////////////////

// ALL
impl<Msg> js::Handler<Msg> for EventHandler<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0.handler(event)
    }
}
impl<Msg> js::Handler<Msg> for IEventHandler<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        match self {
            IEventHandler::OnClick(x) => x.handler(event),
            IEventHandler::OnMouseDown(x) => x.handler(event),
            IEventHandler::OnMouseUp(x) => x.handler(event),
            IEventHandler::OnMouseEnter(x) => x.handler(event),
            IEventHandler::OnMouseLeave(x) => x.handler(event),
            IEventHandler::OnMouseOver(x) => x.handler(event),
            IEventHandler::OnMouseOut(x) => x.handler(event),
            IEventHandler::OnInput(x) => x.handler(event),
            IEventHandler::OnCheck(x) => x.handler(event),
            IEventHandler::OnSubmit(x) => x.handler(event),
            IEventHandler::OnBlur(x) => x.handler(event),
            IEventHandler::OnFocus(x) => x.handler(event),
        }
    }
}

// MOUSE
impl<Msg> js::Handler<Msg> for OnClick<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}
impl<Msg> js::Handler<Msg> for OnMouseDown<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}
impl<Msg> js::Handler<Msg> for OnMouseUp<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}
impl<Msg> js::Handler<Msg> for OnMouseEnter<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}
impl<Msg> js::Handler<Msg> for OnMouseLeave<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}
impl<Msg> js::Handler<Msg> for OnMouseOver<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}
impl<Msg> js::Handler<Msg> for OnMouseOut<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}


// FORMS
impl<Msg> js::Handler<Msg> for OnInput<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0(dom::event::get_oninput_value(&event))
    }
}
impl<Msg> js::Handler<Msg> for OnCheck<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        let event: web_sys::Event = From::from(event.clone());
        let target: web_sys::EventTarget = event
            .target()
            .expect("target failed");
        let target: JsValue = From::from(target);
        let target: web_sys::HtmlInputElement = From::from(target);
        let value: bool = target.checked();
        self.0(value)
    }
}
impl<Msg> js::Handler<Msg> for OnSubmit<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}

// FOCUS
impl<Msg> js::Handler<Msg> for OnBlur<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}
impl<Msg> js::Handler<Msg> for OnFocus<Msg> {
    fn handler(&self, event: JsValue) -> Msg {
        self.0()
    }
}