PartBrowser.jsx 8.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// https://github.com/alexcurtis/react-treebeard
import React from 'react';
import PropTypes from 'prop-types';

import {Treebeard} from 'react-treebeard';
import ExpansionPanel from '@material-ui/core/ExpansionPanel';
import ExpansionPanelDetails from '@material-ui/core/ExpansionPanelDetails';
import ExpansionPanelSummary from '@material-ui/core/ExpansionPanelSummary';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import Typography from '@material-ui/core/Typography';

import SingleConnectedNode from '../SingleConnectedNode';
import {nameSort} from '../../utils/getObjectSorter';

import {treeBeardTheme, getTreeDecorators} from './TreeOverrides';
Patrik Meijer's avatar
Patrik Meijer committed
16
17
18
19
import PartBrowserItem from './PartBrowserItem';
import PartBrowserDragPreview from './PartBrowserDragPreview';

import defaultSVG from '../../utils/defaultSVG';
20
21
22
23
24
25
26
27
28
29
30

const TREE_PATH_SEP = '$';
const EXPAND_ALL = false;

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

        activeNode: PropTypes.string.isRequired,
        scale: PropTypes.number.isRequired,
        minimized: PropTypes.bool.isRequired,
Patrik Meijer's avatar
Patrik Meijer committed
31
32
33
34
35
36
37
38
39
40
41
42
43

        getTreePath: PropTypes.func,
        getTitle: PropTypes.func,
    };

    static defaultProps = {
        getTreePath: (metaNode) => {
            const modelicaUri = metaNode.getAttribute('ModelicaURI');
            return modelicaUri ? modelicaUri.split('.')
                .slice(1)
                .join('$') : null;
        },
        getTitle: metaNode => metaNode.getAttribute('ShortName') || metaNode.getAttribute('name'),
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
    };

    state = {
        loaded: false,
        validChildren: [],
        cursor: null,
    };

    constructor(props) {
        super(props);
        const {scale} = this.props;

        this.tree = {};
        this.items = [];
        this.decorators = getTreeDecorators(PartBrowserItem, {scale});
    }

    componentWillUpdate(nextProps, nextState) {
        // TODO: Revise this check when/if incremental updates to validChildren will be done.
        const {validChildren} = nextState;
        if (validChildren !== this.state.validChildren) {
            // Build up a new tree structure.
            this.tree = {
                children: [],
                folders: {},
                path: 'ROOT',
            };

            validChildren
                .forEach((childDesc) => {
                    let treeNode = this.tree;
                    this.items.push(childDesc);

                    if (typeof childDesc.treePath === 'string') {
                        childDesc.treePath.split(TREE_PATH_SEP)
                            .forEach((path, i, arr) => {
                                if (i === arr.length - 1) {
                                    treeNode.children.push(childDesc);
                                } else {
                                    if (!treeNode.folders[path]) {
                                        treeNode.folders[path] = {
                                            isFolder: true,
                                            isRoot: i === 0,
                                            toggled: EXPAND_ALL || i === 0,
                                            name: path,
                                            path: `${treeNode.path}$${path}`,
                                            description: 'TODO: Fetch info',
                                            folders: {},
                                            children: [],
                                        };

                                        treeNode.children.push(treeNode.folders[path]);
                                    }

                                    treeNode = treeNode.folders[path];
                                }
                            });
                    } else {
                        treeNode.children.push(childDesc);
                    }
                });
        }
    }

    updateValidChildren(validChildrenMap) {
Patrik Meijer's avatar
Patrik Meijer committed
109
110
111
112
113
114
        const {
            gmeClient,
            getTreePath,
            getTitle,
        } = this.props;

115
116
117
118
119
120
        const validChildren = Object.keys(validChildrenMap)
            .map((id) => {
                const metaNode = gmeClient.getNode(id);

                return {
                    id: metaNode.getId(),
Patrik Meijer's avatar
Patrik Meijer committed
121
122
123
                    name: getTitle(metaNode),
                    treePath: getTreePath(metaNode),
                    svg: defaultSVG,
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
233
                };
            });

        this.setState({
            loaded: true,
            validChildren,
            validChildrenMap,
        });
    }

    onNodeLoad(nodeObj) {
        this.updateValidChildren(nodeObj.getValidChildrenTypesDetailed());
    }

    onNodeUpdate(nodeObj) {
        const newValidChildrenMap = nodeObj.getValidChildrenTypesDetailed();
        const currentValidChildrenMap = this.state.validChildren;
        const [newIds, prevIds] = [newValidChildrenMap, currentValidChildrenMap].map(Object.keys);

        if (newIds.length !== prevIds.length) {
            this.updateValidChildren(newValidChildrenMap);
        } else {
            for (let i = 0; i < newValidChildrenMap.length; i += 1) {
                if (typeof currentValidChildrenMap[newValidChildrenMap[i]] !== 'string') {
                    this.updateValidChildren(newValidChildrenMap);
                    break;
                }
            }
        }
    }

    onStateChanged() {
        const {gmeClient, activeNode} = this.props;

        if (this.state.loaded) {
            this.onNodeUpdate(gmeClient.getNode(activeNode));
        }
    }

    onNodeUnload() {
        this.setState({validChildren: []});
    }

    onTreeNodeToggle = (node, toggled) => {
        if (this.state.cursor) {
            this.state.cursor.active = false;
        }

        /* eslint-disable */
        node.active = true;
        if (node.children) {
            node.toggled = toggled;
            if (node.children.length === 1 && node.children[0].isFolder) {
                node.children[0].toggled = true;
            }
        }
        /* eslint-enable */

        this.setState({cursor: node});
    };

    buildTreeStructure = (treeNode) => {
        const {scale} = this.props;

        if (treeNode.isFolder) {
            if (treeNode.isRoot) {
                return (
                    <ExpansionPanel key={treeNode.path} defaultExpanded>
                        <ExpansionPanelSummary expandIcon={<ExpandMoreIcon/>}>
                            <Typography variant="subheading">{treeNode.name}</Typography>
                        </ExpansionPanelSummary>
                        <ExpansionPanelDetails style={{
                            display: 'block',
                            padding: 0,
                            paddingBottom: 10,
                        }}
                        >
                            <Treebeard
                                data={treeNode}
                                onToggle={this.onTreeNodeToggle}
                                decorators={this.decorators}
                                style={treeBeardTheme}
                            />
                        </ExpansionPanelDetails>
                    </ExpansionPanel>);
            }
            return (
                treeNode.children.sort(nameSort)
                    .map(this.buildTreeStructure)
            );
        }
        return (
            <PartBrowserItem key={treeNode.id} nodeData={treeNode} scale={scale}/>
        );
    };

    render() {
        const {minimized, scale} = this.props;
        let cnt = 0;

        if (!this.state.loaded) {
            return null;
        }

        return (
            <div style={{
                width: '100%',
                textAlign: minimized ? 'center' : undefined,
            }}
            >
Patrik Meijer's avatar
Patrik Meijer committed
234
                <PartBrowserDragPreview scale={scale}/>
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
                <div style={{display: minimized ? 'none' : undefined}}>
                    {this.tree.children.map(this.buildTreeStructure)}
                </div>
                <div style={{display: minimized ? undefined : 'none'}}>
                    {this.items
                        .filter((/* child */) => {
                            // TODO: These should be filtered based on recent components used..
                            cnt += 1;
                            return cnt <= 10;
                        })
                        .map(child => (
                            <PartBrowserItem
                                key={child.id}
                                nodeData={child}
                                scale={scale}
                                listView
                            />))
                    }
                </div>
            </div>
        );
    }
}

export default PartBrowser;