2016-07-16 02:06:10 +00:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
2016-08-15 17:04:36 +00:00
|
|
|
import classnames from 'classnames';
|
2016-11-16 17:18:39 +00:00
|
|
|
import EnvironmentsDropdown from '../dropdowns/EnvironmentsDropdown';
|
2016-07-16 02:06:10 +00:00
|
|
|
import SidebarRequestRow from './SidebarRequestRow';
|
2016-07-19 16:59:26 +00:00
|
|
|
import SidebarRequestGroupRow from './SidebarRequestGroupRow';
|
2016-08-15 17:04:36 +00:00
|
|
|
import SidebarFilter from './SidebarFilter';
|
2016-11-11 23:06:24 +00:00
|
|
|
import SyncButton from '../dropdowns/SyncDropdown';
|
2016-11-16 17:18:39 +00:00
|
|
|
import WorkspaceDropdown from '../dropdowns/WorkspaceDropdown';
|
2016-10-21 17:20:36 +00:00
|
|
|
import {
|
|
|
|
SIDEBAR_SKINNY_REMS,
|
|
|
|
COLLAPSE_SIDEBAR_REMS
|
2016-11-10 02:40:53 +00:00
|
|
|
} from '../../../common/constants';
|
2016-03-18 00:36:00 +00:00
|
|
|
|
2016-07-25 22:27:29 +00:00
|
|
|
|
2016-03-23 05:26:27 +00:00
|
|
|
class Sidebar extends Component {
|
2016-04-26 07:29:24 +00:00
|
|
|
_filterChildren (filter, children, extra = null) {
|
2016-10-21 17:20:36 +00:00
|
|
|
filter = filter || '';
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
return children.filter(child => {
|
|
|
|
if (child.doc.type !== 'Request') {
|
2016-04-05 05:35:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
const request = child.doc;
|
2016-04-05 05:35:21 +00:00
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
const otherMatches = extra || '';
|
|
|
|
const toMatch = `${request.method}❅${request.name}❅${otherMatches}`.toLowerCase();
|
|
|
|
const matchTokens = filter.toLowerCase().split(' ');
|
2016-04-05 05:35:21 +00:00
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 01:11:16 +00:00
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
return true;
|
|
|
|
})
|
2016-03-24 10:20:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
_renderChildren (children, requestGroup) {
|
2016-04-29 04:57:03 +00:00
|
|
|
const {
|
|
|
|
filter,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleCreateRequest,
|
2016-11-23 20:44:46 +00:00
|
|
|
handleCreateRequestGroup,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSetRequestGroupCollapsed,
|
2016-07-19 16:59:26 +00:00
|
|
|
moveRequest,
|
|
|
|
moveRequestGroup,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleActivateRequest,
|
|
|
|
activeRequest,
|
|
|
|
workspace,
|
2016-04-29 04:57:03 +00:00
|
|
|
} = this.props;
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
const filteredChildren = this._filterChildren(
|
|
|
|
filter,
|
|
|
|
children,
|
|
|
|
requestGroup && requestGroup.name
|
2016-07-19 16:59:26 +00:00
|
|
|
);
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
const activeRequestId = activeRequest ? activeRequest._id : 'n/a';
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
return filteredChildren.map(child => {
|
|
|
|
if (child.doc.type === 'Request') {
|
|
|
|
return (
|
|
|
|
<SidebarRequestRow
|
|
|
|
key={child.doc._id}
|
2016-07-19 16:59:26 +00:00
|
|
|
moveRequest={moveRequest}
|
2016-11-16 17:18:39 +00:00
|
|
|
handleActivateRequest={handleActivateRequest}
|
|
|
|
requestCreate={handleCreateRequest.bind(null, workspace._id)}
|
2016-07-19 16:59:26 +00:00
|
|
|
isActive={child.doc._id === activeRequestId}
|
2016-04-26 07:29:24 +00:00
|
|
|
request={child.doc}
|
2016-11-16 17:18:39 +00:00
|
|
|
workspace={workspace}
|
2016-04-26 07:29:24 +00:00
|
|
|
/>
|
|
|
|
)
|
2016-04-29 04:57:03 +00:00
|
|
|
}
|
2016-05-01 19:56:30 +00:00
|
|
|
|
2016-04-29 04:57:03 +00:00
|
|
|
// We have a RequestGroup!
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-04-29 04:57:03 +00:00
|
|
|
const requestGroup = child.doc;
|
2016-11-16 17:18:39 +00:00
|
|
|
|
|
|
|
function hasActiveChild (children) {
|
|
|
|
for (const c of children) {
|
|
|
|
if (c.children.length) {
|
|
|
|
return hasActiveChild(c.children);
|
|
|
|
} else if (c.doc._id === activeRequestId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find anything, so return
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isActive = hasActiveChild(child.children);
|
2016-04-29 04:57:03 +00:00
|
|
|
|
|
|
|
const children = this._renderChildren(child.children, requestGroup);
|
|
|
|
|
|
|
|
// Don't render the row if there are no children while filtering
|
|
|
|
if (filter && !children.length) {
|
2016-04-26 07:29:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
2016-04-29 04:57:03 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SidebarRequestGroupRow
|
2016-11-16 17:18:39 +00:00
|
|
|
handleActivateRequest={handleActivateRequest}
|
2016-04-29 04:57:03 +00:00
|
|
|
key={requestGroup._id}
|
|
|
|
isActive={isActive}
|
2016-07-19 16:59:26 +00:00
|
|
|
moveRequestGroup={moveRequestGroup}
|
2016-07-21 03:58:52 +00:00
|
|
|
moveRequest={moveRequest}
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSetRequestGroupCollapsed={handleSetRequestGroupCollapsed}
|
|
|
|
isCollapsed={child.collapsed}
|
|
|
|
handleCreateRequest={handleCreateRequest.bind(null, requestGroup._id)}
|
2016-11-23 20:44:46 +00:00
|
|
|
handleCreateRequestGroup={handleCreateRequestGroup.bind(null, requestGroup._id)}
|
2016-04-29 04:57:03 +00:00
|
|
|
numChildren={child.children.length}
|
2016-11-16 17:18:39 +00:00
|
|
|
workspace={workspace}
|
|
|
|
requestGroup={requestGroup}
|
|
|
|
children={children}
|
|
|
|
/>
|
2016-04-29 04:57:03 +00:00
|
|
|
)
|
2016-04-26 07:29:24 +00:00
|
|
|
})
|
2016-03-24 10:20:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:26:27 +00:00
|
|
|
render () {
|
2016-08-15 17:04:36 +00:00
|
|
|
const {
|
|
|
|
showCookiesModal,
|
|
|
|
filter,
|
|
|
|
children,
|
2016-09-12 20:04:15 +00:00
|
|
|
hidden,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleCreateRequest,
|
|
|
|
handleCreateRequestGroup,
|
2016-10-24 23:30:37 +00:00
|
|
|
width,
|
2016-11-16 17:18:39 +00:00
|
|
|
workspace,
|
|
|
|
workspaces,
|
|
|
|
environments,
|
|
|
|
activeEnvironment,
|
|
|
|
handleSetActiveEnvironment,
|
|
|
|
handleSetActiveWorkspace,
|
|
|
|
handleImportFile,
|
|
|
|
handleExportFile,
|
|
|
|
handleChangeFilter,
|
|
|
|
isLoading,
|
2016-08-15 17:04:36 +00:00
|
|
|
} = this.props;
|
2016-03-23 05:26:27 +00:00
|
|
|
|
|
|
|
return (
|
2016-08-15 17:04:36 +00:00
|
|
|
<aside className={classnames('sidebar', {
|
2016-09-12 20:04:15 +00:00
|
|
|
'sidebar--hidden': hidden,
|
2016-08-15 17:04:36 +00:00
|
|
|
'sidebar--skinny': width < SIDEBAR_SKINNY_REMS,
|
|
|
|
'sidebar--collapsed': width < COLLAPSE_SIDEBAR_REMS
|
|
|
|
})}>
|
|
|
|
<WorkspaceDropdown
|
|
|
|
className="sidebar__header"
|
2016-11-16 17:18:39 +00:00
|
|
|
activeWorkspace={workspace}
|
|
|
|
workspaces={workspaces}
|
|
|
|
handleExportFile={handleExportFile}
|
|
|
|
handleImportFile={handleImportFile}
|
|
|
|
handleSetActiveWorkspace={handleSetActiveWorkspace}
|
|
|
|
isLoading={isLoading}
|
2016-08-15 17:04:36 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="sidebar__menu">
|
2016-11-16 17:18:39 +00:00
|
|
|
<EnvironmentsDropdown
|
|
|
|
handleChangeEnvironment={id => handleSetActiveEnvironment(workspace._id, id)}
|
|
|
|
activeEnvironment={activeEnvironment}
|
|
|
|
environments={environments}
|
|
|
|
workspace={workspace}
|
|
|
|
/>
|
2016-10-21 17:20:36 +00:00
|
|
|
<button className="btn btn--super-compact"
|
|
|
|
onClick={e => showCookiesModal()}>
|
2016-08-15 17:04:36 +00:00
|
|
|
<div className="sidebar__menu__thing">
|
|
|
|
<span>Cookies</span>
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<SidebarFilter
|
2016-11-16 17:18:39 +00:00
|
|
|
onChange={filter => handleChangeFilter(filter)}
|
|
|
|
requestCreate={handleCreateRequest.bind(null, workspace._id)}
|
|
|
|
requestGroupCreate={handleCreateRequestGroup.bind(null, workspace._id)}
|
2016-08-15 17:04:36 +00:00
|
|
|
filter={filter}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<ul className="sidebar__list sidebar__list-root">
|
2016-05-01 19:56:30 +00:00
|
|
|
{this._renderChildren(children)}
|
|
|
|
</ul>
|
2016-07-06 20:18:26 +00:00
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
<SyncButton
|
|
|
|
className="sidebar__footer"
|
|
|
|
key={workspace._id}
|
|
|
|
workspace={workspace}
|
|
|
|
/>
|
2016-05-01 19:56:30 +00:00
|
|
|
</aside>
|
2016-03-23 05:26:27 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 00:36:00 +00:00
|
|
|
|
2016-03-20 04:00:40 +00:00
|
|
|
Sidebar.propTypes = {
|
2016-04-26 07:29:24 +00:00
|
|
|
// Functions
|
2016-11-16 17:18:39 +00:00
|
|
|
handleActivateRequest: PropTypes.func.isRequired,
|
|
|
|
handleSetRequestGroupCollapsed: PropTypes.func.isRequired,
|
|
|
|
handleChangeFilter: PropTypes.func.isRequired,
|
|
|
|
handleImportFile: PropTypes.func.isRequired,
|
|
|
|
handleExportFile: PropTypes.func.isRequired,
|
|
|
|
handleSetActiveWorkspace: PropTypes.func.isRequired,
|
|
|
|
handleSetActiveEnvironment: PropTypes.func.isRequired,
|
2016-07-19 16:59:26 +00:00
|
|
|
moveRequest: PropTypes.func.isRequired,
|
|
|
|
moveRequestGroup: PropTypes.func.isRequired,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleCreateRequest: PropTypes.func.isRequired,
|
|
|
|
handleCreateRequestGroup: PropTypes.func.isRequired,
|
2016-08-15 17:04:36 +00:00
|
|
|
showEnvironmentsModal: PropTypes.func.isRequired,
|
|
|
|
showCookiesModal: PropTypes.func.isRequired,
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
// Other
|
2016-11-16 17:18:39 +00:00
|
|
|
hidden: PropTypes.bool.isRequired,
|
|
|
|
width: PropTypes.number.isRequired,
|
|
|
|
isLoading: PropTypes.bool.isRequired,
|
2016-04-26 07:29:24 +00:00
|
|
|
children: PropTypes.array.isRequired,
|
2016-11-16 17:18:39 +00:00
|
|
|
workspace: PropTypes.object.isRequired,
|
|
|
|
workspaces: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
|
|
environments: PropTypes.arrayOf(PropTypes.object).isRequired,
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
// Optional
|
|
|
|
filter: PropTypes.string,
|
2016-11-16 17:18:39 +00:00
|
|
|
activeRequest: PropTypes.object,
|
|
|
|
activeEnvironment: PropTypes.object,
|
2016-03-20 04:00:40 +00:00
|
|
|
};
|
2016-03-18 00:36:00 +00:00
|
|
|
|
|
|
|
export default Sidebar;
|