Passed
Push — develop ( 439163...06f922 )
by Nikolay
13:40
created

sites/admin-cabinet/assets/js/src/SoundFiles/sound-files-selector.js   A

Complexity

Total Complexity 16
Complexity/F 1.45

Size

Lines of Code 99
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 51
c 0
b 0
f 0
dl 0
loc 99
rs 10
mnd 5
bc 5
fnc 11
bpm 0.4545
cpm 1.4544
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 globalRootUrl, sessionStorage */
20
21
const SoundFilesSelector = {
22
23
	initialize() {
24
		window.addEventListener('ConfigDataChanged', SoundFilesSelector.cbOnDataChanged);
25
	},
26
	/**
27
	 * We will drop all caches if data changes
28
	 */
29
	cbOnDataChanged() {
30
		sessionStorage.removeItem(`${globalRootUrl}sound-files/getSoundFiles/custom`);
31
	},
32
	/**
33
	 * Makes dropdown menu for soundFiles with empty field
34
	 * @param cbOnChange - on change callback function
35
	 * @returns  dropdown settings
36
	 */
37
	getDropdownSettingsWithEmpty(cbOnChange = null) {
38
		return {
39
			apiSettings: {
40
				url: `${globalRootUrl}sound-files/getSoundFiles/custom`,
41
				// cache: false,
42
				// throttle: 400,
43
				onResponse(response) {
44
					return SoundFilesSelector.formatDropdownResults(response, true);
45
				},
46
			},
47
			onChange(value) {
48
				if (parseInt(value, 10) === -1) $(this).dropdown('clear');
49
				if (cbOnChange !== null) cbOnChange(value);
50
			},
51
			ignoreCase: true,
52
			fullTextSearch: true,
53
			filterRemoteData: true,
54
			saveRemoteData: true,
55
			forceSelection: false,
56
			// direction: 'downward',
57
			hideDividers: 'empty',
58
59
		};
60
	},
61
	/**
62
	 * Makes dropdown menu for soundFiles without empty field
63
	 * @param cbOnChange - on change callback function
64
	 * @returns  dropdown settings
65
	 */
66
	getDropdownSettingsWithoutEmpty(cbOnChange = null) {
67
		return {
68
			apiSettings: {
69
				url: `${globalRootUrl}sound-files/getSoundFiles/custom`,
70
				// cache: false,
71
				// throttle: 400,
72
				onResponse(response) {
73
					return SoundFilesSelector.formatDropdownResults(response, false);
74
				},
75
			},
76
			ignoreCase: true,
77
			fullTextSearch: true,
78
			filterRemoteData: true,
79
			saveRemoteData: true,
80
			forceSelection: false,
81
			// direction: 'downward',
82
			hideDividers: 'empty',
83
			onChange(value) {
84
				if (cbOnChange !== null) cbOnChange(value);
85
			},
86
		};
87
	},
88
	/**
89
	 * Makes formatted menu structure
90
	 */
91
	formatDropdownResults(response, addEmpty) {
92
		const formattedResponse = {
93
			success: false,
94
			results: [],
95
		};
96
		if (addEmpty) {
97
			formattedResponse.results.push({
98
				name: '-',
99
				value: -1
100
			});
101
		}
102
103
		if (response) {
104
			formattedResponse.success = true;
105
			$.each(response.results, (index, item) => {
106
				formattedResponse.results.push({
107
					name: item.name,
108
					value: item.value
109
				});
110
			});
111
		}
112
		return formattedResponse;
113
	},
114
}
115
116
117
$(document).ready(() => {
118
	SoundFilesSelector.initialize();
119
});
120