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
|
<?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\CWidgetFieldThresholds;
class CWidgetFieldThresholdsView extends CWidgetFieldView {
public function __construct(CWidgetFieldThresholds $field) {
$this->field = $field;
}
public function getView(): CDiv {
$thresholds_table = (new CTable())
->setId($this->field->getName().'-table')
->addClass(ZBX_STYLE_TABLE_FORMS)
->setHeader([
'',
(new CColHeader(_('Threshold')))->setWidth('100%'),
_('Action')
])
->setFooter(new CRow(
new CCol(
(new CSimpleButton(_('Add')))
->addClass(ZBX_STYLE_BTN_LINK)
->addClass('element-table-add')
)
));
foreach ($this->field->getValue() as $i => $threshold) {
$thresholds_table->addRow(
$this->getRowTemplate($i, $threshold['color'], $threshold['threshold'])
);
}
return (new CDiv($thresholds_table))
->addClass('table-forms-separator')
->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH);
}
public function getJavaScript(): string {
return '
var $thresholds_table = jQuery("#'.$this->field->getName().'-table");
$thresholds_table
.dynamicRows({template: "#'.$this->field->getName().'-row-tmpl"})
.on("afteradd.dynamicRows", function(opt) {
const rows = this.querySelectorAll(".form_row");
const colors = jQuery("#widget-dialogue-form")[0]
.querySelectorAll(".'.ZBX_STYLE_COLOR_PICKER.' input");
const used_colors = [];
for (const color of colors) {
if (color.value !== "" && color.name.includes("thresholds")) {
used_colors.push(color.value);
}
}
jQuery(".color-picker input", rows[rows.length - 1])
.val(colorPalette.getNextColor(used_colors))
.colorpicker({
appendTo: ".overlay-dialogue-body"
});
});
';
}
public function getTemplates(): array {
return [
new CTemplateTag($this->field->getName().'-row-tmpl', $this->getRowTemplate())
];
}
public function getRowTemplate($row_num = '#{rowNum}', $color = '#{color}', $threshold = '#{threshold}'): CRow {
return (new CRow([
(new CColor($this->field->getName().'['.$row_num.'][color]', $color))->appendColorPickerJs(false),
(new CTextBox($this->field->getName().'['.$row_num.'][threshold]', $threshold, false))
->setWidth(ZBX_TEXTAREA_TINY_WIDTH)
->setAriaRequired(),
(new CButton($this->field->getName().'['.$row_num.'][remove]', _('Remove')))
->addClass(ZBX_STYLE_BTN_LINK)
->addClass('element-table-remove')
]))->addClass('form_row');
}
}
|