diff --git a/application/client/src/app/service/history.ts b/application/client/src/app/service/history.ts index f21782f084..143519ef59 100644 --- a/application/client/src/app/service/history.ts +++ b/application/client/src/app/service/history.ts @@ -76,7 +76,7 @@ export class Service extends Implementation { return this.provider.export(uuids, filename); } - public import(filename: string): Promise { + public import(filename: string): Promise { return this.provider.import(filename); } } diff --git a/application/client/src/app/service/history/provider.ts b/application/client/src/app/service/history/provider.ts index 2b42ffdb29..36767fc4e4 100644 --- a/application/client/src/app/service/history/provider.ts +++ b/application/client/src/app/service/history/provider.ts @@ -58,7 +58,7 @@ export class Provider implements EntryConvertable { return bridge.entries({ file: filename }).overwrite([this.entry().to()]); } - public import(filename: string): Promise { + public import(filename: string): Promise { return bridge .entries({ file: filename }) .get() @@ -83,11 +83,12 @@ export class Provider implements EntryConvertable { }); return col; }); + const uuid: string[] = this.collections.map(collection => collection.uuid); this.storage.definitions.add(this.definitions); this.storage.collections.add(this.collections); this.collections = []; this.definitions = []; - return undefined; + return uuid; }); } diff --git a/application/client/src/app/ui/views/sidebar/search/component.ts b/application/client/src/app/ui/views/sidebar/search/component.ts index 9ec1046b0b..7e26b58234 100644 --- a/application/client/src/app/ui/views/sidebar/search/component.ts +++ b/application/client/src/app/ui/views/sidebar/search/component.ts @@ -59,10 +59,10 @@ export class Filters extends ChangesDetector implements OnDestroy, AfterContentI handler: () => { this.log().debug(`Not implemented yet`); }, - }, + } ]; contextmenu.show({ - items: items, + items: this.providers.contextMenuOptions(items), x: event.pageX, y: event.pageY, }); diff --git a/application/client/src/app/ui/views/sidebar/search/providers/providers.ts b/application/client/src/app/ui/views/sidebar/search/providers/providers.ts index a18c4f99b6..ef61c62e4e 100644 --- a/application/client/src/app/ui/views/sidebar/search/providers/providers.ts +++ b/application/client/src/app/ui/views/sidebar/search/providers/providers.ts @@ -15,6 +15,10 @@ import { Entity } from './definitions/entity'; import { unique } from '@platform/env/sequence'; import { Logger } from '@platform/log'; import { ProvidersEvents } from './definitions/events'; +import { history } from '@service/history'; +import { bridge } from '@service/bridge'; +import { HistorySession } from '@service/history/session'; +import { Notification, notifications } from '@ui/service/notifications'; type TSelectedEntities = string[]; @@ -485,6 +489,7 @@ export class Providers { }); }, }); + this._providers.forEach((provider: Provider) => { const custom: IMenuItem[] = provider.getContextMenuItems( event.entity, @@ -497,7 +502,90 @@ export class Providers { event.items = event.items.concat(custom); } }); + + this.injectGeneralMenuItems(event.items); + this.subjects.get().context.emit(event); + + } + + + public injectGeneralMenuItems(items: IMenuItem[]): void { + const historySession = history.get(this.session); + if(historySession === undefined) { + this.logger.error('History session is not defined'); + return; + } + items.push({ + /* Delimiter */ + }); + const store = this.session.search.store(); + const showExport: boolean = (store.filters().get().length + store.charts().get().length + store.disabled().get().length) !== 0; + showExport && items.push( + { + caption: 'Export All to File', + handler: () => this.filters(historySession).export(), + }); + items.push( + { + caption: 'Import from File', + handler: () => this.filters(historySession).import(), + }); + } + + protected filters(historySession: HistorySession): { import(): void; export(): void } { + const logAndNotifyError = (error: Error): void => { + this.logger.error(error.message); + notifications.notify( + new Notification({ + message: error.message, + session: this.session.uuid(), + actions: [] + }) + ); + }; + + return { + import: (): void => { + bridge.files().select.text() + .then(file => { + if (file.length !== 1) { + this.logger.error('No file selected'); + return; + } + history.import(file[0].filename) + .then((uuids: string[]) => { + if (uuids.length === 0) { + this.logger.warn('File does not have a collection'); + return; + } + if (uuids.length > 1) { + this.session.switch().toolbar.presets(); + return; + } else { + const collection = history.collections.get(uuids[0]); + if (collection === undefined) { + this.logger.error(`Cannot find imported collection with UUID: ${uuids[0]}`); + return; + } + historySession.apply(collection); + } + }) + .catch(error => logAndNotifyError(error)); + }) + .catch(error => logAndNotifyError(error)) + }, + export: (): void => { + bridge.files().select.save() + .then((filename: string | undefined) => { + if (filename === undefined) + return; + history.export([historySession.collections.uuid], filename) + .catch(error => logAndNotifyError(error)) + }) + .catch(error => logAndNotifyError(error)) + }, + }; } private _onDoubleclickEvent(event: IDoubleclickEvent) {