-
Notifications
You must be signed in to change notification settings - Fork 74
/
graph-job-editor.component.ts
307 lines (236 loc) · 9.86 KB
/
graph-job-editor.component.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Inject,
Input,
Output,
TemplateRef,
ViewChild,
SimpleChanges,
OnChanges,
OnDestroy
} from "@angular/core";
import {FormControl} from "@angular/forms";
import {SelectionPlugin, SVGArrangePlugin, SVGEdgeHoverPlugin, SVGNodeMovePlugin, Workflow as WorkflowGraph, ZoomPlugin} from "cwl-svg";
import {StepModel} from "cwlts/models/generic/StepModel";
import {WorkflowInputParameterModel} from "cwlts/models/generic/WorkflowInputParameterModel";
import {WorkflowModel} from "cwlts/models/generic/WorkflowModel";
import {WorkflowOutputParameterModel} from "cwlts/models/generic/WorkflowOutputParameterModel";
import {JobHelper} from "cwlts/models/helpers/JobHelper";
import {AppMetaManager} from "../../core/app-meta/app-meta-manager";
import {AppMetaManagerToken} from "../../core/app-meta/app-meta-manager-factory";
import {AppHelper} from "../../core/helpers/AppHelper";
import {DirectiveBase} from "../../util/directive-base/directive-base";
import {SVGExecutionProgressPlugin} from "../svg-execution-progress-plugin/svg-execution-progress-plugin";
import {SVGJobFileDropPlugin} from "../../workflow-editor/svg-plugins/job-file-drop/job-file-drop";
import {SVGRequiredInputMarkup} from "../../workflow-editor/svg-plugins/required-input-markup/required-input-markup";
import {EditorInspectorService} from "../../editor-common/inspector/editor-inspector.service";
import {Store} from "@ngrx/store";
import {StepExecution} from "../../execution/models";
import {findParentInputOfType} from "../utilities/dom";
import {map, distinctUntilChanged, filter, take} from "rxjs/operators";
@Component({
selector: "ct-graph-job-editor",
templateUrl: "./graph-job-editor.component.html",
styleUrls: ["./graph-job-editor.component.scss"]
})
export class GraphJobEditorComponent extends DirectiveBase implements AfterViewInit, OnChanges, OnDestroy {
@Input()
appID: string;
@Input()
model: WorkflowModel;
@Output()
draw = new EventEmitter<any>();
inspectedNode: StepModel | WorkflowOutputParameterModel | WorkflowInputParameterModel = null;
relativePathRoot: string;
@ViewChild("canvas")
private canvas: ElementRef;
@ViewChild("inspector", {read: TemplateRef})
private inspectorTemplate: TemplateRef<any>;
private graph: WorkflowGraph;
private jobControl = new FormControl({});
private inspectedInputs = [];
constructor(private inspector: EditorInspectorService,
private store: Store<any>,
@Inject(AppMetaManagerToken) private metaManager: AppMetaManager) {
super();
}
onNativeBrowserDrop(event) {
for (let i = 0; i < event.dataTransfer.files.length; i++) {
// This is only supported in Chrome, Firefox and Microsoft Edge
if (event.dataTransfer.items && typeof event.dataTransfer.items[0].webkitGetAsEntry) {
const path = event.dataTransfer.files[i].path;
const type = event.dataTransfer.items[0].webkitGetAsEntry().isDirectory ? "directory" : "file";
this.onDrop(event.target, {
name: path,
type
});
}
}
}
onDrop(element, data: { name: string, type: "cwl" | "file" | "directory" }) {
if (!AppHelper.isLocal(data.name)) {
return;
}
const type = data.type === "directory" ? "Directory" : "File";
const inputEl = findParentInputOfType(element, type);
if (!inputEl) {
return;
}
const inputID = inputEl.getAttribute("data-id");
const inputModel = this.model.inputs.find(input => input.id === inputID) as WorkflowInputParameterModel;
const inputTypeIsArray = inputModel.type.type === "array";
const job = this.jobControl.value;
const inputJobValue = job[inputID];
const jobValueIsArray = Array.isArray(inputJobValue);
const newEntry = {class: type, path: data.name};
const patch = {[inputID]: newEntry} as any;
if (jobValueIsArray) {
patch[inputID] = inputJobValue.concat(newEntry);
} else if (inputTypeIsArray) {
patch[inputID] = [newEntry];
} else {
patch[inputID] = newEntry;
}
const newJob = {...job, ...patch};
this.jobControl.patchValue(newJob);
}
ngOnChanges(changes: SimpleChanges) {
if (changes["appID"]) {
this.relativePathRoot = AppHelper.isLocal(this.appID) ? AppHelper.getDirname(this.appID) : undefined;
}
}
ngAfterViewInit() {
this.graph = new WorkflowGraph({
svgRoot: this.canvas.nativeElement,
model: this.model as any,
plugins: [
new SVGEdgeHoverPlugin(),
new SelectionPlugin(),
new ZoomPlugin(),
new SVGNodeMovePlugin(),
new SVGJobFileDropPlugin(),
new SVGArrangePlugin(),
new SVGRequiredInputMarkup(),
new SVGExecutionProgressPlugin(),
]
});
this.graph.fitToViewport();
this.draw.emit(this);
this.jobControl.valueChanges.pipe(
map(v => this.normalizeJob(v))
// comment for now because JSON.stringify returns null for NaN and Infinity (then not working for numbers)
// distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b))
).subscribeTracked(this, changes => {
this.graph.getPlugin(SVGJobFileDropPlugin).updateToJobState(changes);
this.metaManager.patchAppMeta("job", changes);
});
this.metaManager.getAppMeta("job").pipe(
filter(Boolean) // job might be empty if we havent modified anything yet
).subscribeTracked(this, job => {
const markupPlugin = this.graph.getPlugin(SVGRequiredInputMarkup);
const missingInputConnectionIDs = this.model.inputs
.filter(input => !input.type.isNullable && (job[input.id] === null || job[input.id] === undefined))
.map(input => input.connectionId);
markupPlugin.markMissing(...missingInputConnectionIDs);
});
this.metaManager.getAppMeta("job").pipe(
take(1)
).subscribeTracked(this, storedJob => {
this.updateJob(storedJob);
});
this.store.select("jobEditor", "progress", this.appID, "stepExecution").pipe(
distinctUntilChanged()
).subscribeTracked(this, (states: StepExecution[] = []) => {
const update = states.reduce((acc, step) => ({...acc, [step.id]: step.state}), {});
this.graph.getPlugin(SVGExecutionProgressPlugin).setAllStates(update);
});
}
private normalizeJob(jobObject: Object) {
const nullJob = JobHelper.getNullJobInputs(this.model);
const job = jobObject || {};
for (const key in job) {
if (!nullJob.hasOwnProperty(key)) {
delete job[key];
}
}
return {...nullJob, ...job};
}
updateJob(jobObject = {}) {
const normalizedJob = this.normalizeJob(jobObject);
const controlValue = normalizedJob;
this.jobControl.patchValue(controlValue, {emitEvent: false});
this.graph.getPlugin(SVGJobFileDropPlugin).updateToJobState(controlValue);
// If we modified the job, push the update back
if (JSON.stringify(normalizedJob) !== JSON.stringify(jobObject)) {
this.metaManager.patchAppMeta("job", normalizedJob);
}
}
openInspector(ev: Event) {
let current = ev.target as Element;
// Check if clicked element is a node or any descendant of a node (in order to open object inspector if so)
while (current !== this.canvas.nativeElement) {
if (current.classList.contains("node")) {
this.openNodeInInspector(current);
break;
}
current = current.parentNode as Element;
}
}
/**
* Returns type of inspected node to determine which template to render for object inspector
*/
typeOfInspectedNode() {
if (this.inspectedNode instanceof StepModel) {
return "Step";
} else if (this.inspectedNode instanceof WorkflowInputParameterModel) {
return "Input";
} else {
return "Output";
}
}
/**
* Open node in object inspector
*/
private openNodeInInspector(node: Element) {
let typeOfNode = "steps";
if (node.classList.contains("input")) {
typeOfNode = "inputs";
} else if (node.classList.contains("output")) {
typeOfNode = "outputs";
} else {
}
this.inspectedNode = this.model[typeOfNode].find((input) => input.id === node.getAttribute("data-id"));
switch (typeOfNode) {
case "inputs":
this.inspectedInputs = [this.inspectedNode];
break;
case "outputs":
this.inspectedInputs = [];
break;
default:
this.inspectedInputs = (this.inspectedNode as StepModel).in.filter(n => n.status === "exposed");
break;
}
this.inspector.show(this.inspectorTemplate, this.inspectedNode.id);
}
inspectStep(nodeID: string) {
const el = this.graph.svgRoot.querySelector(`[data-connection-id="${nodeID}"]`);
if (el) {
this.openNodeInInspector(el);
}
const selectionPlugin = this.graph.getPlugin(SelectionPlugin);
if (selectionPlugin) {
selectionPlugin.selectStep(nodeID);
}
}
ngOnDestroy(): void {
super.ngOnDestroy();
if (this.graph) {
this.graph.destroy();
}
this.inspector.hide();
}
}