Fix passive color radius and fix switch label clicks (#4703)

* Fix passive color radius and fix switch label clicks

* Colors vars
This commit is contained in:
Bram Kragten 2020-02-04 07:32:22 +01:00 committed by GitHub
parent ccc42dad79
commit 42cbe863bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 76 additions and 10 deletions

View File

@ -1,10 +1,18 @@
import { customElement, CSSResult, css, query, property } from "lit-element"; import {
customElement,
CSSResult,
css,
query,
html,
property,
} from "lit-element";
import "@material/mwc-switch"; import "@material/mwc-switch";
import { style } from "@material/mwc-switch/mwc-switch-css"; import { style } from "@material/mwc-switch/mwc-switch-css";
// tslint:disable-next-line // tslint:disable-next-line
import { Switch } from "@material/mwc-switch"; import { Switch } from "@material/mwc-switch";
import { Constructor } from "../types"; import { Constructor } from "../types";
import { forwardHaptic } from "../data/haptics"; import { forwardHaptic } from "../data/haptics";
import { ripple } from "@material/mwc-ripple/ripple-directive";
// tslint:disable-next-line // tslint:disable-next-line
const MwcSwitch = customElements.get("mwc-switch") as Constructor<Switch>; const MwcSwitch = customElements.get("mwc-switch") as Constructor<Switch>;
@ -26,7 +34,6 @@ export class HaSwitch extends MwcSwitch {
"slotted", "slotted",
Boolean(this._slot.assignedNodes().length) Boolean(this._slot.assignedNodes().length)
); );
this._slot.addEventListener("click", () => (this.checked = !this.checked));
this.addEventListener("change", () => { this.addEventListener("change", () => {
if (this.haptic) { if (this.haptic) {
forwardHaptic("light"); forwardHaptic("light");
@ -34,6 +41,31 @@ export class HaSwitch extends MwcSwitch {
}); });
} }
protected render() {
return html`
<div class="mdc-switch">
<div class="mdc-switch__track"></div>
<div
class="mdc-switch__thumb-underlay"
.ripple="${ripple({
interactionNode: this,
})}"
>
<div class="mdc-switch__thumb">
<input
type="checkbox"
id="basic-switch"
class="mdc-switch__native-control"
role="switch"
@change="${this._haChangeHandler}"
/>
</div>
</div>
</div>
<label for="basic-switch"><slot></slot></label>
`;
}
protected static get styles(): CSSResult[] { protected static get styles(): CSSResult[] {
return [ return [
style, style,
@ -65,6 +97,12 @@ export class HaSwitch extends MwcSwitch {
`, `,
]; ];
} }
private _haChangeHandler(e: Event) {
this.mdcFoundation.handleChange(e);
// catch "click" event and sync properties
this.checked = this.formElement.checked;
}
} }
declare global { declare global {

View File

@ -23,11 +23,13 @@ import {
} from "../../common/dom/setup-leaflet-map"; } from "../../common/dom/setup-leaflet-map";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import { nextRender } from "../../common/util/render-status"; import { nextRender } from "../../common/util/render-status";
import { defaultRadiusColor } from "../../data/zone";
@customElement("ha-location-editor") @customElement("ha-location-editor")
class LocationEditor extends LitElement { class LocationEditor extends LitElement {
@property() public location?: [number, number]; @property() public location?: [number, number];
@property() public radius?: number; @property() public radius?: number;
@property() public radiusColor?: string;
@property() public icon?: string; @property() public icon?: string;
public fitZoom = 16; public fitZoom = 16;
private _iconEl?: DivIcon; private _iconEl?: DivIcon;
@ -83,6 +85,9 @@ class LocationEditor extends LitElement {
if (changedProps.has("radius")) { if (changedProps.has("radius")) {
this._updateRadius(); this._updateRadius();
} }
if (changedProps.has("radiusColor")) {
this._updateRadiusColor();
}
if (changedProps.has("icon")) { if (changedProps.has("icon")) {
this._updateIcon(); this._updateIcon();
} }
@ -213,7 +218,7 @@ class LocationEditor extends LitElement {
this._leafletMap!.addLayer(this._locationMarker); this._leafletMap!.addLayer(this._locationMarker);
} else { } else {
this._locationMarker = this.Leaflet!.circle(this.location, { this._locationMarker = this.Leaflet!.circle(this.location, {
color: "#FF9800", color: this.radiusColor || defaultRadiusColor,
radius: this.radius, radius: this.radius,
}); });
this._leafletMap!.addLayer(this._locationMarker); this._leafletMap!.addLayer(this._locationMarker);
@ -228,6 +233,13 @@ class LocationEditor extends LitElement {
(this._locationMarker as Circle).setRadius(this.radius); (this._locationMarker as Circle).setRadius(this.radius);
} }
private _updateRadiusColor(): void {
if (!this._locationMarker || !this.radius) {
return;
}
(this._locationMarker as Circle).setStyle({ color: this.radiusColor });
}
static get styles(): CSSResult { static get styles(): CSSResult {
return css` return css`
:host { :host {

View File

@ -22,6 +22,7 @@ import {
LeafletModuleType, LeafletModuleType,
} from "../../common/dom/setup-leaflet-map"; } from "../../common/dom/setup-leaflet-map";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import { defaultRadiusColor } from "../../data/zone";
declare global { declare global {
// for fire event // for fire event
@ -202,7 +203,7 @@ export class HaLocationsEditor extends LitElement {
const circle = this.Leaflet!.circle( const circle = this.Leaflet!.circle(
[location.latitude, location.longitude], [location.latitude, location.longitude],
{ {
color: location.radius_color ? location.radius_color : "#FF9800", color: location.radius_color || defaultRadiusColor,
radius: location.radius, radius: location.radius,
} }
); );

View File

@ -1,5 +1,9 @@
import { HomeAssistant } from "../types"; import { HomeAssistant } from "../types";
export const defaultRadiusColor = "#FF9800";
export const homeRadiusColor: string = "#03a9f4";
export const passiveRadiusColor: string = "#9b9b9b";
export interface Zone { export interface Zone {
id: string; id: string;
name: string; name: string;

View File

@ -16,7 +16,11 @@ import "../../../components/ha-dialog";
import { ZoneDetailDialogParams } from "./show-dialog-zone-detail"; import { ZoneDetailDialogParams } from "./show-dialog-zone-detail";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { ZoneMutableParams } from "../../../data/zone"; import {
ZoneMutableParams,
passiveRadiusColor,
defaultRadiusColor,
} from "../../../data/zone";
import { addDistanceToCoord } from "../../../common/location/add_distance_to_coord"; import { addDistanceToCoord } from "../../../common/location/add_distance_to_coord";
class DialogZoneDetail extends LitElement { class DialogZoneDetail extends LitElement {
@ -127,6 +131,9 @@ class DialogZoneDetail extends LitElement {
class="flex" class="flex"
.location=${this._locationValue} .location=${this._locationValue}
.radius=${this._radius} .radius=${this._radius}
.radiusColor=${this._passive
? passiveRadiusColor
: defaultRadiusColor}
.icon=${this._icon} .icon=${this._icon}
@change=${this._locationChanged} @change=${this._locationChanged}
></ha-location-editor> ></ha-location-editor>

View File

@ -31,6 +31,9 @@ import {
updateZone, updateZone,
deleteZone, deleteZone,
ZoneMutableParams, ZoneMutableParams,
homeRadiusColor,
passiveRadiusColor,
defaultRadiusColor,
} from "../../../data/zone"; } from "../../../data/zone";
// tslint:disable-next-line // tslint:disable-next-line
import { import {
@ -68,17 +71,17 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
radius: state.attributes.radius, radius: state.attributes.radius,
radius_color: radius_color:
state.entity_id === "zone.home" state.entity_id === "zone.home"
? "#03a9f4" ? homeRadiusColor
: state.attributes.passive : state.attributes.passive
? "#9b9b9b" ? passiveRadiusColor
: "#FF9800", : defaultRadiusColor,
editable: false, editable: false,
}; };
}); });
const storageLocations: MarkerLocation[] = storageItems.map((zone) => { const storageLocations: MarkerLocation[] = storageItems.map((zone) => {
return { return {
...zone, ...zone,
radius_color: zone.passive ? "#9b9b9b" : "#FF9800", radius_color: zone.passive ? passiveRadiusColor : defaultRadiusColor,
editable: true, editable: true,
}; };
}); });

View File

@ -12,6 +12,7 @@ import { computeStateDomain } from "../../common/entity/compute_state_domain";
import { computeStateName } from "../../common/entity/compute_state_name"; import { computeStateName } from "../../common/entity/compute_state_name";
import LocalizeMixin from "../../mixins/localize-mixin"; import LocalizeMixin from "../../mixins/localize-mixin";
import { setupLeafletMap } from "../../common/dom/setup-leaflet-map"; import { setupLeafletMap } from "../../common/dom/setup-leaflet-map";
import { defaultRadiusColor } from "../../data/zone";
/* /*
* @appliesMixin LocalizeMixin * @appliesMixin LocalizeMixin
@ -175,7 +176,7 @@ class HaPanelMap extends LocalizeMixin(PolymerElement) {
[entity.attributes.latitude, entity.attributes.longitude], [entity.attributes.latitude, entity.attributes.longitude],
{ {
interactive: false, interactive: false,
color: "#FF9800", color: defaultRadiusColor,
radius: entity.attributes.radius, radius: entity.attributes.radius,
} }
).addTo(map) ).addTo(map)