forked from neos/neos-ui
/
index.js
230 lines (202 loc) · 8.6 KB
/
index.js
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
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
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {$get} from 'plow-js';
import mergeClassNames from 'classnames';
import {Tree, Icon} from '@neos-project/react-ui-components';
import {actions, selectors} from '@neos-project/neos-ui-redux-store';
import {SelectionModeTypes} from '@neos-project/neos-ts-interfaces';
import {dndTypes} from '@neos-project/neos-ui-constants';
import {PageTreeNode, ContentTreeNode} from './Node/index';
import style from './style.module.css';
import {neos} from '@neos-project/neos-ui-decorators';
const ConnectedDragLayer = connect((state, {currentlyDraggedNodes}) => {
const getNodeByContextPath = selectors.CR.Nodes.nodeByContextPath(state);
return {
currentlyDraggedNodes: currentlyDraggedNodes ? currentlyDraggedNodes.map(contextPath => getNodeByContextPath(contextPath)) : []
};
})(Tree.DragLayer);
export default class NodeTree extends PureComponent {
static propTypes = {
ChildRenderer: PropTypes.func,
rootNode: PropTypes.object,
allowOpeningNodesInNewWindow: PropTypes.bool,
nodeTypeRole: PropTypes.string,
toggle: PropTypes.func,
collapseAll: PropTypes.func,
focus: PropTypes.func,
requestScrollIntoView: PropTypes.func,
setActiveContentCanvasSrc: PropTypes.func,
setActiveContentCanvasContextPath: PropTypes.func,
moveNodes: PropTypes.func,
allCollapsableNodes: PropTypes.object,
loadingDepth: PropTypes.number
};
state = {
currentlyDraggedNodes: []
};
handleToggle = contextPath => {
const {toggle} = this.props;
toggle(contextPath);
}
handleCollapseAll = () => {
const {collapseAll, allCollapsableNodes, rootNode, loadingDepth} = this.props
let nodeContextPaths = []
const collapsedByDefaultNodesContextPaths = []
Object.values(allCollapsableNodes).forEach(node => {
const collapsedByDefault = loadingDepth === 0 ? false : node.depth - rootNode.depth >= loadingDepth
if (collapsedByDefault) {
collapsedByDefaultNodesContextPaths.push(node.contextPath)
} else {
nodeContextPaths.push(node.contextPath)
}
});
// Do not Collapse RootNode
nodeContextPaths = nodeContextPaths.filter(i => i !== rootNode.contextPath);
collapseAll(nodeContextPaths, collapsedByDefaultNodesContextPaths);
}
handleFocus = (contextPath, metaKeyPressed, altKeyPressed, shiftKeyPressed) => {
const {focus} = this.props;
if (altKeyPressed) {
return;
}
const selectionMode = shiftKeyPressed ? SelectionModeTypes.RANGE_SELECT : (metaKeyPressed ? SelectionModeTypes.MULTIPLE_SELECT : SelectionModeTypes.SINGLE_SELECT);
focus(contextPath, undefined, selectionMode);
}
handleClick = (src, contextPath, metaKeyPressed, altKeyPressed, shiftKeyPressed) => {
const {setActiveContentCanvasSrc, setActiveContentCanvasContextPath, requestScrollIntoView} = this.props;
if (altKeyPressed) {
window.open(window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '') + window.location.pathname + '?node=' + contextPath);
return;
}
if (metaKeyPressed || shiftKeyPressed) {
return;
}
// Set a flag that will imperatively tell ContentCanvas to scroll to focused node
if (requestScrollIntoView) {
requestScrollIntoView(true);
}
if (setActiveContentCanvasSrc) {
setActiveContentCanvasSrc(src);
}
if (setActiveContentCanvasContextPath) {
setActiveContentCanvasContextPath(contextPath);
}
}
handleDrag = node => {
this.setState({
currentlyDraggedNodes:
this.props.focusedNodesContextPaths.includes(node.contextPath) ?
this.props.focusedNodesContextPaths :
[node.contextPath] // moving a node outside of focused nodes
});
}
handeEndDrag = () => {
this.setState({
currentlyDraggedNodes: []
});
}
handleDrop = (targetNode, position) => {
const {currentlyDraggedNodes} = this.state;
const {moveNodes, focus} = this.props;
moveNodes(currentlyDraggedNodes, $get('contextPath', targetNode), position);
// We need to refocus the tree, so all focus would be reset, because its context paths have changed while moving
// Could be removed with the new CR
focus($get('contextPath', targetNode));
this.setState({
currentlyDraggedNodes: []
});
}
render() {
const {rootNode, ChildRenderer} = this.props;
if (!rootNode) {
return (
<div className={style.loader}>
<Icon icon="spinner" spin={true} />
</div>
);
}
const classNames = mergeClassNames({
[style.pageTree]: true
});
return (
<Tree className={classNames}>
<a
role="button"
onClick={this.handleCollapseAll}
className={style.collapseAll}
>
Collapse All
</a>
<ConnectedDragLayer
nodeDndType={dndTypes.NODE}
ChildRenderer={ChildRenderer}
currentlyDraggedNodes={this.state.currentlyDraggedNodes}
/>
<ChildRenderer
ChildRenderer={ChildRenderer}
nodeDndType={dndTypes.NODE}
node={rootNode}
level={1}
onNodeToggle={this.handleToggle}
onToggleChildren={this.handleToggleChildren}
onNodeClick={this.handleClick}
onNodeFocus={this.handleFocus}
onNodeDrag={this.handleDrag}
onNodeDrop={this.handleDrop}
onNodeEndDrag={this.handeEndDrag}
currentlyDraggedNodes={this.state.currentlyDraggedNodes}
/>
</Tree>
);
}
}
const withNodeTypeRegistry = neos(globalRegistry => ({
nodeTypesRegistry: globalRegistry.get('@neos-project/neos-ui-contentrepository')
}));
export const PageTree = withNodeTypeRegistry(connect(
(state, {neos, nodeTypesRegistry}) => {
const documentNodesSelector = selectors.CR.Nodes.makeGetCollapsableDocumentNodes(nodeTypesRegistry);
return ({
rootNode: selectors.CR.Nodes.siteNodeSelector(state),
focusedNodesContextPaths: selectors.UI.PageTree.getAllFocused(state),
ChildRenderer: PageTreeNode,
allowOpeningNodesInNewWindow: true,
loadingDepth: neos.configuration.structureTree.loadingDepth,
allCollapsableNodes: documentNodesSelector(state)
})
}, {
toggle: actions.UI.PageTree.toggle,
collapseAll: actions.UI.PageTree.collapseAll,
focus: actions.UI.PageTree.focus,
setActiveContentCanvasSrc: actions.UI.ContentCanvas.setSrc,
setActiveContentCanvasContextPath: actions.CR.Nodes.setDocumentNode,
moveNodes: actions.CR.Nodes.moveMultiple,
requestScrollIntoView: null,
isContentTree: false
}, (stateProps, dispatchProps, ownProps) => {
return Object.assign({}, stateProps, dispatchProps, ownProps);
}
)(NodeTree));
export const ContentTree = withNodeTypeRegistry(connect(
(state, {neos, nodeTypesRegistry}) => {
const contentNodesSelector = selectors.CR.Nodes.makeGetCollapsableContentNodes(nodeTypesRegistry);
return ({
rootNode: selectors.CR.Nodes.documentNodeSelector(state),
focusedNodesContextPaths: selectors.CR.Nodes.focusedNodePathsSelector(state),
ChildRenderer: ContentTreeNode,
allowOpeningNodesInNewWindow: false,
loadingDepth: neos.configuration.structureTree.loadingDepth,
allCollapsableNodes: contentNodesSelector(state)
})
}, {
toggle: actions.UI.ContentTree.toggle,
collapseAll: actions.UI.ContentTree.collapseAll,
focus: actions.CR.Nodes.focus,
moveNodes: actions.CR.Nodes.moveMultiple,
requestScrollIntoView: actions.UI.ContentCanvas.requestScrollIntoView,
isContentTree: true
}, (stateProps, dispatchProps, ownProps) => {
return Object.assign({}, stateProps, dispatchProps, ownProps);
}
)(NodeTree));