ContainmentCanvas.jsx 7.53 KB
Newer Older
Tamas Kecskes's avatar
Tamas Kecskes committed
1
2
3
4
import React from 'react';
import PropTypes from 'prop-types';
import {DropTarget} from 'react-dnd';

5
import SingleConnectedNode from '../SingleConnectedNode/SingleConnectedNode';
Tamas Kecskes's avatar
Tamas Kecskes committed
6
import DRAG_TYPES from '../../utils/dragTypes';
7
import {SVGRegistryBasedCanvasItem} from '../SVGRegistryBasedCanvasItem';
8
9
import ConnectionManager from '../ConnectionManager/ConnectionManager';
import BasicConnectingComponent from '../ConnectionManager/BasicConnectingComponent';
Tamas Kecskes's avatar
Tamas Kecskes committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import BasicEventManager from '../BasicEventManager/BasicEventManager';
import getIndexedName from '../../utils/getIndexedName';

import Z_LEVELS from '../../utils/zLevels';

// TODO we only take loaded children into account
function getChildrenNames(gmeClient, nodeId) {
    const container = gmeClient.getNode(nodeId);
    const childrenIds = container.getChildrenIds();
    const names = [];

    childrenIds.forEach((childId) => {
        const node = gmeClient.getNode(childId);
        if (node) {
            names.push(node.getAttribute('name'));
        }
    });

    return names;
}

const canvasTarget = {
    drop(props, monitor, canvas) {
33
        console.log('dropping...');
Tamas Kecskes's avatar
Tamas Kecskes committed
34
35
        const dragItem = monitor.getItem();
        if (dragItem.move) {
36
            console.log('dropping...move');
Tamas Kecskes's avatar
Tamas Kecskes committed
37
38
39
40
41
42
43
44
45
46
47
48
            const offset = monitor.getDifferenceFromInitialOffset();
            const node = props.gmeClient.getNode(dragItem.gmeId);
            const position = node.getRegistry('position');

            position.x += offset.x / props.scale;
            position.y += offset.y / props.scale;

            position.x = Math.trunc(position.x);
            position.y = Math.trunc(position.y);

            props.gmeClient.setRegistry(dragItem.gmeId, 'position', position);
        } else if (dragItem.create) {
49
            console.log('dropping...create');
Tamas Kecskes's avatar
Tamas Kecskes committed
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
            const dragOffset = monitor.getClientOffset();
            const metaNode = props.gmeClient.getNode(dragItem.gmeId);
            const position = {
                x: ((dragOffset.x - canvas.offset.x) + canvas.props.scrollPos.x) / props.scale,
                y: ((dragOffset.y - canvas.offset.y) + canvas.props.scrollPos.y) / props.scale,
            };
            let name = metaNode.getAttribute('ShortName') || metaNode.getAttribute('name');

            name = getIndexedName(name, getChildrenNames(props.gmeClient, props.activeNode));

            position.x -= dragItem.nodeData.bbox.width / 2;
            position.y -= dragItem.nodeData.bbox.height / 2;

            position.x = Math.trunc(position.x);
            position.y = Math.trunc(position.y);

            // TODO: Fix when client accepts 0
            position.x = position.x > 0 ? position.x : 1;
            position.y = position.y > 0 ? position.y : 1;

            props.gmeClient.createNode({
                parentId: props.activeNode,
                baseId: dragItem.gmeId,
            }, {
                attributes: {
                    name,
                },
                registry: {
                    position,
                },
            });
        }

        canvas.setState({dragMode: 'none'});
    },
    hover(props, monitor, component) {
        const item = monitor.getItem();
        let dragState;
        if (item.create) {
            dragState = 'create';
        }
        if (item.move) {
            dragState = 'move';
        }

        component.setState({dragMode: dragState});
    },
};

function collect(connector, monitor) {
    return {
        connectDropTarget: connector.dropTarget(),
        isOver: monitor.isOver(),
    };
}

class ContainmentCanvas extends SingleConnectedNode {
    static propTypes = {
        gmeClient: PropTypes.object.isRequired,
        scrollPos: PropTypes.object.isRequired,

        activeNode: PropTypes.string.isRequired,
        scale: PropTypes.number.isRequired,

        connectDropTarget: PropTypes.func.isRequired,
        isOver: PropTypes.bool.isRequired,
    };

    state = {
        children: [],
        nodeInfo: {},
        dragMode: 'none',
    };

    cm = null;
    em = null;
126
127
    item = null;
    infoCard = null;
Tamas Kecskes's avatar
Tamas Kecskes committed
128
129
130
131
132
133
134
135
136
137

    offset = {
        x: 0,
        y: 0,
    };

    constructor(props) {
        super(props);
        this.cm = new ConnectionManager();
        this.em = new BasicEventManager();
138
139
140
141
        this.item = React.createElement(SVGRegistryBasedCanvasItem);
        if (this.props.children !== undefined) {
            if (this.props.children[0] !== undefined) {
                this.item = this.props.children[0];
142
143
144
                if (this.props.children[1] !== undefined) {
                    this.infoCard = this.props.children[1];
                }
145
146
147
148
            } else {
                this.item = this.props.children;
            }
        }
Tamas Kecskes's avatar
Tamas Kecskes committed
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
    }

    populateChildren(nodeObj) {
        const childrenIds = nodeObj.getChildrenIds();
        const newChildren = childrenIds.map(id => ({id}));

        this.setState({
            children: newChildren,
            nodeInfo: {
                name: nodeObj.getAttribute('name'),
            },
        });
    }

    onNodeLoad(nodeObj) {
        this.populateChildren(nodeObj, true);
    }

    onNodeUpdate(nodeObj) {
        this.populateChildren(nodeObj);
    }

    onMouseClick = (event) => {
        event.stopPropagation();
        event.preventDefault();
        if (this.cm.isConnecting) {
            this.cm.endConnection();
        }
        this.props.clearSelection();
    };

    onMouseLeave = (event) => {
        event.stopPropagation();
        if (this.cm.isConnecting) {
            this.cm.endConnection();
        }
    };

    onMouseMove = (event) => {
        const {scrollPos} = this.props;
        this.cm.onMouseMove({
            x: event.clientX + (scrollPos.x - this.offset.x),
            y: event.clientY + (scrollPos.y - this.offset.y),
        });
    };

    render() {
        const {connectDropTarget, activeNode, gmeClient} = this.props;
        const {children, dragMode} = this.state;

199
200
201
202
203
204
205
206
207
208
209
        const childrenItems = children.map(child => {
            return React.cloneElement(this.item, {
                key: child.id,
                gmeClient: gmeClient,
                activeNode: child.id,
                contextNode: activeNode,
                connectionManager: this.cm,
                eventManager: this.em,
                scale: 1,
            });
        });
Tamas Kecskes's avatar
Tamas Kecskes committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

        const content = (
            <div
                ref={(canvas) => {
                    if (canvas) {
                        const {offsetLeft, offsetTop} = canvas.offsetParent;

                        this.offset = {
                            x: offsetLeft,
                            y: offsetTop,
                        };
                    }
                }}
                style={{
                    backgroundColor: dragMode === 'create' ? 'lightgreen' : undefined,
                    width: '100%',
                    height: '100%',
                    overflow: 'auto',
                    zIndex: Z_LEVELS.canvas,
                    position: 'absolute',
                }}
                role="presentation"
                onClick={this.onMouseClick}
233
234
                onKeyDown={() => {
                }}
Tamas Kecskes's avatar
Tamas Kecskes committed
235
236
237
238
239
                onContextMenu={this.onMouseClick}
                onMouseLeave={this.onMouseLeave}
                onMouseMove={this.onMouseMove}
            >
                <BasicConnectingComponent connectionManager={this.cm}/>
240
                {childrenItems.length > 0 ? childrenItems : this.infoCard}
Tamas Kecskes's avatar
Tamas Kecskes committed
241
242
243
244
245
246
            </div>);

        return connectDropTarget(content);
    }
}

247
export default DropTarget(DRAG_TYPES.GME_NODE, canvasTarget, collect)(ContainmentCanvas);