1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22/**
23 * @var CView $this
24 */
25?>
26$(() => {
27	let VALUEMAP_MAPPING_TYPE_DEFAULT = <?= VALUEMAP_MAPPING_TYPE_DEFAULT ?>;
28	let type_placeholder = <?= json_encode([
29		VALUEMAP_MAPPING_TYPE_EQUAL => _('value'),
30		VALUEMAP_MAPPING_TYPE_GREATER_EQUAL => _('value'),
31		VALUEMAP_MAPPING_TYPE_LESS_EQUAL => _('value'),
32		VALUEMAP_MAPPING_TYPE_IN_RANGE => _('value'),
33		VALUEMAP_MAPPING_TYPE_REGEXP => _('regexp')
34	]) ?>;
35	let table = document.querySelector('#mappings_table');
36	let observer = new MutationObserver(mutationHandler);
37
38	// Observe changes for form fields: type, value.
39	observer.observe(table, {
40		childList: true,
41		subtree: true,
42		attributes: true,
43		attributeFilter: ['value']
44	});
45	updateOnTypeChange();
46
47	function updateOnTypeChange() {
48		let default_select = table.querySelector(`z-select[value="${VALUEMAP_MAPPING_TYPE_DEFAULT}"]`);
49
50		table.querySelectorAll('tr').forEach((row) => {
51			let zselect = row.querySelector('z-select[name$="[type]"]');
52			let input = row.querySelector('input[name$="[value]"]');
53
54			if (zselect) {
55				zselect.getOptionByValue(VALUEMAP_MAPPING_TYPE_DEFAULT).disabled = (default_select
56					&& zselect !== default_select
57				);
58				input.classList.toggle('visibility-hidden', (zselect === default_select));
59				input.disabled = (zselect === default_select);
60				input.setAttribute('placeholder', type_placeholder[zselect.value]||'');
61			}
62		});
63	}
64
65	function mutationHandler(mutation_records, observer) {
66		let update = mutation_records.filter((mutation) => {
67			return (mutation.target.tagName === 'INPUT' && mutation.target.getAttribute('name').substr(-6) === '[type]')
68				|| (mutation.target.tagName === 'TBODY' && mutation.removedNodes.length > 0);
69		});
70
71		if (update.length) {
72			updateOnTypeChange();
73		}
74	}
75});
76
77function submitValueMap(overlay) {
78	var $form = overlay.$dialogue.find('form'),
79		url = new Curl($form.attr('action'));
80
81	$form.trimValues(['input[type="text"]']);
82
83	fetch(url.getUrl(), {
84		method: 'POST',
85		body: new URLSearchParams(new FormData($form.get(0)))
86	})
87		.then(response => response.json())
88		.then(response => {
89			overlay.$dialogue.find('.msg-bad, .msg-good').remove();
90
91			if (response.errors) {
92				document
93					.querySelector(`.overlay-dialogue[data-dialogueid='${overlay.dialogueid}'] .overlay-dialogue-body`)
94					.prepend($(response.errors).get(0));
95				overlay.unsetLoading();
96
97				return;
98			}
99
100			new AddValueMap(response, response.edit ? overlay.element.closest('tr') : null);
101			overlayDialogueDestroy(overlay.dialogueid);
102		})
103		.catch((e) => {
104			document
105				.querySelector(`.overlay-dialogue[data-dialogueid='${overlay.dialogueid}'] .overlay-dialogue-body`)
106				.prepend(makeMessageBox('bad', e, null)[0]);
107			overlay.unsetLoading();
108		});
109
110	return;
111}
112