18874: Merge commit '6f8dcb2b13f3058db656908fb26b09e23b527f08' into 18874-merge-wb2
[arvados.git] / services / workbench2 / src / lib / cwl-svg / plugins / deletion / deletion.ts
1 import {PluginBase} from "../plugin-base";
2 import {SelectionPlugin} from "../selection/selection";
3 import {StepModel, WorkflowInputParameterModel, WorkflowOutputParameterModel} from "cwlts/models";
4
5 export class DeletionPlugin extends PluginBase {
6
7     private boundDeleteFunction = this.onDelete.bind(this);
8
9     afterRender(): void {
10         this.attachDeleteBehavior();
11     }
12
13     onEditableStateChange(enable: boolean) {
14         if (enable) {
15             this.attachDeleteBehavior();
16         } else {
17             this.detachDeleteBehavior();
18         }
19     }
20
21     private attachDeleteBehavior() {
22
23         this.detachDeleteBehavior();
24         window.addEventListener("keyup", this.boundDeleteFunction, true);
25     }
26
27     private detachDeleteBehavior() {
28         window.removeEventListener("keyup", this.boundDeleteFunction, true);
29     }
30
31     private onDelete(ev: KeyboardEvent) {
32         if ((ev.which !== 8 && ev.which !== 46) || !(ev.target instanceof SVGElement)) {
33             return;
34         }
35
36         this.deleteSelection();
37     }
38
39     public deleteSelection() {
40         const selection = this.workflow.getPlugin(SelectionPlugin);
41
42         if (!selection || !this.workflow.editingEnabled) {
43             return;
44         }
45
46         const selected = selection.getSelection();
47         selected.forEach((type, id) => {
48             if (type === "node") {
49                 const model = this.workflow.model.findById(id);
50
51                 if (model instanceof StepModel) {
52                     this.workflow.model.removeStep(model);
53                     selection.clearSelection();
54
55                 } else if (model instanceof WorkflowInputParameterModel) {
56                     this.workflow.model.removeInput(model);
57                     selection.clearSelection();
58
59                 } else if (model instanceof WorkflowOutputParameterModel) {
60
61                     this.workflow.model.removeOutput(model);
62                     selection.clearSelection();
63                 }
64             } else {
65                 const [source, destination] = id.split(SelectionPlugin.edgePortsDelimiter);
66                 this.workflow.model.disconnect(source, destination);
67                 selection.clearSelection();
68             }
69         });
70     }
71
72     destroy() {
73         this.detachDeleteBehavior();
74     }
75 }