/
MetadataProperty.js
159 lines (133 loc) · 4.69 KB
/
MetadataProperty.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React, {Component, PropTypes} from 'react';
import {DragSource, DropTarget} from 'react-dnd';
import {bindActionCreators} from 'redux';
import {reduxForm} from 'redux-form';
import {editProperty} from 'app/Templates/actions/uiActions';
import {showModal} from 'app/Modals/actions/modalActions';
import {reorderProperty, addProperty} from 'app/Templates/actions/templateActions';
import FormConfigInput from 'app/Templates/components/FormConfigInput';
import FormConfigSelect from 'app/Templates/components/FormConfigSelect';
import validate from 'app/Templates/components/ValidateTemplate';
export class MetadataProperty extends Component {
renderForm() {
if (this.props.type === 'select' || this.props.type === 'list') {
return <FormConfigSelect formKey={this.props.localID} index={this.props.index} />;
}
return <FormConfigInput formKey={this.props.localID} index={this.props.index} />;
}
hasError(propertyErrors) {
let errors = Object.keys(propertyErrors);
return errors.length > 0;
}
render() {
const {inserting, label, connectDragSource, isDragging, connectDropTarget, editingProperty, index, localID, errors} = this.props;
let propertyClass = 'list-group-item';
let touched = this.props.fields.properties.reduce((isTouched, property) => {
return isTouched || property.touched;
}, false);
if (touched && errors.properties && this.hasError(errors.properties[index])) {
propertyClass += ' error';
}
if (isDragging || inserting) {
propertyClass += ' dragging';
}
let iconClass = 'fa fa-font';
if (this.props.type === 'select') {
iconClass = 'fa fa-sort';
}
if (this.props.type === 'list') {
iconClass = 'fa fa-list';
}
if (this.props.type === 'date') {
iconClass = 'fa fa-calendar';
}
if (this.props.type === 'checkbox') {
iconClass = 'fa fa-check-square-o';
}
return connectDragSource(connectDropTarget(
<li className={propertyClass}>
<div>
<span className="property-name"><i className="fa fa-arrows-v"></i> <i className={iconClass}></i> {label}</span>
<button className="btn btn-danger btn-xs pull-right property-remove" onClick={() =>
this.props.removeProperty('RemovePropertyModal', index)}
>
<i className="fa fa-trash"></i> Delete
</button>
<button className="btn btn-default btn-xs pull-right property-edit" onClick={() => this.props.editProperty(localID)}>
<i className="fa fa-pencil"></i> Edit
</button>
</div>
<div className={'propery-form' + (editingProperty === localID ? ' expand' : '') }>
{this.renderForm()}
</div>
</li>
));
}
}
MetadataProperty.propTypes = {
connectDragSource: PropTypes.func.isRequired,
connectDropTarget: PropTypes.func.isRequired,
index: PropTypes.number.isRequired,
fields: PropTypes.object,
errors: PropTypes.object,
isDragging: PropTypes.bool.isRequired,
localID: PropTypes.any.isRequired,
type: PropTypes.string,
label: PropTypes.string.isRequired,
inserting: PropTypes.bool,
removeProperty: PropTypes.func,
editingProperty: PropTypes.string,
editProperty: PropTypes.func,
form: PropTypes.object
};
const target = {
drop(props) {
return props;
},
hover(props, monitor) {
const dragIndex = monitor.getItem().index;
const hoverIndex = props.index;
if (typeof dragIndex === 'undefined') {
let item = monitor.getItem();
item.index = 0;
return props.addProperty({label: item.label, type: item.type, inserting: true}, item.index);
}
props.reorderProperty(dragIndex, hoverIndex);
monitor.getItem().index = hoverIndex;
}
};
let dropTarget = DropTarget(['METADATA_PROPERTY', 'METADATA_OPTION'], target, (connector) => ({
connectDropTarget: connector.dropTarget()
}))(MetadataProperty);
const source = {
beginDrag(props) {
return {
index: props.index,
label: props.label,
type: props.type
};
}
};
let dragSource = DragSource('METADATA_PROPERTY', source, (connector, monitor) => ({
connectDragSource: connector.dragSource(),
isDragging: monitor.isDragging()
}))(dropTarget);
function mapDispatchToProps(dispatch) {
return bindActionCreators({removeProperty: showModal, reorderProperty, addProperty, editProperty}, dispatch);
}
const mapStateToProps = (state) => {
return {
editingProperty: state.template.uiState.toJS().editingProperty
};
};
export {dragSource, dropTarget};
let form = reduxForm({
form: 'template',
fields: ['properties[]'],
readonly: true,
destroyOnUnmount: false,
validate
},
mapStateToProps, mapDispatchToProps)(dragSource);
export default form;