From 37df602ffba9e7110f1b202e8e40c1e715280dc6 Mon Sep 17 00:00:00 2001 From: Nathan Bijnens Date: Tue, 28 May 2024 20:22:33 +0200 Subject: [PATCH] Adding on #37, fixing #1, refactored Entity object (no longer multiple) enabling tap actions --- map-card.js | 173 +++++++++++++++++++--------------------------------- 1 file changed, 62 insertions(+), 111 deletions(-) diff --git a/map-card.js b/map-card.js index adb381b..522eb9b 100644 --- a/map-card.js +++ b/map-card.js @@ -289,15 +289,33 @@ class Entity { /** @type {[EntityHistory]} */ histories = []; + _currentState; + get id() { return this.config.id; } + get display() { + return this.config.display; + } + + constructor(config, latitude, longitude, icon, title, state, picture) { + this.config = config; + if(this.display == "state") { + title = state; + this._currentState = state; + } + this.marker = this._createMapMarker(latitude, longitude, icon, title, picture); + } + _markerCss(size) { return `style="height: ${size}px; width: ${size}px;"`; } _abbr(title) { + if(title.length < 5) { + return title; + } return title .split(" ") .map((part) => part[0]) @@ -325,6 +343,15 @@ class Entity { } update(map, latitude, longitude, state) { + if(this.display == "state") { + if(state != this._currentState) { + console.debug("Entity: updating marker for " + this.id + " from " + this._currentState + " to " + state); + this.marker.remove(); + this.marker = this._createMapMarker(latitude, longitude, null, state, null); + this.marker.addTo(map); + this._currentState = state; + } + } this.marker.setLatLng(this._latlong(latitude, longitude)); } @@ -340,30 +367,29 @@ class Entity { renderHistory() { return this.histories.map((entHist) => entHist.render()).flat(); } -} -class MarkerEntity extends Entity { - - constructor(config, latitude, longitude, icon, title, picture) { - super(); - this.config = config; - this.marker = this._createMapMarker(latitude, longitude, icon, title, picture); - } + _createMapMarker(latitude, longitude, icon, title, picture) { + console.debug("MarkerEntity: creating marker for " + this.id + " with display mode " + this.display); + if(this.display == "icon") { + picture = null; + } + if(this.display == "state") { + picture = null; + icon = null; + } - _createMapMarker(latitude, longitude, icon, title, entityPicture) { - console.debug("MarkerEntity: creating marker for " + this.id); const marker = L.marker(this._latlong(latitude, longitude), { icon: L.divIcon({ html: ` + > `, iconSize: [48, 48], className: "", @@ -372,77 +398,6 @@ class MarkerEntity extends Entity { }); return marker; } - -} - -class StateEntity extends Entity { - - _currentState; - - constructor(config, latitude, longitude, state) { - super(); - this.config = config; - this.marker = this._createMapMarker(latitude, longitude, state); - } - - _createMapMarker(latitude, longitude, state) { - console.debug("StateEntity: creating marker for " + this.id); - const marker = L.marker(this._latlong(latitude, longitude), { - icon: L.divIcon({ - html: ` - - `, - iconSize: [48, 48], - className: "", - }), - title: this.id, - }); - this._currentState = state; - return marker; - } - - update(map, latitude, longitude, state) { - if(state != this._currentState) { - console.debug("StateEntity: updating marker for " + this.id + " from " + this._currentState + " to " + state); - this.marker.remove(); - this.marker = this._createMapMarker(latitude, longitude, state); - this.marker.addTo(map); - } - super.update(map, latitude, longitude, state); - } - -} - -class IconEntity extends Entity { - - constructor(config, latitude, longitude, icon, title) { - super(); - this.config = config; - this.marker = this._createMapMarker(latitude, longitude, icon, title); - } - - _createMapMarker(latitude, longitude, icon, title) { - console.debug("IconEntity: creating marker for " + this.id); - let iconHtml = ""; - if(icon) { - iconHtml = `
icon
` - } else { - iconHtml = `
${this._abbr(title)}
` - } - const marker = L.marker(this._latlong(latitude, longitude), { - icon: L.divIcon({ - html: iconHtml, - iconSize: [this.config.size, this.config.size], - className: "", - }), - title: this.id, - }); - return marker; - } } class TimelineEntry { @@ -608,20 +563,10 @@ class MapCard extends LitElement { gps_accuracy: gpsAccuracy, friendly_name } = stateObj.attributes; - let entity = null; - switch(configEntity.display) { - case "icon": - entity = new IconEntity(configEntity, latitude, longitude, icon, friendly_name); - break; - case "state": - entity = new StateEntity(configEntity, latitude, longitude, hass.formatEntityState(stateObj)); - break; - case 'marker': - default: - let resolvedPicture = entityPicture ? this.hass.hassUrl(entityPicture) : null; - entity = new MarkerEntity(configEntity, latitude, longitude, icon, friendly_name, resolvedPicture); - break; - } + const state = hass.formatEntityState(stateObj); + const picture = entityPicture ? hass.hassUrl(entityPicture) : null; + + const entity = new Entity(configEntity, latitude, longitude, icon, friendly_name, state, picture); entity.marker.addTo(map); return entity; }); @@ -769,25 +714,21 @@ class MapCardEntityMarker extends LitElement { static get properties() { return { 'entityId': {type: String, attribute: 'entity-id'}, - 'entityName': {type: String, attribute: 'entity-name'}, - 'entityPicture': {type: String, attribute: 'entity-picture'}, - 'entityColor': {type: String, attribute: 'entity-color'} + 'title': {type: String, attribute: 'title'}, + 'picture': {type: String, attribute: 'picture'}, + 'icon': {type: String, attribute: 'icon'}, + 'color': {type: String, attribute: 'color'} }; } render() { return html`
- ${this.entityPicture - ? html`
` - : this.entityName} + ${this._inner()}
`; }; @@ -809,6 +750,16 @@ class MapCardEntityMarker extends LitElement { } } + _inner() { + if(this.picture) { + return html`
` + } + if(this.icon) { + return html`icon` + } + return this.title; + } + static get styles() { return css` .marker {