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
use crate::CONFIG;
use crate::theme::{Config, ConfigLoader};

fn get_stylesheets(config: &Config) -> Vec<String> {
    let mut styles = vec![
        format!("
@use \"sass:color\";
@use \"sass:math\";
$accent-light: {accent_light};
$accent-dark: {accent_dark};
$on-accent: {on_accent};
$destructive: {destructive};
$on-destructive: {on_destructive};
$border-radius: {border_radius};
$background-light: {background_light};
$background-dark: {background_dark};
$surface-light: {surface_light};
$surface-dark: {surface_dark};
            ",
                accent_light = config.colors.accent.light,
                accent_dark = config.colors.accent.dark,
                destructive = config.colors.destructive.light,
                on_destructive = config.colors.on_destructive.light,
                on_accent = config.colors.on_accent.light,
                border_radius = config.shapes.border_radius,
                background_light = config.colors.background.light,
                background_dark = config.colors.background.dark,
                surface_light = config.colors.surface.light,
                surface_dark = config.colors.surface.dark
        ),
        include_str!("../themes/luminance.scss").to_string(),
        include_str!("../themes/palette.scss").to_string(),
        include_str!("../themes/sizing.scss").to_string(),
        include_str!("../themes/typography.scss").to_string(),
        include_str!("../themes/commons.scss").to_string(),
        include_str!("../themes/view.scss").to_string(),
        include_str!("../themes/components/button.scss").to_string(),
        include_str!("../themes/components/card.scss").to_string(),
        include_str!("../themes/components/divider.scss").to_string(),
        include_str!("../themes/components/form.scss").to_string(),
        include_str!("../themes/components/grid.scss").to_string(),
        include_str!("../themes/components/image.scss").to_string(),
        include_str!("../themes/components/picker.scss").to_string(),
        include_str!("../themes/components/popover.scss").to_string(),
        include_str!("../themes/components/stack.scss").to_string(),
        include_str!("../themes/components/text.scss").to_string(),
        include_str!("../themes/components/textfield.scss").to_string(),
        include_str!("../themes/components/titlebar.scss").to_string(),
        include_str!("../themes/components/menu.scss").to_string(),
        include_str!("../themes/components/table.scss").to_string(),
        include_str!("../themes/components/checkbox.scss").to_string(),
        include_str!("../themes/components/avatar.scss").to_string(),
        include_str!("../themes/components/tag.scss").to_string(),
        include_str!("../themes/components/popup.scss").to_string(),
        include_str!("../themes/components/file-input.scss").to_string(),
        include_str!("../themes/components/color-picker.scss").to_string(),
        include_str!("../themes/components/signature-field.scss").to_string(),
        include_str!("../themes/components/snackbar.scss").to_string(),
        include_str!("../themes/components/tabs.scss").to_string(),
        include_str!("../themes/components/badge.scss").to_string(),
        include_str!("../themes/print.scss").to_string(),
    ];
    if config.features.rich_text_editor {
        styles.push(include_str!("../themes/quill.scss").to_string());
    }
    if config.features.sortable_stack {
        styles.push(include_str!("../themes/components/sortable-stack.scss").to_string());
    }

    styles
}

fn get_scripts(config: &Config) -> Vec<String> {

    let mut scripts = vec![
        include_str!("../js/index.js").to_string(),
        include_str!("../js/popper.js").to_string(),
        include_str!("../js/form.js").to_string(),
        include_str!("../js/menu.js").to_string(),
        include_str!("../js/popover.js").to_string(),
        include_str!("../js/table.js").to_string(),
        include_str!("../js/popup.js").to_string(),
        include_str!("../js/file-input.js").to_string(),
        include_str!("../js/searchable-input.js").to_string(),
        include_str!("../js/signature-field.js").to_string(),
        include_str!("../js/tabs.js").to_string(),
        include_str!("../js/snackbar.js").to_string(),
        include_str!("../js/dynamic_content.js").to_string(),
        include_str!("../js/picker.js").to_string(),
    ];
    if config.features.rich_text_editor {
        scripts.push(include_str!("../js/quill-editor.js").to_string());
    }
    if config.features.sortable_stack {
        scripts.push(include_str!("../js/Sortable.min.js").to_string());
        scripts.push(include_str!("../js/sortable-stack.js").to_string());
    }
    scripts
}


/// At startup it compile theme and minify js sources.
/// You have to use it to serve these assets to the client.
///
/// **How to use**
///
/// You have to add these routes to your project
/// ```rust
/// use viewy::engine::Assets;
/// #[get("/app.css")]
/// fn get_stylesheet(assets: rocket::State<Assets>) -> Css<String> {
///     Css(assets.inner().clone().stylesheet)
/// }
///
/// #[get("/app.js")]
/// fn get_scripts(assets: rocket::State<Assets>) -> JavaScript<String> {
///     JavaScript(assets.inner().clone().script)
/// }
/// ```
/// And add an `Assets` instance to rocket state
/// ```rust
/// rocket::ignite()
///     .manage(rocket::Assets::new())
/// ```
#[derive(Clone)]
pub struct Assets {
    pub script: String,
    pub stylesheet: String,
}

impl Assets {
    pub fn new() -> Self {
        print!("Load config");
        let config = &*CONFIG;
        let theme = Self {
            script: Assets::compile_scripts(config),
            stylesheet: Assets::compile_theme(config),
        };

        println!(" [Done]");
        theme
    }
    fn compile_theme(config: &Config) -> String {
        println!("Compile theme ");
        let stylesheets = get_stylesheets(&config).join("");
        match grass::from_string(
            stylesheets,
            &grass::Options::default(),
        ) {
            Ok(css) => {
                println!(" [Done]");
                minifier::css::minify(css.as_str()).unwrap().to_string()
            }
            Err(err) => {
                println!(" [Error]");
                println!("{:?}", err);
                get_stylesheets(config).join("")
            }
        }
    }
    fn compile_scripts(config: &Config) -> String {
        let joined_scripts: String = get_scripts(config).join("");
        minifier::js::minify(joined_scripts.as_str()).to_string();
        joined_scripts
    }
}