1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* Toggle visibility and access to interface select options.
*
* @param {object} interface_ids_by_types Interface ids grouped by interface type.
* @param {object} item_interface_types Item type to interface type map.
* @param {number|null} item_type Current item type.
*/
function organizeInterfaces(interface_ids_by_types, item_interface_types, item_type) {
const INTERFACE_TYPE_ANY = -1,
INTERFACE_TYPE_OPT = -2,
$interface_select = $('#interface-select'),
interface_select_node = $interface_select.get(0),
selected_interfaceid = +$('#selectedInterfaceId').val();
if (!interface_select_node) {
return;
}
if ($('#visible_interfaceid').data('multipleInterfaceTypes') && !$('#visible_type').is(':checked')) {
$('#interface_not_defined')
.html(t('To set a host interface select a single item type for all items'))
.show();
interface_select_node.disabled = true;
$interface_select.hide();
return;
}
const interface_type = item_interface_types[item_type],
available_interfaceids = interface_ids_by_types[interface_type],
select_options = interface_select_node.getOptions();
/**
* Hide interface select, show "requires host interfaces" message.
*/
function noRequiredInterfacesFound() {
interface_select_node.disabled = true;
$interface_select.hide();
$('#interface_not_defined')
.html(t('No interface found'))
.show();
}
// If no interface is required.
if (typeof interface_type === 'undefined') {
interface_select_node.disabled = true;
$interface_select.hide();
$('#interface_not_defined').
html(t('Item type does not use interface'))
.show();
}
// If any interface type allowed, enable all options.
else if (select_options.length && (interface_type == INTERFACE_TYPE_ANY || interface_type == INTERFACE_TYPE_OPT)) {
interface_select_node.disabled = false;
select_options.map(opt => opt.disabled = false);
$interface_select.show();
$('#interface_not_defined').hide();
}
// If none of required interfaces found.
else if (!available_interfaceids) {
noRequiredInterfacesFound();
}
// Enable required interfaces, disable other interfaces.
else {
interface_select_node.disabled = false;
select_options.map((option) => option.disabled = (
typeof available_interfaceids === 'undefined' || !available_interfaceids.includes(option.value)
));
$interface_select.show();
$('#interface_not_defined').hide();
}
if (selected_interfaceid) {
interface_select_node.value = selected_interfaceid;
}
const allowed_opt_interface = (interface_type == INTERFACE_TYPE_OPT);
$(interface_select_node.getOptionByValue(0)).attr('disabled', !allowed_opt_interface);
$interface_select.find('li[value="0"]')
.toggle(allowed_opt_interface)
.parents('li[optgroup]:first')
.toggle(allowed_opt_interface);
// If value current option is disabled, select first available interface.
const selected_option = interface_select_node.getOptionByValue(interface_select_node.value);
if (!selected_option || selected_option.disabled) {
for (let option of select_options) {
if (!option.disabled) {
interface_select_node.value = option.value;
break;
}
}
}
if (typeof interface_type !== 'undefined' && !select_options.some((option) => !option.disabled)) {
noRequiredInterfacesFound();
}
}
|