Passed
Push — develop ( 58a177...e3504d )
by Nikolay
04:46
created

sites/admin-cabinet/assets/js/src/Extensions/extensions-index-status-worker.js   A

Complexity

Total Complexity 11
Complexity/F 1.38

Size

Lines of Code 50
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 37
c 0
b 0
f 0
dl 0
loc 50
rs 10
mnd 3
bc 3
fnc 8
bpm 0.375
cpm 1.375
noi 0
1
/*
2
 * MikoPBX - free phone system for small business
3
 * Copyright (C) 2017-2022 Alexey Portnov and Nikolay Beketov
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along with this program.
16
 * If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
/* global PbxApi, DebuggerInfo */
20
21
const extensionsStatusLoopWorker = {
22
	timeOut: 3000,
23
	timeOutHandle: '',
24
	green: '<div class="ui green empty circular label" style="width: 1px;height: 1px;"></div>',
25
	grey: '<div class="ui grey empty circular label" style="width: 1px;height: 1px;"></div>',
26
	initialize() {
27
		// Запустим обновление статуса провайдера
28
		DebuggerInfo.initialize();
29
		extensionsStatusLoopWorker.restartWorker();
30
	},
31
	restartWorker() {
32
		window.clearTimeout(extensionsStatusLoopWorker.timeoutHandle);
33
		extensionsStatusLoopWorker.worker();
34
	},
35
	worker() {
36
		window.clearTimeout(extensionsStatusLoopWorker.timeoutHandle);
37
		PbxApi.GetPeersStatus(extensionsStatusLoopWorker.cbRefreshExtensionsStatus);
38
	},
39
	cbRefreshExtensionsStatus(response) {
40
		extensionsStatusLoopWorker.timeoutHandle =
41
			window.setTimeout(extensionsStatusLoopWorker.worker, extensionsStatusLoopWorker.timeOut);
42
		if (response.length === 0 || response === false) return;
43
		let htmlTable = '<table class="ui very compact table">';
44
		$.each(response, (key, value) => {
45
			htmlTable += '<tr>';
46
			htmlTable += `<td>${value.id}</td>`;
47
			htmlTable += `<td>${value.state}</td>`;
48
			htmlTable += '</tr>';
49
		});
50
		htmlTable += '</table>';
51
		DebuggerInfo.UpdateContent(htmlTable);
52
		$('.extension-row').each((index, obj) => {
53
			const number = $(obj).attr('data-value');
54
			const result = $.grep(response, e => e.id === number);
55
			if (result.length === 0) {
56
				// not found
57
				$(obj).find('.extension-status').html(extensionsStatusLoopWorker.grey);
58
			} else if (result[0].state.toUpperCase() === 'OK') {
59
				$(obj).find('.extension-status').html(extensionsStatusLoopWorker.green);
60
			} else {
61
				$(obj).find('.extension-status').html(extensionsStatusLoopWorker.grey);
62
			}
63
		});
64
	},
65
};
66
67
68
$(document).ready(() => {
69
	extensionsStatusLoopWorker.initialize();
70
});