Passed
Push — develop ( 73b673...d6f831 )
by Nikolay
05:32
created

PbxExtensionStatus.cbAfterModuleDisable   A

Complexity

Conditions 4

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 34
rs 9.328
c 0
b 0
f 0
cc 4
1
/*
2
 * MikoPBX - free phone system for small business
3
 * Copyright (C) 2017-2020 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 */
20
class PbxExtensionStatus {
21
	initialize(uniqid, changeLabel = true) {
22
		this.$toggle = $(`.ui.toggle.checkbox[data-value="${uniqid}"]`);
23
		this.$allToggles = $(`.ui.toggle.checkbox`);
24
		this.$statusIcon = $(`tr#${uniqid} i.status-icon`);
25
		if (changeLabel) {
26
			this.$label = $(`.ui.toggle.checkbox[data-value="${uniqid}"]`).find('label');
27
		} else {
28
			this.$label = false;
29
		}
30
		this.uniqid = uniqid;
31
		this.$disabilityFields = $(`tr#${uniqid} .disability`);
32
		const cbOnChecked = $.proxy(this.cbOnChecked, this);
33
		const cbOnUnchecked = $.proxy(this.cbOnUnchecked, this);
34
		this.$toggle.checkbox({
35
			onChecked: cbOnChecked,
36
			onUnchecked: cbOnUnchecked,
37
		});
38
	}
39
	changeLabelText(newText) {
40
		if (this.$label) {
41
			this.$label.text(newText);
42
		}
43
	}
44
	cbOnChecked() {
45
		this.$statusIcon.addClass('spinner loading icon');
46
		this.$allToggles.addClass('disabled');
47
		$('a.button').addClass('disabled');
48
		this.changeLabelText(globalTranslate.ext_ModuleStatusChanging);
49
		const cbAfterModuleEnable = $.proxy(this.cbAfterModuleEnable, this);
50
		PbxApi.SystemEnableModule(this.uniqid, cbAfterModuleEnable);
51
	}
52
	cbOnUnchecked() {
53
		this.$statusIcon.addClass('spinner loading icon');
54
		this.$allToggles.addClass('disabled');
55
		$('a.button').addClass('disabled');
56
		this.changeLabelText(globalTranslate.ext_ModuleStatusChanging);
57
		const cbAfterModuleDisable = $.proxy(this.cbAfterModuleDisable, this);
58
		PbxApi.SystemDisableModule(this.uniqid, cbAfterModuleDisable);
59
	}
60
	// Callback function after disabling the module
61
	cbAfterModuleDisable(response, success) {
62
		if (success) {
63
			// Update UI to show module is disabled
64
			this.$toggle.checkbox('set unchecked');
65
			this.$statusIcon.removeClass('spinner loading icon');
66
			this.changeLabelText(globalTranslate.ext_ModuleDisabledStatusDisabled);
67
68
			// Trigger events to indicate module status and config data has changed
69
			const event = document.createEvent('Event');
70
			event.initEvent('ModuleStatusChanged', false, true);
71
			window.dispatchEvent(event);
72
			event.initEvent('ConfigDataChanged', false, true);
73
			window.dispatchEvent(event);
74
75
			// Disable input fields and show message for changed objects
76
			this.$disabilityFields.addClass('disabled');
77
			if (response.data.changedObjects !== undefined){
78
				UserMessage.showMultiString(response.data.changedObjects, globalTranslate.ext_ModuleChangedObjects);
79
			}
80
81
			// Refresh the page to reflect changes
82
			window.location.reload();
83
		} else {
84
			this.$toggle.checkbox('set checked');
85
			this.changeLabelText(globalTranslate.ext_ModuleDisabledStatusEnabled);
86
			this.$disabilityFields.removeClass('disabled');
87
			if (response !== undefined && response.messages !== undefined) {
88
				UserMessage.showMultiString(response.messages, globalTranslate.ext_ModuleChangeStatusError);
89
			}
90
		}
91
		this.$allToggles.removeClass('disabled');
92
		$('a.button').removeClass('disabled');
93
		this.$statusIcon.removeClass('spinner loading icon');
94
	}
95
96
	// Callback function after enabling the module
97
	cbAfterModuleEnable(response, success) {
98
		if (success) {
99
			// Update UI to show module is enabled
100
			this.$toggle.checkbox('set checked');
101
			this.changeLabelText(globalTranslate.ext_ModuleDisabledStatusEnabled);
102
103
			// Trigger events to indicate module status and config data has changed
104
			const event = document.createEvent('Event');
105
			event.initEvent('ModuleStatusChanged', false, true);
106
			window.dispatchEvent(event);
107
			event.initEvent('ConfigDataChanged', false, true);
108
			window.dispatchEvent(event);
109
110
			// Enable input fields and show message for changed objects
111
			this.$disabilityFields.removeClass('disabled');
112
			if (response.data.changedObjects !== undefined){
113
				UserMessage.showMultiString(response.data.changedObjects, globalTranslate.ext_ModuleChangedObjects);
114
			}
115
116
			// Refresh the page to reflect changes
117
			window.location.reload();
118
		} else {
119
			this.$toggle.checkbox('set unchecked');
120
			this.changeLabelText(globalTranslate.ext_ModuleDisabledStatusDisabled);
121
			this.$disabilityFields.addClass('disabled');
122
			if (response !== undefined && response.messages !== undefined) {
123
				UserMessage.showMultiString(response.messages, globalTranslate.ext_ModuleChangeStatusError);
124
			}
125
		}
126
		this.$allToggles.removeClass('disabled');
127
		this.$statusIcon.removeClass('spinner loading icon');
128
		$('a.button').removeClass('disabled');
129
	}
130
}
131
132
$(document).ready(() => {
133
	const uniqId = $('#module-status-toggle').attr('data-value');
134
	if (uniqId) {
135
		const pageStatus = new PbxExtensionStatus();
136
		pageStatus.initialize(uniqId, true);
137
	}
138
});
139