ContextMenu.jsx 4.02 KB
Newer Older
Patrik Meijer's avatar
Patrik Meijer committed
1
2
/* globals document */
/**
3
 * TODO: Consider moving this outside of the GraphEditor and let it be passed as child instead.
Patrik Meijer's avatar
Patrik Meijer committed
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
 * @author pmeijer / https://github.com/pmeijer
 */
import React, {Component} from 'react';
import PropTypes from 'prop-types';

import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';

export default class ContextMenu extends Component {
    static propTypes = {
        gmeClient: PropTypes.object.isRequired,
        data: PropTypes.object.isRequired,
        nodeId: PropTypes.string.isRequired, // FIXME: should be nodeIds
        eventX: PropTypes.number.isRequired,
        eventY: PropTypes.number.isRequired,
        onClose: PropTypes.func.isRequired,
        setActiveNode: PropTypes.func.isRequired,

        readOnly: PropTypes.bool,
    };

    static defaultProps = {
        readOnly: false,
    }

    setActiveNode = () => {
        const {nodeId, onClose, setActiveNode} = this.props;

        setActiveNode(nodeId);
        onClose();
    }

    deleteNode = () => {
        const {gmeClient, nodeId, onClose} = this.props;

        gmeClient.deleteNode(nodeId);

        onClose();
    }

    render() {
        const {
            gmeClient,
            nodeId,
            eventX,
            eventY,
            readOnly,
            data,
        } = this.props;

        // console.log(data);
        const menuItems = [];
        const nodeObj = gmeClient.getNode(nodeId);
        if (false) {
            const metaNodeObj = gmeClient.getNode(nodeObj.getMetaTypeId());

            menuItems.push((
                <MenuItem key="meta-type" onClick={this.props.onClose} disabled={readOnly}>
                    {`<<${metaNodeObj.getAttribute('name')}>>`}
                </MenuItem>));

            if (metaNodeObj.getAttribute('name') === 'System') {
                menuItems.push((
                    <MenuItem key="open-sub-system" onClick={this.setActiveNode} >
                        Open Subsystem ...
                    </MenuItem>));
            }
        } else if (data.memberAttrs && data.memberAttrs.length > 0) {
            let args;
            data.memberAttrs.forEach((memAttr) => {
                if (memAttr.name === 'args') {
                    args = memAttr.value ? `(${memAttr.value})` : '()';
                }
            });

            if (args) {
                menuItems.push((
                    <MenuItem key="invocation-args" onClick={this.props.onClose}>
                        Invocation arguments: {args}
                    </MenuItem>));
            }
        } else if (data.pointerName === 'post' || data.pointerName === 'pre') {
            const methodNode = gmeClient.getNode(data.source);
            const modeNode = gmeClient.getNode(data.target);
            if (methodNode && modeNode) {
                // const parentName = gmeClient.getNode(modeNode.getParentId()).getAttribute('name');
                // const modeName = modeNode.getAttribute('name');
                const methodName = methodNode.getAttribute('name');
                menuItems.push((
                    <MenuItem key="pre-post-condition" onClick={this.props.onClose}>
                        {`${data.pointerName}-condition for ${methodName}`}
                    </MenuItem>));
            }
        } else if (data.setName === 'network-nodes') {
            menuItems.push((
                <MenuItem key="cell-network-nodes" onClick={this.props.onClose}>
                    {`${data.setName}`}
                </MenuItem>));
        }

        if (menuItems.length === 0) {
            this.props.onClose();
            return <div/>;
        }

        return (
            <Menu
                id="simple-menu"
                anchorEl={document.body}
                style={{
                    position: 'absolute',
                    top: eventY - (document.body.clientHeight / 2),
                    left: eventX,
                }}
                open
                onClose={this.props.onClose}
            >
                {menuItems}
            </Menu>
        );
    }
}