2016-11-25 23:21:07 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2016-11-25 23:09:17 +00:00
|
|
|
import SidebarRequestRow from './SidebarRequestRow';
|
|
|
|
import SidebarRequestGroupRow from './SidebarRequestGroupRow';
|
|
|
|
|
|
|
|
|
2016-11-25 23:21:07 +00:00
|
|
|
class SidebarChildren extends PureComponent {
|
2016-11-25 23:09:17 +00:00
|
|
|
|
|
|
|
_filterChildren (filter, children, extra = null) {
|
|
|
|
filter = filter || '';
|
|
|
|
|
|
|
|
return children.filter(child => {
|
|
|
|
if (child.doc.type !== 'Request') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const request = child.doc;
|
|
|
|
|
|
|
|
const otherMatches = extra || '';
|
|
|
|
const toMatch = `${request.method}❅${request.name}❅${otherMatches}`.toLowerCase();
|
|
|
|
const matchTokens = filter.toLowerCase().split(' ');
|
|
|
|
|
|
|
|
for (let i = 0; i < matchTokens.length; i++) {
|
|
|
|
let token = `${matchTokens[i]}`;
|
|
|
|
if (toMatch.indexOf(token) === -1) {
|
|
|
|
// Filter failed. Don't render children
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderChildren (children, requestGroup) {
|
|
|
|
const {
|
|
|
|
filter,
|
|
|
|
handleCreateRequest,
|
|
|
|
handleCreateRequestGroup,
|
|
|
|
handleSetRequestGroupCollapsed,
|
|
|
|
moveRequest,
|
|
|
|
moveRequestGroup,
|
|
|
|
handleActivateRequest,
|
|
|
|
activeRequest,
|
|
|
|
workspace,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const filteredChildren = this._filterChildren(
|
|
|
|
filter,
|
|
|
|
children,
|
|
|
|
requestGroup && requestGroup.name
|
|
|
|
);
|
|
|
|
|
|
|
|
const activeRequestId = activeRequest ? activeRequest._id : 'n/a';
|
|
|
|
|
|
|
|
return filteredChildren.map(child => {
|
|
|
|
if (child.doc.type === 'Request') {
|
|
|
|
return (
|
|
|
|
<SidebarRequestRow
|
|
|
|
key={child.doc._id}
|
|
|
|
moveRequest={moveRequest}
|
|
|
|
handleActivateRequest={handleActivateRequest}
|
|
|
|
requestCreate={handleCreateRequest}
|
|
|
|
isActive={child.doc._id === activeRequestId}
|
|
|
|
request={child.doc}
|
|
|
|
workspace={workspace}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a RequestGroup!
|
|
|
|
|
|
|
|
const requestGroup = child.doc;
|
|
|
|
|
|
|
|
function hasActiveChild (children) {
|
|
|
|
for (const c of children) {
|
2016-11-25 23:21:07 +00:00
|
|
|
if (hasActiveChild(c.children || [])) {
|
|
|
|
return true;
|
2016-11-25 23:09:17 +00:00
|
|
|
} else if (c.doc._id === activeRequestId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find anything, so return
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isActive = hasActiveChild(child.children);
|
|
|
|
const children = this._renderChildren(child.children, requestGroup);
|
|
|
|
|
|
|
|
// Don't render the row if there are no children while filtering
|
|
|
|
if (filter && !children.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SidebarRequestGroupRow
|
|
|
|
handleActivateRequest={handleActivateRequest}
|
|
|
|
key={requestGroup._id}
|
|
|
|
isActive={isActive}
|
|
|
|
moveRequestGroup={moveRequestGroup}
|
|
|
|
moveRequest={moveRequest}
|
|
|
|
handleSetRequestGroupCollapsed={handleSetRequestGroupCollapsed}
|
|
|
|
isCollapsed={child.collapsed}
|
|
|
|
handleCreateRequest={handleCreateRequest}
|
|
|
|
handleCreateRequestGroup={handleCreateRequestGroup}
|
|
|
|
numChildren={child.children.length}
|
|
|
|
workspace={workspace}
|
|
|
|
requestGroup={requestGroup}
|
|
|
|
children={children}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {children} = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className="sidebar__list sidebar__list-root">
|
|
|
|
{this._renderChildren(children)}
|
|
|
|
</ul>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SidebarChildren.propTypes = {
|
|
|
|
// Required
|
|
|
|
handleActivateRequest: PropTypes.func.isRequired,
|
|
|
|
handleCreateRequest: PropTypes.func.isRequired,
|
|
|
|
handleCreateRequestGroup: PropTypes.func.isRequired,
|
|
|
|
handleSetRequestGroupCollapsed: PropTypes.func.isRequired,
|
|
|
|
moveRequest: PropTypes.func.isRequired,
|
|
|
|
moveRequestGroup: PropTypes.func.isRequired,
|
|
|
|
children: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
|
|
filter: PropTypes.string.isRequired,
|
|
|
|
workspace: PropTypes.object.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
|
|
|
activeRequest: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SidebarChildren;
|