2016-11-26 00:37:59 +00:00
|
|
|
import React, {PropTypes, PureComponent} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
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';
|
2017-03-08 05:52:17 +00:00
|
|
|
import RequestActionsDropdown from '../dropdowns/request-actions-dropdown';
|
|
|
|
import Editable from '../base/editable';
|
|
|
|
import MethodTag from '../tags/method-tag';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../../../models';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2017-06-01 22:58:09 +00:00
|
|
|
import {showModal} from '../modals/index';
|
|
|
|
import RequestSettingsModal from '../modals/request-settings-modal';
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2016-11-26 00:37:59 +00:00
|
|
|
class SidebarRequestRow extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
dragDirection: 0,
|
2017-03-03 20:09:08 +00:00
|
|
|
isEditing: false
|
2017-03-03 01:44:07 +00:00
|
|
|
};
|
|
|
|
}
|
2016-12-21 23:37:48 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_setRequestActionsDropdownRef (n) {
|
|
|
|
this._requestActionsDropdown = n;
|
|
|
|
}
|
2017-02-08 00:31:48 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleShowRequestActions (e) {
|
2017-02-08 00:31:48 +00:00
|
|
|
e.preventDefault();
|
|
|
|
this._requestActionsDropdown.show();
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-02-08 00:31:48 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleEditStart () {
|
2016-12-21 23:37:48 +00:00
|
|
|
trackEvent('Request', 'Rename', 'In Place');
|
|
|
|
this.setState({isEditing: true});
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-07-19 16:59:26 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleRequestUpdateName (name) {
|
2017-03-03 20:09:08 +00:00
|
|
|
models.request.update(this.props.request, {name});
|
2017-01-24 00:48:28 +00:00
|
|
|
this.setState({isEditing: false});
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-25 20:55:15 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleRequestCreateFromEmpty () {
|
2016-11-25 23:09:17 +00:00
|
|
|
const parentId = this.props.requestGroup._id;
|
|
|
|
this.props.requestCreate(parentId);
|
2016-11-25 20:55:15 +00:00
|
|
|
trackEvent('Request', 'Create', 'Empty Folder');
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-25 20:55:15 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleRequestActivate () {
|
2016-11-25 20:55:15 +00:00
|
|
|
const {isActive, request, handleActivateRequest} = this.props;
|
|
|
|
|
|
|
|
if (isActive) {
|
|
|
|
return;
|
2016-07-19 16:59:26 +00:00
|
|
|
}
|
2016-11-25 20:55:15 +00:00
|
|
|
|
2016-11-25 23:09:17 +00:00
|
|
|
handleActivateRequest(request._id);
|
2016-11-25 20:55:15 +00:00
|
|
|
trackEvent('Request', 'Activate', 'Sidebar');
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-07-19 16:59:26 +00:00
|
|
|
|
2017-06-01 22:58:09 +00:00
|
|
|
_handleShowRequestSettings () {
|
|
|
|
showModal(RequestSettingsModal, this.props.request);
|
|
|
|
}
|
|
|
|
|
2017-06-05 15:09:06 +00:00
|
|
|
_handleClickDescription () {
|
|
|
|
trackEvent('Request', 'Click Description Icon');
|
|
|
|
this._handleShowRequestSettings();
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
setDragDirection (dragDirection) {
|
|
|
|
if (dragDirection !== this.state.dragDirection) {
|
2017-03-03 20:09:08 +00:00
|
|
|
this.setState({dragDirection});
|
2016-07-19 16:59:26 +00:00
|
|
|
}
|
2016-05-01 19:56:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
render () {
|
|
|
|
const {
|
2016-11-27 21:42:38 +00:00
|
|
|
handleDuplicateRequest,
|
2016-11-28 07:12:17 +00:00
|
|
|
handleGenerateCode,
|
2016-07-19 16:59:26 +00:00
|
|
|
connectDragSource,
|
|
|
|
connectDropTarget,
|
|
|
|
isDragging,
|
|
|
|
isDraggingOver,
|
|
|
|
request,
|
|
|
|
requestGroup,
|
2017-03-03 20:09:08 +00:00
|
|
|
isActive
|
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}>
|
2017-06-02 00:00:57 +00:00
|
|
|
<button className="sidebar__clickable" onClick={this._handleRequestCreateFromEmpty}>
|
2017-01-23 22:41:31 +00:00
|
|
|
<em className="faded">click to add first request...</em>
|
2016-07-19 16:59:26 +00:00
|
|
|
</button>
|
2016-07-08 06:02:40 +00:00
|
|
|
</div>
|
2016-07-19 16:59:26 +00:00
|
|
|
</li>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-07-19 16:59:26 +00:00
|
|
|
} else {
|
|
|
|
node = (
|
|
|
|
<li className={classes}>
|
2017-02-28 06:26:24 +00:00
|
|
|
<div className={classnames('sidebar__item', 'sidebar__item--request', {
|
|
|
|
'sidebar__item--active': isActive
|
|
|
|
})}>
|
|
|
|
<button className="wide"
|
|
|
|
onClick={this._handleRequestActivate}
|
|
|
|
onContextMenu={this._handleShowRequestActions}>
|
2016-07-19 16:59:26 +00:00
|
|
|
<div className="sidebar__clickable">
|
|
|
|
<MethodTag method={request.method}/>
|
2017-06-01 22:58:09 +00:00
|
|
|
<div>
|
|
|
|
<Editable value={request.name}
|
|
|
|
className="inline-block"
|
|
|
|
onEditStart={this._handleEditStart}
|
|
|
|
onSubmit={this._handleRequestUpdateName}/>
|
|
|
|
{request.description && (
|
2017-06-05 15:09:06 +00:00
|
|
|
<a title={isActive ? 'View description' : null}
|
|
|
|
onClick={isActive ? this._handleClickDescription : null}
|
|
|
|
className={classnames('space-left super-duper-faint a--nocolor', {
|
|
|
|
'icon': isActive
|
|
|
|
})}>
|
|
|
|
<i className="fa fa-file-text-o space-left"/>
|
|
|
|
</a>
|
2017-06-01 22:58:09 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2016-07-19 16:59:26 +00:00
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
<div className="sidebar__actions">
|
|
|
|
<RequestActionsDropdown
|
2017-06-01 22:58:09 +00:00
|
|
|
right
|
2017-02-08 00:31:48 +00:00
|
|
|
ref={this._setRequestActionsDropdownRef}
|
2016-11-27 21:42:38 +00:00
|
|
|
handleDuplicateRequest={handleDuplicateRequest}
|
2016-11-28 07:12:17 +00:00
|
|
|
handleGenerateCode={handleGenerateCode}
|
2017-06-01 22:58:09 +00:00
|
|
|
handleShowSettings={this._handleShowRequestSettings}
|
2016-07-19 16:59:26 +00:00
|
|
|
request={request}
|
|
|
|
requestGroup={requestGroup}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</li>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-07-19 16:59:26 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 23:37:48 +00:00
|
|
|
if (!this.state.isEditing) {
|
|
|
|
return connectDragSource(connectDropTarget(node));
|
|
|
|
} else {
|
|
|
|
return connectDropTarget(node);
|
|
|
|
}
|
2016-07-19 16:59:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
SidebarRequestRow.propTypes = {
|
|
|
|
// Functions
|
2016-11-16 17:18:39 +00:00
|
|
|
handleActivateRequest: PropTypes.func.isRequired,
|
2016-11-27 21:42:38 +00:00
|
|
|
handleDuplicateRequest: PropTypes.func.isRequired,
|
2016-11-28 07:12:17 +00:00
|
|
|
handleGenerateCode: PropTypes.func.isRequired,
|
2016-08-15 17:04:36 +00:00
|
|
|
requestCreate: PropTypes.func.isRequired,
|
2017-06-13 20:45:15 +00:00
|
|
|
moveDoc: 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
|
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
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
// Optional
|
|
|
|
requestGroup: PropTypes.object,
|
|
|
|
request: PropTypes.object
|
|
|
|
};
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
const dragSource = {
|
2017-03-03 20:09:08 +00:00
|
|
|
beginDrag (props) {
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Request', 'Drag', 'Begin');
|
2017-02-28 06:26:24 +00:00
|
|
|
return {request: props.request};
|
2016-07-19 16:59:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2017-06-13 20:45:15 +00:00
|
|
|
const movingDoc = monitor.getItem().requestGroup || monitor.getItem().request;
|
2016-07-21 03:58:52 +00:00
|
|
|
|
2017-06-13 20:45:15 +00:00
|
|
|
const parentId = props.requestGroup ? props.requestGroup._id : props.request.parentId;
|
|
|
|
const targetId = props.request ? props.request._id : null;
|
2016-07-21 03:58:52 +00:00
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
if (isAbove(monitor, component)) {
|
2017-06-13 20:45:15 +00:00
|
|
|
props.moveDoc(movingDoc, parentId, targetId, 1);
|
2016-07-19 16:59:26 +00:00
|
|
|
} else {
|
2017-06-13 20:45:15 +00:00
|
|
|
props.moveDoc(movingDoc, 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()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
const source = DragSource('SIDEBAR_REQUEST_ROW', dragSource, sourceCollect)(SidebarRequestRow);
|
2017-03-08 05:52:17 +00:00
|
|
|
export default DropTarget('SIDEBAR_REQUEST_ROW', dragTarget, targetCollect)(source);
|