Commit 5ca711aef0bb874d213e3d71da8f385fc563f3dc

Authored by Igor Kulikov
1 parent 1077def2

UI: Import/export code improvements

... ... @@ -80,17 +80,20 @@ export interface CsvColumnParam {
80 80 sampleData: any;
81 81 }
82 82
83   -export enum FileType {
84   - zip = 'application/zip',
85   - json = 'text/json'
  83 +export interface FileType {
  84 + mimeType: string;
  85 + extension: string;
86 86 }
87 87
88   -export const FileTypeExtension = new Map<FileType, string>(
89   - [
90   - [FileType.zip, 'zip'],
91   - [FileType.json, 'json'],
92   - ]
93   -);
  88 +export const JSON_TYPE: FileType = {
  89 + mimeType: 'text/json',
  90 + extension: 'json'
  91 +};
  92 +
  93 +export const ZIP_TYPE: FileType = {
  94 + mimeType: 'application/zip',
  95 + extension: 'zip'
  96 +};
94 97
95 98 export function convertCSVToJson(csvdata: string, config: CsvToJsonConfig,
96 99 onError: (messageId: string, params?: any) => void): CsvToJsonResult | number {
... ...
... ... @@ -44,7 +44,7 @@ import {
44 44 EntityAliasesDialogData
45 45 } from '@home/components/alias/entity-aliases-dialog.component';
46 46 import { ItemBufferService, WidgetItem } from '@core/services/item-buffer.service';
47   -import { FileType, FileTypeExtension, ImportWidgetResult, WidgetsBundleItem } from './import-export.models';
  47 +import { FileType, ImportWidgetResult, JSON_TYPE, WidgetsBundleItem, ZIP_TYPE } from './import-export.models';
48 48 import { EntityType } from '@shared/models/entity-type.models';
49 49 import { UtilsService } from '@core/services/utils.service';
50 50 import { WidgetService } from '@core/http/widget.service';
... ... @@ -80,7 +80,7 @@ export class ImportExportService {
80 80 (dashboard) => {
81 81 let name = dashboard.title;
82 82 name = name.toLowerCase().replace(/\W/g, '_');
83   - this.exportToFile(this.prepareDashboardExport(dashboard), name, FileType.json);
  83 + this.exportToPc(this.prepareDashboardExport(dashboard), name);
84 84 },
85 85 (e) => {
86 86 this.handleExportError(e, 'dashboard.export-failed-error');
... ... @@ -137,7 +137,7 @@ export class ImportExportService {
137 137 const widgetItem = this.itembuffer.prepareWidgetItem(dashboard, sourceState, sourceLayout, widget);
138 138 let name = widgetItem.widget.config.title;
139 139 name = name.toLowerCase().replace(/\W/g, '_');
140   - this.exportToFile(this.prepareExport(widgetItem), name, FileType.json);
  140 + this.exportToPc(this.prepareExport(widgetItem), name);
141 141 }
142 142
143 143 public importWidget(dashboard: Dashboard, targetState: string,
... ... @@ -236,7 +236,7 @@ export class ImportExportService {
236 236 }
237 237 let name = widgetType.name;
238 238 name = name.toLowerCase().replace(/\W/g, '_');
239   - this.exportToFile(this.prepareExport(widgetType), name, FileType.json);
  239 + this.exportToPc(this.prepareExport(widgetType), name);
240 240 },
241 241 (e) => {
242 242 this.handleExportError(e, 'widget-type.export-failed-error');
... ... @@ -282,7 +282,7 @@ export class ImportExportService {
282 282 }
283 283 let name = widgetsBundle.title;
284 284 name = name.toLowerCase().replace(/\W/g, '_');
285   - this.exportToFile(widgetsBundleItem, name, FileType.json);
  285 + this.exportToPc(widgetsBundleItem, name);
286 286 },
287 287 (e) => {
288 288 this.handleExportError(e, 'widgets-bundle.export-failed-error');
... ... @@ -384,7 +384,7 @@ export class ImportExportService {
384 384 ).subscribe((ruleChainExport) => {
385 385 let name = ruleChainExport.ruleChain.name;
386 386 name = name.toLowerCase().replace(/\W/g, '_');
387   - this.exportToFile(ruleChainExport, name, FileType.json);
  387 + this.exportToPc(ruleChainExport, name);
388 388 },
389 389 (e) => {
390 390 this.handleExportError(e, 'rulechain.export-failed-error');
... ... @@ -424,7 +424,7 @@ export class ImportExportService {
424 424 }
425 425 }
426 426 jsZip.generateAsync({type: 'blob'}).then(content => {
427   - this.exportToFile(content, filename, FileType.zip);
  427 + this.downloadFile(content, filename, ZIP_TYPE);
428 428 });
429 429 }
430 430
... ... @@ -687,19 +687,27 @@ export class ImportExportService {
687 687 ));
688 688 }
689 689
690   - private exportToFile(data: any, filename: string, fileType: FileType) {
  690 + private exportToPc(data: any, filename: string) {
691 691 if (!data) {
692 692 console.error('No data');
693 693 return;
694 694 }
  695 + this.exportJson(data, filename);
  696 + }
  697 +
  698 + private exportJson(data: any, filename: string) {
  699 + if (isObject(data)) {
  700 + data = JSON.stringify(data, null, 2);
  701 + }
  702 + this.downloadFile(data, filename, JSON_TYPE);
  703 + }
  704 +
  705 + private downloadFile(data: any, filename: string, fileType: FileType) {
695 706 if (!filename) {
696 707 filename = 'download';
697 708 }
698   - filename += '.' + FileTypeExtension.get(fileType);
699   - if (fileType === FileType.json && isObject(data)) {
700   - data = JSON.stringify(data, null, 2);
701   - }
702   - const blob = new Blob([data], {type: fileType});
  709 + filename += '.' + fileType.extension;
  710 + const blob = new Blob([data], {type: fileType.mimeType});
703 711 if (this.window.navigator && this.window.navigator.msSaveOrOpenBlob) {
704 712 this.window.navigator.msSaveOrOpenBlob(blob, filename);
705 713 } else {
... ... @@ -707,7 +715,7 @@ export class ImportExportService {
707 715 const a = this.document.createElement('a');
708 716 a.download = filename;
709 717 a.href = URL.createObjectURL(blob);
710   - a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
  718 + a.dataset.downloadurl = [fileType.mimeType, a.download, a.href].join(':');
711 719 // @ts-ignore
712 720 e.initEvent('click', true, false, this.window,
713 721 0, 0, 0, 0, 0, false, false, false, false, 0, null);
... ...