aboutsummaryrefslogtreecommitdiff
path: root/htdocs/js/common/30-static-manager.js
blob: 8899d2bcfc7d62d7e535ccec7ed1e968d7b81263 (plain)
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
var StaticManager = {
    /**
     * @type {string[]}
     */
    loadedStyles: [],

    /**
     * @type {object}
     */
    versions: {},

    /**
     * @param {string[]} loadedStyles
     * @param {object} versions
     */
    init: function(loadedStyles, versions) {
        this.loadedStyles = loadedStyles;
        this.versions = versions;
    },

    /**
     * @param {string} name
     * @param {string} theme
     * @param {function} callback
     */
    loadStyle: function(name, theme, callback) {
        var url, id;
        if (!window.appConfig.devMode) {
            if (theme === 'dark')
                name += '_dark';
            url = '/css/'+name+'.css?'+this.versions.css[name];
            id = 'style_'+name;
        } else {
            url = '/sass.php?name='+name+'&theme='+theme+'&v='+timestamp();
            id = 'style_'+name+(theme === 'dark' ? '_dark' : '');
        }

        var el = document.createElement('link');
        el.onerror = callback;
        el.onload = callback;
        el.setAttribute('rel', 'stylesheet');
        el.setAttribute('type', 'text/css');
        el.setAttribute('id', id);
        el.setAttribute('href', url);

        document.getElementsByTagName('head')[0].appendChild(el);
    }
};