summaryrefslogtreecommitdiffstats
path: root/ogcp/static/js/ogcp.js
blob: a52ff93bac1ec5fa664a8db95863ff4bbaa0cf69 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
const Endpoint = '/scopes/status';
const macs = new Map();
const Interval = 1000;
let updateTimeoutId = null;

async function show_client_mac(pill_id) {
    const pill = $('#' +pill_id);

    if (!pill.length) {
        return
    }

    const ip = pill.html().split('<br>')[1]

    if (!macs.get(ip)) {
        const resp = await fetch('/client/mac?ip=' + ip);
        const resp_mac = await resp.json();
        macs.set(ip, resp_mac)
    }

    const mac = macs.get(ip)
    pill.append('<br>' + mac);
}

function showSelectedClient(client_checkbox) {
    const container = $('#selected-clients');
    const pill_id = 'pill-' + client_checkbox.name.replaceAll(/[. ()]/g, '_');
    const client_name = client_checkbox.name.replaceAll(/_\d+$/g, '');

    if (client_name === 'scope-room' || client_name === 'scope-center' || client_name === 'folder' || client_name === 'scope-server') {
        return;
    }
    if (client_checkbox.checked) {
        if (!($('#' + pill_id).length)) {
            $(container).append('<div class="badge badge-pill og-pill badge-light" ' +
                                'id="'+ pill_id + '">' + client_name +
                                '<br>' + client_checkbox.value + '</div>');

            show_client_mac(pill_id);
        }
        return;
    }

    $('#' + pill_id, container).remove();
}

function showSelectedClientsOnEvents() {
    const checkboxes = $('input:checkbox[form|="scopesForm"]');
    const container = $('#selected-clients');

    checkboxes.on('change show-client', function () {
        showSelectedClient(this);
    });
}

function storeCheckboxStatus(checkbox) {
        if (checkbox.checked)
            localStorage.setItem(checkbox.name, "check");
        else
            localStorage.removeItem(checkbox.name);
}

function checkParentsCheckboxes() {
    const checkboxes = $('input:checkbox[form|="scopesForm"]');
    const reversedCheckboxes = $(checkboxes.get().reverse())

    reversedCheckboxes.each(function() {
        const checkbox = this;
        const checkboxChildren = $('input:checkbox', this.parentNode).not(this);

        if (checkboxChildren.length == 0) return;

        if (this.name == "scope-server") {
            const checkedChildren = checkboxChildren.filter(":checked");
            checkbox.checked = checkedChildren.length > 0;
            return;
        }

        const unCheckedChildren = checkboxChildren.filter(":not(:checked)");

        checkbox.indeterminate =
          unCheckedChildren.length > 0 &&
          unCheckedChildren.length < checkboxChildren.length;
        checkbox.checked = unCheckedChildren.length === 0;
    });
}

function checkChildrenCheckboxes() {
    const checkboxes = $('input:checkbox[form|="scopesForm"]')

    checkboxes.on('change', function () {
        const checked = this.checked
        const children = $('input:checkbox', this.parentNode).not(this)

        if (checked) {
            // Only for rooms, deselect other rooms
            if (this.name === 'scope-room') {
                const others = $('input:checkbox[form|="scopesForm"]').not(this);
                others.prop('checked', false);
                others.each(function() {
                    showSelectedClient(this);
                    storeCheckboxStatus(this);
                });
                //others.trigger('change');
            } else {
                // Look for room, deselect all other rooms
                const selectedRoom = $('[data-room="' + $(this).data('parentRoom') + '"]');
                const others = $('input:checkbox[name="scope-room"]').not(selectedRoom);
                others.prop('checked', false).prop('indeterminate', false);
                others.each(function() {
                    const checks = $(this).parent().find('input:checkbox').prop('checked', false);
                    storeCheckboxStatus(this);
                    checks.each(function() {
                        showSelectedClient(this);
                        storeCheckboxStatus(this);
                    });
                });
            }
        }

        children.each(function () {
            this.checked = checked;
            storeCheckboxStatus(this);
            $(this).trigger('show-client');
        });
        checkParentsCheckboxes();
    });
}

function keepSelectedClients() {
    const checkboxes = $('input:checkbox[form|="scopesForm"]')

    checkboxes.on('change', function (event) {
            storeCheckboxStatus(this);
    });

    checkboxes.each(function () {
        if (localStorage.getItem(this.name) == 'check') {
            this.checked = true;
            $(this).trigger('show-client');
        }
    });
}

function keepImagesTreeState() {
    const images_tree = $('#servers .collapse')
    images_tree.on('hidden.bs.collapse', function (event) {
        event.stopPropagation();
        localStorage.removeItem(this.id);
    });
    images_tree.on('shown.bs.collapse', function (event) {
        event.stopPropagation();
        localStorage.setItem(this.id, 'show');
    });

    images_tree.each(function () {
        if (localStorage.getItem(this.id) == 'show') {
            $(this).collapse('show');
        }
    });
}

function keepReposTreeState() {
    const repos_tree = $('#repos-list .collapse')

    repos_tree.on('hidden.bs.collapse', function (event) {
        event.stopPropagation();
        localStorage.removeItem(this.id);
    });
    repos_tree.on('shown.bs.collapse', function (event) {
        event.stopPropagation();
        localStorage.setItem(this.id, 'show');
    });

    repos_tree.each(function () {
        if (localStorage.getItem(this.id) == 'show') {
            $(this).collapse('show');
        }
    });
}

function keepScopesTreeState() {
    const scopes_tree = $('#scopes .collapse')

    scopes_tree.on('hidden.bs.collapse', function (event) {
        event.stopPropagation();
        localStorage.removeItem(this.id);
    });
    scopes_tree.on('shown.bs.collapse', function (event) {
        event.stopPropagation();
        localStorage.setItem(this.id, 'show');
    });

    scopes_tree.each(function () {
        if (localStorage.getItem(this.id) == 'show') {
            $(this).collapse('show');
        }
    });
}

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 updatePillStatus(scope, pill) {
    const state = scope.state
    let link = scope.link
    let units = 'Mb/s'
    const pillCls = ['badge-danger', 'badge-success', 'badge-warning',
                     'badge-wol', 'badge-light', 'text-linux', 'text-windows'];
    pill.classList.remove(...pillCls);
    if (state === 'OPG') {
        pill.classList.add('badge-warning');
    } else if (state === 'LNX') {
        pill.classList.add('badge-linux');
    } else if (state === 'LNXS') {
        pill.classList.add('badge-linux');
    } else if (state === 'WIN') {
        pill.classList.add('badge-windows');
    } else if (state === 'WINS') {
        pill.classList.add('badge-windows');
    } else if (state === 'BSY') {
        pill.classList.add('badge-danger');
    } else if (state === 'VDI') {
        pill.classList.add('badge-success');
    } else if (state === 'WOL_SENT') {
        pill.classList.add('badge-wol');
    } else {
        pill.classList.add('badge-light');
    }

    $('[name="link"]', pill).remove()
    if (link) {
        if (link >= 1000) {
            link = link / 1000
            units = 'Gb/s'
        }
        $(pill).append('<b name="link"><br>' + link + ' ' + units + '</b>');
    }
}

function updateScopes(scopes) {
    scopes.forEach((scope) => {
        if (scope.state) {
            const scopeId = `${scope.name}_${scope.id}`.replaceAll(/[.]|[ ]/g, '_');
            const iconEl = document.querySelector(`#${scopeId} .nav-icon`);
            const iconCls = ['fas', 'far', 'fa-circle', 'fa-check-circle',
                             'fa-times-circle', 'fa-user-circle', 'text-danger',
                             'text-success', 'text-warning', 'text-wol',
                             'text-linux', 'text-windows'];
            iconEl.classList.remove(...iconCls);
            let newIconCls = [];
            if (scope.state === 'OPG') {
                newIconCls.push('fas', 'text-warning');
                if (scope.last_cmd.result === 'failure')
                    newIconCls.push('fa-times-circle');
                else
                    newIconCls.push('fa-circle');
            } else if (scope.state === 'LNX') {
                newIconCls.push('fas', 'fa-circle', 'text-linux');
            } else if (scope.state === 'LNXS') {
                newIconCls.push('fas', 'fa-user-circle', 'text-linux');
            } else if (scope.state === 'WIN') {
                newIconCls.push('fas', 'fa-circle', 'text-windows');
            } else if (scope.state === 'WINS') {
                newIconCls.push('fas', 'fa-user-circle', 'text-windows');
            } else if (scope.state === 'BSY') {
                newIconCls.push('fas', 'fa-circle', 'text-danger');
            } else if (scope.state === 'VDI') {
                newIconCls.push('fas', 'fa-circle', 'text-success');
            } else if (scope.state === 'WOL_SENT') {
                newIconCls.push('fas', 'fa-circle', 'text-wol');
            } else {
                newIconCls.push('far', 'fa-circle');
            }
            iconEl.classList.add(...newIconCls);

            const pillScopeId = `pill-${scopeId}`;
            const pillEl = document.querySelector(`#${pillScopeId}`);
            if (pillEl)
                updatePillStatus(scope, pillEl);
        }
        if (scope.scope) {
            // This is a level so we should update all childs
            updateScopes(scope.scope);
        }
    });
}

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

function AddPartition(evt) {
    const target = $($(evt).data("target"));
    const oldrow = target.find("[data-toggle=fieldset-entry]:last");
    const row = oldrow.clone(true, true);
    const elem_id = row.find(".form-control")[0].id;
    const elem_num = parseInt(elem_id.replace(/(.*)-(\d{1,4})/m, '$2')) + 1;
    // Increment WTForms unique identifiers
    row.find('.form-control').each(function() {
        const id = $(this).attr('id').replace(/(.*)-(\d{1,4})-(.*)/, `$1-${elem_num}-$3`);
        $(this).attr('name', id).attr('id', id).val('').removeAttr("checked");
    });
    let part_field = row.find('td').filter(':first')[0];
    part_field.innerText = elem_num + 1;
    row.show();
    oldrow.after(row);
}

function RemovePartition(evt) {
    const target = $(evt).parent().parent();
    const table = target.parent();

    if (table[0].children.length > 1) {
        target.remove();
        // Reassign rows ids
        table.find('tr').each(function(index) {
            function update_references() {
                const id = $(this).attr('id').replace(/(.*)-(\d{1,4})-(.*)/, `$1-${index}-$3`);
                $(this).attr('name', id).attr('id', id);
            }

            let part_field = $(this).find('td').filter(':first')[0];
            part_field.innerText = index + 1;
            $(this).find('input').filter(':first').each(update_references);
            $(this).find('.form-control').each(update_references);
        });
    } else {
        table.find('tr').each(function(index) {
            $(this).find('.form-control').each(function() {
                $(this).val('').removeAttr("checked");
            });
        });
    }
}

function checkImageServer() {
    const images = $('input:checkbox[form|="imagesForm"][name!="image-server"]')

    images.on('change', function() {
        const selectedServer = $('#' + $.escapeSelector(this.dataset.server));
        const serversSelector = 'input:checkbox[name|="image-server"]';
        const nonSelectedServers = $(serversSelector).not(selectedServer);

        selectedServer.prop('checked', true);

        nonSelectedServers.each(function() {
            $(this).prop('checked', false);
            const checkboxes = $('input:checkbox[data-server|="' + this.id + '"]');
            checkboxes.prop('checked', false);
        });
    });
}

function checkRepoServer() {
    const repos = $('input:checkbox[form|="reposForm"][name!="repos-server"]')
    repos.on('change', function() {
        const selectedServer = $('#' + $.escapeSelector(this.dataset.server));
        const serversSelector = 'input:checkbox[name|="repos-server"]';
        const nonSelectedServers = $(serversSelector).not(selectedServer);

        selectedServer.prop('checked', true);

        nonSelectedServers.each(function() {
            $(this).prop('checked', false);
            const checkboxes = $('input:checkbox[data-server|="' + this.id + '"]');
            checkboxes.prop('checked', false);
        });
    });
}

function checkFolderParent() {
    const folder = $('input:checkbox[form|="scopesForm"][name="folder"]')
    folder.on('change', function() {
        const folder_parent = $('#' + $.escapeSelector(this.dataset.parentInput));
        folder_parent.prop('checked', true);
    });
}

function limitCheckboxes() {
    const checkboxes = $('input:checkbox[form|="scopesForm"]');

    checkboxes.on('change', function () {
        const checked = this;
        checkboxes.filter((i, c) => c !== checked).prop('checked', false);
        checkboxes.each(function() {
            showSelectedClient(this);
        });
        checkScopeServer();
    });
}

function checkScopeServer() {
    const servers = $('input:checkbox[form|="scopesForm"][name="scope-server"]');
    servers.each(function() {
        const checkbox = this;
        const checkboxChildren = $('input:checkbox', this.parentNode).not(this);
        if (checkboxChildren.length == 0) return;

        const checkedChildren = checkboxChildren.filter(":checked");
        checkbox.checked = checkedChildren.length > 0;
    });
}