2015-09-11 16:25:45 +02:00
|
|
|
var React = require('react/addons');
|
|
|
|
var classNames = require('classnames');
|
|
|
|
var AppIcon = require('../common/app-icon.jsx');
|
|
|
|
var DragSource = require('react-dnd').DragSource;
|
|
|
|
var DropTarget = require('react-dnd').DropTarget;
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
|
|
|
var TreeItem = React.createClass({
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
|
|
|
|
var data = this.props.data;
|
2015-09-16 17:26:56 +02:00
|
|
|
var appIcon = data.icon ? <AppIcon icon={data.icon} theme={this.props.theme} /> : null;
|
2015-09-11 16:25:45 +02:00
|
|
|
|
|
|
|
var connectDragSource = this.props.connectDragSource;
|
|
|
|
var connectDropTarget = this.props.connectDropTarget;
|
|
|
|
|
|
|
|
var classes = classNames({
|
|
|
|
'alert': true,
|
2015-09-16 17:26:56 +02:00
|
|
|
'alert-default': !this.props.isOver,
|
|
|
|
'alert-info': this.props.isOver && this.props.canDrop,
|
|
|
|
'alert-success': this.props.selected
|
2015-09-11 16:25:45 +02:00
|
|
|
});
|
|
|
|
|
2015-09-16 17:26:56 +02:00
|
|
|
var style = {
|
|
|
|
opacity: this.props.isDragging ? 0.5 : 1
|
|
|
|
};
|
|
|
|
|
2015-09-11 16:25:45 +02:00
|
|
|
return connectDropTarget(connectDragSource(
|
2015-09-16 17:26:56 +02:00
|
|
|
<div className={classes} style={style} onClick={this.handleClick}>
|
2015-09-11 16:25:45 +02:00
|
|
|
{appIcon}
|
|
|
|
<span className="app-label">{data.label}</span>
|
|
|
|
</div>
|
|
|
|
));
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-09-16 17:26:56 +02:00
|
|
|
handleClick: function(evt) {
|
|
|
|
evt.preventDefault();
|
|
|
|
this.props.onItemClicked(this.props.data);
|
|
|
|
}
|
|
|
|
|
2015-09-11 16:25:45 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
var dragSourceSpec = {
|
|
|
|
|
|
|
|
beginDrag: function(props) {
|
|
|
|
return props.data;
|
|
|
|
},
|
|
|
|
|
|
|
|
endDrag: function(props, monitor) {
|
|
|
|
|
|
|
|
if (!monitor.didDrop()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var dropResult = monitor.getDropResult();
|
|
|
|
|
|
|
|
return props.onItemMoved(props.data, dropResult);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var dropTargetSpec = {
|
|
|
|
|
|
|
|
drop: function(props, monitor, component) {
|
|
|
|
return props.data;
|
|
|
|
},
|
|
|
|
|
|
|
|
canDrop: function(props, monitor) {
|
|
|
|
var draggedItem = monitor.getItem();
|
|
|
|
return !_.isEqual(draggedItem, props.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
function dragSourceCollect(connect, monitor) {
|
|
|
|
return {
|
|
|
|
connectDragSource: connect.dragSource(),
|
|
|
|
isDragging: monitor.isDragging()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function dropTargetCollect(connect, monitor) {
|
|
|
|
return {
|
|
|
|
connectDropTarget: connect.dropTarget(),
|
2015-09-16 17:26:56 +02:00
|
|
|
isOver: monitor.isOver(),
|
|
|
|
canDrop: monitor.canDrop()
|
2015-09-11 16:25:45 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = DropTarget(['ITEM', 'NEW_ITEM'], dropTargetSpec, dropTargetCollect)(
|
|
|
|
DragSource('ITEM', dragSourceSpec, dragSourceCollect)(TreeItem)
|
|
|
|
);
|