ha-frontend/panels/map/ha-panel-map.html

198 lines
5.4 KiB
HTML
Raw Normal View History

2015-09-21 08:12:58 +02:00
<link rel="import" href="../../bower_components/polymer/polymer.html">
2016-08-02 18:06:22 +02:00
2015-09-29 08:06:41 +02:00
<link rel='import' href='../../bower_components/iron-icon/iron-icon.html'>
2016-07-19 11:25:26 +02:00
2016-07-31 07:48:39 +02:00
<link rel="import" href="../../bower_components/app-layout/app-toolbar/app-toolbar.html">
2016-12-09 06:35:54 +01:00
<script src="../../bower_components/leaflet/dist/leaflet.js"></script>
2016-02-09 06:57:42 +01:00
<link rel="stylesheet" href="../../bower_components/leaflet/dist/leaflet.css" />
2016-07-31 07:48:39 +02:00
<link rel="import" href="../../src/components/ha-menu-button.html">
2016-07-19 11:25:26 +02:00
2016-07-17 08:19:49 +02:00
<link rel="import" href="./ha-entity-marker.html">
2015-09-21 08:12:58 +02:00
2016-07-17 08:19:49 +02:00
<dom-module id="ha-panel-map">
2015-09-21 08:12:58 +02:00
<template>
2016-07-31 07:48:39 +02:00
<style include="ha-style">
2016-12-09 06:35:54 +01:00
#map {
2016-07-31 07:48:39 +02:00
height: calc(100% - 64px);
width: 100%;
2016-12-09 06:35:54 +01:00
z-index: 0;
2016-07-18 08:18:48 +02:00
}
</style>
2016-07-31 07:48:39 +02:00
<app-toolbar>
<ha-menu-button narrow='[[narrow]]' show-menu='[[showMenu]]'></ha-menu-button>
<div main-title>Map</div>
</app-toolbar>
2016-12-09 06:35:54 +01:00
<div id='map'></div>
2015-09-21 08:12:58 +02:00
</template>
</dom-module>
2016-07-12 08:57:17 +02:00
<script>
window.L.Icon.Default.imagePath = '/static/images/leaflet';
Polymer({
2016-07-17 08:19:49 +02:00
is: 'ha-panel-map',
2016-07-12 08:57:17 +02:00
properties: {
hass: {
type: Object,
2016-12-09 06:35:54 +01:00
observer: 'drawEntities',
2016-07-12 08:57:17 +02:00
},
narrow: {
type: Boolean,
},
showMenu: {
type: Boolean,
value: false,
},
},
attached: function () {
2016-12-09 06:35:54 +01:00
var map = this._map = window.L.map(this.$.map);
map.setView([51.505, -0.09], 13);
window.L.tileLayer(
'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png',
{
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="https://cartodb.com/attributions">CartoDB</a>',
maxZoom: 18,
}
).addTo(map);
this.drawEntities(this.hass);
2016-12-09 06:35:54 +01:00
this.async(function () {
map.invalidateSize();
this.fitMap();
}.bind(this), 1);
},
fitMap: function () {
var bounds;
if (this._mapItems.length === 0) {
bounds = new window.L.latLngBounds(
2017-01-21 08:00:59 +01:00
[window.L.latLng(this.locationGPS.latitude, this.locationGPS.longitude)]);
} else {
bounds = new window.L.latLngBounds(
this._mapItems.map(function (item) { return item.getLatLng(); }));
}
2016-12-09 06:35:54 +01:00
this._map.fitBounds(bounds.pad(0.5));
},
drawEntities: function (hass) {
2016-12-09 06:35:54 +01:00
/* eslint-disable vars-on-top */
var map = this._map;
if (!map) return;
if (this._mapItems) {
this._mapItems.forEach(function (marker) { marker.remove(); });
2016-07-12 08:57:17 +02:00
}
2016-12-09 06:35:54 +01:00
var mapItems = this._mapItems = [];
Object.keys(hass.states).forEach(function (entityId) {
var entity = hass.states[entityId];
var title = window.hassUtil.computeStateName(entity);
if ((entity.attributes.hidden &&
window.hassUtil.computeDomain(entity) !== 'zone') ||
entity.state === 'home' ||
!('latitude' in entity.attributes) ||
!('longitude' in entity.attributes)) {
return;
}
2016-12-09 06:35:54 +01:00
var icon;
if (window.hassUtil.computeDomain(entity) === 'zone') {
2016-12-09 06:35:54 +01:00
// DRAW ZONE
if (entity.attributes.passive) return;
// create icon
var iconHTML = '';
if (entity.attributes.icon) {
iconHTML = (
"<iron-icon icon='" + entity.attributes.icon + "'></iron-icon>");
} else {
iconHTML = title;
2016-12-09 06:35:54 +01:00
}
icon = window.L.divIcon({
html: iconHTML,
iconSize: [24, 24],
className: '',
});
// create market with the icon
mapItems.push(window.L.marker(
[entity.attributes.latitude, entity.attributes.longitude],
{
icon,
interactive: false,
title: title,
2016-12-09 06:35:54 +01:00
}
).addTo(map));
// create circle around it
mapItems.push(window.L.circle(
[entity.attributes.latitude, entity.attributes.longitude],
{
interactive: false,
color: '#FF9800',
radius: entity.attributes.radius,
}
).addTo(map));
return;
}
// DRAW ENTITY
// create icon
var entityPicture = entity.attributes.entity_picture || '';
var entityName = title.split(' ').map(function (part) { return part.substr(0, 1); }).join('');
/* Leaflet clones this element before adding it to the map. This messes up
our Poylmer object and we can't pass data through. Thus we hack like this. */
2016-12-09 06:35:54 +01:00
icon = window.L.divIcon({
html: "<ha-entity-marker entity-id='" + entity.entity_id + "' entity-name='" + entityName + "' entity-picture='" + entityPicture + "'></ha-entity-marker>",
2016-12-09 06:35:54 +01:00
iconSize: [45, 45],
className: '',
});
// create market with the icon
mapItems.push(window.L.marker(
[entity.attributes.latitude, entity.attributes.longitude],
{
icon,
title: window.hassUtil.computeStateName(entity),
2016-12-09 06:35:54 +01:00
}
).addTo(map));
// create circle around if entity has accuracy
if (entity.attributes.gps_accuracy) {
mapItems.push(window.L.circle(
[entity.attributes.latitude, entity.attributes.longitude],
{
interactive: false,
color: '#0288D1',
radius: entity.attributes.gps_accuracy,
}
).addTo(map));
}
});
2016-07-12 08:57:17 +02:00
},
computeMenuButtonClass: function (narrow, showMenu) {
return !narrow && showMenu ? 'menu-icon invisible' : 'menu-icon';
},
toggleMenu: function () {
this.fire('hass-open-menu');
2016-07-12 08:57:17 +02:00
},
});
</script>