From a799e8e9e8e992557afad5e1b3305bf43388e4b7 Mon Sep 17 00:00:00 2001 From: Michael Czechowski Date: Tue, 7 Aug 2018 13:21:22 +0200 Subject: [PATCH] added history --- src/app/app.component.html | 48 ++++++++++++-------- src/app/app.component.ts | 10 +++-- src/app/components/graphs.component.ts | 62 +++++++++++++++----------- src/app/services/history.service.ts | 11 ++++- 4 files changed, 82 insertions(+), 49 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index a69b35d..41cbdd6 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,4 +1,4 @@ - + diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 1863632..d0d7ffa 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -24,6 +24,7 @@ import { Param } from './models/param.model'; import { Config } from './models/config.model'; import { CanvasService } from './services/canvas.service'; import { HistoryService } from './services/history.service'; +import { Graph } from './models/graph.model'; @Component({ selector: 'app-root', @@ -38,6 +39,7 @@ export class AppComponent implements OnInit { public url: any; public list: any[]; public showList: boolean; + public restoredHistory: any; constructor( private canvasService: CanvasService, @@ -46,12 +48,13 @@ export class AppComponent implements OnInit { this.config = env.formDefaults; this.configForm = ConfigForm; this.list = []; - this.showList = false; + this.showList = true; } ngOnInit() { this.configForm.reset({...this.config}); this.list = this.historyService.list(); + // console.log(this.graphs); } public updateGraphs() { @@ -85,7 +88,8 @@ export class AppComponent implements OnInit { this.showList = !this.showList; } - public restoreGraph(item) { - console.log(item); + public restoreGraph(history) { + this.configForm.reset({...history.config}); + this.restoredHistory = history; } } diff --git a/src/app/components/graphs.component.ts b/src/app/components/graphs.component.ts index 2aa5896..94ad20e 100644 --- a/src/app/components/graphs.component.ts +++ b/src/app/components/graphs.component.ts @@ -34,14 +34,16 @@ import { Point } from '../models/point.model'; }) export class GraphsComponent implements OnChanges { - public graphs: Graph[]; public canvas: any | null; public matrix: any | null; + public graphs: Graph[]; private genShiftPoint: any | null; private genLoadedAllGraphs: any | null; + private hash: string; @Input() config: any; + @Input() restoredHistory: any; @Output() svgChange = new EventEmitter(); @Output() graphChange = new EventEmitter(); @ViewChild('svg') svgElementRef; @@ -54,35 +56,23 @@ export class GraphsComponent implements OnChanges { } ngOnChanges(changes: SimpleChanges) { - this.init(); - } - - private init() { this.updateCanvas(); this.updateMatrix(); - this.updateGraphs(); - this.historyService.save(this.graphs, this.config); - } - - public prepareGuillocheExport(guillocheElement) { - if (this.genLoadedAllGraphs.next().value) { - this.svgChange.emit(this.svgElementRef); - } - } - - private *countLoadedGraphs() { - let cycles = 1; - - while (true) { - if (cycles < this.graphs.length) { - yield false; - cycles++; - } else { - yield true; - cycles = 1; + if (changes.restoredHistory) { + if (changes.restoredHistory.currentValue) { + if (this.restoredHistory.hash !== this.hash) { + this.graphs = this.restoredHistory.graphs; + this.hash = this.restoredHistory.hash; + } + return; } } + this.updateGraphs(); + } + + private saveHistory() { + this.historyService.save(this.graphs, this.config); } private updateGraphs(): void { @@ -103,6 +93,8 @@ export class GraphsComponent implements OnChanges { ]; this.graphs = curveList.map(curve => this.adjustGraph(curve)); + this.hash = this.historyService.hash(this.graphs); + this.saveHistory(); } private adjustGraph(curve) { @@ -235,4 +227,24 @@ export class GraphsComponent implements OnChanges { yield sign = sign * (-1); } } + + public prepareGuillocheExport(guillocheElement) { + if (this.genLoadedAllGraphs.next().value) { + this.svgChange.emit(this.svgElementRef); + } + } + + private *countLoadedGraphs() { + let cycles = 1; + + while (true) { + if (cycles < this.graphs.length) { + yield false; + cycles++; + } else { + yield true; + cycles = 1; + } + } + } } diff --git a/src/app/services/history.service.ts b/src/app/services/history.service.ts index adb4293..8a2a4e5 100644 --- a/src/app/services/history.service.ts +++ b/src/app/services/history.service.ts @@ -32,15 +32,22 @@ export class HistoryService { this.history.push({ date: new Date(), graphs: graphs, - config: config + config: config, + hash: this.hash(graphs) }); + } - console.log(config); + public hash(graphs) { + return btoa(JSON.stringify([graphs])); } public list() { return this.history; } + public restore(graphs: Graph[]) { + console.log(graphs); + } + }