SubTreeWatcher.jsx 7.53 KB
Newer Older
Patrik Meijer's avatar
Patrik Meijer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * This component will create a territory of depth 10 (by default)
 * and which ever react component is a child of the SubTreeWatcher will receive
 * an object "nodes" as property that contains information of all nodes in the subtree
 * starting from the activeNode. The "nodes" object is a map from path to info where info
 * is of the format:
 * {
 *   parent: nodeObj.getParentId(),
 *   metaType: getMetaTypeName(nodeObj),
 *   attributes: {
 *       name: 'FCO'
 *   },
 *   registries: {
 *       position: {x: 100, y: 100}
 *   },
 *   pointers: {
 *       ptr: '/a/target/path'
 *   },
 *   sets: {
 *       // In order to get the memberAttr options.setMemberAttributes need to be set.
 *       mySet: [{id: '/a/member/path', memberAttr: []}, {id: '/another/member/path', memberAttr: []}]
 *   },
 * },
Patrik Meijer's avatar
Patrik Meijer committed
24
25
26
 *
 * For example of usage see GraphEditor/demo.jsx
 *
Patrik Meijer's avatar
Patrik Meijer committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 * @author pmeijer / https://github.com/pmeijer
 */
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import update from 'immutability-helper';

import Territory from '../Territory';

const DEFAULT_OPTIONS = {
    // Array of attribute names to extract - null (nodeObj.getAttributeNames).
    attributeNames: null,
    // Array of registry names to extract - null (nodeObj.getRegistryNames).
    registryNames: null,
    // Array of pointer names to extract - null is all defined pointers (nodeObj.getSetNames).
    pointerNames: null,
    // Array of set names to extract as edges - default is all defined sets (nodeObj.getSetNames).
    setNames: null,
    // Mapping from set name to set attribute name
    setMemberAttributes: {},
46
    // The depth of the territory, i.e. the number of levels of containment-hierarchy to load.
Patrik Meijer's avatar
Patrik Meijer committed
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
    depth: 10,
};

export default class SubTreeWatcher extends Component {
    static propTypes = {
        gmeClient: PropTypes.object.isRequired,
        activeNode: PropTypes.string,
        options: PropTypes.object,
        children: PropTypes.object.isRequired,
    };

    static defaultProps = {
        activeNode: null,
        options: {},
    };

    constructor(props) {
        super(props);
        this.options = Object.assign(DEFAULT_OPTIONS, props.options);
        const {activeNode} = this.props;
        this.state = {
            territory: (() => {
                if (typeof activeNode === 'string') {
                    return {
                        [activeNode]: {children: this.options.depth},
                    };
                }

                return {};
            })(),
            children: {},
        };
    }

    componentWillReceiveProps(newProps) {
        const {activeNode} = newProps;

        if (newProps.options !== this.props.options) {
            this.options = Object.assign(DEFAULT_OPTIONS, newProps.options);
        }

        if (activeNode !== this.props.activeNode) {
            this.setState({
                territory: {
                    [activeNode]: {children: this.options.depth},
                },
                children: {},
            });
        }
    }

    handleEvents = (hash, loads, updates, unloads) => {
        const {gmeClient, activeNode} = this.props;
        const {options} = this;
        const updateDesc = {};
        const activeNodeObj = gmeClient.getNode(activeNode);

        if (!activeNodeObj) {
            this.setState({children: {}});
            return;
        }

        const getMembers = (nodeObj, setName) => {
            let result;
            if (nodeObj.getSetNames().includes(setName)) {
                result = nodeObj.getMemberIds(setName).map((id) => {
                    const entry = {
                        id,
115
                        setMemberAttributes: {},
Patrik Meijer's avatar
Patrik Meijer committed
116
117
118
119
                    };

                    if (options.setMemberAttributes[setName]) {
                        options.setMemberAttributes[setName]
120
121
                            .forEach((name) => {
                                entry.setMemberAttributes[name] = nodeObj.getMemberAttribute(setName, id, name) || '';
Patrik Meijer's avatar
Patrik Meijer committed
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
                            });
                    }

                    return entry;
                });
            }

            return result;
        };

        const getMetaTypeName = (nodeObj) => {
            const metaId = nodeObj.getMetaTypeId();

            if (metaId) {
                const metaObj = nodeObj.getNode(nodeObj.getMetaTypeId());
                return metaObj.getAttribute('name');
            }

            return 'N/A';
        };

        loads.forEach((nodeId) => {
            const nodeObj = gmeClient.getNode(nodeId);

            updateDesc[nodeId] = {
                $set: {
                    parent: nodeObj.getParentId(),
                    metaType: getMetaTypeName(nodeObj),
                    attributes: {},
                    registries: {},
                    pointers: {},
                    sets: {},
                },
            };

            (options.attributeNames || nodeObj.getAttributeNames()).forEach((name) => {
                updateDesc[nodeId].$set.attributes[name] = nodeObj.getAttribute(name);
            });

            (options.registryNames || nodeObj.getRegistryNames()).forEach((name) => {
                updateDesc[nodeId].$set.registries[name] = nodeObj.getRegistry(name);
            });

            (options.pointerNames || nodeObj.getPointerNames()).forEach((name) => {
                updateDesc[nodeId].$set.pointers[name] = nodeObj.getPointerId(name);
            });

            (options.setNames || nodeObj.getValidSetNames()).forEach((name) => {
                updateDesc[nodeId].$set.sets[name] = getMembers(nodeObj, name);
            });
        });

        updates.forEach((nodeId) => {
            const nodeObj = gmeClient.getNode(nodeId);
            updateDesc[nodeId] = {
                metaType: {
                    $set: getMetaTypeName(nodeObj),
                },
                attributes: {},
                registries: {},
                pointers: {},
                sets: {},
            };

            if (updateDesc[nodeId].metaType);

            (options.attributeNames || nodeObj.getAttributeNames()).forEach((name) => {
                updateDesc[nodeId].attributes[name] = {$set: nodeObj.getAttribute(name)};
            });

            (options.registryNames || nodeObj.getRegistryNames()).forEach((name) => {
                updateDesc[nodeId].registries[name] = {$set: nodeObj.getRegistry(name)};
            });

            (options.pointerNames || nodeObj.getPointerNames()).forEach((name) => {
                updateDesc[nodeId].pointers[name] = {$set: nodeObj.getPointerId(name)};
            });

            (options.setNames || nodeObj.getValidSetNames()).forEach((name) => {
                updateDesc[nodeId].sets[name] = {$set: getMembers(nodeObj, name)};
            });
        });

        updateDesc.$unset = unloads;

        const {children} = this.state;
        const newState = {children: update(children, updateDesc)};

        this.setState(newState);
    };

    render() {
        const {territory, children} = this.state;
        const {gmeClient} = this.props;

        return (
            <div style={{width: '100%', height: '100%'}}>
                <Territory
                    gmeClient={gmeClient}
                    territory={territory}
                    onUpdate={this.handleEvents}
                    onlyActualEvents
                    reuseTerritory={false}
                />
                {React.cloneElement(
                    React.Children.only(this.props.children),
                    Object.assign({}, this.props, {nodes: children}),
                )}
            </div>);
    }
}