From 0c788b98986941b19f53943133d2dbe33664804e Mon Sep 17 00:00:00 2001
From: Piotr Pekala <piotrpawelpekala@gmail.com>
Date: Fri, 25 Oct 2019 05:19:30 -0700
Subject: [PATCH] Code cleaned up

---
 .../components/d3-map/d3-map.component.ts     |  7 +++--
 .../project-map/project-map.component.ts      | 31 +++++++++++++------
 src/app/services/mapsettings.service.ts       |  3 +-
 3 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/src/app/cartography/components/d3-map/d3-map.component.ts b/src/app/cartography/components/d3-map/d3-map.component.ts
index 9f799d2e..d0ed257e 100644
--- a/src/app/cartography/components/d3-map/d3-map.component.ts
+++ b/src/app/cartography/components/d3-map/d3-map.component.ts
@@ -33,6 +33,7 @@ import { ToolsService } from '../../../services/tools.service';
 import { TextEditorComponent } from '../text-editor/text-editor.component';
 import { MapScaleService } from '../../../services/mapScale.service';
 import { Project } from '../../../models/project';
+import { MapSettingsService } from '../../../services/mapsettings.service';
 
 @Component({
   selector: 'app-d3-map',
@@ -75,7 +76,8 @@ export class D3MapComponent implements OnInit, OnChanges, OnDestroy {
     protected movingToolWidget: MovingTool,
     public graphLayout: GraphLayout,
     private toolsService: ToolsService,
-    private mapScaleService: MapScaleService
+    private mapScaleService: MapScaleService,
+    private mapSettingsService: MapSettingsService
   ) {
     this.parentNativeElement = element.nativeElement;
   }
@@ -193,8 +195,7 @@ export class D3MapComponent implements OnInit, OnChanges, OnDestroy {
     this.graphLayout.draw(this.svg, this.context);
     this.textEditor.activateTextEditingForDrawings();
     this.textEditor.activateTextEditingForNodeLabels();
-
-    console.log("DONE!!!")
+    this.mapSettingsService.mapRenderedEmitter.emit(true);
   }
 
   @HostListener('window:resize', ['$event'])
diff --git a/src/app/components/project-map/project-map.component.ts b/src/app/components/project-map/project-map.component.ts
index 366338ed..b77716c9 100644
--- a/src/app/components/project-map/project-map.component.ts
+++ b/src/app/components/project-map/project-map.component.ts
@@ -95,6 +95,9 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
 
   protected settings: Settings;
   private inReadOnlyMode = false;
+  private scrollX: number = 0;
+  private scrollY: number = 0;
+  private scrollEnabled: boolean = false;
 
   @ViewChild(ContextMenuComponent, {static: false}) contextMenu: ContextMenuComponent;
   @ViewChild(D3MapComponent, {static: false}) mapChild: D3MapComponent;
@@ -200,6 +203,12 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
 
     this.subscriptions.push(routeSub);
 
+    this.subscriptions.push(
+      this.mapSettingsService.mapRenderedEmitter.subscribe((value: boolean) => {
+        if (this.scrollEnabled) this.centerCanvas();
+      })
+    );
+
     this.subscriptions.push(
       this.drawingsDataSource.changes.subscribe((drawings: Drawing[]) => {
         this.drawings = drawings;
@@ -522,21 +531,23 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
       this.mapScaleService.setScale(scale);
       this.project.scene_width = this.project.scene_width * scale;
       this.project.scene_height = this.project.scene_height * scale;
-      let scrollX: number = 0;
-      let scrollY: number = 0;
       if (heightToSceneHeightRatio < widthOfAreaToShow) {
-        scrollX = (minX * scale) - ((windowWidth - widthOfAreaToShow*scale)/2) + this.project.scene_width/2;
-        scrollY = (minY * scale) + this.project.scene_height/2;
+        this.scrollX = (minX * scale) - ((windowWidth - widthOfAreaToShow*scale)/2) + this.project.scene_width/2;
+        this.scrollY = (minY * scale) + this.project.scene_height/2;
       } else {
-        scrollX = (minX * scale) + this.project.scene_width/2;
-        scrollY = (minY * scale) - ((windowHeight - heightOfAreaToShow*scale)/2) + this.project.scene_height/2;
+        this.scrollX = (minX * scale) + this.project.scene_width/2;
+        this.scrollY = (minY * scale) - ((windowHeight - heightOfAreaToShow*scale)/2) + this.project.scene_height/2;
       }
-      setTimeout(function(){window.scrollTo(scrollX, scrollY)}, 100);     
     } else {
-      let scrollX: number = (minX * scale) + this.project.scene_width/2;
-      let scrollY: number = (minY * scale) + this.project.scene_height/2;
-      window.scrollTo(scrollX, scrollY);
+      this.scrollX = (minX * scale) + this.project.scene_width/2;
+      this.scrollY = (minY * scale) + this.project.scene_height/2;
     }
+    this.scrollEnabled = true;
+  }
+
+  public centerCanvas() {
+    window.scrollTo(this.scrollX, this.scrollY);
+    this.scrollEnabled = false;
   }
 
   public centerView() {
diff --git a/src/app/services/mapsettings.service.ts b/src/app/services/mapsettings.service.ts
index 2c14f21d..ca0f385b 100644
--- a/src/app/services/mapsettings.service.ts
+++ b/src/app/services/mapsettings.service.ts
@@ -1,4 +1,4 @@
-import { Injectable } from "@angular/core";
+import { Injectable, EventEmitter } from "@angular/core";
 import { Subject } from 'rxjs';
 
 @Injectable()
@@ -8,6 +8,7 @@ export class MapSettingsService {
     public isLogConsoleVisible: boolean = false;
     public isLayerNumberVisible: boolean = false;
     public interfaceLabels: Map<string, boolean> = new Map<string, boolean>();
+    public mapRenderedEmitter = new EventEmitter<boolean>();
 
     constructor() {
         this.isLayerNumberVisible = localStorage.getItem('layersVisibility') === 'true' ? true : false;