UI: Import/export code improvements
This commit is contained in:
parent
1077def2f5
commit
5ca711aef0
@ -80,17 +80,20 @@ export interface CsvColumnParam {
|
|||||||
sampleData: any;
|
sampleData: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum FileType {
|
export interface FileType {
|
||||||
zip = 'application/zip',
|
mimeType: string;
|
||||||
json = 'text/json'
|
extension: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const FileTypeExtension = new Map<FileType, string>(
|
export const JSON_TYPE: FileType = {
|
||||||
[
|
mimeType: 'text/json',
|
||||||
[FileType.zip, 'zip'],
|
extension: 'json'
|
||||||
[FileType.json, 'json'],
|
};
|
||||||
]
|
|
||||||
);
|
export const ZIP_TYPE: FileType = {
|
||||||
|
mimeType: 'application/zip',
|
||||||
|
extension: 'zip'
|
||||||
|
};
|
||||||
|
|
||||||
export function convertCSVToJson(csvdata: string, config: CsvToJsonConfig,
|
export function convertCSVToJson(csvdata: string, config: CsvToJsonConfig,
|
||||||
onError: (messageId: string, params?: any) => void): CsvToJsonResult | number {
|
onError: (messageId: string, params?: any) => void): CsvToJsonResult | number {
|
||||||
|
|||||||
@ -44,7 +44,7 @@ import {
|
|||||||
EntityAliasesDialogData
|
EntityAliasesDialogData
|
||||||
} from '@home/components/alias/entity-aliases-dialog.component';
|
} from '@home/components/alias/entity-aliases-dialog.component';
|
||||||
import { ItemBufferService, WidgetItem } from '@core/services/item-buffer.service';
|
import { ItemBufferService, WidgetItem } from '@core/services/item-buffer.service';
|
||||||
import { FileType, FileTypeExtension, ImportWidgetResult, WidgetsBundleItem } from './import-export.models';
|
import { FileType, ImportWidgetResult, JSON_TYPE, WidgetsBundleItem, ZIP_TYPE } from './import-export.models';
|
||||||
import { EntityType } from '@shared/models/entity-type.models';
|
import { EntityType } from '@shared/models/entity-type.models';
|
||||||
import { UtilsService } from '@core/services/utils.service';
|
import { UtilsService } from '@core/services/utils.service';
|
||||||
import { WidgetService } from '@core/http/widget.service';
|
import { WidgetService } from '@core/http/widget.service';
|
||||||
@ -80,7 +80,7 @@ export class ImportExportService {
|
|||||||
(dashboard) => {
|
(dashboard) => {
|
||||||
let name = dashboard.title;
|
let name = dashboard.title;
|
||||||
name = name.toLowerCase().replace(/\W/g, '_');
|
name = name.toLowerCase().replace(/\W/g, '_');
|
||||||
this.exportToFile(this.prepareDashboardExport(dashboard), name, FileType.json);
|
this.exportToPc(this.prepareDashboardExport(dashboard), name);
|
||||||
},
|
},
|
||||||
(e) => {
|
(e) => {
|
||||||
this.handleExportError(e, 'dashboard.export-failed-error');
|
this.handleExportError(e, 'dashboard.export-failed-error');
|
||||||
@ -137,7 +137,7 @@ export class ImportExportService {
|
|||||||
const widgetItem = this.itembuffer.prepareWidgetItem(dashboard, sourceState, sourceLayout, widget);
|
const widgetItem = this.itembuffer.prepareWidgetItem(dashboard, sourceState, sourceLayout, widget);
|
||||||
let name = widgetItem.widget.config.title;
|
let name = widgetItem.widget.config.title;
|
||||||
name = name.toLowerCase().replace(/\W/g, '_');
|
name = name.toLowerCase().replace(/\W/g, '_');
|
||||||
this.exportToFile(this.prepareExport(widgetItem), name, FileType.json);
|
this.exportToPc(this.prepareExport(widgetItem), name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public importWidget(dashboard: Dashboard, targetState: string,
|
public importWidget(dashboard: Dashboard, targetState: string,
|
||||||
@ -236,7 +236,7 @@ export class ImportExportService {
|
|||||||
}
|
}
|
||||||
let name = widgetType.name;
|
let name = widgetType.name;
|
||||||
name = name.toLowerCase().replace(/\W/g, '_');
|
name = name.toLowerCase().replace(/\W/g, '_');
|
||||||
this.exportToFile(this.prepareExport(widgetType), name, FileType.json);
|
this.exportToPc(this.prepareExport(widgetType), name);
|
||||||
},
|
},
|
||||||
(e) => {
|
(e) => {
|
||||||
this.handleExportError(e, 'widget-type.export-failed-error');
|
this.handleExportError(e, 'widget-type.export-failed-error');
|
||||||
@ -282,7 +282,7 @@ export class ImportExportService {
|
|||||||
}
|
}
|
||||||
let name = widgetsBundle.title;
|
let name = widgetsBundle.title;
|
||||||
name = name.toLowerCase().replace(/\W/g, '_');
|
name = name.toLowerCase().replace(/\W/g, '_');
|
||||||
this.exportToFile(widgetsBundleItem, name, FileType.json);
|
this.exportToPc(widgetsBundleItem, name);
|
||||||
},
|
},
|
||||||
(e) => {
|
(e) => {
|
||||||
this.handleExportError(e, 'widgets-bundle.export-failed-error');
|
this.handleExportError(e, 'widgets-bundle.export-failed-error');
|
||||||
@ -384,7 +384,7 @@ export class ImportExportService {
|
|||||||
).subscribe((ruleChainExport) => {
|
).subscribe((ruleChainExport) => {
|
||||||
let name = ruleChainExport.ruleChain.name;
|
let name = ruleChainExport.ruleChain.name;
|
||||||
name = name.toLowerCase().replace(/\W/g, '_');
|
name = name.toLowerCase().replace(/\W/g, '_');
|
||||||
this.exportToFile(ruleChainExport, name, FileType.json);
|
this.exportToPc(ruleChainExport, name);
|
||||||
},
|
},
|
||||||
(e) => {
|
(e) => {
|
||||||
this.handleExportError(e, 'rulechain.export-failed-error');
|
this.handleExportError(e, 'rulechain.export-failed-error');
|
||||||
@ -424,7 +424,7 @@ export class ImportExportService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
jsZip.generateAsync({type: 'blob'}).then(content => {
|
jsZip.generateAsync({type: 'blob'}).then(content => {
|
||||||
this.exportToFile(content, filename, FileType.zip);
|
this.downloadFile(content, filename, ZIP_TYPE);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -687,19 +687,27 @@ export class ImportExportService {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
private exportToFile(data: any, filename: string, fileType: FileType) {
|
private exportToPc(data: any, filename: string) {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
console.error('No data');
|
console.error('No data');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
this.exportJson(data, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private exportJson(data: any, filename: string) {
|
||||||
|
if (isObject(data)) {
|
||||||
|
data = JSON.stringify(data, null, 2);
|
||||||
|
}
|
||||||
|
this.downloadFile(data, filename, JSON_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private downloadFile(data: any, filename: string, fileType: FileType) {
|
||||||
if (!filename) {
|
if (!filename) {
|
||||||
filename = 'download';
|
filename = 'download';
|
||||||
}
|
}
|
||||||
filename += '.' + FileTypeExtension.get(fileType);
|
filename += '.' + fileType.extension;
|
||||||
if (fileType === FileType.json && isObject(data)) {
|
const blob = new Blob([data], {type: fileType.mimeType});
|
||||||
data = JSON.stringify(data, null, 2);
|
|
||||||
}
|
|
||||||
const blob = new Blob([data], {type: fileType});
|
|
||||||
if (this.window.navigator && this.window.navigator.msSaveOrOpenBlob) {
|
if (this.window.navigator && this.window.navigator.msSaveOrOpenBlob) {
|
||||||
this.window.navigator.msSaveOrOpenBlob(blob, filename);
|
this.window.navigator.msSaveOrOpenBlob(blob, filename);
|
||||||
} else {
|
} else {
|
||||||
@ -707,7 +715,7 @@ export class ImportExportService {
|
|||||||
const a = this.document.createElement('a');
|
const a = this.document.createElement('a');
|
||||||
a.download = filename;
|
a.download = filename;
|
||||||
a.href = URL.createObjectURL(blob);
|
a.href = URL.createObjectURL(blob);
|
||||||
a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
|
a.dataset.downloadurl = [fileType.mimeType, a.download, a.href].join(':');
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
e.initEvent('click', true, false, this.window,
|
e.initEvent('click', true, false, this.window,
|
||||||
0, 0, 0, 0, 0, false, false, false, false, 0, null);
|
0, 0, 0, 0, 0, false, false, false, false, 0, null);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user