From 7fd0f89c1129b3ceec8ee619ca7488bbbd1a7704 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Thu, 10 Oct 2019 10:46:31 +0200
Subject: [PATCH] Fix #302 add session notes

---
 src/app/app.component.ts                      | 10 ++++
 src/app/app.module.ts                         | 12 +++-
 .../session-properties.component.html         | 44 +++++++-------
 .../session-properties.component.scss         | 15 +++--
 .../session-properties.component.ts           | 59 ++++---------------
 src/locale/messages.en.json                   |  7 ++-
 src/locale/messages.fr.json                   | 13 ++--
 7 files changed, 75 insertions(+), 85 deletions(-)

diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 4b42b4214..7d38021e9 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -506,6 +506,7 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
     }
     // just to be sure, get rid of any Nub possibly stuck in session without any form attached
     Session.getInstance().clear();
+    Session.getInstance().documentation = "";
   }
 
   public loadSession() {
@@ -525,12 +526,21 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
   }
 
   public loadSessionFile(f: File, info?: any) {
+    // notes merge detection: was there already some notes ?
+    const existingNotes = Session.getInstance().documentation;
+    // load
     this.formulaireService.loadSession(f, info)
     .then((data) => {
       if (data.hasErrors) {
         this.notificationsService.notify(this.intlService.localizeText("ERROR_PROBLEM_LOADING_SESSION"), 3500);
       } else {
         if (data.loaded && data.loaded.length > 0) {
+          // notes merge detection: was there already some notes ?
+          const currentNotes = Session.getInstance().documentation;
+          if (existingNotes !== "" && currentNotes !== existingNotes) {
+            this.notificationsService.notify(this.intlService.localizeText("WARNING_SESSION_LOAD_NOTES_MERGED"), 3500);
+          }
+          // go to calc or diagram depending on what was loaded
           if (data.loaded.length > 1) {
             this.toDiagram();
           } else {
diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index 460d1f592..6cb34de59 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -31,7 +31,7 @@ import { DragDropModule } from "@angular/cdk/drag-drop";
 
 import { TableModule } from "primeng/components/table/table";
 import { KonamiModule } from "ngx-konami";
-import { MarkdownModule } from "ngx-markdown";
+import { MarkdownModule, MarkedOptions } from "ngx-markdown";
 
 import { FlexLayoutModule } from "@angular/flex-layout";
 import {
@@ -133,7 +133,15 @@ const appRoutes: Routes = [
     FlexLayoutModule,
     HotkeyModule.forRoot(),
     HttpClientModule,
-    MarkdownModule.forRoot(),
+    MarkdownModule.forRoot({
+      markedOptions: {
+        provide: MarkedOptions,
+        useValue: {
+          gfm: true,
+          breaks: true
+        }
+      }
+    }),
     MatBadgeModule,
     MatButtonModule,
     MatButtonToggleModule,
diff --git a/src/app/components/session-properties/session-properties.component.html b/src/app/components/session-properties/session-properties.component.html
index 82095cb78..13211dc48 100644
--- a/src/app/components/session-properties/session-properties.component.html
+++ b/src/app/components/session-properties/session-properties.component.html
@@ -1,25 +1,29 @@
-<mat-card id="session-properties">
+<div class="container" fxLayout="row" fxLayoutAlign="center space-evenly">
 
-    <mat-card-header>
-        <mat-card-title>
-            <h1>{{ uitextTitle }}</h1>
-        </mat-card-title>
-    </mat-card-header>
+    <mat-card id="session-properties">
 
-    <mat-card-content>
+        <mat-card-header>
+            <mat-card-title>
+                <h1>{{ uitextTitle }}</h1>
+            </mat-card-title>
+        </mat-card-header>
 
-        <mat-tab-group>
-            <mat-tab [label]="uitextPreview">
-                <markdown [data]="content" katex [katexOptions]="options"></markdown>
-            </mat-tab>
-            <mat-tab [label]="uitextEdit">
-                <mat-form-field>
-                    <textarea matInput id="md-editor" [(ngModel)]="content"
-                              cdkTextareaAutosize cdkAutosizeMinRows="15" #autosize="cdkTextareaAutosize"></textarea>
-                </mat-form-field>
-            </mat-tab>
-        </mat-tab-group>
+        <mat-card-content>
 
-    </mat-card-content>
+            <mat-tab-group [selectedIndex]="selectedTab">
+                <mat-tab [label]="uitextPreview">
+                    <markdown [data]="content" katex [katexOptions]="options"></markdown>
+                </mat-tab>
+                <mat-tab [label]="uitextEdit">
+                    <mat-form-field>
+                        <textarea matInput id="md-editor" [(ngModel)]="content"
+                                cdkTextareaAutosize cdkAutosizeMinRows="15" #autosize="cdkTextareaAutosize"></textarea>
+                    </mat-form-field>
+                </mat-tab>
+            </mat-tab-group>
 
-</mat-card>
+        </mat-card-content>
+
+    </mat-card>
+
+</div>
diff --git a/src/app/components/session-properties/session-properties.component.scss b/src/app/components/session-properties/session-properties.component.scss
index 48efb11ec..1b9bdd483 100644
--- a/src/app/components/session-properties/session-properties.component.scss
+++ b/src/app/components/session-properties/session-properties.component.scss
@@ -1,8 +1,13 @@
-mat-form-field {
-    width: 100%;
+#session-properties {
+    width: 600px;
+    padding: 3em;
+    
+    mat-form-field {
+        width: 100%;
 
-    textarea {
-        resize: none;
-        overflow: hidden;
+        textarea {
+            resize: none;
+            overflow: hidden;
+        }
     }
 }
\ No newline at end of file
diff --git a/src/app/components/session-properties/session-properties.component.ts b/src/app/components/session-properties/session-properties.component.ts
index e44fcc49b..630377813 100644
--- a/src/app/components/session-properties/session-properties.component.ts
+++ b/src/app/components/session-properties/session-properties.component.ts
@@ -1,17 +1,10 @@
-import {
-    Component,
-    ViewChild,
-    AfterContentInit,
-    OnInit,
-    AfterViewChecked,
-    AfterViewInit
-} from "@angular/core";
+import { Component, OnInit } from "@angular/core";
 import { Router } from "@angular/router";
 
-import { Session } from "jalhyd";
-
 import { I18nService } from "../../services/internationalisation.service";
 
+import { Session } from "jalhyd";
+
 import { KatexOptions } from "ngx-markdown/src/katex-options";
 
 @Component({
@@ -19,7 +12,9 @@ import { KatexOptions } from "ngx-markdown/src/katex-options";
     templateUrl: "./session-properties.component.html",
     styleUrls: ["./session-properties.component.scss"]
 })
-export class SessionPropertiesComponent implements /* AfterContentInit, AfterViewChecked, AfterViewInit, */ OnInit {
+export class SessionPropertiesComponent implements OnInit {
+
+    public selectedTab = 0;
 
     public options: KatexOptions;
 
@@ -59,45 +54,11 @@ export class SessionPropertiesComponent implements /* AfterContentInit, AfterVie
         if (! this.hasModules) {
             this.router.navigate([ "/list" ]);
         }
-    }
-
-    /* public ngAfterViewInit(): void {
-        // add click listener on every calculator node in the graph, that
-        // corresponds to an open module
-        this.nativeElement.querySelectorAll("g.node").forEach(item => {
-            if (item.id && this.formIsOpen(item.id)) {
-                item.style.cursor = "pointer";
-                item.addEventListener("click", () => {
-                    this.openCalc(item.id);
-                });
-            }
-        });
-    }
-
-    public ngAfterContentInit(): void {
-        this.error = false;
-        mermaid.initialize({
-            // theme: "forest", // @TODO thème Irstea !
-            flowchart: {
-                curve: "basis"
-            }
-        });
-        this.nativeElement = this.diagram.nativeElement;
-
-        if (this.hasModules) {
-            // generate graph description
-            const graphDefinition = this.graphDefinition();
-            // draw
-            try {
-                mermaid.render("graphDiv", graphDefinition, (svgCode, bindFunctions) => {
-                    this.nativeElement.innerHTML = svgCode;
-                });
-            } catch (e) {
-                console.error(e);
-                this.error = true;
-            }
+        // if notes content is empty, switch to editor
+        if (this.content === undefined || this.content === "") {
+            this.selectedTab = 1;
         }
-    } */
+    }
 
     public get hasModules(): boolean {
         return Session.getInstance().getNumberOfNubs() > 0;
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index 12a293a5d..5a3e63c64 100644
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -423,9 +423,9 @@
     "INFO_SETUP_PRECISION_CALCUL": "Computation accuracy",
     "INFO_SETUP_RESTORE_DEFAULT_VALUES": "Restore default values",
     "INFO_SETUP_STORE_PREFERENCES": "Save preferences",
-    "INFO_SETUP_TITLE": "Application setup",
+    "INFO_SETUP_TITLE": "Setup",
     "INFO_SESSION_PROPERTIES_EDIT": "Edit",
-    "INFO_SESSION_PROPERTIES_PREVIEW": "Documentation",
+    "INFO_SESSION_PROPERTIES_PREVIEW": "Preview",
     "INFO_SESSION_PROPERTIES_TITLE": "Session properties",
     "INFO_SNACKBAR_DEFAULT_SETTINGS_RESTORED": "Default settings restored",
     "INFO_SNACKBAR_RESULTS_CALCULATED": "Results calculated for",
@@ -465,5 +465,6 @@
     "WARNING_UPSTREAM_BOTTOM_HIGHER_THAN_WATER": "Upstream water elevation is lower or equal to bottom elevation",
     "WARNING_DOWNSTREAM_BOTTOM_HIGHER_THAN_WATER": "Downstream water elevation is lower or equal to bottom elevation",
     "WARNING_YN_SECTION_PENTE_NEG_NULLE_HNORMALE_INF": "Normal depth: slope is negative or zero, normal depth is infinite",
-    "WARNING_YN_SECTION_NON_CONVERGENCE_NEWTON_HNORMALE": "Normal depth: non convergence of the calculation (Newton's method)"
+    "WARNING_YN_SECTION_NON_CONVERGENCE_NEWTON_HNORMALE": "Normal depth: non convergence of the calculation (Newton's method)",
+    "WARNING_SESSION_LOAD_NOTES_MERGED": "Notes have been merged"
 }
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index 4b3184c18..523d6cd37 100644
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -311,7 +311,7 @@
     "INFO_MENU_SAVE_SESSION_TITLE": "Enregistrer la session",
     "INFO_MENU_SAVE_SETTINGS": "Enregistrer les paramètres",
     "INFO_MENU_SELECT_CALC": "Choisir un module de calcul",
-    "INFO_MENU_SESSION_PROPS": "Propriétés de la session",
+    "INFO_MENU_SESSION_PROPS": "Notes",
     "INFO_OPTION_ALL_F": "Toutes",
     "INFO_OPTION_ALL": "Tous",
     "INFO_OPTION_CANCEL": "Annuler",
@@ -422,10 +422,10 @@
     "INFO_SETUP_PRECISION_CALCUL": "Précision de calcul",
     "INFO_SETUP_RESTORE_DEFAULT_VALUES": "Restaurer les valeurs par défaut",
     "INFO_SETUP_STORE_PREFERENCES": "Enregistrer les préférences",
-    "INFO_SETUP_TITLE": "Paramètres de l'application",
-    "INFO_SESSION_PROPERTIES_EDIT": "Modifier",
-    "INFO_SESSION_PROPERTIES_PREVIEW": "Documentation",
-    "INFO_SESSION_PROPERTIES_TITLE": "Propriétés de la session",
+    "INFO_SETUP_TITLE": "Paramètres",
+    "INFO_SESSION_PROPERTIES_EDIT": "Éditer",
+    "INFO_SESSION_PROPERTIES_PREVIEW": "Aperçu",
+    "INFO_SESSION_PROPERTIES_TITLE": "Notes",
     "INFO_SNACKBAR_DEFAULT_SETTINGS_RESTORED": "Paramètres par défaut restaurés",
     "INFO_SNACKBAR_RESULTS_CALCULATED": "Résultats calculés pour",
     "INFO_SNACKBAR_RESULTS_INVALIDATED": "Résultats invalidés pour",
@@ -464,5 +464,6 @@
     "WARNING_UPSTREAM_BOTTOM_HIGHER_THAN_WATER": "La cote de l'eau à l'amont est plus basse ou égale à la cote de fond",
     "WARNING_DOWNSTREAM_BOTTOM_HIGHER_THAN_WATER": "La cote de l'eau à l'aval est plus basse ou égale à la cote de fond",
     "WARNING_YN_SECTION_PENTE_NEG_NULLE_HNORMALE_INF": "Hauteur normale: pente négative ou nulle, hauteur normale infinie",
-    "WARNING_YN_SECTION_NON_CONVERGENCE_NEWTON_HNORMALE": "Hauteur normale: non convergence du calcul (méthode de Newton)"
+    "WARNING_YN_SECTION_NON_CONVERGENCE_NEWTON_HNORMALE": "Hauteur normale: non convergence du calcul (méthode de Newton)",
+    "WARNING_SESSION_LOAD_NOTES_MERGED": "Les notes ont été fusionnées"
 }
-- 
GitLab