summaryrefslogtreecommitdiffstats
path: root/ogcp/static/js/ogcp.js
blob: ab06dfc2289d5cb60c17383305612429115627cc (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
const Endpoint = '/scopes/status';
const Interval = 1000;
let updateTimeoutId = null;

function updateScopeState() {
    if (updateTimeoutId) {
        clearTimeout(updateTimeoutId);
    }

    updateTimeoutId = setTimeout(() => {
        updateTimeoutId = null;
        fetch(Endpoint)
            .then(response => response.json())
            .then((data) => {
                updateScopes(data.scope);
            })
            .catch((error) => { console.error(error); })
            .finally(() => {
                updateScopeState();
            });
    }, Interval);
}

function updateScopes(scopes) {
    scopes.forEach((scope) => {
        if (scope.state) {
            const scopeId = `${scope.name}_${scope.id}`;
            const scopeEl = document.querySelector(`#${scopeId}`);
            const stateCls = ['state--on', 'state--off'];
            scopeEl.classList.remove(...stateCls);
            const stateClass = `state--${scope.state}`;
            scopeEl.classList.add(stateClass);

            const iconEl = document.querySelector(`#${scopeId} .nav-icon`);
            const iconCls = ['fas', 'far', 'text-danger', 'text-success'];
            iconEl.classList.remove(...iconCls);
            let newIconCls = [];
            if (scope.state === 'on') {
                newIconCls.push('fas', 'text-success');
            } else {
                newIconCls.push('far', 'text-danger');
            }
            iconEl.classList.add(...newIconCls);
        }
        if (scope.scope) {
            // This is a level so we should update all childs
            updateScopes(scope.scope);
        }
    });
}

function unfoldAll() {
    $('#scopes .collapse').collapse('show');
}