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="macro-row-tmpl">
28	<?= (new CRow([
29			(new CCol(
30				(new CTextAreaFlexible('macros[#{rowNum}][macro]', '', ['add_post_js' => false]))
31					->addClass('macro')
32					->setWidth(ZBX_TEXTAREA_MACRO_WIDTH)
33					->setAttribute('placeholder', '{$MACRO}')
34			))->addClass(ZBX_STYLE_TEXTAREA_FLEXIBLE_PARENT),
35			(new CCol(
36				new CMacroValue(ZBX_MACRO_TYPE_TEXT, 'macros[#{rowNum}]', '', false)
37			))->addClass(ZBX_STYLE_TEXTAREA_FLEXIBLE_PARENT),
38			(new CCol(
39				(new CTextAreaFlexible('macros[#{rowNum}][description]', '', ['add_post_js' => false]))
40					->setWidth(ZBX_TEXTAREA_MACRO_VALUE_WIDTH)
41					->setMaxlength(DB::getFieldLength('globalmacro' , 'description'))
42					->setAttribute('placeholder', _('description'))
43			))->addClass(ZBX_STYLE_TEXTAREA_FLEXIBLE_PARENT),
44			(new CCol(
45				(new CButton('macros[#{rowNum}][remove]', _('Remove')))
46					->addClass(ZBX_STYLE_BTN_LINK)
47					->addClass('element-table-remove')
48			))->addClass(ZBX_STYLE_NOWRAP)
49		]))
50			->addClass('form_row')
51			->toString()
52	?>
53</script>
54
55<script type="text/javascript">
56	$(function() {
57		const table = $('#tbl_macros');
58		let removed = 0;
59
60		table
61			.on('click', 'button.element-table-remove', function() {
62				// check if the macro has an hidden ID element, if it does - increment the deleted macro counter
63				removed += $('#macros_' + $(this).attr('id').split('_')[1] + '_globalmacroid').length;
64			})
65			.dynamicRows({template: '#macro-row-tmpl'})
66			.on('afteradd.dynamicRows', function() {
67				$('.input-group', table).macroValue();
68				$('.<?= ZBX_STYLE_TEXTAREA_FLEXIBLE ?>', table).textareaFlexible();
69			})
70			.find('.input-group')
71			.macroValue();
72
73		table
74			.on('change keydown', '.<?= ZBX_STYLE_TEXTAREA_FLEXIBLE ?>.macro', function(event) {
75				if (event.type === 'change' || event.which === 13) {
76					$(this)
77						.val($(this).val().replace(/([^:]+)/, (value) => value.toUpperCase('$1')))
78						.textareaFlexible();
79				}
80			})
81			.find('.<?= ZBX_STYLE_TEXTAREA_FLEXIBLE ?>')
82			.textareaFlexible();
83
84		$('#update').click(function() {
85			if (removed) {
86				return confirm(<?= json_encode(_('Are you sure you want to delete')) ?> + ' ' + removed + ' '
87					+ <?= json_encode(_('macro(s)')) ?> + '?'
88				);
89			}
90		});
91	});
92</script>
93