aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/app/directive/drag-select/drag-select.directive.ts
blob: 395cf0408f9bc6cb130c540aae27c5d4dceabb22 (plain)
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
/**
 * Copyright (c) 2017 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 */

import { AfterViewInit, Directive, ElementRef } from '@angular/core';
import { JsPlumbService } from "../../services/jsplumb.service";

@Directive({
    selector: '[b4tDragSelect]'
})
export class DragSelectDirective implements AfterViewInit {
    private selectBox: any;
    public selecting = false;

    public relatvieX: number;
    public relatvieY: number;

    public startX: number;
    public startY: number;
    public endX: number;
    public endY: number;

    public left: number = 0;
    public top: number = 0;
    public width: number = 0;
    public height: number = 0;

    constructor(private el: ElementRef, private jsPlumbService: JsPlumbService) { }

    public ngAfterViewInit(): void {
        this.selectBox = document.createElement('div');
        const selectArea = this.el.nativeElement.appendChild(this.selectBox);
        this.el.nativeElement.addEventListener("mousedown", (event: MouseEvent) => this.mouseDown(event));
        this.el.nativeElement.addEventListener("mousemove", (event: MouseEvent) => this.mouseMove(event));
        this.el.nativeElement.addEventListener("mouseup", (event: MouseEvent) => this.mouseUp(event));
    }

    public mouseDown(event: MouseEvent) {
        this.relatvieX = event.clientX - event.offsetX;
        this.relatvieY = event.clientY - event.offsetY;

        this.width = 0;
        this.height = 0;
        this.startX = event.clientX;
        this.startY = event.clientY;
        this.endX = this.startX;
        this.endY = this.startY;
        this.selecting = true;
        this.updateSelectArea();
    }

    public mouseMove(event: MouseEvent) {
        this.endX = event.clientX;
        this.endY = event.clientY;

        this.updateSelectArea();
    }

    public mouseUp(event: MouseEvent) {
        this.selecting = false;
        this.updateSelectArea();
    }

    private updateSelectArea() {
        if (this.selecting) {
            this.selectBox.className = 'selecting';
        } else {
            this.selectBox.className = '';
            return;
        }

        this.getAllSelectedNodes();

        const leftTmp = this.startX >= this.endX ? this.endX : this.startX;
        const topTmp = this.startY >= this.endY ? this.endY : this.startY;

        this.left = leftTmp - this.relatvieX;
        this.top = topTmp - this.relatvieY;

        this.width = Math.abs(this.startX - this.endX);
        this.height = Math.abs(this.endY - this.startY);

        this.selectBox.style.top = this.top + 'px';
        this.selectBox.style.left = this.left + 'px';
        this.selectBox.style.width = this.width + 'px';
        this.selectBox.style.height = this.height + 'px';
    }

    public getAllSelectedNodes() {
        if(!this.selecting) {
            return;
        }
        const selectedNodes = [];

        const nodes = this.el.nativeElement.querySelectorAll('div.node');
        nodes.forEach(node => {
            if(this.checkNodeSelected(node)) {
                selectedNodes.push(node);
            }
        });

        this.jsPlumbService.jsplumbInstance.clearDragSelection();
        this.jsPlumbService.jsplumbInstance.addToDragSelection(selectedNodes);

    }

    private checkNodeSelected(node: any): boolean {
        const nodeLeft = node.offsetLeft;
        const nodeTop = node.offsetTop;
        const nodeRigth = nodeLeft + node.clientWidth;
        const nodeBottom = nodeTop + node.clientHeight;

        const selectedRight = this.left + this.width;
        const selectedBottom = this.top + this.height;

        return this.between(nodeLeft, this.left, selectedRight)
            && this.between(nodeRigth, this.left, selectedRight)
            && this.between(nodeTop, this.top, selectedBottom)
            && this.between(nodeBottom, this.top, selectedBottom);
    }

    private between(value, min, max): boolean {
        return min <= value && value <= max;
    }

}