Passed
Push — develop ( 42d31a...412c5e )
by Nikolay
05:02
created

PbxExtensionStatus.initialize   A

Complexity

Conditions 2

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 16
rs 9.75
cc 2
1
/*
2
 * Copyright (C) MIKO LLC - All Rights Reserved
3
 * Unauthorized copying of this file, via any medium is strictly prohibited
4
 * Proprietary and confidential
5
 * Written by Nikolay Beketov, 12 2019
6
 *
7
 */
8
9
/* global PbxApi, globalTranslate, UserMessage */
10
class PbxExtensionStatus {
11
	initialize(uniqid, changeLabel = true) {
12
		this.$toggle = $(`.ui.toggle.checkbox[data-value="${uniqid}"]`);
13
		if (changeLabel) {
14
			this.$label = $(`.ui.toggle.checkbox[data-value="${uniqid}"]`).find('label');
15
		} else {
16
			this.$label = false;
17
		}
18
		this.uniqid = uniqid;
19
		this.$disabilityFields = $(`tr#${uniqid} .disability`);
20
		const cbOnChecked = $.proxy(this.cbOnChecked, this);
21
		const cbOnUnchecked = $.proxy(this.cbOnUnchecked, this);
22
		this.$toggle.checkbox({
23
			onChecked: cbOnChecked,
24
			onUnchecked: cbOnUnchecked,
25
		});
26
	}
27
	changeLabelText(newText) {
28
		if (this.$label) {
29
			this.$label.text(newText);
30
		}
31
	}
32
	cbOnChecked() {
33
		this.$toggle.addClass('disabled');
34
		this.changeLabelText(globalTranslate.ext_ModuleStatusChanging);
35
		const cbAfterModuleEnable = $.proxy(this.cbAfterModuleEnable, this);
36
		PbxApi.SystemEnableModule(this.uniqid, cbAfterModuleEnable);
37
	}
38
	cbOnUnchecked() {
39
		this.$toggle.addClass('disabled');
40
		this.changeLabelText(globalTranslate.ext_ModuleStatusChanging);
41
		const cbAfterModuleDisable = $.proxy(this.cbAfterModuleDisable, this);
42
		PbxApi.SystemDisableModule(this.uniqid, cbAfterModuleDisable);
43
	}
44
	cbAfterModuleDisable(response, success) {
45
		if (success) {
46
			this.$toggle.checkbox('set unchecked');
47
			PbxApi.SystemReloadModule(this.uniqid);
48
			this.changeLabelText(globalTranslate.ext_ModuleDisabledStatusDisabled);
49
			const event = document.createEvent('Event');
50
			event.initEvent('ModuleStatusChanged', false, true);
51
			window.dispatchEvent(event);
52
			event.initEvent('ConfigDataChanged', false, true);
53
			window.dispatchEvent(event);
54
			this.$disabilityFields.addClass('disabled');
55
		} else {
56
			this.$toggle.checkbox('set checked');
57
			this.changeLabelText(globalTranslate.ext_ModuleDisabledStatusEnabled);
58
			this.$disabilityFields.removeClass('disabled');
59
		}
60
		if (response !== undefined && response.messages !== undefined) {
61
			UserMessage.showMultiString(response.message, globalTranslate.ext_ModuleChangeStatusError);
62
		}
63
		this.$toggle.removeClass('disabled');
64
	}
65
	cbAfterModuleEnable(response, success) {
66
		if (success) {
67
			this.$toggle.checkbox('set checked');
68
			PbxApi.SystemReloadModule(this.uniqid);
69
			this.changeLabelText(globalTranslate.ext_ModuleDisabledStatusEnabled);
70
			const event = document.createEvent('Event');
71
			event.initEvent('ModuleStatusChanged', false, true);
72
			window.dispatchEvent(event);
73
			event.initEvent('ConfigDataChanged', false, true);
74
			window.dispatchEvent(event);
75
			this.$disabilityFields.removeClass('disabled');
76
		} else {
77
			this.$toggle.checkbox('set unchecked');
78
			this.changeLabelText(globalTranslate.ext_ModuleDisabledStatusDisabled);
79
			this.$disabilityFields.addClass('disabled');
80
		}
81
		if (response !== undefined && response.messages !== undefined) {
82
			UserMessage.showMultiString(response.messages, globalTranslate.ext_ModuleChangeStatusError);
83
		}
84
		this.$toggle.removeClass('disabled');
85
	}
86
}
87
88
$(document).ready(() => {
89
	const uniqId = $('#module-status-toggle').attr('data-value');
90
	if (uniqId) {
91
		const pageStatus = new PbxExtensionStatus();
92
		pageStatus.initialize(uniqId, true);
93
	}
94
});
95