100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
UI updates: Add rules validity modal
Robin Dekkers8119754587da
diff --git a/deployment/manager/shared/locales/en/or.json b/deployment/manager/shared/locales/en/or.jsonindex 6a8c95126..e9a22ba84 100644--- a/deployment/manager/shared/locales/en/or.json+++ b/deployment/manager/shared/locales/en/or.json@@ -332,5 +332,12 @@"urn:openremote:protocol:knx": {"description": "Protocol for connecting to a KNX"}- }+ },+ "validityType":"Schedule type",+ "validityAlways":"Always active",+ "validityPeriod":"Plan a occurence",+ "validityRecurrence":"Plan a repeating occurence",+ "activeFromTo": "Active from {{start}} to {{end}}",+ "fromTo":"From {{start}} to {{end}}",+ "fromToDays":"from {{start}} to {{days}} days later {{end}}"}diff --git a/ui/component/or-input/src/index.ts b/ui/component/or-input/src/index.tsindex 256b5de1b..ea5edc1e0 100644--- a/ui/component/or-input/src/index.ts+++ b/ui/component/or-input/src/index.ts@@ -65,6 +65,7 @@ export enum InputType {BUTTON_TOGGLE = "button-toggle",BUTTON_MOMENTARY = "button-momentary",CHECKBOX = "checkbox",+ CHECKBOX_LIST = "checkbox-list",COLOUR = "color",DATE = "date",DATETIME = "datetime-local",@@ -135,7 +136,27 @@ const style = css`#menu-anchor {max-width: 100%;}-++ .mdc-checkbox-list input {+ display: none;+ }++ .mdc-checkbox-list label {+ display: block;+ border-radius: 50%;+ text-align: center;+ width: 32px;+ line-height: 32px;+ height: 32px;+ background-color: var(--or-app-color2);+ font-size: 13px;+ }++ .mdc-checkbox-list input:checked + label {+ color: var(--or-app-color2);+ background-color: var(--mdc-theme-primary);+ }+.or-input--rounded {border-radius: 50% !important;}@@ -417,7 +438,8 @@ export class OrInput extends LitElement {switch (this.type) {case InputType.RADIO:- return html`<span>RADIO</span>`;+ return html`RADIO`+ break;case InputType.SWITCH:return html`<span id="wrapper">@@ -579,6 +601,44 @@ export class OrInput extends LitElement {</button>`;}+ case InputType.CHECKBOX_LIST:++ let optsRadio: [string, string][] | undefined;+ if (this.options && this.options.length > 0) {+ if (Array.isArray(this.options[0])) {+ optsRadio = this.options as [string, string][];+ } else {+ optsRadio = (this.options as string[]).map((option) => [option, option]);+ }+ }++ this._selectedIndex = -1;+ return html`+ <div class="mdc-checkbox-list">+ ${optsRadio ? optsRadio.map(([optValue, optDisplay], index) => {+ if (this.value === optValue) {+ this._selectedIndex = index;+ }+ return html`+ <div id="field" class="mdc-form-field">+ <div id="component" class="mdc-checkbox">+ <input type="checkbox"+ ?checked="${this.value && this.value.includes(optValue)}"+ ?required="${this.required}"+ name="${optValue}"+ ?disabled="${this.disabled || this.readonly}"+ @change="${(e: Event) => this.onValueChange((e.target as HTMLInputElement), {name: (e.target as HTMLInputElement).name, checked: (e.target as HTMLInputElement).checked})}"+ class="mdc-checkbox__native-control" id="elem-${optValue}"/>++ <label for="elem-${optValue}"><or-translate value="${optDisplay}"></or-translate></label>++ </div>+ </div>++ `;+ }) : ``}+ </div>+ `;case InputType.CHECKBOX:return html`<div id="field" class="mdc-form-field">@@ -596,7 +656,7 @@ export class OrInput extends LitElement {<div class="mdc-checkbox__mixedmark"></div></div></div>- <label for="elem">${this.label}</label>+ <label class="mdc-checkbox-circle" for="elem">${this.label}</label></div>`;case InputType.NUMBER:diff --git a/ui/component/or-rules/src/json-viewer/or-rule-action-notification.ts b/ui/component/or-rules/src/json-viewer/or-rule-action-notification.tsindex 948832baa..235f485db 100644--- a/ui/component/or-rules/src/json-viewer/or-rule-action-notification.ts+++ b/ui/component/or-rules/src/json-viewer/or-rule-action-notification.ts@@ -7,7 +7,8 @@ import {AssetQueryOrderBy$Property,JsonRule,RuleAction,- RuleActionNotification+ RuleActionNotification,+ UserQuery} from "@openremote/model";import {InputType, OrInputChangedEvent} from "@openremote/or-input";import {getTargetTypeMap, OrRulesJsonRuleChangedEvent} from "./or-rule-json-viewer";@@ -304,6 +305,12 @@ export class OrRuleActionNotification extends LitElement {protected _onTargetChanged(targetType: ActionTargetType, value: string | undefined) {switch (targetType) {+ case ActionTargetType.USER:+ if(value){+ const users:UserQuery = {ids: [value]}+ this.action.target = {users: users}+ }+ break;case ActionTargetType.CUSTOM:this.action.target = {custom: valuediff --git a/ui/component/or-rules/src/or-rule-validity.ts b/ui/component/or-rules/src/or-rule-validity.tsnew file mode 100644index 000000000..0bd606839--- /dev/null+++ b/ui/component/or-rules/src/or-rule-validity.ts@@ -0,0 +1,305 @@+import { css, customElement, html, LitElement, property, PropertyValues} from "lit-element";+import { RulesetUnion } from "@openremote/model";+import {+ OrRulesRuleChangedEvent+} from "./index";+import "@openremote/or-input";+import {InputType, OrInputChangedEvent} from "@openremote/or-input";+import i18next from "i18next";+import {translate} from "@openremote/or-translate";++import {DialogAction, OrMwcDialog } from "@openremote/or-mwc-components/dist/or-mwc-dialog";+import { RRule, Weekday } from 'rrule'+import moment from "moment";++@customElement("or-rule-validity")+export class OrRuleValidity extends translate(i18next)(LitElement) {+++ @property({type: Object})+ public ruleset?: RulesetUnion;++ @property({type: Object})+ public rrule: RRule = new RRule({+ freq: RRule.DAILY,+ dtstart: new Date()+ });++ constructor() {+ super();+ }+ public static styles = css`+ :host {+ margin-left: 20px;+ }+ `;++ protected updated(changedProps: PropertyValues) {+ super.updated(changedProps);+ if(changedProps.has("ruleset") && this.ruleset) {+ if(!this.ruleset.meta) this.ruleset.meta = {};+ if(!this.ruleset.meta["urn:openremote:rule:meta:validity"]) return;++ if(this.ruleset.meta["urn:openremote:rule:meta:validity"].recurrence) {+ this.rrule = RRule.fromString(this.ruleset.meta["urn:openremote:rule:meta:validity"].recurrence)+ }+ }+ }++ getWeekDay(weekday: string) {+ switch (weekday) {+ case "MO":+ return RRule.MO+ case "TU":+ return RRule.TU+ case "WE":+ return RRule.WE+ case "TH":+ return RRule.TH+ case "FR":+ return RRule.FR+ case "SA":+ return RRule.SA+ case "SU":+ return RRule.SU+ }+ }++ isAllDay() {+ if(this.ruleset && this.ruleset.meta && this.ruleset.meta["urn:openremote:rule:meta:validity"]) {+ if(moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].start).hours() === 0 && moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].start).minutes() === 0+ && moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).hours() === 23 && moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).minutes() === 59) {+ return true+ }+ }+ }++ protected setRRuleValue(value: any, key?: string) {+ if(key && this.rrule && this.ruleset){+ const origOptions:any = this.rrule.origOptions+ switch (key) {+ case "all-day":+ if(value) {+ if(this.ruleset.meta) {+ this.ruleset.meta["urn:openremote:rule:meta:validity"].start = moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].start).startOf('day');+ this.ruleset.meta["urn:openremote:rule:meta:validity"].end = moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).endOf('day');+ }+ } else {+ if(this.ruleset.meta) {+ this.ruleset.meta["urn:openremote:rule:meta:validity"].start = moment.utc();+ this.ruleset.meta["urn:openremote:rule:meta:validity"].end = moment.utc().add(1, 'hour');+ }++ }+ break;+ case "start":+ if(this.ruleset.meta)+ this.ruleset.meta["urn:openremote:rule:meta:validity"]['start'] = moment.utc(value).set({hour:0,minute:0,second:0,millisecond:0}).format();++ if(this.getValidityType() === "validityRecurrence")+ origOptions[key] = moment.utc(value).format();+ this.rrule = new RRule(origOptions);+ break;+ case "end":+ if(this.ruleset.meta)+ this.ruleset.meta["urn:openremote:rule:meta:validity"]['end'] = moment.utc(value).set({hour:23,minute:59,second:0,millisecond:0}).format();+ break;+ case "never-ends":+ if(value) {+ delete origOptions.until+ } else {+ origOptions.until = new Date(moment.utc().add(1, 'year').format());+ }+ if(this.getValidityType() === "validityRecurrence") this.rrule = new RRule(origOptions);+ break;+ case "byweekday":+ if(!origOptions[key]) origOptions[key] = [];+ if(value.checked) {+ origOptions[key].push(this.getWeekDay(value.name))+ } else {+ origOptions[key] = origOptions[key].filter((day:Weekday) => day !== this.getWeekDay(value.name));+ }+ if(this.getValidityType() === "validityRecurrence") this.rrule = new RRule(origOptions);+ break;+ case "dtstart":+ case "until":+ break;+ case "dtstart-time":+ const timeParts = value.split(':');+ origOptions["dtstart"] = moment.utc(origOptions["dtstart"]).set({hour:timeParts[0],minute:timeParts[1],second:0,millisecond:0}).format()+ if(this.ruleset.meta)+ this.ruleset.meta["urn:openremote:rule:meta:validity"].start = moment.utc(origOptions["dtstart"]).format();+ if(this.getValidityType() === "validityRecurrence") this.rrule = new RRule(origOptions);+ break;+ case "until-time":+ const untilParts = value.split(':');+ if(this.rrule.options.until) {+ origOptions["until"] = moment.utc(origOptions["until"]).set({hour:untilParts[0],minute:untilParts[1],second:0,millisecond:0}).format()+ }+ if(this.ruleset.meta)+ this.ruleset.meta["urn:openremote:rule:meta:validity"].end = moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).set({hour:untilParts[0],minute:untilParts[1],second:0,millisecond:0}).format();+ if(this.getValidityType() === "validityRecurrence") this.rrule = new RRule(origOptions);+ break;+ }+ }+ this.requestUpdate();+ }++ timeLabel() {+ if(this.getValidityType() === "validityAlways") {+ return i18next.t("validityAlways")+ } else if(this.ruleset && this.ruleset.meta && this.ruleset.meta["urn:openremote:rule:meta:validity"] && this.ruleset.meta["urn:openremote:rule:meta:validity"].recurrence){+ const diff = moment(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).diff(this.ruleset.meta["urn:openremote:rule:meta:validity"].start, 'days');+ let diffString = "";+ if(this.isAllDay()) {+ if(diff > 0) diffString = " "+diff+ " " + i18next.t("days") + " " + i18next.t("later")+ " ";+ return this.rrule.toText()+diffString;+ } else {+ if(diff > 0) diffString = i18next.t("fromToDays", {start: moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].start).format("HH:mm"), end: moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).format("HH:mm"), days: diff })+ if(diff === 0) diffString = i18next.t("fromTo", {start: moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].start).format("HH:mm"), end: moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).format("HH:mm") })+ return this.rrule.toText()+" "+diffString;+ }+ } else if(this.ruleset && this.ruleset.meta && this.ruleset.meta["urn:openremote:rule:meta:validity"]){+ let format = "DD-MM-YYYY";+ if(!this.isAllDay()) format = "DD-MM-YYYY HH:mm";+ return i18next.t("activeFromTo", {start: moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].start).format(format), end: moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).format(format) })+ }+ }++ setValidityType(value: any) {+ if(!this.ruleset || !this.ruleset.meta) return+ switch (value) {+ case "validityAlways":+ this.ruleset.meta["urn:openremote:rule:meta:validity"] = null;+ break;+ case "validityPeriod":+ this.ruleset.meta["urn:openremote:rule:meta:validity"] = {+ start: moment.utc().startOf('day'),+ end:moment.utc().endOf('day')+ };+ break;+ case "validityRecurrence":+ this.ruleset.meta["urn:openremote:rule:meta:validity"] = {+ start: moment.utc().startOf('day'),+ end:moment.utc().endOf('day'),+ recurrence: {}+ };+ break;+ }+ this.requestUpdate('ruleset')+ }++ getValidityType () {+ if(this.ruleset && this.ruleset.meta && this.ruleset.meta["urn:openremote:rule:meta:validity"]) {+ if(this.ruleset.meta["urn:openremote:rule:meta:validity"].recurrence) {+ return "validityRecurrence"+ } else {+ return "validityPeriod"++ }+ }+ return "validityAlways";+ }++ renderDialogHTML() {+ const dialog: OrMwcDialog = this.shadowRoot!.getElementById("radial-modal") as OrMwcDialog;+ const options = [RRule.MO.toString(), RRule.TU.toString(), RRule.WE.toString(), RRule.TH.toString(), RRule.FR.toString(), RRule.SA.toString(), RRule.SU.toString()];+ const validityTypes = ["validityAlways", "validityPeriod", "validityRecurrence"];+ const validityType = this.getValidityType()+ const selectedOptions = this.rrule.options && this.rrule.options.byweekday ? this.rrule.options.byweekday.map(day => new Weekday(day).toString()) : [];++ if (dialog && this.ruleset && this.ruleset.meta) {+ dialog.dialogContent = html`+ <div style="min-height: 200px; min-width: 635px; display:grid; flex-direction: row;">+ <div class="layout horizontal">+ <or-input .value="${validityType}" .type="${InputType.SELECT}" .options="${validityTypes}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValidityType(e.detail.value)}" ></or-input>+ </div>++ ${(validityType === "validityPeriod" || validityType === "validityRecurrence") ? html`+ <label style="display:block; margin-top: 10px;">Period</label>+ <div class="layout horizontal">+ <or-input value="${moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].start).format("YYYY-MM-DD")}" .type="${InputType.DATE}" @or-input-changed="${(e: OrInputChangedEvent) => this.setRRuleValue(e.detail.value, "start")}" .label="${i18next.t("from")}"></or-input>+ <or-input .disabled=${this.isAllDay()} .value="${moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].start).format("HH:mm")}" .type="${InputType.TIME}" @or-input-changed="${(e: OrInputChangedEvent) => this.setRRuleValue(e.detail.value, "dtstart-time")}" .label="${i18next.t("from")}"></or-input>++ <or-input .value="${moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).format("YYYY-MM-DD")}" .type="${InputType.DATE}" @or-input-changed="${(e: OrInputChangedEvent) => this.setRRuleValue(e.detail.value, "end")}" .label="${i18next.t("to")}"></or-input>++ <or-input .disabled=${this.isAllDay()} .value="${moment.utc(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).format("HH:mm")}" .type="${InputType.TIME}" @or-input-changed="${(e: OrInputChangedEvent) => this.setRRuleValue(e.detail.value, "until-time")}" .label="${i18next.t("to")}"></or-input>+ </div>++ <div class="layout horizontal">+ <or-input .value=${this.isAllDay()} @or-input-changed="${(e: OrInputChangedEvent) => this.setRRuleValue(e.detail.value, "all-day")}" .type="${InputType.CHECKBOX}" .label="${i18next.t("all day")}"></or-input>+ </div>+ ` : ``}++ ${validityType === "validityRecurrence" ? html`+ <label style="display:block; margin-top: 10px;">Repeat occurence every</label>+ <div class="layout horizontal">+ <or-input .value="${selectedOptions}" .type="${InputType.CHECKBOX_LIST}" .options="${options}" .label="${i18next.t("days of the week")}" @or-input-changed="${(e: OrInputChangedEvent) => this.setRRuleValue(e.detail.value, "byweekday")}" ></or-input>+ </div>++ <label style="display:block; margin-top: 10px;">Repetition ends</label>+ <div class="layout horizontal">+ <or-input .value="${!this.rrule.options.until}" @or-input-changed="${(e: OrInputChangedEvent) => this.setRRuleValue(e.detail.value, "never-ends")}" .type="${InputType.CHECKBOX}" .label="${i18next.t("never")}"></or-input>+ </div>+ <div class="layout horizontal">+ <or-input ?disabled="${!this.rrule.options.until}" .value="${this.rrule.options.until ? moment.utc(this.rrule.options.until).format("YYYY-MM-DD") : moment.utc().add(1, 'year').format('YYYY-MM-DD')}" .type="${InputType.DATE}" @or-input-changed="${(e: OrInputChangedEvent) => this.setRRuleValue(e.detail.value, "until")}" .label="${i18next.t("to")}"></or-input>+ </div>+ ` : ``}++++ </div>`;+ }+ }++ protected render() {+ if(!this.ruleset) return html``;+++ // @ts-ignore++ const validityModalActions: DialogAction[] = [+ {+ actionName: "cancel",+ content: html`<or-input class="button" .type="${InputType.BUTTON}" .label="${i18next.t("cancel")}"></or-input>`,+ action: () => {+ // Nothing to do here+ }+ },+ {+ actionName: "ok",+ default: true,+ content: html`<or-input class="button" .type="${InputType.BUTTON}" .label="${i18next.t("apply")}"></or-input>`,+ action: () => {+ if(this.ruleset && this.ruleset.meta) {+ if(this.getValidityType() === "validityRecurrence") {+ this.ruleset.meta["urn:openremote:rule:meta:validity"].recurrence = this.rrule.toString()+ }+ if(this.getValidityType() === "validityPeriod" || this.getValidityType() === "validityRecurrence") {+ this.ruleset.meta["urn:openremote:rule:meta:validity"].start = moment(this.ruleset.meta["urn:openremote:rule:meta:validity"].start).valueOf()+ this.ruleset.meta["urn:openremote:rule:meta:validity"].end = moment(this.ruleset.meta["urn:openremote:rule:meta:validity"].end).valueOf()+ }+ this.dispatchEvent(new OrRulesRuleChangedEvent(true));+ }+ }+ },+ ];+++ const validityModalOpen = () => {+ const dialog: OrMwcDialog = this.shadowRoot!.getElementById("radial-modal") as OrMwcDialog;+ if (dialog) {+ dialog.open();+ this.renderDialogHTML();++ }+ };++ this.renderDialogHTML();++ return html`+ <or-input .type="${InputType.BUTTON}" .label="${this.timeLabel()}" @click="${validityModalOpen}"></or-input>+ <or-mwc-dialog id="radial-modal" dialogTitle="Schedule rule activity" .dialogActions="${validityModalActions}"></or-mwc-dialog>+ `+ }+}diff --git a/ui/component/or-rules/src/or-rule-viewer.ts b/ui/component/or-rules/src/or-rule-viewer.tsindex 112816e18..ce33325ef 100644--- a/ui/component/or-rules/src/or-rule-viewer.ts+++ b/ui/component/or-rules/src/or-rule-viewer.ts@@ -11,6 +11,7 @@ import {ClientRole, RulesetLang, RulesetUnion} from "@openremote/model";import manager, { Util } from "@openremote/core";import "./json-viewer/or-rule-json-viewer";import "./or-rule-text-viewer";+import "./or-rule-validity";import "./flow-viewer/components/flow-editor";import "@openremote/or-input";import {translate} from "@openremote/or-translate";@@ -54,7 +55,7 @@ export const style = css`box-sizing: border-box;min-height: var(--internal-or-rules-header-height);height: var(--internal-or-rules-header-height);- z-index: 1;+ z-index: 2;padding: 15px 20px;--or-icon-fill: var(--internal-or-rules-panel-color);}@@ -179,10 +180,11 @@ export class OrRuleViewer extends translate(i18next)(LitElement) {<div id="main-wrapper" class="wrapper"><div id="rule-header"><or-input id="rule-name" outlined .type="${InputType.TEXT}" .label="${i18next.t("ruleName")}" ?focused="${this._focusName}" .value="${this.ruleset ? this.ruleset.name : null}" ?disabled="${this._isReadonly()}" required minlength="3" maxlength="255" @or-input-changed="${(e: OrInputChangedEvent) => this._changeName(e.detail.value)}"></or-input>+ <or-rule-validity id="rule-header-validity" .ruleset="${this.ruleset}"></or-rule-validity><div id="rule-header-controls"><span id="active-wrapper">- <or-translate value="active"></or-translate>+ <or-translate value="enabled"></or-translate><or-input .type="${InputType.SWITCH}" .value="${this.ruleset && this.ruleset.enabled}" ?disabled="${!this.ruleset.id}" @or-input-changed="${this._toggleEnabled}"></or-input></span>