Passed
Push — develop ( 719c22...1c19cc )
by Nikolay
12:41 queued 11s
created

sites/admin-cabinet/assets/js/src/Update/update-status-worker.js   A

Complexity

Total Complexity 8
Complexity/F 2

Size

Lines of Code 37
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 31
c 0
b 0
f 0
dl 0
loc 37
rs 10
mnd 4
bc 4
fnc 4
bpm 1
cpm 2
noi 0
1
/*
2
 * MikoPBX - free phone system for small business
3
 * Copyright (C) 2017-2021 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, globalTranslate, UserMessage, updatePBX */
20
21
const upgradeStatusLoopWorker = {
22
	timeOut: 1000,
23
	timeOutHandle: '',
24
	iterations: 0,
25
	filename: '',
26
	initialize(filename) {
27
		upgradeStatusLoopWorker.filename = filename;
28
		upgradeStatusLoopWorker.iterations = 0;
29
		upgradeStatusLoopWorker.restartWorker();
30
	},
31
	restartWorker() {
32
		window.clearTimeout(upgradeStatusLoopWorker.timeoutHandle);
33
		upgradeStatusLoopWorker.worker();
34
	},
35
	worker() {
36
		window.clearTimeout(upgradeStatusLoopWorker.timeoutHandle);
37
		PbxApi.FilesFirmwareDownloadStatus(upgradeStatusLoopWorker.filename, upgradeStatusLoopWorker.cbRefreshUpgradeStatus);
38
	},
39
	cbRefreshUpgradeStatus(response) {
40
		upgradeStatusLoopWorker.iterations += 1;
41
		upgradeStatusLoopWorker.timeoutHandle =
42
			window.setTimeout(upgradeStatusLoopWorker.worker, upgradeStatusLoopWorker.timeOut);
43
		if (response.length === 0 || response === false) return;
44
		if (response.d_status === 'DOWNLOAD_IN_PROGRESS') {
45
			$('i.loading.redo').closest('a').find('.percent').text(`${response.d_status_progress}%`);
46
		} else if (response.d_status === 'DOWNLOAD_COMPLETE') {
47
			window.clearTimeout(upgradeStatusLoopWorker.timeoutHandle);
48
			$('i.loading.redo').closest('a').find('.percent').text(`${response.d_status_progress}%`);
49
			$('i.loading.redo').addClass('sync').removeClass('redo');
50
			PbxApi.SystemUpgrade(response.filePath, updatePBX.cbAfterStartUpdate);
51
		} else if (response.d_status === 'DOWNLOAD_ERROR') {
52
			window.clearTimeout(upgradeStatusLoopWorker.timeoutHandle);
53
			UserMessage.showMultiString(globalTranslate.upd_DownloadUpgradeError);
54
			$('i.loading.redo').addClass('redo').removeClass('loading');
55
		}
56
	},
57
};