summaryrefslogtreecommitdiffstats
path: root/web/gui/src/dashboard.js/boot.js
blob: c448213b30e86b0d5831697e02793d7af1807dc2 (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
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
// Load required JS libraries and CSS

NETDATA.requiredJs = [
    {
        url: NETDATA.serverStatic + 'lib/bootstrap-3.3.7.min.js',
        async: false,
        isAlreadyLoaded: function () {
            // check if bootstrap is loaded
            if (typeof $().emulateTransitionEnd === 'function') {
                return true;
            } else {
                return typeof netdataNoBootstrap !== 'undefined' && netdataNoBootstrap;
            }
        }
    },
    {
        url: NETDATA.serverStatic + 'lib/fontawesome-all-5.0.1.min.js',
        async: true,
        isAlreadyLoaded: function () {
            return typeof netdataNoFontAwesome !== 'undefined' && netdataNoFontAwesome;
        }
    },
    {
        url: NETDATA.serverStatic + 'lib/perfect-scrollbar-0.6.15.min.js',
        isAlreadyLoaded: function () {
            return false;
        }
    }
];

NETDATA.requiredCSS = [
    {
        url: NETDATA.themes.current.bootstrap_css,
        isAlreadyLoaded: function () {
            return typeof netdataNoBootstrap !== 'undefined' && netdataNoBootstrap;
        }
    },
    {
        url: NETDATA.themes.current.dashboard_css,
        isAlreadyLoaded: function () {
            return false;
        }
    }
];

NETDATA.loadedRequiredJs = 0;
NETDATA.loadRequiredJs = function (index, callback) {
    if (index >= NETDATA.requiredJs.length) {
        if (typeof callback === 'function') {
            return callback();
        }
        return;
    }

    if (NETDATA.requiredJs[index].isAlreadyLoaded()) {
        NETDATA.loadedRequiredJs++;
        NETDATA.loadRequiredJs(++index, callback);
        return;
    }

    if (NETDATA.options.debug.main_loop) {
        console.log('loading ' + NETDATA.requiredJs[index].url);
    }

    let async = true;
    if (typeof NETDATA.requiredJs[index].async !== 'undefined' && NETDATA.requiredJs[index].async === false) {
        async = false;
    }

    $.ajax({
        url: NETDATA.requiredJs[index].url,
        cache: true,
        dataType: "script",
        xhrFields: {withCredentials: true} // required for the cookie
    })
        .done(function () {
            if (NETDATA.options.debug.main_loop) {
                console.log('loaded ' + NETDATA.requiredJs[index].url);
            }
        })
        .fail(function () {
            alert('Cannot load required JS library: ' + NETDATA.requiredJs[index].url);
        })
        .always(function () {
            NETDATA.loadedRequiredJs++;

            // if (async === false)
            if (!async) {
                NETDATA.loadRequiredJs(++index, callback);
            }
        });

    // if (async === true)
    if (async) {
        NETDATA.loadRequiredJs(++index, callback);
    }
};

NETDATA.loadRequiredCSS = function (index) {
    if (index >= NETDATA.requiredCSS.length) {
        return;
    }

    if (NETDATA.requiredCSS[index].isAlreadyLoaded()) {
        NETDATA.loadRequiredCSS(++index);
        return;
    }

    if (NETDATA.options.debug.main_loop) {
        console.log('loading ' + NETDATA.requiredCSS[index].url);
    }

    NETDATA._loadCSS(NETDATA.requiredCSS[index].url);
    NETDATA.loadRequiredCSS(++index);
};

// Boot it!

if (typeof netdataPrepCallback === 'function') {
    netdataPrepCallback();
}

NETDATA.errorReset();
NETDATA.loadRequiredCSS(0);

NETDATA._loadjQuery(function () {
    NETDATA.loadRequiredJs(0, function () {
        if (typeof $().emulateTransitionEnd !== 'function') {
            // bootstrap is not available
            NETDATA.options.current.show_help = false;
        }

        if (typeof netdataDontStart === 'undefined' || !netdataDontStart) {
            if (NETDATA.options.debug.main_loop) {
                console.log('starting chart refresh thread');
            }

            NETDATA.start();
        }
    });
});