2016-07-19 16:59:26 +00:00
|
|
|
import React, {PropTypes, Component} from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import {DragSource, DropTarget} from 'react-dnd'
|
2016-07-16 02:06:10 +00:00
|
|
|
import classnames from 'classnames';
|
2016-07-08 06:02:40 +00:00
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
import RequestGroupActionsDropdown from '../containers/RequestGroupActionsDropdown';
|
2016-07-16 02:06:10 +00:00
|
|
|
import SidebarRequestRow from './SidebarRequestRow';
|
2016-05-01 19:56:30 +00:00
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
class SidebarRequestGroupRow extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
dragDirection: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setDragDirection (dragDirection) {
|
|
|
|
if (dragDirection !== this.state.dragDirection) {
|
|
|
|
this.setState({dragDirection})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
connectDragSource,
|
|
|
|
connectDropTarget,
|
2016-07-21 03:58:52 +00:00
|
|
|
moveRequest,
|
2016-07-19 16:59:26 +00:00
|
|
|
children,
|
|
|
|
requestGroup,
|
|
|
|
isActive,
|
|
|
|
toggleRequestGroup,
|
|
|
|
addRequestToRequestGroup,
|
|
|
|
isDragging,
|
|
|
|
isDraggingOver,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {dragDirection} = this.state;
|
|
|
|
|
|
|
|
let folderIconClass = 'fa-folder';
|
|
|
|
let expanded = !requestGroup.metaCollapsed;
|
|
|
|
|
|
|
|
folderIconClass += !expanded ? '' : '-open';
|
|
|
|
folderIconClass += isActive ? '' : '-o';
|
|
|
|
|
|
|
|
const classes = classnames('sidebar__row', {
|
|
|
|
'sidebar__row--dragging': isDragging,
|
|
|
|
'sidebar__row--dragging-above': isDraggingOver && dragDirection > 0,
|
|
|
|
'sidebar__row--dragging-below': isDraggingOver && dragDirection < 0
|
|
|
|
});
|
|
|
|
|
|
|
|
return connectDragSource(connectDropTarget(
|
|
|
|
<li key={requestGroup._id} className={classes}>
|
|
|
|
<div className={classnames('sidebar__item sidebar__item--big', {'sidebar__item--active': isActive})}>
|
|
|
|
<button onClick={e => toggleRequestGroup(requestGroup)}>
|
|
|
|
<div className="sidebar__clickable">
|
|
|
|
<i className={'sidebar__item__icon fa ' + folderIconClass}></i>
|
|
|
|
<span>{requestGroup.name}</span>
|
|
|
|
</div>
|
2016-04-29 03:37:49 +00:00
|
|
|
</button>
|
2016-07-19 16:59:26 +00:00
|
|
|
|
|
|
|
<div className="sidebar__actions">
|
|
|
|
<button onClick={(e) => addRequestToRequestGroup(requestGroup)}>
|
|
|
|
<i className="fa fa-plus-circle"></i>
|
|
|
|
</button>
|
|
|
|
<RequestGroupActionsDropdown
|
|
|
|
requestGroup={requestGroup}
|
|
|
|
right={true}
|
|
|
|
/>
|
|
|
|
</div>
|
2016-04-29 03:37:49 +00:00
|
|
|
</div>
|
2016-07-19 16:59:26 +00:00
|
|
|
|
|
|
|
<ul className="sidebar__list">
|
|
|
|
{!expanded || children.length > 0 ? null : (
|
|
|
|
<SidebarRequestRow
|
2016-07-21 03:58:52 +00:00
|
|
|
activateRequest={() => null}
|
|
|
|
moveRequest={moveRequest}
|
2016-07-19 16:59:26 +00:00
|
|
|
isActive={false}
|
|
|
|
request={null}
|
|
|
|
requestGroup={requestGroup}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{expanded ? children : null}
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
SidebarRequestGroupRow.propTypes = {
|
|
|
|
// Functions
|
|
|
|
toggleRequestGroup: PropTypes.func.isRequired,
|
|
|
|
addRequestToRequestGroup: PropTypes.func.isRequired,
|
2016-07-19 16:59:26 +00:00
|
|
|
moveRequestGroup: PropTypes.func.isRequired,
|
2016-07-21 03:58:52 +00:00
|
|
|
moveRequest: PropTypes.func.isRequired,
|
2016-04-29 03:37:49 +00:00
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
// Other
|
|
|
|
isActive: PropTypes.bool.isRequired,
|
2016-07-19 16:59:26 +00:00
|
|
|
requestGroup: PropTypes.object.isRequired,
|
|
|
|
|
|
|
|
// React DnD
|
|
|
|
isDragging: PropTypes.bool.isRequired,
|
|
|
|
isDraggingOver: PropTypes.bool.isRequired,
|
|
|
|
connectDragSource: PropTypes.func.isRequired,
|
|
|
|
connectDropTarget: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements the drag source contract.
|
|
|
|
*/
|
|
|
|
const dragSource = {
|
|
|
|
beginDrag(props) {
|
|
|
|
return {
|
|
|
|
requestGroup: props.requestGroup
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function isAbove (monitor, component) {
|
|
|
|
const hoveredNode = ReactDOM.findDOMNode(component);
|
|
|
|
|
|
|
|
const hoveredTop = hoveredNode.getBoundingClientRect().top;
|
|
|
|
const draggedTop = monitor.getSourceClientOffset().y;
|
|
|
|
|
|
|
|
return hoveredTop > draggedTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dragTarget = {
|
|
|
|
drop (props, monitor, component) {
|
|
|
|
if (isAbove(monitor, component)) {
|
|
|
|
props.moveRequestGroup(monitor.getItem().requestGroup, props.requestGroup, 1);
|
|
|
|
} else {
|
|
|
|
props.moveRequestGroup(monitor.getItem().requestGroup, props.requestGroup, -1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hover (props, monitor, component) {
|
|
|
|
if (isAbove(monitor, component)) {
|
|
|
|
component.decoratedComponentInstance.setDragDirection(1);
|
|
|
|
} else {
|
|
|
|
component.decoratedComponentInstance.setDragDirection(-1);
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
};
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
function sourceCollect (connect, monitor) {
|
|
|
|
return {
|
|
|
|
connectDragSource: connect.dragSource(),
|
|
|
|
isDragging: monitor.isDragging()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function targetCollect (connect, monitor) {
|
|
|
|
return {
|
|
|
|
connectDropTarget: connect.dropTarget(),
|
|
|
|
isDraggingOver: monitor.isOver()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-21 16:33:35 +00:00
|
|
|
const source = DragSource('SIDEBAR_ROW_2', dragSource, sourceCollect)(SidebarRequestGroupRow);
|
|
|
|
const target = DropTarget('SIDEBAR_ROW_2', dragTarget, targetCollect)(source);
|
2016-07-19 16:59:26 +00:00
|
|
|
export default target;
|