From b196154f828d12081a8f3ddb7668f04aeffc1842 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Fri, 17 May 2019 17:31:11 +0200
Subject: [PATCH] Fix #201 : barre de progression lors du changement de route

---
 src/app/app.component.html |  3 +++
 src/app/app.component.scss |  7 +++++++
 src/app/app.component.ts   | 29 ++++++++++++++++++++++++++++-
 src/app/app.module.ts      |  2 ++
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/src/app/app.component.html b/src/app/app.component.html
index 880492fa2..4c520d3c7 100644
--- a/src/app/app.component.html
+++ b/src/app/app.component.html
@@ -57,6 +57,9 @@
       <mat-icon>add_box</mat-icon>
     </button>
 
+    <mat-progress-bar *ngIf="showProgressBar" color="accent" id="loading-progress-bar"
+      [mode]="progressBarMode" [value]="progessBarValue">
+    </mat-progress-bar>
     <div id="toolbar-bottom-spacer"></div>
   </mat-toolbar>
 
diff --git a/src/app/app.component.scss b/src/app/app.component.scss
index 6cf6170ef..0a4cebf95 100644
--- a/src/app/app.component.scss
+++ b/src/app/app.component.scss
@@ -24,6 +24,13 @@ button:focus {
 #toolbar-bottom-spacer {
     height: 3px;
     background-color: rgb(250, 250, 250);
+    z-index: 190;
+    position: absolute;
+    top: $navbar_height;
+    width: 100%;
+}
+
+#loading-progress-bar {
     z-index: 200;
     position: absolute;
     top: $navbar_height;
diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 429eb28ce..ae8842a56 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -1,5 +1,5 @@
 import { Component, ApplicationRef, OnInit, OnDestroy, HostListener, ViewChild, ComponentRef } from "@angular/core";
-import { Router, Event, NavigationEnd, ActivationEnd } from "@angular/router";
+import { Router, Event, NavigationEnd, ActivationEnd, NavigationStart, NavigationCancel, NavigationError } from "@angular/router";
 import { MatSidenav, MatToolbar, MatDialog, MatIconRegistry } from "@angular/material";
 import { DomSanitizer } from "@angular/platform-browser";
 
@@ -40,6 +40,15 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
   /** current calculator, inferred from _currentFormId by setActiveCalc() (used for navbar menu) */
   public currentCalc: any;
 
+  /** shows or hides the progressbar under the navbar */
+  public showProgressBar = false;
+
+  /** if true, progress bar will be in "determinate" mode, else in "indeterminate" mode */
+  public progressBarDeterminate = true;
+
+  /** progress bar percentage, for "determinate" mode */
+  public progessBarValue = 0;
+
   /**
    * liste des modules de calcul. Forme des objets :
    * "title": nom du module de calcul
@@ -85,10 +94,15 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
     ServiceFactory.instance.notificationsService = notificationsService;
 
     this.router.events.subscribe((event: Event) => {
+      // show loading bar when changing route
+      if (event instanceof NavigationStart) {
+        this.showLoading(true);
+      }
       // close side navigation when clicking a calculator tab
       if (event instanceof NavigationEnd) {
         this.sidenav.close();
         window.scrollTo(0, 0);
+        this.showLoading(false);
       }
       // [de]activate calc tabs depending on loaded route
       if (event instanceof ActivationEnd) {
@@ -105,6 +119,10 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
           this.setActiveCalc(null);
         }
       }
+      // hide loading bar on routing errors
+      if (event instanceof NavigationCancel || event instanceof NavigationError) {
+        this.showLoading(false);
+      }
     });
 
     // icônes personnalisées
@@ -184,6 +202,10 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
     return this.router.url;
   }
 
+  public get progressBarMode() {
+    return this.progressBarDeterminate ? "determinate" : "indeterminate";
+  }
+
   public setActiveCalc(uid: string) {
     this._calculators.forEach((calc) => {
       calc.active = (calc.uid === uid);
@@ -251,6 +273,11 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
     this.errorService.removeObserver(this);
   }
 
+  private showLoading(show: boolean) {
+    this.showProgressBar = show;
+    this.progressBarDeterminate = ! show;
+  }
+
   // interface Observer
 
   update(sender: any, data: any): void {
diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index 6b7d73fd1..6506b6c3a 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -17,6 +17,7 @@ import {
   MatInputModule,
   MatListModule,
   MatCardModule,
+  MatProgressBarModule,
   MatTableModule,
   MatSnackBarModule,
   MatBadgeModule,
@@ -121,6 +122,7 @@ const appRoutes: Routes = [
     MatInputModule,
     MatListModule,
     MatMenuModule,
+    MatProgressBarModule,
     MatSelectModule,
     MatSidenavModule,
     MatSnackBarModule,
-- 
GitLab