From 7382c5347d97aaf5e0149c1409ffcff26cb40f4f Mon Sep 17 00:00:00 2001
From: Anthony Borghi <anthony.borghi@veremes.com>
Date: Wed, 7 Nov 2018 16:04:35 +0100
Subject: [PATCH] =?UTF-8?q?Correction=20Zoom=20et=20d=C3=A9formation=20d'?=
 =?UTF-8?q?=C3=A9tendue?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../javascript/app/vmap/map/mapcompare.js     | 106 +++++++++++-------
 1 file changed, 66 insertions(+), 40 deletions(-)

diff --git a/src/module_vmap/module/javascript/app/vmap/map/mapcompare.js b/src/module_vmap/module/javascript/app/vmap/map/mapcompare.js
index c894667b..f08edde3 100644
--- a/src/module_vmap/module/javascript/app/vmap/map/mapcompare.js
+++ b/src/module_vmap/module/javascript/app/vmap/map/mapcompare.js
@@ -355,6 +355,28 @@ nsVmap.MapCompare.prototype.getScale = function (opt_options) {
     return scale;
 };
 
+/**
+ * Set the map scale
+ * @param {number} scale
+ * @returns {number} new scale
+ * @export
+ */
+nsVmap.MapCompare.prototype.setScale = function (scale) {
+    oVmap.log('nsVmap.Map.prototype.setScale');
+
+    // calcule et va à l'échelle celon une règle de 3
+    var currentScale = this.getScale();
+    var currentResolution = this.oOpenLayersMap_.getView().getResolution();
+
+    var scaleResolution = scale * currentResolution / currentScale;
+    this.oOpenLayersMap_.getView().setResolution(scaleResolution);
+
+    // Ajuste l'échelle en augementant la résolution
+    currentScale = this.getScale();
+
+    oVmap.log(currentScale);
+};
+
 /**
  * Get the scale like (1:25,000)
  * @param {number} scale
@@ -434,11 +456,6 @@ nsVmap.MapCompare.prototype.mapCompareController = function ($scope, $window, $e
      * @private
      */
     this.targetOverlay_ = null;
-    /**
-     * @type {string}
-     * @private
-     */
-    this.isOnElement  = 'map';
 
     /**
      * Objet permettant de lancer des fonctions utiles
@@ -534,14 +551,15 @@ nsVmap.MapCompare.prototype.mapCompareController.prototype.startAnimation = func
 nsVmap.MapCompare.prototype.mapCompareController.prototype.synchronizeMap = function () {
     oVmap.log('nsVmap.MapCompare.prototype.mapCompareController.prototype.synchronizeMap');
     var this_ = this;
-    var isMouseDown = false;
     // blocage infinite loop
-    oVmap.getMap().getOLMap().getTargetElement().addEventListener('mousedown', function(){this_.isOnElement = 'map';isMouseDown = true;});
-    this_.map.getTargetElement().addEventListener('mousedown', function(){this_.isOnElement = 'mapCompare';isMouseDown = true;});
-    oVmap.getMap().getOLMap().getTargetElement().addEventListener('mouseup', function(){this_.isOnElement = 'map';isMouseDown = false;});
-    this_.map.getTargetElement().addEventListener('mouseup', function(){this_.isOnElement = 'mapCompare';isMouseDown = false;});
-    oVmap.getMap().getOLMap().getTargetElement().addEventListener('mouseenter', function(){if(!isMouseDown)this_.isOnElement = 'map';});
-    this_.map.getTargetElement().addEventListener('mouseenter', function(){if(!isMouseDown)this_.isOnElement = 'mapCompare';});
+    oVmap.getMap().getOLMap().getTargetElement().addEventListener('mouseenter', function(){
+        oVmap.getMap().getOLMap().getView().on("change:resolution", this_.updateCompareScaleMap, this_);
+        this_.map.getView().un("change:resolution", this_.updateScaleMap, this_);
+    });
+    this_.map.getTargetElement().addEventListener('mouseenter', function(){
+      oVmap.getMap().getOLMap().getView().un("change:resolution", this_.updateCompareScaleMap, this_);
+      this_.map.getView().on("change:resolution", this_.updateScaleMap, this_);
+    });
     // position du curseur sur l'autre carte
     oVmap.getMap().getOLMap().on('pointermove', function(e){
       var coord = e.coordinate;
@@ -602,51 +620,59 @@ nsVmap.MapCompare.prototype.mapCompareController.prototype.synchronizeMap = func
       this_.targetOverlay_.setPosition(convertCoord);
     });
     // synchro de la carte avec la carte de comparaison
-    oVmap.getMap().getOLMap().on("postrender", this_.updateCompareMap, this);
+    oVmap.getMap().getOLMap().on("pointerdrag", this_.updateCenterCompareMap, this_);
     // synchro de la carte de comapraison avec la carte de base
-    this_.map.on("postrender", this_.updateBasicMap, this);
+    this_.map.on("pointerdrag", this_.updateCenterMap, this_);
 };
 /**
  * Update the map extent with animation
  * @returns {undefined}
  */
-nsVmap.MapCompare.prototype.mapCompareController.prototype.updateBasicMap = function(e){
+nsVmap.MapCompare.prototype.mapCompareController.prototype.updateCompareScaleMap = function(e){
   var this_ = this
-  if(this_.isOnElement === 'mapCompare'){
-    // Récupère l'étendue de la carte principale
-    var oldExtent = this_.map.getView().calculateExtent(this_['map'].getSize());
-
-    // Récupère l'ancienne et la nouvelle projection
-    var newProjection = oVmap.getMap().getOLMap().getView().getProjection().getCode();
-    var oldProjection = this_.map.getView().getProjection().getCode();
+  this_.updateCenterCompareMap();
+  oVmap.getMapCompare().setScale(oVmap.getMap().getScale());
+}
+/**
+ * Update the map extent with animation
+ * @returns {undefined}
+ */
+nsVmap.MapCompare.prototype.mapCompareController.prototype.updateScaleMap = function(e){
+  var this_ = this
+  this_.updateCenterMap();
+  oVmap.getMap().setScale(oVmap.getMapCompare().getScale())
+}
+/**
+ * Update the map extent with animation
+ * @returns {undefined}
+ */
+nsVmap.MapCompare.prototype.mapCompareController.prototype.updateCenterMap = function(e){
+  var this_ = this
+  // Récupère l'étendue de la carte principale
+  var oldCenter = this_.map.getView().getCenter();
 
-    var newExtent = ol.proj.transformExtent(oldExtent, oldProjection, newProjection);
-    oVmap.getMap().getOLMap().getView().fit(newExtent, {
-        nearest: true
-    });
+  // Récupère l'ancienne et la nouvelle projection
+  var newProjection = oVmap.getMap().getOLMap().getView().getProjection().getCode();
+  var oldProjection = this_.map.getView().getProjection().getCode();
 
-  }
+  var newCenter = ol.proj.transform(oldCenter, oldProjection, newProjection);
+  oVmap.getMap().getOLMap().getView().setCenter(newCenter);
 }
 /**
  * Update the map extent with animation
  * @returns {undefined}
  */
-nsVmap.MapCompare.prototype.mapCompareController.prototype.updateCompareMap = function(e){
+nsVmap.MapCompare.prototype.mapCompareController.prototype.updateCenterCompareMap = function(e){
     var this_ = this;
-    if(this_.isOnElement === 'map'){
-      // Récupère l'étendue de la carte principale
-      var oldExtent = oVmap.getMap().getOLMap().getView().calculateExtent(oVmap.getMap().getOLMap().getSize());
-
-      // Récupère l'ancienne et la nouvelle projection
-      var oldProjection = oVmap.getMap().getOLMap().getView().getProjection().getCode();
-      var newProjection = this_.map.getView().getProjection().getCode();
+    // Récupère l'étendue de la carte principale
+    var oldCenter = oVmap.getMap().getOLMap().getView().getCenter();
 
-      var newExtent = ol.proj.transformExtent(oldExtent, oldProjection, newProjection);
-      this_['map'].getView().fit(newExtent, {
-          nearest: true
-      });
+    // Récupère l'ancienne et la nouvelle projection
+    var oldProjection = oVmap.getMap().getOLMap().getView().getProjection().getCode();
+    var newProjection = this_.map.getView().getProjection().getCode();
 
-    }
+    var newCenter = ol.proj.transform(oldCenter, oldProjection, newProjection);
+    this_['map'].getView().setCenter(newCenter);
 }
 
 /**
-- 
GitLab