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
use crate::{
    dom::{
        events::on_component_mount, program::MountProcedure, Application, Cmd, Component, DomAttr,
        DomAttrValue, DomNode, Program,
    },
    vdom::{Attribute, AttributeName, Leaf, Node},
};
use std::{any::TypeId, cell::RefCell, fmt, rc::Rc};

/// A component that can be used directly in the view without mapping
pub trait StatefulComponent {
    /// This will be invoked when a component is used as a custom element
    /// and the attributes of the custom-element has been modified
    ///
    /// if the listed attributes in the observed attributes are modified
    fn attribute_changed(&mut self, attr: DomAttr);
    /// remove the attribute with this name
    fn remove_attribute(&mut self, _attr_name: AttributeName) {}

    /// return the DomNode which contains the children DomNode
    fn child_container(&self) -> Option<DomNode>;

    /// append a child into this component
    fn append_children(&mut self, _children: Vec<DomNode>) {}

    /// remove a child in this index
    fn remove_child(&mut self, _index: usize) {}

    /// the component is attached to the dom
    fn connected_callback(&mut self) {}
    /// the component is removed from the DOM
    fn disconnected_callback(&mut self) {}

    /// the component is moved or attached to the dom
    fn adopted_callback(&mut self) {}
}

/// Wrapper for stateful component
pub struct StatefulModel<MSG> {
    ///
    pub comp: Rc<RefCell<dyn StatefulComponent>>,
    /// component type id
    pub type_id: TypeId,
    /// component attributes
    pub attrs: Vec<Attribute<MSG>>,
    /// external children component
    pub children: Vec<Node<MSG>>,
}

impl<MSG> fmt::Debug for StatefulModel<MSG> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "StatefulModel")
    }
}

impl<MSG> StatefulModel<MSG> {
    /// mape the msg of this Leaf such that `Leaf<MSG>` becomes `Leaf<MSG2>`
    pub fn map_msg<F, MSG2>(self, cb: F) -> StatefulModel<MSG2>
    where
        F: Fn(MSG) -> MSG2 + Clone + 'static,
        MSG2: 'static,
        MSG: 'static,
    {
        StatefulModel {
            type_id: self.type_id,
            comp: self.comp,
            attrs: self
                .attrs
                .into_iter()
                .map(|a| a.map_msg(cb.clone()))
                .collect(),
            children: self
                .children
                .into_iter()
                .map(|c| c.map_msg(cb.clone()))
                .collect(),
        }
    }
}

impl<MSG> Clone for StatefulModel<MSG> {
    fn clone(&self) -> Self {
        Self {
            comp: Rc::clone(&self.comp),
            type_id: self.type_id,
            attrs: self.attrs.clone(),
            children: self.children.clone(),
        }
    }
}

impl<MSG> PartialEq for StatefulModel<MSG> {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.comp, &other.comp)
            && self.type_id == other.type_id
            && self.attrs == other.attrs
            && self.children == other.children
    }
}

impl<COMP> Application for COMP
where
    COMP: Component<XMSG = ()> + StatefulComponent + 'static,
{
    type MSG = COMP::MSG;

    fn init(&mut self) -> Cmd<Self::MSG> {
        Cmd::from(<Self as Component>::init(self))
    }

    fn update(&mut self, msg: COMP::MSG) -> Cmd<Self::MSG> {
        let effects = <Self as Component>::update(self, msg);
        Cmd::from(effects)
    }

    fn view(&self) -> Node<Self::MSG> {
        Component::view(self)
    }

    fn stylesheet() -> Vec<String> {
        <Self as Component>::stylesheet()
    }

    fn style(&self) -> Vec<String> {
        <Self as Component>::style(self)
    }
}

/// create a stateful component node
pub fn stateful_component<COMP, MSG, MSG2>(
    app: COMP,
    attrs: impl IntoIterator<Item = Attribute<MSG>>,
    children: impl IntoIterator<Item = Node<MSG>>,
) -> Node<MSG>
where
    COMP: Component<MSG = MSG2, XMSG = ()> + StatefulComponent + Application<MSG = MSG2> + 'static,
    MSG: 'static,
    MSG2: 'static,
{
    let type_id = TypeId::of::<COMP>();
    let attrs = attrs.into_iter().collect::<Vec<_>>();

    let app = Rc::new(RefCell::new(app));

    let mut program = Program::from_rc_app(Rc::clone(&app));
    let children: Vec<Node<MSG>> = children.into_iter().collect();
    let mount_event = on_component_mount(move |me| {
        program.mount(
            &me.target_node.as_node(),
            //MountProcedure::append_to_shadow(),
            MountProcedure::append(),
        );
        let stylesheet = <COMP as Component>::stylesheet().join("\n");
        log::info!("stylesheet: {}", stylesheet);
        program.inject_style_to_mount(&stylesheet);
        program.inject_style_to_mount(&program.app_context.dynamic_style());
        program.update_dom().expect("update dom");
    });
    Node::Leaf(Leaf::StatefulComponent(StatefulModel {
        comp: app,
        type_id,
        attrs: attrs.into_iter().chain([mount_event]).collect(),
        children: children.into_iter().collect(),
    }))
}

#[cfg(feature = "with-dom")]
impl From<wasm_bindgen::JsValue> for DomAttrValue {
    fn from(val: wasm_bindgen::JsValue) -> Self {
        if val.is_null() {
            DomAttrValue::Empty
        } else if let Some(v) = val.as_bool() {
            DomAttrValue::Simple(v.into())
        } else if let Some(v) = val.as_f64() {
            DomAttrValue::Simple(v.into())
        } else if let Some(v) = val.as_string() {
            DomAttrValue::Simple(v.into())
        } else {
            todo!("for: {:?}", val)
        }
    }
}