Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CWidgetFieldTagsView.php « widgets « html « classes « include « ui - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a7d08e29fdbf746c4e9322a31017929cafbd34d6 (plain)
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
<?php declare(strict_types = 0);
/*
** 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.
**/


use Zabbix\Widgets\Fields\CWidgetFieldTags;

class CWidgetFieldTagsView extends CWidgetFieldView {

	public function __construct(CWidgetFieldTags $field) {
		$this->field = $field;
	}

	public function getView(): CTable {
		$tags = $this->field->getValue();

		if (!$tags) {
			$tags = [CWidgetFieldTags::DEFAULT_TAG];
		}

		$tags_table = (new CTable())
			->setId('tags_table_'.$this->field->getName())
			->addClass('table-tags')
			->addClass('table-initial-width');

		$i = 0;

		foreach ($tags as $tag) {
			$tags_table->addItem($this->getRowTemplate($tag, $i));

			$i++;
		}

		$tags_table->addRow(
			(new CCol(
				(new CButton('tags_add', _('Add')))
					->addClass(ZBX_STYLE_BTN_LINK)
					->addClass('element-table-add')
					->setEnabled(!$this->isDisabled())
			))->setColSpan(3)
		);

		return $tags_table;
	}

	public function getJavaScript(): string {
		return '
			jQuery("#tags_table_'.$this->field->getName().'")
				.dynamicRows({template: "#'.$this->field->getName().'-row-tmpl"})
				.on("afteradd.dynamicRows", function() {
					const rows = this.querySelectorAll(".form_row");
					new CTagFilterItem(rows[rows.length - 1]);
				});

			// Init existing fields once loaded.
			document.querySelectorAll("#tags_table_'.$this->field->getName().' .form_row").forEach(row => {
				new CTagFilterItem(row);
			});
		';
	}

	public function getTemplates(): array {
		return [
			new CTemplateTag($this->field->getName().'-row-tmpl', $this->getRowTemplate(CWidgetFieldTags::DEFAULT_TAG))
		];
	}

	private function getRowTemplate(array $tag, $row_num = '#{rowNum}'): CRow {
		return (new CRow([
			(new CTextBox($this->field->getName().'['.$row_num.'][tag]', $tag['tag']))
				->setWidth(ZBX_TEXTAREA_FILTER_SMALL_WIDTH)
				->setAriaRequired($this->isRequired())
				->setEnabled(!$this->isDisabled() || $row_num === '#{rowNum}')
				->setAttribute('placeholder', _('tag')),
			(new CSelect($this->field->getName().'['.$row_num.'][operator]'))
				->addOptions(CSelect::createOptionsFromArray([
					TAG_OPERATOR_EXISTS => _('Exists'),
					TAG_OPERATOR_EQUAL => _('Equals'),
					TAG_OPERATOR_LIKE => _('Contains'),
					TAG_OPERATOR_NOT_EXISTS => _('Does not exist'),
					TAG_OPERATOR_NOT_EQUAL => _('Does not equal'),
					TAG_OPERATOR_NOT_LIKE => _('Does not contain')
				]))
				->setValue($tag['operator'])
				->setFocusableElementId($this->field->getName().'-'.$row_num.'-operator-select')
				->setId($this->field->getName().'_'.$row_num.'_operator')
				->setDisabled($this->isDisabled() && $row_num !== '#{rowNum}'),
			(new CTextBox($this->field->getName().'['.$row_num.'][value]', $tag['value']))
				->setWidth(ZBX_TEXTAREA_FILTER_SMALL_WIDTH)
				->setAriaRequired($this->isRequired())
				->setId($this->field->getName().'_'.$row_num.'_value')
				->setEnabled(!$this->isDisabled() || $row_num === '#{rowNum}')
				->setAttribute('placeholder', _('value')),
			(new CCol(
				(new CButton($this->field->getName().'['.$row_num.'][remove]', _('Remove')))
					->addClass(ZBX_STYLE_BTN_LINK)
					->addClass('element-table-remove')
					->setEnabled(!$this->isDisabled() || $row_num === '#{rowNum}')
			))->addClass(ZBX_STYLE_NOWRAP)
		]))->addClass('form_row');
	}
}