Skip to content

Commit

Permalink
Move default out of Map component and into own method.
Browse files Browse the repository at this point in the history
I don't think there is much added value of a full class here, as we only ever need to single entry point - can go for more class like style if u prefer.
  • Loading branch information
thybag committed Jun 19, 2024
1 parent 0048c1e commit df383cc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 36 deletions.
42 changes: 6 additions & 36 deletions src/components/MapCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import HaDateRangeService from "../services/HaDateRangeService.js"
import HaLinkedEntityService from "../services/HaLinkedEntityService.js"
import HaMapUtilities from "../util/HaMapUtilities.js"
import Logger from "../util/Logger.js"

import Entity from "../models/Entity.js"
import Layer from '../models/Layer.js';
import LayerWithHistory from '../models/LayerWithHistory.js';
import LayerConfig from '../configs/LayerConfig.js';
// Methods
import setInitialView from "../util/setInitialView.js"

export default class MapCard extends LitElement {
static get properties() {
Expand Down Expand Up @@ -245,15 +246,10 @@ export default class MapCard extends LitElement {
// Remove skipped entities.
.filter(v => v);

// If we have a focus entity or an X/Y - use those.
if (this._config.focusEntity || (this._config.x && this._config.y)){
this.map.setView(this._getLatLong(), this._config.zoom);
} else {
// If not, fit to view all markers.
const markerGroup = new L.FeatureGroup(renderedEntities.map((e) => e.marker)).addTo(this.map);
this.map.fitBounds(markerGroup.getBounds());
}

console.log(hass);
// Setup initial view based on config - or show all
setInitialView(map, renderedEntities, this._config, hass);

return renderedEntities;
}

Expand Down Expand Up @@ -337,32 +333,6 @@ export default class MapCard extends LitElement {
this.linkedEntityService?.disconnect();
}

/** @returns {[number, number]} latitude & longitude */
_getLatLong() {
if(Number.isFinite(this._config.x) && Number.isFinite(this._config.y)) {
return [this._config.x, this._config.y];
} else {
return this._getLatLongFromFocusedEntity();
}
}

/** @returns {[number, number]} latitude & longitude */
_getLatLongFromFocusedEntity() {
const entityId = this._config.focusEntity;
const entity = this.hass.states[entityId];

if (!entity) {
throw new Error(`Entity ${entityId} not found`);
}

// Get lat/lng (inc sub trackers)
let latLng = HaMapUtilities.getEntityLatLng(entityId, this.hass.states);
if (latLng) return latLng;

// Unable to find
throw new Error(`Entity ${entityId} has no longitude & latitude.`);
}

_isDarkMode() {
return (
this.themeMode === "dark" ||
Expand Down
67 changes: 67 additions & 0 deletions src/util/setInitialView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import HaMapUtilities from "./HaMapUtilities.js";

/**
* Get latLng based on configuration
* - Return specific x/y if set
* - Return x/y of focused entity if provided
* - Return null (default behavior)
*
* @param {[type]} config [description]
* @return {[type]} [description]
*/
function getConfiguredLatLong(config, hass) {

if (Number.isFinite(config.x) && Number.isFinite(config.y)) {
return [config.x, config.y];
}

if (config.focusEntity) {
return getFocusedEntityLatLng(config.focusEntity, hass)
}

// Default
return null;
}

/**
*
* @param string entityId
* @param {} hass
* @returns [number, number] latitude & longitude
*/
function getFocusedEntityLatLng(entityId, hass) {
const entity = hass.states[entityId];

if (!entity) {
throw new Error(`Entity ${entityId} not found`);
}

// Get lat/lng (inc sub trackers)
let latLng = HaMapUtilities.getEntityLatLng(entityId, hass.states);
if (latLng) return latLng;

// Unable to find
throw new Error(`Entity ${entityId} has no longitude & latitude.`);
}


/**
* setInitialView exported for use by Map component.
*
* @param L.Map map
* @param array entities
* @param MapConfig config
* @param {} hass
*/
export default function setInitialView(map, entities, config, hass)
{
const latLng = getConfiguredLatLong(config, hass);

if (latLng) {
return map.setView(latLng, config.zoom);
}

// If not, get bounds of all markers rendered
const markerGroup = new L.FeatureGroup(entities.map((e) => e.marker)).addTo(map);
map.fitBounds(markerGroup.getBounds());
}

0 comments on commit df383cc

Please sign in to comment.