Skip to content

armgit / core

Public Imported from GitHub
0

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

UI update: Fixed issue on checkbox_list event data changed and also in rule validity (#681)

Jérôme Vervier21d70d4ca9f9
diff --git a/ui/component/or-mwc-components/src/or-mwc-input.ts b/ui/component/or-mwc-components/src/or-mwc-input.ts
index d4e12554e..d0dc592d7 100644
--- a/ui/component/or-mwc-components/src/or-mwc-input.ts
+++ b/ui/component/or-mwc-components/src/or-mwc-input.ts
@@ -1132,7 +1132,9 @@ export class OrMwcInput extends LitElement {
@change="${(e: Event) => {
let val: any[] = this.value;
if ((e.target as HTMLInputElement).checked) {
- val = [optValue,...val];
+ if (!val.includes(optValue)) {
+ val = [optValue,...val];
+ }
} else {
val = val.filter((v: any) => v !== optValue);
}
@@ -1582,7 +1584,6 @@ export class OrMwcInput extends LitElement {
}
protected onValueChange(elem: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | undefined, newValue: any | undefined, enterPressed?: boolean) {
-
let previousValue = this.value;
let errorMsg: string | undefined;
@@ -1636,7 +1637,7 @@ export class OrMwcInput extends LitElement {
this.setCustomValidity(errorMsg);
this.reportValidity();
- if (newValue !== previousValue) {
+ if (this.type !== InputType.CHECKBOX_LIST && newValue !== previousValue) {
if (this.type === InputType.RANGE) {
(this._mdcComponent as MDCSlider).setValue(newValue);
if (this._mdcComponent2) {
@@ -1653,6 +1654,10 @@ export class OrMwcInput extends LitElement {
(searchableElement as HTMLInputElement).value = "";
}
}
+
+ if (this.type === InputType.CHECKBOX_LIST && !Util.objectsEqual(newValue, previousValue, true)) {
+ this.dispatchEvent(new OrInputChangedEvent(newValue, previousValue, enterPressed));
+ }
}
public get valid(): boolean {
diff --git a/ui/component/or-rules/src/or-rule-validity.ts b/ui/component/or-rules/src/or-rule-validity.ts
index b240c7fb9..077bc687f 100644
--- a/ui/component/or-rules/src/or-rule-validity.ts
+++ b/ui/component/or-rules/src/or-rule-validity.ts
@@ -44,6 +44,8 @@ export class OrRuleValidity extends translate(i18next)(LitElement) {
if (this._validity && this._validity.recurrence) {
this._rrule = RRule.fromString(this._validity.recurrence);
+ } else {
+ this._rrule = undefined;
}
}
}
@@ -73,7 +75,7 @@ export class OrRuleValidity extends translate(i18next)(LitElement) {
}
protected setRRuleValue(value: any, key: string) {
- const origOptions = this._rrule ? this._rrule.origOptions : undefined;
+ let origOptions = this._rrule ? this._rrule.origOptions : undefined;
const validity = this._validity!;
switch (key) {
@@ -113,12 +115,14 @@ export class OrRuleValidity extends translate(i18next)(LitElement) {
case "byweekday":
if (!origOptions!.byweekday) origOptions!.byweekday = [];
if (!Array.isArray(origOptions!.byweekday)) origOptions!.byweekday = [origOptions!.byweekday as ByWeekday];
- if (value.checked) {
- const weekDay = this.getWeekDay(value.name);
- if (weekDay) origOptions!.byweekday.push(weekDay);
- } else {
- origOptions!.byweekday = origOptions!.byweekday.filter((day) => day !== this.getWeekDay(value.name));
- }
+ const newDays: string[] = value;
+ origOptions!.byweekday = [];
+ newDays.forEach((d: any) => {
+ const weekDay = this.getWeekDay(d);
+ if (weekDay) {
+ (origOptions!.byweekday! as ByWeekday[]).push(weekDay);
+ }
+ });
if (this.getValidityType() === "validityRecurrence") this._rrule = new RRule(origOptions);
break;
case "until":
@@ -130,20 +134,32 @@ export class OrRuleValidity extends translate(i18next)(LitElement) {
break;
case "dtstart-time":
const timeParts = value.split(':');
- origOptions!.dtstart = moment(origOptions!.dtstart).set({hour:timeParts[0],minute:timeParts[1],second:0,millisecond:0}).toDate();
+ if (origOptions) {
+ origOptions!.dtstart = moment(origOptions.dtstart).set({hour:timeParts[0],minute:timeParts[1],second:0,millisecond:0}).toDate();
+ } else {
+ origOptions = new RRule({
+ dtstart: moment(this._validity!.start).set({hour:timeParts[0],minute:timeParts[1],second:0,millisecond:0}).toDate()
+ }).origOptions;
+ }
validity.start = moment(origOptions!.dtstart).toDate().getTime();
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(origOptions!.until).set({hour:untilParts[0],minute:untilParts[1],second:0,millisecond:0}).toDate()
+ if (this._rrule && this._rrule.options.until) {
+ if (origOptions) {
+ origOptions!.until = moment(origOptions.until).set({hour:untilParts[0],minute:untilParts[1],second:0,millisecond:0}).toDate();
+ } else {
+ origOptions = new RRule({
+ until: moment(this._validity!.end).set({hour:untilParts[0],minute:untilParts[1],second:0,millisecond:0}).toDate()
+ }).origOptions;
+ }
}
validity.end = moment(validity.end).set({hour:untilParts[0],minute:untilParts[1],second:0,millisecond:0}).toDate().getTime();
if(this.getValidityType() === "validityRecurrence") this._rrule = new RRule(origOptions);
break;
}
- this._validity = {...validity}
+ this._validity = {...validity};
this._dialog!.requestUpdate();
}
@@ -223,7 +239,6 @@ export class OrRuleValidity extends translate(i18next)(LitElement) {
}
protected showDialog() {
-
this._dialog = showDialog(new OrMwcDialog()
.setHeading(i18next.t("scheduleRuleActivity"))
.setStyles(html`
@@ -315,7 +330,13 @@ export class OrRuleValidity extends translate(i18next)(LitElement) {
${validityType === "validityRecurrence" ? html`
<label style="display: block; margin-top: 20px;"><or-translate value="repeatOccurrenceEvery"></or-translate></label>
<div class="layout horizontal">
- <or-mwc-input .value="${selectedOptions}" .type="${InputType.CHECKBOX_LIST}" .options="${options}" .label="${i18next.t("daysOfTheWeek")}" @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setRRuleValue(e.detail.value, "byweekday")}" ></or-mwc-input>
+ <or-mwc-input .value="${selectedOptions}"
+ .type="${InputType.CHECKBOX_LIST}"
+ .options="${options}"
+ .label="${i18next.t("daysOfTheWeek")}"
+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => {
+ this.setRRuleValue(e.detail.value, "byweekday");
+ }}" ></or-mwc-input>
</div>
<label style="display:block; margin-top: 20px;"><or-translate value="repetitionEnds"></or-translate></label>