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
use crate::node::{Node, NodeContainer};
use crate::components::Alignment;
use std::borrow::BorrowMut;
use crate::{DefaultModifiers, sp, Renderable};
use crate::components::{Appendable, ChildContainer};


#[derive(Debug, Clone)]
pub struct Grid {
    children: Vec<Box<dyn Renderable>>,
    node: Node,
    pub alignment: Alignment,
}

impl NodeContainer for Grid {
    fn get_node(&mut self) -> &mut Node {
        self.node.borrow_mut()
    }
}

impl DefaultModifiers<Grid> for Grid {}

impl Grid {
    pub fn new(alignment: Alignment) -> Self {
        Grid {
            children: vec![],
            node: Default::default(),
            alignment,
        }
    }
    pub fn gap(&mut self, gaps: Vec<i32>) -> Self {
        let params: Vec<String> = gaps.iter().map(|size| sp(size.clone())).collect();
        self.node.node_style.push(("grid-gap".to_string(), params.join(" ")));
        self.clone()
    }
    pub fn columns(&mut self, schema: &str) -> Self {
        self.node.node_style.push(("grid-template-columns".to_string(), schema.to_string()));
        self.clone()
    }
    pub fn rows(&mut self, schema: &str) -> Self {
        self.node.node_style.push(("grid-template-rows".to_string(), schema.to_string()));
        self.clone()
    }
    pub fn auto_columns(&mut self, size: &str) -> Self {
        self.node.node_style.push(("grid-auto-columns".to_string(), size.to_string()));
        self.clone()
    }
    pub fn auto_rows(&mut self, size: &str) -> Self {
        self.node.node_style.push(("grid-auto-rows".to_string(), size.to_string()));
        self.clone()
    }
    pub fn auto_flow(&mut self, direction: &str) -> Self {
        self.node.node_style.push(("grid-auto-flow".to_string(), direction.to_string()));
        self.clone()
    }
}

impl ChildContainer for Grid {
    fn get_children(&mut self) -> &mut Vec<Box<dyn Renderable>> {
        return self.children.borrow_mut();
    }
}

impl Appendable for Grid {}

impl Renderable for Grid {
    fn render(&self) -> Node {
        let mut view = self
            .clone()
            .add_class("grid")
            .add_class(
                format!("grid--align-{:?}", self.alignment)
                    .to_lowercase()
                    .as_str()
            )
            .node;
        self.children.iter()
            .for_each(|child|
                view.children.push(child.render()));
        view
    }
}