-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.es6.js
115 lines (99 loc) · 2.73 KB
/
index.es6.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
import React from 'react/addons'
import classSet from 'class-set'
var itemBeingDragged
export default React.createClass({
propTypes: {
className: React.PropTypes.string,
type: React.PropTypes.string,
data: React.PropTypes.any,
handleAcceptTest: React.PropTypes.func,
handleDrop: React.PropTypes.func,
handleDragStart: React.PropTypes.func,
handleDragOver: React.PropTypes.func,
handleDragEnd: React.PropTypes.func,
handleDragLeave: React.PropTypes.func
},
getInitialState() {
return {
dragging: false,
hover: false,
isOverSelf: false,
hoverAbove: false
}
},
handleDragStart(event) {
event.dataTransfer.setData(this.props.type || 'text/plain', this.props.data)
itemBeingDragged = React.findDOMNode(this.refs.item)
this.setState({dragging: true})
if(this.props.handleDragStart) {
this.props.handleDragStart(event)
}
},
handleDragOver(event) {
var isOverSelf = React.findDOMNode(this.refs.item) === itemBeingDragged
var isOverTopHalf = event.clientY < (event.target.offsetTop + (event.target.offsetHeight / 2))
if(isOverSelf) {
event.stopPropagation()
return
}
if(this.props.handleAcceptTest && !this.props.handleAcceptTest(this.props.data, isOverTopHalf ? 0 : 1, event)) {
return
}
this.setState({
hover: true,
isOverSelf: isOverSelf,
hoverAbove: isOverTopHalf
})
event.preventDefault()
if(this.props.handleDragOver) {
this.props.handleDragOver(event)
}
},
handleDragEnd(event) {
this.setState({dragging: false})
if(this.props.handleDragEnd) {
this.props.handleDragEnd(event)
}
},
handleDragLeave(event) {
this.setState({
hover: false
})
event.preventDefault()
if(this.props.handleDragLeave) {
this.props.handleDragLeave(event)
}
},
handleDrop(event) {
event.stopPropagation()
event.preventDefault()
this.setState({
hover: false
})
if(this.state.isOverSelf) {
return
}
if(this.props.handleDrop) {
this.props.handleDrop(this.props.data, this.state.hoverAbove ? 0 : 1, event)
}
},
render() {
var classes = classSet(this.props.className, {
'dragging': this.state.dragging,
'hover': this.state.hover,
'hover-above': this.state.hoverAbove,
'hover-below': !this.state.hoverAbove
})
return React.cloneElement(this.props.children, {
ref: "item",
draggable: "true",
className: classes,
onDragStart: this.handleDragStart,
onDragOver: this.handleDragOver,
onDragEnter: this.handleDragEnter,
onDragLeave: this.handleDragLeave,
onDragEnd: this.handleDragEnd,
onDrop: this.handleDrop
})
}
})