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<script type="text/x-jquery-tmpl" id="iconMapRowTPL">
28<?=
29	(new CRow([
30		(new CCol(
31			(new CDiv())->addClass(ZBX_STYLE_DRAG_ICON)
32		))->addClass(ZBX_STYLE_TD_DRAG_ICON),
33		(new CSpan('#0:'))->addClass('rowNum'),
34		(new CSelect('iconmap[mappings][#{iconmappingid}][inventory_link]'))
35			->addOptions(CSelect::createOptionsFromArray($data['inventory_list']))
36			->setId('iconmap_mappings_#{iconmappingid}_inventory_link'),
37		(new CTextBox('iconmap[mappings][#{iconmappingid}][expression]', '', false, 64))
38			->setId('iconmap_mappings_#{iconmappingid}_expression')
39			->setAriaRequired()
40			->setWidth(ZBX_TEXTAREA_SMALL_WIDTH),
41		(new CSelect('iconmap[mappings][#{iconmappingid}][iconid]'))
42			->addOptions(CSelect::createOptionsFromArray($data['icon_list']))
43			->setId('iconmap_mappings_#{iconmappingid}_iconid')
44			->addClass('js-mapping-icon'),
45		(new CCol(
46			(new CImg('imgstore.php?iconid='.$data['default_imageid'].'&width='.ZBX_ICON_PREVIEW_WIDTH.
47				'&height='.ZBX_ICON_PREVIEW_HEIGHT, _('Preview'))
48			)
49				->setAttribute('data-image-full', 'imgstore.php?iconid='.$data['default_imageid'])
50				->addClass(ZBX_STYLE_CURSOR_POINTER)
51				->addClass('preview')
52		))->addStyle('vertical-align: middle'),
53		(new CCol(
54			(new CButton('remove', _('Remove')))
55				->addClass(ZBX_STYLE_BTN_LINK)
56				->addClass('remove_mapping')
57				->removeId()
58		))->addClass(ZBX_STYLE_NOWRAP)
59	]))
60		->setId('iconmapidRow_#{iconmappingid}')
61		->addClass('sortable')
62?>
63</script>
64<script type="text/javascript">
65	jQuery(function($) {
66		var $form = $('form#iconmap');
67
68		$form.on('submit', function() {
69			$form.trimValues(['#iconmap_name']);
70		});
71
72		$form.find('#clone').click(function() {
73			var url = new Curl('zabbix.php?action=iconmap.edit');
74
75			$form.serializeArray().forEach(function(field) {
76				if (field.name !== 'iconmapid') {
77					url.setArgument(field.name, field.value);
78				}
79			});
80
81			redirect(url.getUrl(), 'post', 'action', undefined, false, true);
82		});
83
84		var iconMapTable = $('#iconMapTable'),
85			addMappingButton = $('#addMapping');
86
87		function recalculateSortOrder() {
88			var i = 1;
89
90			iconMapTable.find('tr.sortable .rowNum').each(function() {
91				$(this).text(i++ + ':');
92			});
93		}
94
95		iconMapTable.sortable({
96			disabled: (iconMapTable.find('tr.sortable').length < 2),
97			items: 'tbody tr.sortable',
98			axis: 'y',
99			containment: 'parent',
100			cursor: 'grabbing',
101			handle: 'div.<?= ZBX_STYLE_DRAG_ICON ?>',
102			tolerance: 'pointer',
103			opacity: 0.6,
104			update: recalculateSortOrder,
105			helper: function(e, ui) {
106				ui.children().each(function() {
107					var td = $(this);
108
109					td.width(td.width());
110				});
111
112				// when dragging element on safari, it jumps out of the table
113				if (SF) {
114					// move back draggable element to proper position
115					ui.css('left', (ui.offset().left - 4) + 'px');
116				}
117
118				return ui;
119			},
120			start: function(e, ui) {
121				$(ui.placeholder).height($(ui.helper).height());
122			}
123		});
124
125		iconMapTable.find('tbody')
126			.on('click', '.remove_mapping', function() {
127				$(this).parent().parent().remove();
128
129				if (iconMapTable.find('tr.sortable').length < 2) {
130					iconMapTable.sortable('disable');
131				}
132				recalculateSortOrder();
133			})
134			.on('change', 'z-select.js-mapping-icon, z-select#iconmap_default_iconid', function() {
135				$(this).closest('tr').find('.preview')
136					.attr('src', 'imgstore.php?&width=<?= ZBX_ICON_PREVIEW_WIDTH ?>&height=<?= ZBX_ICON_PREVIEW_HEIGHT ?>&iconid=' + $(this).val())
137					.data('imageFull', 'imgstore.php?iconid=' + $(this).val());
138			})
139			.on('click', 'img.preview', function(e) {
140				var img = $('<img>', {src: $(this).data('imageFull')});
141				hintBox.showStaticHint(e, this, '', true, '', img);
142			});
143
144		addMappingButton.click(function() {
145			var tpl = new Template($('#iconMapRowTPL').html()),
146				iconmappingid = getUniqueId(),
147				mapping = {};
148
149			// on error, whole page reloads and getUniqueId reset ids sequence which can cause in duplicate ids
150			while ($('#iconmapidRow_' + iconmappingid).length != 0) {
151				iconmappingid = getUniqueId();
152			}
153
154			mapping.iconmappingid = iconmappingid;
155			$('#iconMapListFooter').before(tpl.evaluate(mapping));
156
157			iconMapTable.sortable('refresh');
158
159			if (iconMapTable.find('tr.sortable').length > 1) {
160				iconMapTable.sortable('enable');
161			}
162
163			recalculateSortOrder();
164		});
165
166		if (iconMapTable.find('tr.sortable').length === 0) {
167			addMappingButton.click();
168		}
169	});
170</script>
171