2016-11-26 00:37:59 +00:00
|
|
|
import React, {PropTypes, PureComponent} from 'react';
|
2016-07-19 16:59:26 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
2016-11-25 23:09:17 +00:00
|
|
|
import {DragSource, DropTarget} from 'react-dnd';
|
2016-07-16 02:06:10 +00:00
|
|
|
import classnames from 'classnames';
|
2016-11-16 17:18:39 +00:00
|
|
|
import RequestGroupActionsDropdown from '../dropdowns/RequestGroupActionsDropdown';
|
2016-07-16 02:06:10 +00:00
|
|
|
import SidebarRequestRow from './SidebarRequestRow';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2016-05-01 19:56:30 +00:00
|
|
|
|
2016-11-26 00:37:59 +00:00
|
|
|
class SidebarRequestGroupRow extends PureComponent {
|
2016-11-25 20:55:15 +00:00
|
|
|
state = {dragDirection: 0};
|
2016-07-19 16:59:26 +00:00
|
|
|
|
2017-02-08 00:31:48 +00:00
|
|
|
_setRequestGroupActionsDropdownRef = n => this._requestGroupActionsDropdown = n;
|
|
|
|
|
2016-11-25 20:55:15 +00:00
|
|
|
_handleCollapse = () => {
|
|
|
|
const {requestGroup, handleSetRequestGroupCollapsed, isCollapsed} = this.props;
|
|
|
|
handleSetRequestGroupCollapsed(requestGroup._id, !isCollapsed);
|
|
|
|
trackEvent('Folder', 'Toggle Visible', !isCollapsed ? 'Close' : 'Open')
|
|
|
|
};
|
|
|
|
|
2017-02-08 00:31:48 +00:00
|
|
|
_handleShowActions = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
this._requestGroupActionsDropdown.show();
|
|
|
|
};
|
|
|
|
|
2016-11-25 20:55:15 +00:00
|
|
|
_nullFunction = () => null;
|
2016-07-19 16:59:26 +00:00
|
|
|
|
|
|
|
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,
|
2016-11-16 17:18:39 +00:00
|
|
|
isCollapsed,
|
2016-07-19 16:59:26 +00:00
|
|
|
isActive,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleCreateRequest,
|
2016-11-23 20:44:46 +00:00
|
|
|
handleCreateRequestGroup,
|
2016-11-28 07:12:17 +00:00
|
|
|
handleDuplicateRequestGroup,
|
2016-07-19 16:59:26 +00:00
|
|
|
isDragging,
|
|
|
|
isDraggingOver,
|
2016-11-16 17:18:39 +00:00
|
|
|
workspace,
|
2016-07-19 16:59:26 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {dragDirection} = this.state;
|
|
|
|
|
|
|
|
let folderIconClass = 'fa-folder';
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
folderIconClass += isCollapsed ? '' : '-open';
|
2016-07-19 16:59:26 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2017-01-24 00:48:28 +00:00
|
|
|
// NOTE: We only want the button draggable, not the whole container (ie. no children)
|
|
|
|
const button = connectDragSource(connectDropTarget(
|
2017-02-08 00:31:48 +00:00
|
|
|
<button onClick={this._handleCollapse} onContextMenu={this._handleShowActions}>
|
2017-01-24 00:48:28 +00:00
|
|
|
<div className="sidebar__clickable">
|
|
|
|
<i className={'sidebar__item__icon fa ' + folderIconClass}></i>
|
|
|
|
<span>{requestGroup.name}</span>
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
));
|
|
|
|
|
|
|
|
return (
|
2016-07-19 16:59:26 +00:00
|
|
|
<li key={requestGroup._id} className={classes}>
|
2017-01-24 00:48:28 +00:00
|
|
|
<div className={classnames(
|
|
|
|
'sidebar__item sidebar__item--big', {'sidebar__item--active': isActive}
|
|
|
|
)}>
|
|
|
|
{button}
|
2016-07-19 16:59:26 +00:00
|
|
|
|
|
|
|
<div className="sidebar__actions">
|
|
|
|
<RequestGroupActionsDropdown
|
2017-02-08 00:31:48 +00:00
|
|
|
ref={this._setRequestGroupActionsDropdownRef}
|
2016-11-23 20:44:46 +00:00
|
|
|
handleCreateRequest={handleCreateRequest}
|
|
|
|
handleCreateRequestGroup={handleCreateRequestGroup}
|
2016-11-28 07:12:17 +00:00
|
|
|
handleDuplicateRequestGroup={handleDuplicateRequestGroup}
|
2016-11-16 17:18:39 +00:00
|
|
|
workspace={workspace}
|
2016-07-19 16:59:26 +00:00
|
|
|
requestGroup={requestGroup}
|
2017-03-01 21:15:56 +00:00
|
|
|
right
|
2016-07-19 16:59:26 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2016-04-29 03:37:49 +00:00
|
|
|
</div>
|
2016-07-19 16:59:26 +00:00
|
|
|
|
2016-11-26 00:37:59 +00:00
|
|
|
<ul className={classnames('sidebar__list', {'sidebar__list--collapsed': isCollapsed})}>
|
2016-11-26 08:29:16 +00:00
|
|
|
{children.length > 0 ? children : (
|
2017-01-24 00:48:28 +00:00
|
|
|
<SidebarRequestRow
|
|
|
|
handleActivateRequest={this._nullFunction}
|
|
|
|
handleDuplicateRequest={this._nullFunction}
|
|
|
|
handleGenerateCode={this._nullFunction}
|
|
|
|
moveRequest={moveRequest}
|
|
|
|
isActive={false}
|
|
|
|
request={null}
|
|
|
|
requestGroup={requestGroup}
|
|
|
|
workspace={workspace}
|
|
|
|
requestCreate={handleCreateRequest}
|
|
|
|
/>
|
|
|
|
)}
|
2016-07-19 16:59:26 +00:00
|
|
|
</ul>
|
|
|
|
</li>
|
2017-01-24 00:48:28 +00:00
|
|
|
);
|
2016-07-19 16:59:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
SidebarRequestGroupRow.propTypes = {
|
|
|
|
// Functions
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSetRequestGroupCollapsed: PropTypes.func.isRequired,
|
2016-11-28 07:12:17 +00:00
|
|
|
handleDuplicateRequestGroup: 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-11-16 17:18:39 +00:00
|
|
|
handleActivateRequest: PropTypes.func.isRequired,
|
|
|
|
handleCreateRequest: 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-11-16 17:18:39 +00:00
|
|
|
isCollapsed: PropTypes.bool.isRequired,
|
|
|
|
workspace: PropTypes.object.isRequired,
|
2016-11-25 20:55:15 +00:00
|
|
|
requestGroup: PropTypes.object.isRequired,
|
2016-07-19 16:59:26 +00:00
|
|
|
|
|
|
|
// React DnD
|
2016-11-16 17:18:39 +00:00
|
|
|
isDragging: PropTypes.bool,
|
|
|
|
isDraggingOver: PropTypes.bool,
|
|
|
|
connectDragSource: PropTypes.func,
|
|
|
|
connectDropTarget: PropTypes.func
|
2016-07-19 16:59:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements the drag source contract.
|
|
|
|
*/
|
|
|
|
const dragSource = {
|
|
|
|
beginDrag(props) {
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Folder', 'Drag', 'Begin');
|
2016-07-19 16:59:26 +00:00
|
|
|
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-11-16 17:18:39 +00:00
|
|
|
const source = DragSource('SIDEBAR_REQUEST_GROUP_ROW', dragSource, sourceCollect)(SidebarRequestGroupRow);
|
|
|
|
const target = DropTarget('SIDEBAR_REQUEST_GROUP_ROW', dragTarget, targetCollect)(source);
|
2016-07-19 16:59:26 +00:00
|
|
|
export default target;
|