- ${this._error ? html`
${this._error}
` : ""}
-
+
${this._params.entry
? html`
@@ -221,74 +123,94 @@ class DialogZoneDetail extends LitElement {
`;
}
- private _location = memoizeOne(
- (
- lat: number,
- lng: number,
- radius: number,
- passive: boolean,
- icon: string
- ): MarkerLocation[] => {
- const computedStyles = getComputedStyle(this);
- const zoneRadiusColor = computedStyles.getPropertyValue("--accent-color");
- const passiveRadiusColor = computedStyles.getPropertyValue(
- "--secondary-text-color"
- );
- return [
+ private _schema = memoizeOne((icon?: string): HaFormSchema[] => [
+ {
+ name: "name",
+ required: true,
+ selector: {
+ text: {},
+ },
+ },
+ {
+ name: "icon",
+ required: false,
+ selector: {
+ icon: {},
+ },
+ },
+ {
+ name: "location",
+ required: true,
+ selector: { location: { radius: true, icon } },
+ },
+ {
+ name: "",
+ type: "grid",
+ schema: [
{
- id: "location",
- latitude: Number(lat),
- longitude: Number(lng),
- radius,
- radius_color: passive ? passiveRadiusColor : zoneRadiusColor,
- icon,
- location_editable: true,
- radius_editable: true,
+ name: "latitude",
+ required: true,
+ selector: { text: {} },
},
- ];
- }
- );
+ {
+ name: "longitude",
+ required: true,
- private _locationChanged(ev: CustomEvent) {
- [this._latitude, this._longitude] = ev.detail.location;
- }
+ selector: { text: {} },
+ },
+ ],
+ },
+ { name: "passive_note", type: "constant" },
+ { name: "passive", selector: { boolean: {} } },
+ {
+ name: "radius",
+ required: false,
+ selector: { number: { min: 0, max: 999999, mode: "box" } },
+ },
+ ]);
- private _radiusChanged(ev: CustomEvent) {
- this._radius = ev.detail.radius;
- }
-
- private _passiveChanged(ev) {
- this._passive = ev.target.checked;
- }
+ private _formData = memoizeOne((data: ZoneMutableParams) => ({
+ ...data,
+ location: {
+ latitude: data.latitude,
+ longitude: data.longitude,
+ radius: data.radius,
+ },
+ }));
private _valueChanged(ev: CustomEvent) {
- const configValue = (ev.target as any).configValue;
-
this._error = undefined;
- this[`_${configValue}`] = ev.detail.value;
+ const value = ev.detail.value;
+ if (
+ value.location.latitude !== this._data!.latitude ||
+ value.location.longitude !== this._data!.longitude ||
+ value.location.radius !== this._data!.radius
+ ) {
+ value.latitude = value.location.latitude;
+ value.longitude = value.location.longitude;
+ value.radius = Math.round(value.location.radius);
+ }
+ delete value.location;
+ if (!value.icon) {
+ delete value.icon;
+ }
+ this._data = value;
}
+ private _computeLabel = (entry: HaFormSchema): string =>
+ this.hass.localize(`ui.panel.config.zone.detail.${entry.name}`);
+
private async _updateEntry() {
this._submitting = true;
try {
- const values: ZoneMutableParams = {
- name: this._name.trim(),
- latitude: this._latitude,
- longitude: this._longitude,
- passive: this._passive,
- radius: this._radius,
- };
- if (this._icon) {
- values.icon = this._icon.trim();
- }
if (this._params!.entry) {
- await this._params!.updateEntry!(values);
+ await this._params!.updateEntry!(this._data!);
} else {
- await this._params!.createEntry(values);
+ await this._params!.createEntry(this._data!);
}
- this._params = undefined;
+ this.closeDialog();
} catch (err: any) {
- this._error = err ? err.message : "Unknown error";
+ this._error = { base: err ? err.message : "Unknown error" };
} finally {
this._submitting = false;
}
@@ -309,24 +231,18 @@ class DialogZoneDetail extends LitElement {
return [
haStyleDialog,
css`
- .location {
- display: flex;
+ ha-dialog {
+ --mdc-dialog-min-width: 600px;
}
- .location > * {
- flex-grow: 1;
- min-width: 0;
+ @media all and (max-width: 450px), all and (max-height: 500px) {
+ ha-dialog {
+ --mdc-dialog-min-width: calc(
+ 100vw - env(safe-area-inset-right) - env(safe-area-inset-left)
+ );
+ }
}
- .location > *:first-child {
- margin-right: 4px;
- }
- .location > *:last-child {
- margin-left: 4px;
- }
- ha-locations-editor {
- margin-top: 16px;
- }
- a {
- color: var(--primary-color);
+ ha-form.passive {
+ --zone-radius-color: var(--secondary-text-color);
}
`,
];