Skip to content

Commit 8eb3caa

Browse files
SearchEditor: Enhance to configure user-defined condition metadata
Completer.js: Unset values of suggestion-based fields on manual input
1 parent f5b19dc commit 8eb3caa

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

asset/js/widget/Completer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,10 @@ define(["../notjQuery"], function ($) {
730730
dataElement.value = input.value;
731731
}
732732

733+
input.form.querySelectorAll(
734+
`input[type="hidden"][name^="${input.name}-"]:not([name$="search"])`
735+
).forEach(i => i.value = '');
736+
733737
let [value, data] = this.prepareCompletionData(input);
734738
this.completedInput = input;
735739
this.completedValue = value;

src/Control/SearchEditor.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ipl\Web\Control;
44

5+
use Icinga\Exception\ConfigurationError;
56
use ipl\Html\Attributes;
67
use ipl\Html\Form;
78
use ipl\Html\FormDecorator\CallbackDecorator;
@@ -55,6 +56,9 @@ class SearchEditor extends Form
5556
/** @var bool */
5657
protected $cleared = false;
5758

59+
/** @var string[] Additional `metadata` fields for the condition */
60+
protected array $metadataFields = [];
61+
5862
/**
5963
* Set the filter query string to populate the form with
6064
*
@@ -95,6 +99,26 @@ public function setSuggestionUrl(Url $url)
9599
return $this;
96100
}
97101

102+
/**
103+
* Set additional `metadata` fields for the condition
104+
*
105+
* The value of these fields is populated to condition's `metadata`
106+
*
107+
* @param array $fields
108+
*
109+
* @return $this
110+
*/
111+
public function setMetadataFields(array $fields): static
112+
{
113+
if (in_array('search', $fields, true)) {
114+
throw new ConfigurationError("'search' is a reserved keyword and cannot be used as a metadata field");
115+
}
116+
117+
$this->metadataFields = $fields;
118+
119+
return $this;
120+
}
121+
98122
/**
99123
* Get the query string parser being used
100124
*
@@ -195,6 +219,10 @@ protected function applyChanges(Filter\Rule $rule, array &$values, array $path =
195219
} else {
196220
// Make sure we don't forget to present the column labels again
197221
$rule->metaData()->set('columnLabel', $this->popKey($values, $identifier . '-column'));
222+
223+
foreach ($this->metadataFields as $fieldName) {
224+
$rule->metaData()->set($fieldName, $this->popKey($values, $identifier . '-column-' . $fieldName));
225+
}
198226
}
199227

200228
if ($newColumn !== null && $rule->getColumn() !== $newColumn) {
@@ -551,6 +579,16 @@ protected function createCondition(Filter\Condition $condition, $identifier)
551579
}]
552580
]);
553581

582+
$metadataFields = new HtmlDocument();
583+
foreach ($this->metadataFields as $fieldNameSuffix) {
584+
$columnMetaInput = $this->createElement('hidden', $identifier . '-column-' . $fieldNameSuffix, [
585+
'value' => $condition->metaData()->get($fieldNameSuffix)
586+
]);
587+
$this->registerElement($columnMetaInput);
588+
589+
$metadataFields->addHtml($columnMetaInput);
590+
}
591+
554592
$operatorInput = $this->createElement('select', $identifier . '-operator', [
555593
'options' => [
556594
'~' => '~',
@@ -588,6 +626,7 @@ protected function createCondition(Filter\Condition $condition, $identifier)
588626
$columnInput,
589627
$columnFakeInput,
590628
$columnSearchInput,
629+
$metadataFields,
591630
$operatorInput,
592631
$valueInput
593632
);

0 commit comments

Comments
 (0)