From 60d96d5dacb936356fa36d6fcb8e0326b77a7bf6 Mon Sep 17 00:00:00 2001 From: Aaron Louie Date: Tue, 11 Feb 2020 13:12:18 -0500 Subject: [PATCH] Sets filename of downloaded XML file --- package-lock.json | 6 ++++++ package.json | 1 + src/app/diagram/diagram.component.spec.ts | 16 ++++++++++++++-- src/app/diagram/diagram.component.ts | 21 +++++++++++++++++++-- src/app/modeler/modeler.component.html | 2 +- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index e665230..e80a459 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9706,6 +9706,12 @@ } } }, + "mockdate": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/mockdate/-/mockdate-2.0.5.tgz", + "integrity": "sha512-ST0PnThzWKcgSLyc+ugLVql45PvESt3Ul/wrdV/OPc/6Pr8dbLAIJsN1cIp41FLzbN+srVTNIRn+5Cju0nyV6A==", + "dev": true + }, "moddle": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/moddle/-/moddle-4.1.0.tgz", diff --git a/package.json b/package.json index 481b4f1..50fd063 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ "karma-coverage-istanbul-reporter": "^2.1.1", "karma-jasmine": "~1.1.2", "karma-jasmine-html-reporter": "^0.2.2", + "mockdate": "^2.0.5", "postcss-short": "^5.0.0", "protractor": "^5.4.2", "puppeteer": "^1.20.0", diff --git a/src/app/diagram/diagram.component.spec.ts b/src/app/diagram/diagram.component.spec.ts index 01254d6..db3447e 100644 --- a/src/app/diagram/diagram.component.spec.ts +++ b/src/app/diagram/diagram.component.spec.ts @@ -3,6 +3,7 @@ import {DebugNode} from '@angular/core'; import {async, ComponentFixture, TestBed} from '@angular/core/testing'; import {MatIconModule} from '@angular/material/icon'; import * as FileSaver from 'file-saver'; +import MockDate from 'mockdate'; import {ApiService, BPMN_DIAGRAM_DEFAULT, FileType, MockEnvironment} from 'sartography-workflow-lib'; import { BPMN_DIAGRAM, @@ -10,7 +11,6 @@ import { DMN_DIAGRAM, DMN_DIAGRAM_WITH_WARNINGS } from '../../testing/mocks/diagram.mocks'; - import {DiagramComponent} from './diagram.component'; describe('DiagramComponent', () => { @@ -31,9 +31,10 @@ describe('DiagramComponent', () => { ] }); + httpMock = TestBed.get(HttpTestingController); fixture = TestBed.createComponent(DiagramComponent); component = fixture.debugElement.componentInstance; - httpMock = TestBed.get(HttpTestingController); + component.fileName = ''; fixture.detectChanges(); })); @@ -132,6 +133,17 @@ describe('DiagramComponent', () => { expect(fileSaverSpy).toHaveBeenCalled(); }); + it('should insert date into filename', () => { + MockDate.set('02/02/2020 20:20:020 GMT-0500'); + component.fileName = 'file name with extension.bpmn'; + component.diagramType = FileType.BPMN; + expect((component as any).insertDateIntoFileName()).toEqual('file name with extension_2020-02-02_20:20.bpmn'); + + component.fileName = 'file name with no extension'; + component.diagramType = FileType.DMN; + expect((component as any).insertDateIntoFileName()).toEqual('file name with no extension_2020-02-02_20:20.dmn'); + }); + it('should create a new diagram', () => { const initializeModelerSpy = spyOn(component, 'initializeModeler').and.stub(); const importXMLSpy = spyOn(component.modeler, 'importXML').and.stub(); diff --git a/src/app/diagram/diagram.component.ts b/src/app/diagram/diagram.component.ts index d7a4302..a7da842 100644 --- a/src/app/diagram/diagram.component.ts +++ b/src/app/diagram/diagram.component.ts @@ -1,5 +1,6 @@ +import {formatDate} from '@angular/common'; import {HttpErrorResponse} from '@angular/common/http'; -import {AfterViewInit, Component, ElementRef, EventEmitter, NgZone, Output, ViewChild} from '@angular/core'; +import {AfterViewInit, Component, ElementRef, EventEmitter, Input, NgZone, Output, ViewChild} from '@angular/core'; import {ControlValueAccessor} from '@angular/forms'; import BpmnModeler from 'bpmn-js/lib/Modeler'; import DmnModeler from 'dmn-js/lib/Modeler'; @@ -18,6 +19,7 @@ import {dmnModelerConfig} from './dmn-modeler-config'; styleUrls: ['diagram.component.scss'], }) export class DiagramComponent implements ControlValueAccessor, AfterViewInit { + @Input() fileName: string; @ViewChild('containerRef', {static: true}) containerRef: ElementRef; @ViewChild('propertiesRef', {static: true}) propertiesRef: ElementRef; @Output() private importDone: EventEmitter = new EventEmitter(); @@ -118,7 +120,7 @@ export class DiagramComponent implements ControlValueAccessor, AfterViewInit { this.saveDiagram(); this.modeler.saveXML({format: true}, (err, xml) => { const blob = new Blob([xml], {type: 'text/xml'}); - fileSaver.saveAs(blob, `BPMN Diagram - ${new Date().toISOString()}.xml`); + fileSaver.saveAs(blob, this.insertDateIntoFileName()); }); } @@ -216,4 +218,19 @@ export class DiagramComponent implements ControlValueAccessor, AfterViewInit { private getRandomString(len: number): string { return uuidv4().slice(0, len); } + + private insertDateIntoFileName(): string { + const arr = this.fileName.split('.'); + const dateString = formatDate(new Date(), 'yyyy-MM-dd_HH:mm', 'en-us'); + + if (arr.length > 1) { + // Insert date between file name and extension + const ext = arr[arr.length - 1]; + const name = arr.slice(0, -1); + return `${name.join('.')}_${dateString}.${ext}`; + } else { + // No extension in file name yet. Add it, based on the diagram type. + return `${this.fileName}_${dateString}.${this.diagramType}`; + } + } } diff --git a/src/app/modeler/modeler.component.html b/src/app/modeler/modeler.component.html index 5309e3c..ffa9bed 100644 --- a/src/app/modeler/modeler.component.html +++ b/src/app/modeler/modeler.component.html @@ -104,7 +104,7 @@
- +
Failed to render diagram: