-
Notifications
You must be signed in to change notification settings - Fork 1
/
startup.ts
70 lines (60 loc) · 2.78 KB
/
startup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { EnableViewportAction, MoveIntoViewportAction, SetViewportZoomAction, SwitchThemeAction } from '@axonivy/process-editor-protocol';
import { Action, CenterAction, GLSPActionDispatcher, IDiagramStartup, NavigationTarget, SelectAction, TYPES } from '@eclipse-glsp/client';
import { ContainerModule, inject, injectable, interfaces } from 'inversify';
import { IvyDiagramOptions } from './di.config';
import { isInPreviewMode } from './url-helper';
import { MiningAction } from './process-mining-visualisation/mining-action';
const ContainerSymbol = Symbol('ContainerSymbol');
@injectable()
export class ViewerDiagramStartup implements IDiagramStartup {
@inject(GLSPActionDispatcher)
protected actionDispatcher: GLSPActionDispatcher;
@inject(TYPES.IDiagramOptions)
protected options: IvyDiagramOptions;
@inject(ContainerSymbol)
protected container: interfaces.Container;
async postModelInitialization(): Promise<void> {
await this.dispatchAfterModelInitialized();
if (!isInPreviewMode()) {
this.actionDispatcher.dispatch(EnableViewportAction.create());
}
}
protected async dispatchAfterModelInitialized(): Promise<void> {
const actions: Action[] = [];
if (this.isNumeric(this.options.zoom)) {
actions.push(SetViewportZoomAction.create({ zoom: +this.options.zoom / 100 }));
actions.push(...this.showElement((ids: string[]) => CenterAction.create(ids, { animate: false, retainZoom: true })));
} else {
actions.push(
...this.showElement((ids: string[]) => MoveIntoViewportAction.create({ elementIds: ids, animate: false, retainZoom: true }))
);
}
actions.push(SwitchThemeAction.create({ theme: this.options.theme }));
actions.push(MiningAction.create({ data: await (await fetchMiningData(this.options.miningUrl)).json() }));
return this.actionDispatcher.dispatchAll(actions);
}
protected showElement(action: (elementIds: string[]) => Action): Action[] {
if (this.options.highlight) {
return [action([this.options.highlight])];
}
if (this.options.select) {
const elementIds = this.options.select.split(NavigationTarget.ELEMENT_IDS_SEPARATOR);
return [SelectAction.create({ selectedElementsIDs: elementIds }), action(elementIds)];
}
return [];
}
protected isNumeric(num: any): boolean {
return !isNaN(parseFloat(num)) && isFinite(num);
}
}
export const ivyStartupDiagramModule = new ContainerModule(bind => {
bind(TYPES.IDiagramStartup)
.toDynamicValue(ctx => {
const child = ctx.container.createChild();
child.bind(ViewerDiagramStartup).toSelf().inSingletonScope();
child.bind(ContainerSymbol).toConstantValue(ctx.container);
return child.get(ViewerDiagramStartup);
})
.inSingletonScope();
});
const fetchMiningData = async (url: string) => fetch(url);