Commit 5ca711aef0bb874d213e3d71da8f385fc563f3dc
1 parent
1077def2
UI: Import/export code improvements
Showing
2 changed files
with
34 additions
and
23 deletions
@@ -80,17 +80,20 @@ export interface CsvColumnParam { | @@ -80,17 +80,20 @@ export interface CsvColumnParam { | ||
80 | sampleData: any; | 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 | export function convertCSVToJson(csvdata: string, config: CsvToJsonConfig, | 98 | export function convertCSVToJson(csvdata: string, config: CsvToJsonConfig, |
96 | onError: (messageId: string, params?: any) => void): CsvToJsonResult | number { | 99 | onError: (messageId: string, params?: any) => void): CsvToJsonResult | number { |
@@ -44,7 +44,7 @@ import { | @@ -44,7 +44,7 @@ import { | ||
44 | EntityAliasesDialogData | 44 | EntityAliasesDialogData |
45 | } from '@home/components/alias/entity-aliases-dialog.component'; | 45 | } from '@home/components/alias/entity-aliases-dialog.component'; |
46 | import { ItemBufferService, WidgetItem } from '@core/services/item-buffer.service'; | 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 | import { EntityType } from '@shared/models/entity-type.models'; | 48 | import { EntityType } from '@shared/models/entity-type.models'; |
49 | import { UtilsService } from '@core/services/utils.service'; | 49 | import { UtilsService } from '@core/services/utils.service'; |
50 | import { WidgetService } from '@core/http/widget.service'; | 50 | import { WidgetService } from '@core/http/widget.service'; |
@@ -80,7 +80,7 @@ export class ImportExportService { | @@ -80,7 +80,7 @@ export class ImportExportService { | ||
80 | (dashboard) => { | 80 | (dashboard) => { |
81 | let name = dashboard.title; | 81 | let name = dashboard.title; |
82 | name = name.toLowerCase().replace(/\W/g, '_'); | 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 | (e) => { | 85 | (e) => { |
86 | this.handleExportError(e, 'dashboard.export-failed-error'); | 86 | this.handleExportError(e, 'dashboard.export-failed-error'); |
@@ -137,7 +137,7 @@ export class ImportExportService { | @@ -137,7 +137,7 @@ export class ImportExportService { | ||
137 | const widgetItem = this.itembuffer.prepareWidgetItem(dashboard, sourceState, sourceLayout, widget); | 137 | const widgetItem = this.itembuffer.prepareWidgetItem(dashboard, sourceState, sourceLayout, widget); |
138 | let name = widgetItem.widget.config.title; | 138 | let name = widgetItem.widget.config.title; |
139 | name = name.toLowerCase().replace(/\W/g, '_'); | 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 | public importWidget(dashboard: Dashboard, targetState: string, | 143 | public importWidget(dashboard: Dashboard, targetState: string, |
@@ -236,7 +236,7 @@ export class ImportExportService { | @@ -236,7 +236,7 @@ export class ImportExportService { | ||
236 | } | 236 | } |
237 | let name = widgetType.name; | 237 | let name = widgetType.name; |
238 | name = name.toLowerCase().replace(/\W/g, '_'); | 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 | (e) => { | 241 | (e) => { |
242 | this.handleExportError(e, 'widget-type.export-failed-error'); | 242 | this.handleExportError(e, 'widget-type.export-failed-error'); |
@@ -282,7 +282,7 @@ export class ImportExportService { | @@ -282,7 +282,7 @@ export class ImportExportService { | ||
282 | } | 282 | } |
283 | let name = widgetsBundle.title; | 283 | let name = widgetsBundle.title; |
284 | name = name.toLowerCase().replace(/\W/g, '_'); | 284 | name = name.toLowerCase().replace(/\W/g, '_'); |
285 | - this.exportToFile(widgetsBundleItem, name, FileType.json); | 285 | + this.exportToPc(widgetsBundleItem, name); |
286 | }, | 286 | }, |
287 | (e) => { | 287 | (e) => { |
288 | this.handleExportError(e, 'widgets-bundle.export-failed-error'); | 288 | this.handleExportError(e, 'widgets-bundle.export-failed-error'); |
@@ -384,7 +384,7 @@ export class ImportExportService { | @@ -384,7 +384,7 @@ export class ImportExportService { | ||
384 | ).subscribe((ruleChainExport) => { | 384 | ).subscribe((ruleChainExport) => { |
385 | let name = ruleChainExport.ruleChain.name; | 385 | let name = ruleChainExport.ruleChain.name; |
386 | name = name.toLowerCase().replace(/\W/g, '_'); | 386 | name = name.toLowerCase().replace(/\W/g, '_'); |
387 | - this.exportToFile(ruleChainExport, name, FileType.json); | 387 | + this.exportToPc(ruleChainExport, name); |
388 | }, | 388 | }, |
389 | (e) => { | 389 | (e) => { |
390 | this.handleExportError(e, 'rulechain.export-failed-error'); | 390 | this.handleExportError(e, 'rulechain.export-failed-error'); |
@@ -424,7 +424,7 @@ export class ImportExportService { | @@ -424,7 +424,7 @@ export class ImportExportService { | ||
424 | } | 424 | } |
425 | } | 425 | } |
426 | jsZip.generateAsync({type: 'blob'}).then(content => { | 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,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 | if (!data) { | 691 | if (!data) { |
692 | console.error('No data'); | 692 | console.error('No data'); |
693 | return; | 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 | if (!filename) { | 706 | if (!filename) { |
696 | filename = 'download'; | 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 | if (this.window.navigator && this.window.navigator.msSaveOrOpenBlob) { | 711 | if (this.window.navigator && this.window.navigator.msSaveOrOpenBlob) { |
704 | this.window.navigator.msSaveOrOpenBlob(blob, filename); | 712 | this.window.navigator.msSaveOrOpenBlob(blob, filename); |
705 | } else { | 713 | } else { |
@@ -707,7 +715,7 @@ export class ImportExportService { | @@ -707,7 +715,7 @@ export class ImportExportService { | ||
707 | const a = this.document.createElement('a'); | 715 | const a = this.document.createElement('a'); |
708 | a.download = filename; | 716 | a.download = filename; |
709 | a.href = URL.createObjectURL(blob); | 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 | // @ts-ignore | 719 | // @ts-ignore |
712 | e.initEvent('click', true, false, this.window, | 720 | e.initEvent('click', true, false, this.window, |
713 | 0, 0, 0, 0, 0, false, false, false, false, 0, null); | 721 | 0, 0, 0, 0, 0, false, false, false, false, 0, null); |