From 81aab7ddeb000e5108d9ee6e052c1f05fd3267eb Mon Sep 17 00:00:00 2001 From: lolodomo Date: Thu, 22 Aug 2019 20:52:07 +0200 Subject: [PATCH] [BasicUI] mapview : handle refresh and UNDEF state (#101) Signed-off-by: Laurent Garnier --- bundles/org.openhab.ui.basic/.gitignore | 1 - .../internal/render/MapviewRenderer.java | 17 ++++-- .../src/main/resources/snippets/mapview.html | 4 +- .../resources/web/images/map-marker-off.png | Bin 0 -> 819 bytes .../org.openhab.ui.basic/web-src/smarthome.js | 54 +++++++++++++++++- 5 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 bundles/org.openhab.ui.basic/src/main/resources/web/images/map-marker-off.png diff --git a/bundles/org.openhab.ui.basic/.gitignore b/bundles/org.openhab.ui.basic/.gitignore index ea840ff26..4a1ea5b51 100644 --- a/bundles/org.openhab.ui.basic/.gitignore +++ b/bundles/org.openhab.ui.basic/.gitignore @@ -1,7 +1,6 @@ node_modules node npm_cache -web src/main/resources/web/smarthome.css src/main/resources/web/smarthome.js src/main/resources/web/fonts/* diff --git a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/MapviewRenderer.java b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/MapviewRenderer.java index c64148c8a..93209d1aa 100644 --- a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/MapviewRenderer.java +++ b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/MapviewRenderer.java @@ -37,6 +37,9 @@ import org.osgi.service.component.annotations.Reference; @Component(service = WidgetRenderer.class) public class MapviewRenderer extends AbstractWidgetRenderer { + private static final String MAP_URL = "//www.openstreetmap.org/export/embed.html?bbox=%lonminus%,%latminus%,%lonplus%,%latplus%&marker=%lat%,%lon%"; + private static final double MAP_ZOOM = 0.01; + @Override @Activate protected void activate(BundleContext bundleContext) { @@ -67,14 +70,20 @@ public class MapviewRenderer extends AbstractWidgetRenderer { PointType pointState = (PointType) state; double latitude = pointState.getLatitude().doubleValue(); double longitude = pointState.getLongitude().doubleValue(); + snippet = StringUtils.replace(snippet, "%url%", MAP_URL); snippet = StringUtils.replace(snippet, "%lat%", Double.toString(latitude)); snippet = StringUtils.replace(snippet, "%lon%", Double.toString(longitude)); - snippet = StringUtils.replace(snippet, "%lonminus%", Double.toString(longitude - 0.01)); - snippet = StringUtils.replace(snippet, "%lonplus%", Double.toString(longitude + 0.01)); - snippet = StringUtils.replace(snippet, "%latminus%", Double.toString(latitude - 0.01)); - snippet = StringUtils.replace(snippet, "%latplus%", Double.toString(latitude + 0.01)); + snippet = StringUtils.replace(snippet, "%lonminus%", Double.toString(longitude - MAP_ZOOM)); + snippet = StringUtils.replace(snippet, "%lonplus%", Double.toString(longitude + MAP_ZOOM)); + snippet = StringUtils.replace(snippet, "%latminus%", Double.toString(latitude - MAP_ZOOM)); + snippet = StringUtils.replace(snippet, "%latplus%", Double.toString(latitude + MAP_ZOOM)); + } else { + snippet = StringUtils.replace(snippet, "%url%", "images/map-marker-off.png"); } + snippet = StringUtils.replace(snippet, "%map_url%", MAP_URL); + snippet = StringUtils.replace(snippet, "%map_zoom%", Double.toString(MAP_ZOOM)); + int height = mapview.getHeight(); if (height == 0) { height = 4; // set default height to something viewable diff --git a/bundles/org.openhab.ui.basic/src/main/resources/snippets/mapview.html b/bundles/org.openhab.ui.basic/src/main/resources/snippets/mapview.html index 711de19d1..75dc6fb28 100644 --- a/bundles/org.openhab.ui.basic/src/main/resources/snippets/mapview.html +++ b/bundles/org.openhab.ui.basic/src/main/resources/snippets/mapview.html @@ -11,7 +11,9 @@ class="mdl-form__control mdl-form__webview" data-control-type="mapview" data-widget-id="%widget_id%" + data-map-url="%map_url%" + data-map-zoom="%map_zoom%" > - + diff --git a/bundles/org.openhab.ui.basic/src/main/resources/web/images/map-marker-off.png b/bundles/org.openhab.ui.basic/src/main/resources/web/images/map-marker-off.png new file mode 100644 index 0000000000000000000000000000000000000000..806c4837acc6954febf4f20697ca9b31f971a881 GIT binary patch literal 819 zcmeAS@N?(olHy`uVBq!ia0vp^Dj>|k1|%Oc%$NbBSkfJR9T^xl_H+M9WCijWi-X*q z7}lMWc?skwBzpw;GB8xBF)%c=FfjZA3N^f7U???UV0e|lz+eS5K)hhiu0R{01Y44~ zy9wy$!fk$L9koEv$x0Bg+K;yr7x;TbJ9DY0fympJD#BqB~b}{x_uF)EL zb#r85@7z#*_~^+Chd+XSlNL8O|97aHGNoJIQhVc(*2zK(H*DRqh0i#Dul4Tt>t~jm zr|vgxpPT+{rvLdjHs?P(x&C+gdcb4@XAEmVpSAvEOXr(~v8;?}U~dgTwG z1`Uu6#bxPMNr}a&x}`;#3~8A;saE>>`X#vq`nieenaSC@<-r*xRqn=_N1_g-0`)L> My85}Sb4q9e0Qpik$N&HU literal 0 HcmV?d00001 diff --git a/bundles/org.openhab.ui.basic/web-src/smarthome.js b/bundles/org.openhab.ui.basic/web-src/smarthome.js index 93a90746a..38c42218e 100644 --- a/bundles/org.openhab.ui.basic/web-src/smarthome.js +++ b/bundles/org.openhab.ui.basic/web-src/smarthome.js @@ -539,6 +539,56 @@ } } + /* class ControlMap */ + function ControlMap(parentNode) { + Control.call(this, parentNode); + + var + _t = this, + urlMarkerOffIcon = "images/map-marker-off.png", + urlNoneIcon = "images/none.png"; + + _t.iframe = parentNode.querySelector("iframe"); + _t.url = parentNode.getAttribute("data-map-url"); + _t.zoom = parseFloat(parentNode.getAttribute("data-map-zoom")); + + _t.setValuePrivate = function(value, itemState, visible) { + var + mapUrl = urlMarkerOffIcon, + splittedState, + lat, + lon, + val; + + if (!visible) { + mapUrl = urlNoneIcon; + } else if (itemState !== "UNDEF") { + splittedState = itemState.split(","); + lat = parseFloat(splittedState[0]); + lon = parseFloat(splittedState[1]); + mapUrl = _t.url.replace("%lat%", lat.toString()); + mapUrl = mapUrl.replace("%lon%", lon.toString()); + val = lon - _t.zoom; + mapUrl = mapUrl.replace("%lonminus%", val.toString()); + val = lon + _t.zoom; + mapUrl = mapUrl.replace("%lonplus%", val.toString()); + val = lat - _t.zoom; + mapUrl = mapUrl.replace("%latminus%", val.toString()); + val = lat + _t.zoom; + mapUrl = mapUrl.replace("%latplus%", val.toString()); + } + _t.iframe.setAttribute("src", mapUrl); + }; + + _t.destroy = function() { + var + mapParent = _t.iframe.parentNode; + + _t.iframe.setAttribute("src", urlNoneIcon); + mapParent.removeChild(_t.iframe); + }; + } + /* class ControlText extends Control */ function ControlText(parentNode) { Control.call(this, parentNode); @@ -1792,9 +1842,11 @@ case "colorpicker": appendControl(new ControlColorpicker(e)); break; + case "mapview": + appendControl(new ControlMap(e)); + break; case "video": case "webview": - case "mapview": appendControl(new Control(e)); break; default: