From e0a07694948aa5518e5fb0ff65e27eb5e781eb76 Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Wed, 20 Sep 2017 15:47:31 +0200
Subject: [PATCH] [fix] Map: no more use view cache, but instead save/store
 options (center, etc.) to/from MapUtils

---
 www/js/entities/peer.js                       |  2 +
 www/js/services/http-services.js              |  4 +-
 .../map/js/controllers/network-controllers.js | 46 +++++++++++--------
 .../map/js/controllers/wot-controllers.js     | 14 +++++-
 www/plugins/map/js/services/utils-services.js | 27 ++++++++++-
 5 files changed, 70 insertions(+), 23 deletions(-)

diff --git a/www/js/entities/peer.js b/www/js/entities/peer.js
index 6ea32b1b3..198a796e4 100644
--- a/www/js/entities/peer.js
+++ b/www/js/entities/peer.js
@@ -16,8 +16,10 @@ function Peer(json) {
 Peer.prototype.regex = {
   BMA: /^BASIC_MERKLED_API[ ]?/,
   BMAS: /^BMAS[ ]?/,
+  WS2P: /^WS2P[ ]?/,
   BMA_REGEXP: /^BASIC_MERKLED_API([ ]+([a-z_][a-z0-9-_.]*))?([ ]+([0-9.]+))?([ ]+([0-9a-f:]+))?([ ]+([0-9]+))$/,
   BMAS_REGEXP: /^BMAS([ ]+([a-z_][a-z0-9-_.]*))?([ ]+([0-9.]+))?([ ]+([0-9a-f:]+))?([ ]+([0-9]+))$/,
+  WS2P_REGEXP: /^WS2P[ ]+([a-z0-9]+)([ ]+([a-z_][a-z0-9-_.]*))?([ ]+([0-9.]+))?([ ]+([0-9a-f:]+))?([ ]+([0-9]+))$/,
   LOCAL_IP_ADDRESS: /^127[.]0[.]0.|192[.]168[.]|10[.]0[.]0[.]|172[.]16[.]/
 };
 
diff --git a/www/js/services/http-services.js b/www/js/services/http-services.js
index 0d2109b24..fdc299876 100644
--- a/www/js/services/http-services.js
+++ b/www/js/services/http-services.js
@@ -162,9 +162,9 @@ angular.module('cesium.http.services', ['cesium.cache.services'])
         return $q.when(delegate);
       }
       if (delegate.readyState == 3) {
-        return $q.reject('Unable to connect to Websocket ['+delegate.url+']');
+        return $q.reject('Unable to connect to websocket ['+delegate.url+']');
       }
-      console.debug('[http] Waiting websocket ['+path+'] openning...');
+      console.debug('[http] Waiting websocket ['+path+'] opening...');
       return $timeout(_waitOpen, 200);
     }
 
diff --git a/www/plugins/map/js/controllers/network-controllers.js b/www/plugins/map/js/controllers/network-controllers.js
index 8016ab9e3..dd3d3125d 100644
--- a/www/plugins/map/js/controllers/network-controllers.js
+++ b/www/plugins/map/js/controllers/network-controllers.js
@@ -26,6 +26,7 @@ angular.module('cesium.map.network.controllers', ['cesium.services', 'cesium.map
               controller: 'MapNetworkViewCtrl'
             }
           },
+          cache: false,
           data: {
             silentLocationChange: true
           }
@@ -82,25 +83,26 @@ angular.module('cesium.map.network.controllers', ['cesium.services', 'cesium.map
     $scope.helptipPrefix = 'helptip-' + $scope.mapId; // make to override, to avoid error during help tour
 
     $scope.map = MapUtils.map({
-        layers: {
-          overlays: {
-            member: {
-              type: 'featureGroup',
-              name: 'MAP.NETWORK.VIEW.LAYER.MEMBER',
-              visible: true
-            },
-            mirror: {
-              type: 'featureGroup',
-              name: 'MAP.NETWORK.VIEW.LAYER.MIRROR',
-              visible: true
-            },
-            offline: {
-              type: 'featureGroup',
-              name: 'MAP.NETWORK.VIEW.LAYER.OFFLINE',
-              visible: true
-            }
+      cache: 'map-network',
+      layers: {
+        overlays: {
+          member: {
+            type: 'featureGroup',
+            name: 'MAP.NETWORK.VIEW.LAYER.MEMBER',
+            visible: true
+          },
+          mirror: {
+            type: 'featureGroup',
+            name: 'MAP.NETWORK.VIEW.LAYER.MIRROR',
+            visible: true
+          },
+          offline: {
+            type: 'featureGroup',
+            name: 'MAP.NETWORK.VIEW.LAYER.OFFLINE',
+            visible: true
           }
-        },
+        }
+      },
       loading: true,
         markers: {}
       });
@@ -130,6 +132,14 @@ angular.module('cesium.map.network.controllers', ['cesium.services', 'cesium.map
     };
     $scope.$on('$ionicView.enter', $scope.enter);
 
+    // View leave: store map options (center) to cache
+    $scope.leave = function() {
+      if ($scope.map.cache) {
+        MapUtils.cache.save($scope.map);
+      }
+    };
+    $scope.$on('$ionicView.leave', $scope.leave);
+
     var inheritedComputeOptions = $scope.computeOptions;
     $scope.computeOptions = function() {
       var options = inheritedComputeOptions();
diff --git a/www/plugins/map/js/controllers/wot-controllers.js b/www/plugins/map/js/controllers/wot-controllers.js
index 4e17f1355..4158af555 100644
--- a/www/plugins/map/js/controllers/wot-controllers.js
+++ b/www/plugins/map/js/controllers/wot-controllers.js
@@ -26,6 +26,7 @@ angular.module('cesium.map.wot.controllers', ['cesium.services', 'cesium.map.ser
               controller: 'MapWotViewCtrl'
             }
           },
+          cache: false,
           data: {
             silentLocationChange: true
           }
@@ -73,6 +74,7 @@ angular.module('cesium.map.wot.controllers', ['cesium.services', 'cesium.map.ser
     $scope.loading = true;
     $scope.mapId = 'map-wot-' + $scope.$id;
     $scope.map = MapUtils.map({
+      cache: 'map-wot',
       layers: {
         overlays: {
           member: {
@@ -95,7 +97,7 @@ angular.module('cesium.map.wot.controllers', ['cesium.services', 'cesium.map.ser
       bounds: {},
       markers: {},
       loading: true
-    });
+    }, $scope.mapId);
 
     // [NEW] When opening the view
     $scope.enter = function(e, state) {
@@ -103,7 +105,7 @@ angular.module('cesium.map.wot.controllers', ['cesium.services', 'cesium.map.ser
 
         // Load the map (and init if need)
         $scope.loadMap()
-          .then($scope.load);
+          .then($scope.load)
       }
       else {
         // Make sur to have previous center coordinate defined in the location URL
@@ -112,6 +114,14 @@ angular.module('cesium.map.wot.controllers', ['cesium.services', 'cesium.map.ser
     };
     $scope.$on('$ionicView.enter', $scope.enter);
 
+    // View leave: store map options (center) to cache
+    $scope.leave = function() {
+      if ($scope.map.cache) {
+        MapUtils.cache.save($scope.map);
+      }
+    };
+    $scope.$on('$ionicView.leave', $scope.leave);
+
     $scope.loadMap = function() {
       return leafletData.getMap($scope.mapId).then(function(map) {
         if (!$scope.map.loading) return map; // already loaded
diff --git a/www/plugins/map/js/services/utils-services.js b/www/plugins/map/js/services/utils-services.js
index 9598d691b..8e055e698 100644
--- a/www/plugins/map/js/services/utils-services.js
+++ b/www/plugins/map/js/services/utils-services.js
@@ -6,7 +6,7 @@ angular.module('cesium.map.utils.services', ['cesium.services', 'ui-leaflet'])
 
 
   var
-    googleApiKey = csConfig.plugins && csConfig.plugins.es && csConfig.plugins.es.googleApiKey;
+    googleApiKey = csConfig.plugins && csConfig.plugins.es && csConfig.plugins.es.googleApiKey,
     constants = {
       locations: {
         FRANCE: {
@@ -14,12 +14,16 @@ angular.module('cesium.map.utils.services', ['cesium.services', 'ui-leaflet'])
         }
       },
       LOCALIZE_ZOOM: 15
+    },
+    data = {
+      cache: {}
     };
   constants.DEFAULT_CENTER = csSettings.data && csSettings.data.plugins && csSettings.data.plugins.map && csSettings.data.plugins.map.center || constants.locations.FRANCE;
 
   function initMap(options){
     options = angular.merge({
       center: angular.copy(constants.DEFAULT_CENTER),
+      cache: false,
       defaults: {
         scrollWheelZoom: true
       },
@@ -52,6 +56,12 @@ angular.module('cesium.map.utils.services', ['cesium.services', 'ui-leaflet'])
       }
     }, options || {});
 
+    // Restore existing map options
+    if (options.cache && data.cache[options.cache]) {
+      console.debug("Restoring cache :", data.cache[options.cache]);
+      options = angular.merge(options, data.cache[options.cache]);
+    }
+
     // Translate overlays name, if any
     var overlaysNames;
     if (options.layers.overlays) {
@@ -115,6 +125,18 @@ angular.module('cesium.map.utils.services', ['cesium.services', 'ui-leaflet'])
     return false;
   }
 
+  function saveMapOptions(options) {
+    if (options.cache) {
+      data.cache[options.cache] = {
+        center: options.center,
+        bounds: options.bounds,
+        layers: {
+          baselayers: options.layers.baselayers
+        }
+      };
+    }
+  }
+
   // Create a default serach control, with default options
   function initSearchControl(options) {
 
@@ -167,6 +189,9 @@ angular.module('cesium.map.utils.services', ['cesium.services', 'ui-leaflet'])
       search: initSearchControl,
       localizeMe: initLocalizeMeControl
     },
+    cache: {
+      save: saveMapOptions
+    },
     constants: constants
   };
 
-- 
GitLab