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
use crate::{node, StyleSheet};
use elvis_core::Node;
use std::{cell::RefCell, convert::Into, rc::Rc};
use wasm_bindgen::prelude::*;

/// basic widget without lifecycle nor state
#[wasm_bindgen]
#[derive(Clone, Debug, Default)]
pub struct Page {
    tree: Node,
    style: Rc<RefCell<StyleSheet>>,
}

impl<N> From<N> for Page
where
    N: Into<Node>,
{
    fn from(n: N) -> Page {
        let mut node: Node = n.into();
        node.idx(&mut vec![]);
        Page {
            tree: node,
            style: Rc::new(RefCell::new(StyleSheet::default())),
        }
    }
}

impl Page {
    /// Render into body element
    pub fn calling(&mut self) -> Result<(), JsValue> {
        let window = web_sys::window().unwrap();
        let dom = window.document().unwrap();

        // set style
        StyleSheet::shared()?;
        self.style()?;

        // set body
        let body = dom.query_selector("body")?.unwrap();
        body.set_inner_html("");
        body.append_child(
            &node::to_element(&Rc::new(RefCell::new(self.tree.clone())), &dom)?.into(),
        )?;

        // gesture::bind(&self.tree);
        Ok(())
    }

    /// Shoud update style
    fn style(&mut self) -> Result<bool, JsValue> {
        self.style.borrow_mut().batch(&mut self.tree);
        Ok(self.style.borrow().ser(self.tree.attr.id.to_string())?)
    }
}