Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

UI updates: Added UI for attribute duration in JSON rules (#1542)

Martin Peetersa43d18825f1b
diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.json
index 42d235382..c821ca38c 100644
--- a/manager/src/web/shared/locales/en/or.json
+++ b/manager/src/web/shared/locales/en/or.json
@@ -100,6 +100,7 @@
"rulesEditorAddAttribute": "Add attribute",
"rulesEditorAddGroup": "Add group",
"rulesEditorAddAction": "Add action",
+ "rulesEditorDuration": "Duration (min)",
"sortBy": "Sort by",
"assetType": "Asset type",
"creationDate": "Creation date",
diff --git a/manager/src/web/shared/locales/nl/or.json b/manager/src/web/shared/locales/nl/or.json
index 6192d93a3..c6d9db105 100644
--- a/manager/src/web/shared/locales/nl/or.json
+++ b/manager/src/web/shared/locales/nl/or.json
@@ -100,6 +100,7 @@
"rulesEditorAddAttribute": "Voeg attribute toe",
"rulesEditorAddGroup": "Groep",
"rulesEditorAddAction": "Voeg actie toe",
+ "rulesEditorDuration": "Tijdsduur (min)",
"sortBy": "Sorteren op",
"assetType": "Asset type",
"creationDate": "Aangemaakt op",
diff --git a/ui/component/or-rules/src/json-viewer/modals/or-rule-notification-modal.ts b/ui/component/or-rules/src/json-viewer/modals/or-rule-notification-modal.ts
index c6de60cdc..8f366f79d 100644
--- a/ui/component/or-rules/src/json-viewer/modals/or-rule-notification-modal.ts
+++ b/ui/component/or-rules/src/json-viewer/modals/or-rule-notification-modal.ts
@@ -26,8 +26,10 @@ const checkValidity = (form:HTMLElement | null) => {
if(input && input.checkValidity()) {
return true;
} else {
- element._mdcComponent.valid = false;
- element._mdcComponent.helperTextContent = 'required';
+ if(element._mdcComponent) {
+ element._mdcComponent.valid = false;
+ element._mdcComponent.helperTextContent = 'required';
+ }
return false;
}
diff --git a/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts b/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts
index db7daf228..930cadbd3 100644
--- a/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts
+++ b/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts
@@ -31,7 +31,7 @@ import {OrAttributeInputChangedEvent} from "@openremote/or-attribute-input";
import "./modals/or-rule-radial-modal";
import { ifDefined } from "lit/directives/if-defined.js";
import {when} from 'lit/directives/when.js';
-import {getWhenTypesMenu} from "./or-rule-condition";
+import moment from "moment";
// language=CSS
const style = css`
@@ -77,7 +77,19 @@ const style = css`
flex-wrap: wrap;
align-items: center;
gap: 6px;
- }
+ }
+ .attribute > div > or-rule-radial-modal {
+ min-width: auto;
+ }
+ .attribute > div > or-mwc-input[type="button"] {
+ min-width: auto;
+ }
+ .attribute > div > or-mwc-input:not([type="button"]) {
+ width: 200px;
+ }
+ .attribute > div > or-attribute-input {
+ width: 200px;
+ }
.attribute > div > * {
min-width: 200px;
}
@@ -275,6 +287,32 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {
}
}
+ protected attributeDurationTemplate(durationMap: Map<number, string | undefined>, index: number, onAdd: (index: number) => void, onChange: (index: number, duration: string | undefined) => void) {
+ if(durationMap.has(index)) {
+ const isoDuration = durationMap.get(index);
+ const duration = isoDuration ? moment.duration(isoDuration) : undefined;
+ return html`
+ <or-mwc-input type="${InputType.NUMBER}" min="0" .readonly="${this.readonly || false}"
+ .value="${duration?.asMinutes()}" label="${i18next.t("rulesEditorDuration")}"
+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => {
+ const newDuration = moment.duration(ev.detail.value, "minutes");
+ if(newDuration.asMinutes() > 0) {
+ onChange(index, newDuration.toISOString());
+ } else {
+ onChange(index, undefined);
+ }
+ }}"
+ ></or-mwc-input>
+ `;
+ } else {
+ return html`
+ <or-mwc-input type="${InputType.BUTTON}" .readonly="${this.readonly || false}" icon="clock-plus-outline"
+ @or-mwc-input-changed="${() => onAdd(index)}"
+ ></or-mwc-input>
+ `;
+ }
+ }
+
static get styles() {
return style;
}
@@ -292,6 +330,30 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {
return this.condition.assets!;
}
+ /**
+ * Returns a Map<number, string> with all configured durations of a {@link RuleCondition}.
+ * The number represents an index of the attribute array, and the string an ISO8601 duration expression.
+ */
+ protected get duration(): Map<number, string | undefined> {
+ if(this.condition?.duration) {
+ return new Map(Object.entries(this.condition.duration).map(([key, value]) => [Number(key), value as string]));
+ } else {
+ return new Map<number, string | undefined>();
+ }
+ }
+
+ /**
+ * Updates the duration property in {@link condition}, based on the {@link durationMap} parameter.
+ * The number represents an index of the attribute array, and the string an ISO8601 duration expression.
+ */
+ protected set duration(durationMap: Map<number, string | undefined>) {
+ if(durationMap.size > 0) {
+ this.condition.duration = Object.fromEntries(durationMap);
+ } else {
+ this.condition.duration = undefined;
+ }
+ }
+
protected render() {
const assetType = getAssetTypeFromQuery(this.query);
@@ -361,6 +423,17 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {
const showAddAttribute = !this.readonly && (!this.config || !this.config.controls || this.config.controls.hideWhenAddAttribute !== true);
+ const onDurationAdd = (index: number) => {
+ this.duration = this.duration.set(index, undefined);
+ this.requestUpdate();
+ };
+ const onDurationChange = (index: number, duration: string | undefined) => {
+ console.debug("Updating duration of rule condition to ", duration);
+ this.duration = this.duration.set(index, duration);
+ this.dispatchEvent(new OrRulesJsonRuleChangedEvent());
+ this.requestUpdate();
+ };
+
return html`
<or-mwc-input id="idSelect" class="min-width filledSelect" type="${InputType.SELECT}" .readonly="${this.readonly || false}" .label="${i18next.t("asset")}"
.options="${idOptions}" .value="${idValue}" .searchProvider="${searchProvider}"
@@ -372,7 +445,8 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {
${index > 0 ? html`<or-icon class="small" icon="ampersand"></or-icon>` : ``}
<div class="attribute">
<div>
- ${this.attributePredicateEditorTemplate(assetTypeInfo, idValue !== "*" ? this._assets!.find((asset) => asset.id === idValue) : undefined, attributePredicate)}
+ ${this.attributePredicateEditorTemplate(assetTypeInfo, idValue !== "*" ? this._assets!.find((asset) => asset.id === idValue) : undefined, attributePredicate)}
+ ${this.attributeDurationTemplate(this.duration, index, onDurationAdd, onDurationChange)}
</div>
${showRemoveAttribute ? html`
<button class="button-clear" @click="${() => this.removeAttributePredicate(this.query!.attributes!, attributePredicate)}"><or-icon icon="close-circle"></or-icon></input>
@@ -749,6 +823,14 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {
const index = group.items!.indexOf(attributePredicate);
if (index >= 0) {
group.items!.splice(index, 1);
+
+ if(this.duration.has(index)) {
+ const newEntries = Array.from(this.duration.entries())
+ .filter(([k, v]) => k !== index)
+ .map(([k, v], i) => [i, v] as [number, string | undefined]);
+
+ this.duration = new Map<number, string | undefined>(newEntries);
+ }
}
this.dispatchEvent(new OrRulesJsonRuleChangedEvent());
this.requestUpdate();