2016-07-16 02:06:10 +00:00
|
|
|
import React, {PropTypes, Component} from 'react';
|
2016-10-02 20:57:00 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
2016-11-10 01:15:27 +00:00
|
|
|
import {DragSource, DropTarget} from 'react-dnd';
|
2016-07-16 02:06:10 +00:00
|
|
|
import classnames from 'classnames';
|
2016-09-22 20:14:41 +00:00
|
|
|
import RequestActionsDropdown from '../dropdowns/RequestActionsDropdown';
|
2016-07-22 22:27:04 +00:00
|
|
|
import Editable from '../base/Editable';
|
2016-08-15 17:04:36 +00:00
|
|
|
import MethodTag from '../tags/MethodTag';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../../../models';
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-07-08 06:02:40 +00:00
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
class SidebarRequestRow extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
dragDirection: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setDragDirection (dragDirection) {
|
|
|
|
if (dragDirection !== this.state.dragDirection) {
|
|
|
|
this.setState({dragDirection})
|
|
|
|
}
|
2016-05-01 19:56:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
connectDragSource,
|
|
|
|
connectDropTarget,
|
|
|
|
isDragging,
|
|
|
|
isDraggingOver,
|
|
|
|
request,
|
|
|
|
requestGroup,
|
|
|
|
isActive,
|
2016-08-15 17:04:36 +00:00
|
|
|
activateRequest,
|
|
|
|
requestCreate
|
2016-07-19 16:59:26 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {dragDirection} = this.state;
|
|
|
|
|
|
|
|
let node;
|
|
|
|
|
|
|
|
const classes = classnames('sidebar__row', {
|
|
|
|
'sidebar__row--dragging': isDragging,
|
|
|
|
'sidebar__row--dragging-above': isDraggingOver && dragDirection > 0,
|
|
|
|
'sidebar__row--dragging-below': isDraggingOver && dragDirection < 0
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!request) {
|
|
|
|
node = (
|
2016-07-21 03:58:52 +00:00
|
|
|
<li className={classes}>
|
2016-07-20 23:16:28 +00:00
|
|
|
<div className="sidebar__item" tabIndex={0}>
|
2016-07-19 16:59:26 +00:00
|
|
|
<button className="sidebar__clickable"
|
2016-08-15 17:04:36 +00:00
|
|
|
onClick={() => requestCreate()}>
|
2016-07-19 16:59:26 +00:00
|
|
|
<em>click to add first request...</em>
|
|
|
|
</button>
|
2016-07-08 06:02:40 +00:00
|
|
|
</div>
|
2016-07-19 16:59:26 +00:00
|
|
|
</li>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
node = (
|
|
|
|
<li className={classes}>
|
|
|
|
<div className={classnames('sidebar__item', {'sidebar__item--active': isActive})}>
|
2016-09-20 20:53:34 +00:00
|
|
|
<button className="wide" onClick={e => activateRequest(request)}>
|
2016-07-19 16:59:26 +00:00
|
|
|
<div className="sidebar__clickable">
|
|
|
|
<MethodTag method={request.method}/>
|
|
|
|
<Editable
|
|
|
|
value={request.name}
|
2016-11-10 01:15:27 +00:00
|
|
|
onSubmit={name => models.request.update(request, {name})}
|
2016-07-19 16:59:26 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<div className="sidebar__actions">
|
|
|
|
<RequestActionsDropdown
|
|
|
|
right={true}
|
|
|
|
request={request}
|
|
|
|
requestGroup={requestGroup}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return connectDragSource(connectDropTarget(node));
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
SidebarRequestRow.propTypes = {
|
|
|
|
// Functions
|
|
|
|
activateRequest: PropTypes.func.isRequired,
|
2016-08-15 17:04:36 +00:00
|
|
|
requestCreate: PropTypes.func.isRequired,
|
2016-07-19 16:59:26 +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-04-29 03:37:49 +00:00
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
// React DnD
|
|
|
|
isDragging: PropTypes.bool.isRequired,
|
|
|
|
isDraggingOver: PropTypes.bool.isRequired,
|
|
|
|
connectDragSource: PropTypes.func.isRequired,
|
|
|
|
connectDropTarget: PropTypes.func.isRequired,
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
// Optional
|
|
|
|
requestGroup: PropTypes.object,
|
|
|
|
request: PropTypes.object
|
|
|
|
};
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
/**
|
|
|
|
* Implements the drag source contract.
|
|
|
|
*/
|
|
|
|
const dragSource = {
|
|
|
|
beginDrag(props) {
|
|
|
|
return {
|
|
|
|
request: props.request
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2016-07-21 03:58:52 +00:00
|
|
|
const {request} = monitor.getItem();
|
|
|
|
const targetRequest = props.request;
|
|
|
|
|
|
|
|
const {requestGroup} = props;
|
|
|
|
const requestGroupId = requestGroup ? requestGroup._id : null;
|
|
|
|
const parentId = targetRequest ? targetRequest.parentId : requestGroupId;
|
|
|
|
const targetId = targetRequest ? targetRequest._id : null;
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
if (isAbove(monitor, component)) {
|
2016-07-21 03:58:52 +00:00
|
|
|
props.moveRequest(request, parentId, targetId, 1);
|
2016-07-19 16:59:26 +00:00
|
|
|
} else {
|
2016-07-21 03:58:52 +00:00
|
|
|
props.moveRequest(request, parentId, targetId, -1);
|
2016-07-19 16:59:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
hover (props, monitor, component) {
|
|
|
|
if (isAbove(monitor, component)) {
|
|
|
|
component.decoratedComponentInstance.setDragDirection(1);
|
|
|
|
} else {
|
|
|
|
component.decoratedComponentInstance.setDragDirection(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function sourceCollect (connect, monitor) {
|
|
|
|
return {
|
|
|
|
connectDragSource: connect.dragSource(),
|
|
|
|
isDragging: monitor.isDragging()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function targetCollect (connect, monitor) {
|
|
|
|
return {
|
|
|
|
connectDropTarget: connect.dropTarget(),
|
|
|
|
isDraggingOver: monitor.isOver()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const source = DragSource('SIDEBAR_ROW', dragSource, sourceCollect)(SidebarRequestRow);
|
|
|
|
const target = DropTarget('SIDEBAR_ROW', dragTarget, targetCollect)(source);
|
|
|
|
export default target;
|