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
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fmt::Debug;

use educe::Educe;
#[cfg(all(target_arch = "wasm32", feature = "web-csr"))]
use wasm_bindgen::UnwrapThrowExt;

use crate::node::{Node, NodeRef};
use crate::reflow::{Bond, Lotus};
use crate::view::{ViewId, ViewPosition};
use crate::web::events::EventDescriptor;
use crate::web::{AttrValue, ClassPart, Classes, PropValue};
use crate::widget::{Filler, IntoFiller};
use crate::{Scope, Widget};

#[derive(Educe)]
#[educe(Debug)]
pub struct Element {
    pub name: Cow<'static, str>,
    pub is_void: bool,
    pub classes: Classes,
    #[educe(Debug(ignore))]
    pub attrs: BTreeMap<Cow<'static, str>, Box<dyn AttrValue>>,
    #[educe(Debug(ignore))]
    pub props: BTreeMap<Cow<'static, str>, Box<dyn PropValue>>,
    #[educe(Debug(ignore))]
    #[allow(clippy::type_complexity)]
    pub fillers: Vec<Filler>,

    pub(crate) node: Node,
}

impl Widget for Element {
    fn build(&mut self, ctx: &mut Scope) {
        ctx.graff_node = Some(self.node.clone());

        let fillers = std::mem::take(&mut self.fillers);
        for filler in fillers {
            filler.fill(ctx);
        }
        for (name, value) in &self.props {
            value.inject_to(&ctx.view_id, &mut self.node, name, true);
        }

        self.attrs.insert("gly-id".into(), Box::new(ctx.view_id.clone()));
        for (name, value) in &self.attrs {
            value.inject_to(&ctx.view_id, &mut self.node, name, true);
        }
        self.classes.inject_to(&ctx.view_id, &mut self.node, "class", true);
    }

    fn flood(&mut self, ctx: &mut Scope) {
        let parent_node = ctx.parent_node.as_ref().unwrap();
        match &ctx.position {
            ViewPosition::Head => parent_node.prepend_with_node(&self.node),
            ViewPosition::Prev(prev_node) => prev_node.after_with_node(&self.node),
            ViewPosition::Next(next_node) => next_node.before_with_node(&self.node),
            ViewPosition::Tail => parent_node.append_with_node(&self.node),
            ViewPosition::Unset => {
                crate::warn!("node position is unset. {:#?}", &self.node);
                parent_node.append_with_node(&self.node);
            }
        }

        let ids: Vec<ViewId> = ctx.child_views.keys().cloned().collect();
        for id in ids {
            ctx.attach_child(&id);
        }
    }
    fn detach(&mut self, ctx: &mut Scope) {
        if let Some(parent_node) = ctx.parent_node.as_ref() {
            parent_node.remove_child(&self.node);
        }
        let ids: Vec<ViewId> = ctx.child_views.keys().cloned().collect();
        for id in ids {
            ctx.detach_child(&id);
        }
    }
    fn patch(&mut self, ctx: &mut Scope) {
        for (name, value) in &self.props {
            value.inject_to(&ctx.view_id, &mut self.node, name, false);
        }
        for (name, value) in &self.attrs {
            value.inject_to(&ctx.view_id, &mut self.node, name, false);
        }
        self.classes.inject_to(&ctx.view_id, &mut self.node, "class", false);
    }
}

impl Element {
    pub fn new(name: impl Into<Cow<'static, str>>, is_void: bool) -> Self {
        let name = name.into();
        Self {
            name: name.clone(),
            is_void,
            classes: Default::default(),
            attrs: Default::default(),
            props: Default::default(),
            fillers: vec![],
            node: Node::new(name, is_void),
        }
    }

    pub fn node(&self) -> &Node {
        &self.node
    }
    pub fn add_filler(&mut self, filler: impl IntoFiller) {
        self.fillers.push(filler.into_filler());
    }
    pub fn fill(mut self, filler: impl IntoFiller) -> Self {
        self.fillers.push(filler.into_filler());
        self
    }

    pub fn then<F>(self, func: F) -> Self
    where
        F: FnOnce(Self) -> Self,
    {
        func(self)
    }

    pub fn is_void(&self) -> bool {
        self.is_void
    }

    #[track_caller]
    pub fn add_id<V>(&mut self, value: V)
    where
        V: AttrValue + 'static,
    {
        self.attrs.insert("id".into(), Box::new(value));
    }

    #[track_caller]
    pub fn id<V>(mut self, value: V) -> Self
    where
        V: AttrValue + 'static,
    {
        self.attrs.insert("id".into(), Box::new(value));
        self
    }

    #[track_caller]
    pub fn add_class<V>(&mut self, value: V)
    where
        V: ClassPart + 'static,
    {
        self.classes.part(value);
    }

    #[track_caller]
    pub fn class<V>(mut self, value: V) -> Self
    where
        V: ClassPart + 'static,
    {
        self.classes.part(value);
        self
    }

    #[track_caller]
    pub fn toggle_class<V, C>(self, value: V, cond: C) -> Self
    where
        V: Into<String>,
        C: Into<Lotus<bool>>,
    {
        self.switch_class(value, "", cond)
    }

    #[track_caller]
    pub fn switch_class<TV, FV, C>(mut self, tv: TV, fv: FV, cond: C) -> Self
    where
        TV: Into<String>,
        FV: Into<String>,
        C: Into<Lotus<bool>>,
    {
        let tv = tv.into();
        let fv = fv.into();
        let cond = cond.into();
        let part = Bond::new(move || if *cond.get() { tv.clone() } else { fv.clone() });
        self.classes.part(part);
        self
    }

    /// Adds an property to this element.
    #[track_caller]
    pub fn add_prop<V>(&mut self, name: impl Into<Cow<'static, str>>, value: V)
    where
        V: PropValue + 'static,
    {
        self.props.insert(name.into(), Box::new(value));
    }

    /// Adds an property to this element.
    #[track_caller]
    pub fn prop<V>(mut self, name: impl Into<Cow<'static, str>>, value: V) -> Self
    where
        V: PropValue + 'static,
    {
        self.props.insert(name.into(), Box::new(value));
        self
    }
    /// Adds an attribute to this element.
    #[track_caller]
    pub fn add_attr<V>(&mut self, name: impl Into<Cow<'static, str>>, value: V)
    where
        V: AttrValue + 'static,
    {
        self.attrs.insert(name.into(), Box::new(value));
    }
    /// Adds an attribute to this element.
    #[track_caller]
    pub fn attr<V>(mut self, name: impl Into<Cow<'static, str>>, value: V) -> Self
    where
        V: AttrValue + 'static,
    {
        self.attrs.insert(name.into(), Box::new(value));
        self
    }

    /// Adds an event listener to this element.
    #[track_caller]
    pub fn add_event_listener<E: EventDescriptor>(
        &self,
        _event: E,
        #[allow(unused_mut)] // used for tracing in debug
        mut _event_handler: impl FnMut(E::EventType) + 'static,
    ) {
    }

    /// Adds an event listener to this element.
    #[track_caller]
    pub fn on<E: EventDescriptor>(
        self,
        _event: E,
        #[allow(unused_mut)] // used for tracing in debug
        mut _event_handler: impl FnMut(E::EventType) + 'static,
    ) -> Self {
        self
    }

    /// Sets the inner Text of this element from the provided
    /// string slice.
    ///
    /// # Security
    /// Be very careful when using this method. Always remember to
    /// sanitize the input to avoid a cross-site scripting (XSS)
    /// vulnerability.
    pub fn set_text<V>(&mut self, text: V)
    where
        V: AttrValue + 'static,
    {
        self.attrs.insert("inner_text".into(), Box::new(text));
    }
    /// Sets the inner Text of this element from the provided
    /// string slice.
    ///
    /// # Security
    /// Be very careful when using this method. Always remember to
    /// sanitize the input to avoid a cross-site scripting (XSS)
    /// vulnerability.
    pub fn text<V>(mut self, text: V) -> Self
    where
        V: AttrValue + 'static,
    {
        self.set_text(text);
        self
    }

    /// Sets the inner HTML of this element from the provided
    /// string slice.
    ///
    /// # Security
    /// Be very careful when using this method. Always remember to
    /// sanitize the input to avoid a cross-site scripting (XSS)
    /// vulnerability.
    pub fn set_html<V>(&mut self, html: V)
    where
        V: AttrValue + 'static,
    {
        self.attrs.insert("inner_html".into(), Box::new(html));
    }
    /// Sets the inner HTML of this element from the provided
    /// string slice.
    ///
    /// # Security
    /// Be very careful when using this method. Always remember to
    /// sanitize the input to avoid a cross-site scripting (XSS)
    /// vulnerability.
    pub fn html<V>(mut self, html: V) -> Self
    where
        V: AttrValue + 'static,
    {
        self.set_html(html);
        self
    }

    pub fn node_ref<T>(self, _node_ref: &NodeRef<T>) -> Self
    where
        T: Debug,
    {
        self
    }
}