RegionalDesign.js
4.12 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
import React from 'react'
import Loader from 'halogen/PulseLoader'
import Element from './Element'
import ReactAddonsUpdate from 'react-addons-update'
import EventEmitter from '../libs/eventEmitter'
import $ from 'jquery'
import autobind from 'autobind-decorator'
@autobind
export default class RegionalDesign extends React.Component {
constructor(props){
super(props);
this.state = {
data: null, loading: true, error: null, object_name: '', object_type: '', children_object_type: ''
};
this.init_state = this.state;
}
getConfig(props, key) {
const config = {
repo: {
object_url: '/regional_design/repos',
children_object_url: '/regional_design/drug_repos',
children_param_name: 'drug_repo'
},
drug_repo: {
object_url: '/regional_design/drug_repos',
children_object_url: '/regional_design/drug_brandrethes',
children_param_name: 'drug_brandreth'
},
drug_brandreth: {
object_url: '/regional_design/drug_brandrethes',
children_object_url: '/regional_design/drug_brandreth_grids',
children_param_name: 'drug_brandreth_grid'
}
}
const search_str = props.location.search
return config[search_str.substring(search_str.indexOf("=") + 1)][key]
}
componentDidMount() {
const url = this.getConfig(this.props, 'object_url') + '/' + this.props.match.params.id + '.json'
this.getJSON(url)
EventEmitter.subscribe('moveDropZone', function(element){
if (element.target_type == this.state.object_type) {
this.addElement(element)
} else {
EventEmitter.dispatch('alert', '不能移动该元件')
}
}.bind(this))
EventEmitter.subscribe('element:update', function(element){
this.updateElement(element)
}.bind(this))
}
addElement(element) {
const new_element = {
name: element.name,
pos_x: element.initialPos.x,
pos_y: element.initialPos.y,
parent_id: this.props.match.params.id
}
const url = this.getConfig(this.props, 'children_object_url') + '.json'
let data = {}
data[this.getConfig(this.props, 'children_param_name')] = new_element
$.post(url, data, function(data) {
EventEmitter.dispatch('alert', '添加元件成功')
element.id = data.id
element.children_object_type = this.state.children_object_type
const elements = ReactAddonsUpdate(this.state.data, { $push: [element] })
this.setState({data: elements})
}.bind(this), 'JSON')
}
updateElement(element) {
let data = {}
data[this.getConfig(this.props, 'children_param_name')] = element
$.ajax({
method: 'PUT',
url: this.getConfig(this.props, 'children_object_url') + '/' + element.id,
dataType: 'JSON',
data: data,
success: function(data) {
}.bind(this)
})
}
componentWillUnmount() {
EventEmitter.unSubscribe('moveDropZone')
EventEmitter.unSubscribe('element:update')
}
componentWillReceiveProps(nextProps) {
this.setState(this.init_state)
const url = this.getConfig(nextProps, 'object_url') + '/' + nextProps.match.params.id + '.json'
this.getJSON(url)
}
getJSON(url) {
$.getJSON(url).then(
value => this.setState({loading: false, data: value.children, object_name: value.name, object_type: value.type, children_object_type: value.children_object_type}),
error => this.setState({loading: false, error: error}))
}
render() {
if (this.state.loading) {
return (
<Loader color="#286090" size="12px" margin="4px"/>
)
}
else if (this.state.error !== null) {
return <span>Error: {this.state.error}</span>
}
else {
const data = this.state.data.map(function(element) {
return (
<Element hashHistory={ this.props.history } childrenObjectType={ this.state.children_object_type } isDropZoneChild={ true } key={ element.id } id={ element.id } name={ element.name } initialPos={ element.initialPos } />
)
}.bind(this))
return (
<div>
<h1 className='text-center'>{ this.state.object_name }</h1>
<div>{ data }</div>
</div>
)
}
}
}