設定
このページでは、カスタム設定フィールドを使用してウィジェット設定ビューを作成するために使用できるクラスについて説明します。 ウィジェット設定ビューは、ユーザーがプレゼンテーションのウィジェットパラメーターを設定できるウィジェットの一部です。
Widget
プライマリウィジェットクラス、すべてのダッシュボードウィジェットの基本クラスCWidgetを拡張します。 デフォルトのウィジェットの動作をオーバーライドするために必要です。
Widgetクラスは、ウィジェットのルートディレクトリ(zabbix/ui/modules/my_custom_widgetなど)に配置する必要があります。
Widget.php example
<?php
namespace Modules\MyCustomWidget;
use Zabbix\Core\CWidget;
class Widget extends CWidget {
public const MY_CONSTANT = 0;
public function getTranslationStrings(): array {
return [
'class.widget.js' => [
'No data' => _('No data')
]
];
}
}
WidgetForm
WidgetFormクラスは、デフォルトクラスCWidgetFormを拡張し、データベース内でウィジェット構成の保存構造を定義し、入力検証を処理するために必要なCWidgetFieldフィールドのセットを含みます。
WidgetFormクラスはincludesディレクトリに配置する必要があります。 クラスに別の名前が付いている場合は、その名前をmanifest.jsonファイルのwidget/form_classパラメーターで指定する必要があります。
includes/WidgetForm.php example
<?php
namespace Modules\MyCustomWidget\Includes;
use Modules\MyCustomWidget\Widget;
use Zabbix\Widgets\{
CWidgetField,
CWidgetForm
};
use Zabbix\Widgets\Fields\{
CWidgetFieldMultiSelectItem,
CWidgetFieldTextBox,
CWidgetFieldColor
};
class WidgetForm extends CWidgetForm {
public const DEFAULT_COLOR_PALETTE = [
'FF465C', 'B0AF07', '0EC9AC', '524BBC', 'ED1248', 'D1E754', '2AB5FF', '385CC7', 'EC1594', 'BAE37D',
'6AC8FF', 'EE2B29', '3CA20D', '6F4BBC', '00A1FF', 'F3601B', '1CAE59', '45CFDB', '894BBC', '6D6D6D'
];
public function addFields(): self {
return $this
->addField(
(new CWidgetFieldMultiSelectItem('itemid', _('Item')))
->setFlags(CWidgetField::FLAG_NOT_EMPTY | CWidgetField::FLAG_LABEL_ASTERISK)
->setMultiple(false)
)
->addField(
new CWidgetFieldTextBox('description', _('Description'))
)
->addField(
(new CWidgetFieldColor('chart_color', _('Color')))->setDefault('FF0000')
);
}
}
CWidgetFormView
CWidgetFormViewクラスは、WidgetFormクラスで定義されたフィールドのプレゼンテーションロジックを指定し、設定ビューで表示されるときの外観と動作を決定するために必要です。
CWidgetFormViewクラスは次のメソッドをサポートします。
- addField() - CWidgetFieldViewクラスのインスタンスをパラメーターとして受け取ります。 各CWidgetFieldクラスには、ウィジェット設定ビューで使用するためのそれぞれのCWidgetFieldViewクラスがあります。
- addFieldset() - フィールドを結合して折りたたみ可能なコンテナにするCWidgetFieldsGroupViewクラスのインスタンスを受け取ります。
- addFieldsGroup() - フィールドを視覚的に(境界線を使用して)グループに結合するCWidgetFormFieldsetCollapsibleViewのインスタンスを受け取ります。
- includeJsFile() - JavaScriptファイルをウィジェット設定ビューに追加できます。
- addJavaScript() - ウィジェット設定ビューがロードされるとすぐに実行されるインラインJavaScriptを追加できます。
CWidgetFormViewクラスはviewsディレクトリに配置する必要があります。
views/widget.edit.php example
<?php
/**
* My custom widget form view.
*
* @var CView $this
* @var array $data
*/
use Modules\MyCustomWidget\Includes\WidgetForm;
(new CWidgetFormView($data))
->addField(
(new CWidgetFieldMultiSelectItemView($data['fields']['itemid']))->setPopupParameter('numeric', true)
)
->addFieldset(
(new CWidgetFormFieldsetCollapsibleView(_('Advanced configuration')))
->addField(
new CWidgetFieldTextBoxView($data['fields']['description'])
)
->addField(
new CWidgetFieldColorView($data['fields']['chart_color'])
)
)
->includeJsFile('widget.edit.js.php')
->addJavaScript('my_custom_widget_form.init('.json_encode([
'color_palette' => WidgetForm::DEFAULT_COLOR_PALETTE
]).');')
->show();
JavaScript
JavaScriptクラスを使用して、ウィジェット設定ビューに動的な動作と対話性を追加できます。 たとえば、CWidgetFormViewクラスで定義されたカラーピッカーを初期化できます。
JavaScriptクラスはフォームとともにロードする必要があるため、メソッドincludeJsFile()およびaddJavaScript()を使用してCWidgetFormViewクラスで参照する必要があります。
以下の例では、シングルトンクラスインスタンスがすぐに作成され、window.my_custom_widget_formという名前で保存されます。 したがって、フォームを2回目に開くとインスタンスが再作成されます。
JavaScriptクラスはviewsディレクトリに配置する必要があります。
views/widget.edit.js.php example
<?php
use Modules\MyCustomWidget\Widget;
?>
window.my_custom_widget_form = new class {
init({color_palette}) {
colorPalette.setThemeColors(color_palette);
for (const colorpicker of jQuery('.<?= ZBX_STYLE_COLOR_PICKER ?> input')) {
jQuery(colorpicker).colorpicker();
}
const overlay = overlays_stack.getById('widget_properties');
for (const event of ['overlay.reload', 'overlay.close']) {
overlay.$dialogue[0].addEventListener(event, () => { jQuery.colorpicker('hide'); });
}
}
};
CWidgetField
CWidgetField クラスは、すべてのフォームフィールドクラス(CWidgetFieldCheckBox、CWidgetFieldTextArea、CWidgetFieldRadioButtonList など)が継承する基底クラスです。 CWidgetField を拡張するクラスは、ウィジェット設定値の受け取り、保存、検証を担当します。
以下の CWidgetField クラスを利用できます。
| CWidgetField class | Database field type | Description |
|---|---|---|
| CWidgetFieldCheckBox | int32 | 単一のチェックボックス。 |
| CWidgetFieldCheckBoxList | array of int32 | 単一の設定フィールド内の複数のチェックボックス。 |
| CWidgetFieldColor | string | 色選択フィールド。 |
| CWidgetFieldDatePicker | string | 日付選択フィールド。 |
| CWidgetFieldIntegerBox | int32 | 整数を入力するフィールド。最小値と最大値の設定に使用できます。 |
| CWidgetFieldLatLng | string | 緯度、経度、地図のズームレベルをカンマ区切りで入力できるテキストボックス。 |
| CWidgetFieldMultiSelect | - | マルチセレクトフィールドの基底クラスで、すべての CWidgetFieldMultiSelect* クラスによって拡張されます。 |
| CWidgetFieldMultiSelectAction | ID | Alerts → Actions で定義されたアクションの一覧からアクションを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectGraph | ID | カスタムグラフを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectGraphPrototype | ID | カスタムグラフプロトタイプを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectGroup | ID | ホストグループを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectHost | ID | ホストを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectHostInventory | array of int32 | ホストインベントリフィールドを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectItem | ID | アイテムを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectItemPrototype | ID | アイテムプロトタイプを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectMap | ID | マップを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectMediaType | ID | メディアタイプを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectOverrideHost | ID | ダッシュボードまたは別のウィジェットから、ホストのデータソースを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectService | ID | サービスを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectSla | ID | SLA を選択するためのマルチセレクトフィールド。 |
| CWidgetFieldMultiSelectUser | ID | ユーザーを選択するためのマルチセレクトフィールド。 |
| CWidgetFieldNavTree | array of (multiple mixed) | Map navigation tree ウィジェットで使用するマップのナビゲーションツリー構造を保存します。 |
| CWidgetFieldNumericBox | string | 浮動小数点数を入力するフィールド。 |
| CWidgetFieldPatternSelect | - | パターン選択フィールドの基底クラスで、すべての CWidgetPatternSelect* クラスによって拡張されます。 |
| CWidgetFieldPatternSelectHost | string | ホストを選択するためのマルチセレクトフィールド。ホスト名パターンをサポートします。 |
| CWidgetFieldPatternSelectItem | string | アイテムを選択するためのマルチセレクトフィールド。アイテム名パターンをサポートします。 |
| CWidgetFieldRadioButtonList | int32 | 1 つ以上のラジオボックスで構成されるラジオボックスグループ。 |
| CWidgetFieldRangeControl | int32 | 整数型の値を選択するためのスライダー。 |
| CWidgetFieldReference | string | ダッシュボード上のこのウィジェットの一意な識別子を保存します。ウィジェット間通信に使用されます。 |
| CWidgetFieldSelect | int32 | ドロップダウン選択ボックス。 |
| CWidgetFieldSeverities | array of int32 | トリガーの深刻度があらかじめ設定された CWidgetFieldCheckBoxList。 |
| CWidgetFieldSparkline | array of (multiple mixed) | スパークラインチャートを設定できます。 |
| CWidgetFieldTags | array of (string, int32, string) | 1 つ以上のタグフィルター行を設定できます。 |
| CWidgetFieldTextArea | string | 複数行のテキストを入力するためのテキストエリア。 |
| CWidgetFieldTextBox | string | 1 行のテキストを入力するためのテキストボックス。 |
| CWidgetFieldThresholds | array of (string, string) | 色と数値の組み合わせを設定できます。 |
| CWidgetFieldTimePeriod | array of string | 期間選択フィールド。 |
| CWidgetFieldTimeZone | string | タイムゾーンのドロップダウン。 |
| CWidgetFieldUrl | string | URL を入力できるテキストボックス。 |
int32 フィールドの最大長は 10 文字、ID フィールドは 20 文字、string フィールドは 65535 文字です。
複合型(例: array of (string, int32, string))では、制限は各要素に個別に適用されます。
フィールド固有の長さ制限は、setMaxLength() などのメソッドを使用して設定できます。