Element.js
6.56 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
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
234
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
import React from 'react'
import Move from '../libs/move'
import EventEmitter from '../libs/eventEmitter'
import { hashHistory } from 'react-router'
export default class Element extends React.Component {
static get getDefaultProps() {
return {
// allow the initial position to be passed in as a prop
initialPos: {x: 0, y: 0}
}
}
constructor(props){
super(props);
this.state = {
pos: this.props.initialPos,
dragging: false,
rel: null, // position relative to the cursor
zIndex: 2,
parent_id: null,
hasMoved: false,
edit: false,
name: this.props.name,
prev_name: this.props.name
};
}
componentDidUpdate(props, state) {
if (this.state.dragging && !state.dragging) {
document.addEventListener('mousemove', this.onMouseMove)
document.addEventListener('mouseup', this.onMouseUp)
} else if (!this.state.dragging && state.dragging) {
document.removeEventListener('mousemove', this.onMouseMove)
document.removeEventListener('mouseup', this.onMouseUp)
}
}
onMouseDown(event) {
if (event.button !== 0) return
if (!this.props.isDropZoneChild) {
this.handleCloneElement()
}
this.setState({
dragging: true,
rel: {
x: event.pageX - this.element().offsetLeft,
y: event.pageY - this.element().offsetTop
},
zIndex: this.state.zIndex++,
hasMoved: false
})
event.stopPropagation()
event.preventDefault()
}
onMouseMove(event) {
if (!this.state.dragging) return
let pageWidth
if (this.props.isDropZoneChild) {
pageWidth = this.dropzone().clientWidth
} else {
pageWidth = document.documentElement.clientWidth + 20
}
//var pageHeight = document.documentElement.clientHeight
const elementWidth = this.element().offsetWidth
const elementHeight = this.element().offsetHeight
const maxX = pageWidth - elementWidth
const maxY = this.dropzone().clientHeight - elementHeight
let moveX = event.pageX - this.state.rel.x
let moveY = event.pageY - this.state.rel.y
moveX = Math.min( maxX, Math.max(0, moveX) )
moveY = Math.min( maxY, Math.max(0, moveY) )
this.setState({
pos: {
x: moveX,
y: moveY
},
hasMoved: true
})
event.stopPropagation()
event.preventDefault()
}
onMouseUp(event) {
this.setState({ dragging: false })
if (this.props.isDropZoneChild) {
const element = {
id: this.props.id,
pos_x: this.state.pos.x,
pos_y: this.state.pos.y,
}
if (this.state.hasMoved) {
EventEmitter.dispatch('element:update', element)
}
} else if (this.jl(this.element(), this.dropzone())) {
this.handleMoveDropZone()
} else {
Move( this.element() , { left: this.props.initialPos.x , top: this.props.initialPos.y } )
this.handleDeleteLastElement()
}
event.stopPropagation()
event.preventDefault()
}
element() {
return this.refs.meta_element
}
dropzone() {
return document.getElementById('dropzone')
}
sidebar() {
return document.getElementById('sidebar')
}
handleCloneElement() {
if (this.props.handleCloneElement) {
const element = {
id: this.props.id + 3,
name: this.props.name,
initialPos: {
x: this.props.initialPos.x,
y: this.props.initialPos.y
},
target_type: this.props.target_type
}
this.props.handleCloneElement(element)
}
}
handleDeleteLastElement() {
if (this.props.handleDeleteLastElement) {
this.props.handleDeleteLastElement()
}
}
handleMoveDropZone() {
const element = {
name: this.props.name,
target_type: this.props.target_type,
initialPos: {
x: this.state.pos.x - this.sidebar().clientWidth,
y: this.state.pos.y - this.sidebar().clientHeight + 55
}
}
EventEmitter.dispatch('moveDropZone', element)
if (this.props.handleDeleteElement) {
this.props.handleDeleteElement(this.props.meta_element)
}
}
// 碰撞
// pz: function(obj1, obj2) {
// var L1 = obj1.offsetLeft
// var R1 = obj1.offsetLeft + obj1.offsetWidth
// var T1 = obj1.offsetTop
// var B1 = obj1.offsetTop + obj1.offsetHeight
//
// var L2 = obj2.offsetLeft
// var R2 = obj2.offsetLeft + obj2.offsetWidth
// var T2 = obj2.offsetTop
// var B2 = obj2.offsetTop + obj2.offsetHeight
//
// if( R1<L2 || L1>R2 || B1<T2 || T1>B2 ){
// return false
// }
// else{
// return true
// }
// },
jl(obj1, obj2){
let result = obj1.offsetLeft - obj2.offsetLeft - this.props.initialPos.x + 65
return result > 0 ? true : false
}
handleClick() {
if (this.props.isDropZoneChild) {
this.setState({ edit: true })
}
}
handleChange(e) {
this.setState({ name: e.target.value })
}
handleBlur() {
this.handleUpdateName()
}
handleKeyPress(e) {
if (e.key === 'Enter') {
this.handleUpdateName()
}
}
handleUpdateName() {
this.setState({ edit: false })
const element = {
id: this.props.id,
name: this.state.name
}
if (this.state.name != this.state.prev_name) {
if (this.state.name.trim() == "") {
this.setState({ name: this.state.prev_name })
} else {
EventEmitter.dispatch('element:update', element)
this.setState({ prev_name: this.state.name })
}
}
}
handleDoubleClick() {
const childrenObjectType = this.props.childrenObjectType
const url = `/regional_design/${this.props.id}?object=${childrenObjectType}`
if (childrenObjectType != 'drug_brandreth_grid') {
hashHistory.push(url)
}
}
divBlock() {
return (
<div style={{ position: 'absolute', left: this.state.pos.x + 'px', top: this.state.pos.y + 'px', zIndex: this.state.zIndex }} ref='meta_element' className="meta-element text-center" onMouseDown={ this.onMouseDown } onDoubleClick={ this.handleDoubleClick }>
<span onClick={ this.handleClick }>{this.state.name}</span>
</div>
)
}
inputText() {
return (
<div style={{ position: 'absolute', left: this.state.pos.x + 'px', top: this.state.pos.y + 'px', zIndex: this.state.zIndex }} className="input-meta-element text-center">
<input autoFocus type="text" id="formHorizontalName" className="form-control" ref='name' value={ this.state.name } onChange={ this.handleChange } onBlur={ this.handleBlur } onKeyPress={ this.handleKeyPress } />
</div>
)
}
render() {
if (this.state.edit) {
return this.inputText()
} else {
return this.divBlock()
}
}
}