From ab440de1a2b05dcef849f4f278bdb31b273f0e5e Mon Sep 17 00:00:00 2001
From: Per Qvarforth <per@qvarforth.se>
Date: Wed, 3 Aug 2022 10:54:54 +0000
Subject: [PATCH] focus on station after page reload

---
 htdocs/public/js/trackdirect.min.js |  4 ++--
 htdocs/public/views/overview.php    |  8 +++++---
 htdocs/public/views/raw.php         |  8 +++++---
 htdocs/public/views/statistics.php  | 10 ++++++----
 htdocs/public/views/telemetry.php   |  8 +++++---
 htdocs/public/views/trail.php       |  8 +++++---
 htdocs/public/views/weather.php     |  8 +++++---
 jslib/src/trackdirect.js            |  7 +++++--
 8 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/htdocs/public/js/trackdirect.min.js b/htdocs/public/js/trackdirect.min.js
index f341b01..f7129d9 100755
--- a/htdocs/public/js/trackdirect.min.js
+++ b/htdocs/public/js/trackdirect.min.js
@@ -5,7 +5,7 @@ me._setWebsocketStateIdle();if(inIframe()){var parentUrl="";try{parentUrl=window
 me._emitEventListeners("trackdirect-init-done");});this._mapInit(options);},enableImperialUnits:function(){this._map.state.useImperialUnit=true;if(this._map.state.openInfoWindow!==null){this._map.state.openInfoWindow.hide();}},enableMetricUnits:function(){this._map.state.useImperialUnit=false;if(this._map.state.openInfoWindow!==null){this._map.state.openInfoWindow.hide();}},toggleImperialUnits:function(){if(this._map.state.useImperialUnit){this.enableMetricUnits();}else{this.enableImperialUnits();}},isImperialUnits:function(){return this._map.state.useImperialUnit;},addListener:function(event,handler,execOnce){execOnce=typeof execOnce!=="undefined"?execOnce:false;if((event=="map-created"&&this._mapCreated)||(event=="trackdirect-init-done"&&this._trackdirectInitDone)){handler();if(execOnce){return;}}
 if(execOnce){if(!(event in this._eventListenersOnce)){this._eventListenersOnce[event]=[];}
 this._eventListenersOnce[event].push(handler);}else{if(!(event in this._eventListeners)){this._eventListeners[event]=[];}
-this._eventListeners[event].push(handler);}},setCenter:function(latitude,longitude,zoom){latitude=typeof latitude!=="undefined"?latitude:this._defaultLatitude;longitude=typeof longitude!=="undefined"?longitude:this._defaultLongitude;zoom=typeof zoom!=="undefined"?zoom:this.settings.defaultCurrentZoom;if(this._map!==null){this._map.setCenter({lat:latitude,lng:longitude},zoom);}},setZoom:function(value){if(this._map!==null){this._map.setZoom(value);}},addRuler:function(){if(this._rulers.length>0){var ruler=this._rulers.pop();ruler.hide();}else{var ruler=new trackdirect.models.Ruler((this._map.getCurrentRadiusInKm()*1000)/2,this._map);this._rulers.push(ruler);}},isFilteredMode:function(){return this._map.state.isFilterMode;},stopFilterOnStationId:function(stationId){var currentFilterStationIds=this._map.state.getFilterStationIds();if(currentFilterStationIds.length==1&&currentFilterStationIds.indexOf(stationId)>=0){this.filterOnStationId([]);}else{if(this._map.state.getTrackStationId()==stationId){this.stopTrackStation();}
+this._eventListeners[event].push(handler);}},setCenter:function(latitude,longitude,zoom){latitude=typeof latitude!=="undefined"?latitude:this._defaultLatitude;longitude=typeof longitude!=="undefined"?longitude:this._defaultLongitude;zoom=typeof zoom!=="undefined"?zoom:this._map.getZoom();if(this._map!==null){this._map.setCenter({lat:latitude,lng:longitude},zoom);}},setZoom:function(value){if(this._map!==null){this._map.setZoom(value);}},addRuler:function(){if(this._rulers.length>0){var ruler=this._rulers.pop();ruler.hide();}else{var ruler=new trackdirect.models.Ruler((this._map.getCurrentRadiusInKm()*1000)/2,this._map);this._rulers.push(ruler);}},isFilteredMode:function(){return this._map.state.isFilterMode;},stopFilterOnStationId:function(stationId){var currentFilterStationIds=this._map.state.getFilterStationIds();if(currentFilterStationIds.length==1&&currentFilterStationIds.indexOf(stationId)>=0){this.filterOnStationId([]);}else{if(this._map.state.getTrackStationId()==stationId){this.stopTrackStation();}
 this._setWebsocketStateLoading(false);this._websocket.doSendStopFilterRequest(stationId);}},filterOnStationId:function(stationIdArray){if(!Array.isArray(stationIdArray)){stationIdArray=[stationIdArray];}
 var currentFilterStationIds=this._map.state.getFilterStationIds();if(currentFilterStationIds.length==0){if(this._map.state.getTrackStationId()!==null&&stationIdArray.indexOf(this._map.state.getTrackStationId())==-1){this.stopTrackStation();}}
 this._setWebsocketStateLoading(false);this._websocket.doSendFilterRequest(stationIdArray,this._map.state.getTimeLength()/60,this.getTimeTravelTimestamp());},filterOnStationName:function(stationNameArray){if(!Array.isArray(stationNameArray)){stationNameArray=[stationNameArray];}
@@ -13,7 +13,7 @@ var currentFilterStationIds=this._map.state.getFilterStationIds();if(currentFilt
 this._setWebsocketStateLoading(false);this._websocket.doSendFilterRequestByName(stationNameArray,this._map.state.getTimeLength()/60,this.getTimeTravelTimestamp());},stopTrackStation:function(){this._map.state.onlyTrackRecentPackets=false;this._map.state.trackStationId=null;this._emitEventListeners("track-changed",[null,null]);},trackStation:function(stationId,stationName,alsoFilterOnStation,onlyTrackRecentPackets){onlyTrackRecentPackets=typeof onlyTrackRecentPackets!=="undefined"?onlyTrackRecentPackets:false;if(alsoFilterOnStation){var currentFilterStationIds=this._map.state.getFilterStationIds();if(currentFilterStationIds.length>0){if(currentFilterStationIds.indexOf(stationId)==-1){this.filterOnStationId(stationId);}}}
 if(stationId!==null){var trackLinkElementClass="trackStationLink"+stationId;$("."+trackLinkElementClass).html("Untrack");}
 if(this._map.state.trackStationId!==null){var trackLinkElementClass="trackStationLink"+this._map.state.trackStationId;$("."+trackLinkElementClass).html("Track");}
-this._map.state.onlyTrackRecentPackets=onlyTrackRecentPackets;this._map.state.trackStationId=stationId;this._emitEventListeners("track-changed",[stationId,stationName]);},focusOnStation:function(stationId,openInfoWindow){var map=this._map;openInfoWindow=typeof openInfoWindow!=="undefined"?openInfoWindow:false;var marker=map.markerCollection.getStationLatestMarker(stationId);if(marker!==null){marker.show();marker.showLabel();if(openInfoWindow){map.openMarkerInfoWindow(marker,false);}else{this.setCenter(marker.packet.latitude,marker.packet.longitude);}
+this._map.state.onlyTrackRecentPackets=onlyTrackRecentPackets;this._map.state.trackStationId=stationId;this._emitEventListeners("track-changed",[stationId,stationName]);},focusOnStation:function(stationId,openInfoWindow){var map=this._map;openInfoWindow=typeof openInfoWindow!=="undefined"?openInfoWindow:false;var marker=map.markerCollection.getStationLatestMarker(stationId);if(marker!==null){marker.show();marker.showLabel();if(openInfoWindow){map.openMarkerInfoWindow(marker,false);}else{this.setCenter(marker.packet.latitude,marker.packet.longitude,this.settings.defaultCurrentZoom);}
 marker.hide(5000,true);}},focusOnMarkerId:function(markerId,zoom){var map=this._map;var markerIdKey=map.markerCollection.getMarkerIdKey(markerId);if(map.markerCollection.isExistingMarker(markerIdKey)){var marker=map.markerCollection.getMarker(markerIdKey);if(map.markerCollection.hasRelatedDashedPolyline(marker)){newerMarker=map.markerCollection.getMarker(marker._relatedMarkerOriginDashedPolyLine.ownerMarkerIdKey);if(newerMarker.packet.hasConfirmedMapId()){return this.focusOnMarkerId(newerMarker.packet.marker_id);}}
 marker.show();marker.showLabel();this.setCenter(marker.packet.latitude,marker.packet.longitude,zoom);map.openMarkerInfoWindow(marker);marker.hide(5000,true);}},toggleStationCoverage:function(stationId,coverageLinkElementClass){coverageLinkElementClass=typeof coverageLinkElementClass!=="undefined"?coverageLinkElementClass:null;var coveragePolygon=this._map.markerCollection.getStationCoverage(stationId);if(coveragePolygon!==null&&coveragePolygon.isRequestedToBeVisible()){coveragePolygon.hide();if(coverageLinkElementClass!==null){$("."+coverageLinkElementClass).html("Coverage");}}else{if(coveragePolygon!==null){coveragePolygon.show();if(!coveragePolygon.hasContent()){alert("Currently we do not have enough data to create a max range coverage plot for this station. Try again later!");}else{if(coverageLinkElementClass!==null){$("."+coverageLinkElementClass).html("Hide coverage");}}}else{var packet=this._map.markerCollection.getStationLatestPacket(stationId);var center={lat:parseFloat(packet.latitude),lng:parseFloat(packet.longitude),};var coveragePolygon=new trackdirect.models.StationCoveragePolygon(center,this._map,true);this._map.markerCollection.addStationCoverage(stationId,coveragePolygon);coveragePolygon.showWhenDone();if(coverageLinkElementClass!==null){$("."+coverageLinkElementClass).html('Loading <i class="fa fa-spinner fa-spin" style="font-size:12px"></i>');coveragePolygon.addTdListener("visible",function(){if(!coveragePolygon.hasContent()){coveragePolygon.hide();alert("Currently we do not have enough data to create a max range coverage plot for this station. Try again later!");$("."+coverageLinkElementClass).html("Coverage");}else{$("."+coverageLinkElementClass).html("Hide coverage");}},true);}
 var me=this;$.getJSON(this.coverageDataUrl+"?id="+stationId,function(data){if("station_id"in data&&"coverage"in data){coveragePolygon.setData(data["coverage"],me.coveragePercentile);var marker=me._map.markerCollection.getStationLatestMarker(stationId);if(marker.isVisible()){if(coveragePolygon.isRequestedToBeVisible()){coveragePolygon.show();}}}}).fail(function(){coveragePolygon.hide();alert("Failed to fetch coverage data. Try again later!");$("."+coverageLinkElementClass).html("Coverage");}).always(function(){});}}},setMapType:function(mapType){if(this._map!==null){this._map.setMapType(mapType);}},getMapType:function(){if(this._map!==null){return this._map.getMapType();}},setMapDefaultLocation:function(setDefaultZoom){this._map.setMapDefaultLocation(setDefaultZoom);},setMapLocationByGeoLocation:function(failCallBack,successCallBack,timeout){var me=this;if(navigator&&navigator.geolocation){navigator.geolocation.getCurrentPosition(function(position){var pos={lat:position.coords.latitude,lng:position.coords.longitude,};me._map.setCenter(pos,12);if(successCallBack!==null){successCallBack();}},function(error){if(failCallBack!==null){failCallBack(error.message);}},{enableHighAccuracy:false,timeout:timeout,maximumAge:5000,});}else{if(failCallBack!==null){failCallBack();}}},openStationInformationDialog:function(stationId){var packet=this._map.markerCollection.getStationLatestPacket(stationId);if(packet==null){packet={station_id:stationId,id:null};}
diff --git a/htdocs/public/views/overview.php b/htdocs/public/views/overview.php
index 51f76f9..d52b097 100644
--- a/htdocs/public/views/overview.php
+++ b/htdocs/public/views/overview.php
@@ -535,10 +535,12 @@
                 $('#latest-timestamp-age').html(moment(new Date(1000 * $('#latest-timestamp-age').html())).locale('en').fromNow());
             }
 
-            if (window.parent && window.parent.trackdirect) {
+            if (window.trackdirect) {
                 <?php if ($station->latestConfirmedLatitude != null && $station->latestConfirmedLongitude != null) : ?>
-                    window.parent.trackdirect.addListener("map-created", function() {
-                        window.parent.trackdirect.focusOnStation(<?php echo $station->id ?>, true);
+                    window.trackdirect.addListener("map-created", function() {
+                        if (!window.trackdirect.focusOnStation(<?php echo $station->id ?>, true)) {
+                            window.trackdirect.setCenter(<?php echo $station->latestConfirmedLatitude ?>, <?php echo $station->latestConfirmedLongitude ?>);
+                        }
                     });
                 <?php endif; ?>
             }
diff --git a/htdocs/public/views/raw.php b/htdocs/public/views/raw.php
index e1e4b3f..9e776ba 100644
--- a/htdocs/public/views/raw.php
+++ b/htdocs/public/views/raw.php
@@ -496,10 +496,12 @@
                 loadView("/views/raw.php?id=<?php echo $station->id ?>&type=" + $('#raw-type').val() + "&category=" + $('#raw-category').val() + "&rows=" + $('#raw-rows').val() + "&page=1");
             });
 
-            if (window.parent && window.parent.trackdirect) {
+            if (window.trackdirect) {
                 <?php if ($station->latestConfirmedLatitude != null && $station->latestConfirmedLongitude != null) : ?>
-                    window.parent.trackdirect.addListener("map-created", function() {
-                        window.parent.trackdirect.focusOnStation(<?php echo $station->id ?>, true);
+                    window.trackdirect.addListener("map-created", function() {
+                        if (!window.trackdirect.focusOnStation(<?php echo $station->id ?>, true)) {
+                            window.trackdirect.setCenter(<?php echo $station->latestConfirmedLatitude ?>, <?php echo $station->latestConfirmedLongitude ?>);
+                        }
                     });
                 <?php endif; ?>
             }
diff --git a/htdocs/public/views/statistics.php b/htdocs/public/views/statistics.php
index 8929798..38903af 100644
--- a/htdocs/public/views/statistics.php
+++ b/htdocs/public/views/statistics.php
@@ -135,12 +135,14 @@
                 if ($(this).html().trim() != '' && !isNaN($(this).html().trim())) {
                     $(this).html(moment(new Date(1000 * $(this).html())).format('L LTSZ'));
                 }
-            });
+	    });
 
-            if (window.parent && window.parent.trackdirect) {
+            if (window.trackdirect) {
                 <?php if ($station->latestConfirmedLatitude != null && $station->latestConfirmedLongitude != null) : ?>
-                    window.parent.trackdirect.addListener("map-created", function() {
-                        window.parent.trackdirect.focusOnStation(<?php echo $station->id ?>, true);
+                    window.trackdirect.addListener("map-created", function() {
+                        if (!window.trackdirect.focusOnStation(<?php echo $station->id ?>, true)) {
+                            window.trackdirect.setCenter(<?php echo $station->latestConfirmedLatitude ?>, <?php echo $station->latestConfirmedLongitude ?>);
+                        }
                     });
                 <?php endif; ?>
             }
diff --git a/htdocs/public/views/telemetry.php b/htdocs/public/views/telemetry.php
index 1d84911..a425ae0 100644
--- a/htdocs/public/views/telemetry.php
+++ b/htdocs/public/views/telemetry.php
@@ -273,10 +273,12 @@
                 loadView("/views/telemetry.php?id=<?php echo $station->id ?>&category=" + $('#telemetry-category').val() + "&rows=" + $('#telemetry-rows').val() + "&page=1");
             });
 
-            if (window.parent && window.parent.trackdirect) {
+            if (window.trackdirect) {
                 <?php if ($station->latestConfirmedLatitude != null && $station->latestConfirmedLongitude != null) : ?>
-                    window.parent.trackdirect.addListener("map-created", function() {
-                        window.parent.trackdirect.focusOnStation(<?php echo $station->id ?>, true);
+                    window.trackdirect.addListener("map-created", function() {
+                        if (!window.trackdirect.focusOnStation(<?php echo $station->id ?>, true)) {
+                            window.trackdirect.setCenter(<?php echo $station->latestConfirmedLatitude ?>, <?php echo $station->latestConfirmedLongitude ?>);
+                        }
                     });
                 <?php endif; ?>
             }
diff --git a/htdocs/public/views/trail.php b/htdocs/public/views/trail.php
index eff8b85..5f13f2e 100644
--- a/htdocs/public/views/trail.php
+++ b/htdocs/public/views/trail.php
@@ -52,10 +52,12 @@
                 }
             });
 
-            if (window.parent && window.parent.trackdirect) {
+            if (window.trackdirect) {
                 <?php if ($station->latestConfirmedLatitude != null && $station->latestConfirmedLongitude != null) : ?>
-                    window.parent.trackdirect.addListener("map-created", function() {
-                        window.parent.trackdirect.focusOnStation(<?php echo $station->id ?>, true);
+                    window.trackdirect.addListener("map-created", function() {
+                        if (!window.trackdirect.focusOnStation(<?php echo $station->id ?>, true)) {
+                            window.trackdirect.setCenter(<?php echo $station->latestConfirmedLatitude ?>, <?php echo $station->latestConfirmedLongitude ?>);
+                        }
                     });
                 <?php endif; ?>
             }
diff --git a/htdocs/public/views/weather.php b/htdocs/public/views/weather.php
index 127bcaa..5103cee 100644
--- a/htdocs/public/views/weather.php
+++ b/htdocs/public/views/weather.php
@@ -225,10 +225,12 @@
             });
 
 
-            if (window.parent && window.parent.trackdirect) {
+            if (window.trackdirect) {
                 <?php if ($station->latestConfirmedLatitude != null && $station->latestConfirmedLongitude != null) : ?>
-                    window.parent.trackdirect.addListener("map-created", function() {
-                        window.parent.trackdirect.focusOnStation(<?php echo $station->id ?>, true);
+                    window.trackdirect.addListener("map-created", function() {
+                        if (!window.trackdirect.focusOnStation(<?php echo $station->id ?>, true)) {
+                            window.trackdirect.setCenter(<?php echo $station->latestConfirmedLatitude ?>, <?php echo $station->latestConfirmedLongitude ?>);
+                        }
                     });
                 <?php endif; ?>
             }
diff --git a/jslib/src/trackdirect.js b/jslib/src/trackdirect.js
index cab2f1b..4c7f33f 100755
--- a/jslib/src/trackdirect.js
+++ b/jslib/src/trackdirect.js
@@ -191,7 +191,7 @@ var trackdirect = {
     longitude =
       typeof longitude !== "undefined" ? longitude : this._defaultLongitude;
     zoom =
-      typeof zoom !== "undefined" ? zoom : this.settings.defaultCurrentZoom;
+      typeof zoom !== "undefined" ? zoom : this._map.getZoom();
 
     if (this._map !== null) {
       this._map.setCenter({ lat: latitude, lng: longitude }, zoom);
@@ -370,7 +370,7 @@ var trackdirect = {
    * Move focus to specified station
    * @param {int} stationId
    * @param {boolean} openInfoWindow
-   * @return None
+   * @return Boolean
    */
   focusOnStation: function (stationId, openInfoWindow) {
     var map = this._map;
@@ -390,6 +390,9 @@ var trackdirect = {
 
       // This method will hide marker when infowindow is closed, if nessecery
       marker.hide(5000, true);
+      return true;
+    } else {
+      return false;
     }
   },
 
-- 
GitLab