forked from jaredly/treed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context-menu.js
140 lines (122 loc) · 3.36 KB
/
context-menu.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
var React = require('react/addons')
, cx = React.addons.classSet
, PT = React.PropTypes
var ContextMenu = React.createClass({
propTypes: {
onClose: PT.func.isRequired,
onSelect: PT.func,
config: PT.object,
},
_onMouseDown: function (e) {
e.stopPropagation();
},
_onSelect: function (item) {
if (this.props.onSelect) this.props.onSelect(item)
this.props.onClose()
},
render: function () {
return <div
style={{
top: this.props.y,
left: this.props.x,
zIndex: 1000,
}}
onMouseDown={this._onMouseDown}
className='ContextMenu'>
<MenuBody
onSelect={this._onSelect}
onClose={this.props.onClose}
items={this.props.config}
zIndex={1000}/>
</div>
},
})
var MenuItem = React.createClass({
propTypes: {
config: PT.object,
selected: PT.bool,
onSelect: PT.func,
onHover: PT.func,
onClose: PT.func,
zIndex: PT.number,
},
_onClick: function () {
if (this.props.config.disabled) return
if (this.props.config.action) {
this.props.config.action()
return this.props.onClose()
}
this.props.onSelect(this.props.config)
},
render: function () {
return <div className={cx({
'ContextMenu_item': true,
'ContextMenu_item-selected': this.props.selected,
'ContextMenu_item-disabled': this.props.config.disabled,
'ContextMenu_item-parent': this.props.config.children && this.props.config.children.length,
})}>
<div className='ContextMenu_item_title'
onMouseOver={this.props.onHover}
onClick={this._onClick}>
{/* this.props.config.icon &&
<img src={this.props.config.icon}/> */}
{this.props.config.title}
{this.props.config.shortcut &&
<span className='ContextMenu_item_shortcut'>
{this.props.config.shortcut}
</span>}
</div>
{this.props.selected &&
this.props.config.children &&
<MenuBody
onClose={this.props.onClose}
onSelect={this.props.onSelect}
zIndex={this.props.zIndex + 1}
items={this.props.config.children}/>}
</div>
}
})
var MenuBody = React.createClass({
propTypes: {
items: PT.array,
zIndex: PT.number,
onClose: PT.func,
onSelect: PT.func,
},
getInitialState: function () {
return {
selected: null
}
},
_onHover: function (i) {
this.setState({selected: i})
},
render: function () {
return <ul className='ContextMenu_body' style={{zIndex: this.props.zIndex}}>
{this.props.items.map((item, i) =>
<MenuItem
config={item}
zIndex={this.props.zIndex}
onClose={this.props.onClose}
onSelect={this.props.onSelect}
onHover={this._onHover.bind(null, i)}
selected={this.state.selected === i}/>)}
</ul>
},
})
ContextMenu.show = function (definition, x, y, onSelect) {
var node = document.createElement('div')
document.body.appendChild(node)
var closeOut = function (e) {
window.removeEventListener('mousedown', closeOut)
if (!node.parentNode) return
document.body.removeChild(node)
}
window.addEventListener('mousedown', closeOut)
React.render(<ContextMenu
x={x} y={y}
config={definition}
onSelect={onSelect}
onClose={closeOut}/>, node)
}
module.exports = ContextMenu