This repository was archived by the owner on Feb 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmenuNode.jsx
More file actions
128 lines (112 loc) · 4.86 KB
/
menuNode.jsx
File metadata and controls
128 lines (112 loc) · 4.86 KB
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
import React, { PropTypes } from 'react'
import Collapse from 'react-collapse'
const propTypes = {
data: PropTypes.object.isRequired,
nodesAreLinks: PropTypes.bool.isRequired,
nodeDeepness: PropTypes.number.isRequired,
apiChildPath: PropTypes.string.isRequired,
apiChildParameters: PropTypes.string.isRequired,
selectedNode: PropTypes.string
}
const defaultProps = {
selectedNode: ''
}
export default class MenuNode extends React.Component {
constructor (props) {
super(props)
this.state = {
nodeData: props.data,
isNodeHovered: false
}
}
handlerOnClickExpandPicto = (e) => {
e.stopPropagation()
const { nodeData } = this.state
if (nodeData.type !== 'workspace' && nodeData.type !== 'folder') return
if (nodeData.state.opened) {
return this.setState({...this.state, nodeData: {...nodeData, state: {...nodeData.state, opened: false}, children: true}})
}
if (nodeData.type === 'folder' && nodeData.children === false) {
return this.setState({...this.state, nodeData: {...nodeData, state: {...nodeData.state, opened: true}}})
}
// bellow getChildTreeUrl is a bit unnatural because it was a bit complicated to handle GET parameters from tracim to sidebarleft
const getChildTreeUrl = this.props.apiChildPath + '?id=' + nodeData.id + (this.props.apiChildParameters !== ''
? '&' + this.props.apiChildParameters.substr(1) // substr(1) removes the first '?'
: ''
)
fetch(getChildTreeUrl, {
method: 'GET',
headers: { 'Accept': 'application/json' },
credentials: 'include'
})
.then(response => response.json())
.then(json => this.setState({...this.state, nodeData: {...nodeData, state: {...nodeData.state, opened: true}, children: json.d}}))
}
handleMouseEnter = () => this.setState({...this.state, isNodeHovered: true})
handleMouseLeave = () => this.setState({...this.state, isNodeHovered: false})
render () {
const { nodesAreLinks, apiChildPath, apiChildParameters, nodeDeepness, selectedNode, handlerSelectNode } = this.props
const { nodeData, isNodeHovered } = this.state
const styles = {
menuItem: {
paddingLeft: nodeDeepness * 10 + 'px'
}
}
const nodeClass = (rez => {
// displays node as "selected", in sidebarleft, only when they have the selected attribute at true
// or in the 'move folder popup or tracim', only when it is the clicked node
if ((nodesAreLinks && nodeData.state.selected) || selectedNode === nodeData.id) rez += 'textMenuClickedColor textMenuClickedBgColor '
if (isNodeHovered) rez += 'textMenuColor-hover textMenuBgColor-hover '
return rez
})('sidebarleft__menu__item ')
const faIcon = (() => {
switch (nodeData.type) {
case 'workspace': return 'fa-bank'
case 'folder': return 'fa-folder-open-o'
case 'page': return 'fa-file-text-o'
case 'thread': return 'fa-comments-o'
case 'file': return 'fa-paperclip'
}
})()
const expandPicto = (() => nodeData.type === 'folder' || nodeData.type === 'workspace' ? (nodeData.state.opened ? 'fa-caret-down' : 'fa-caret-right') : '')()
return (
<div>
<div className={nodeClass} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
<div className={'sidebarleft__menu__item__name'} id={nodeData.id} style={nodeDeepness !== 0 ? styles.menuItem : {}}>
<div className={'sidebarleft__menu__item__expandpicto'} id={nodeData.id + '_expandpicto'} onClick={e => this.handlerOnClickExpandPicto(e)}>
<i className={'fa ' + expandPicto} />
</div>
{ nodesAreLinks
? <a className={'sidebarleft__menu__item__link'} id={nodeData.id + '_link'} href={nodeData.a_attr.href}><i className={'fa ' + faIcon} />{ nodeData.text }</a>
: (
<div
className={'sidebarleft__menu__item__link'}
id={nodeData.id + '_link'}
onClick={() => handlerSelectNode(nodeData.id)}
>
<i className={'fa ' + faIcon} />{ nodeData.text }
</div>
)
}
</div>
</div>
<Collapse isOpened={Array.isArray(nodeData.children)} springConfig={{stiffness: 500, damping: 40}} hasNestedCollapse>
{ Array.isArray(nodeData.children) && nodeData.children.map((child, i) =>
<MenuNode
data={child}
nodesAreLinks={nodesAreLinks}
nodeDeepness={nodeDeepness + 1}
key={nodeDeepness + '_' + i}
apiChildPath={apiChildPath}
apiChildParameters={apiChildParameters}
selectedNode={selectedNode}
handlerSelectNode={handlerSelectNode}
/>
)}
</Collapse>
</div>
)
}
}
MenuNode.propTypes = propTypes
MenuNode.defaultProps = defaultProps