insomnia/app/components/Sidebar.js

134 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-03-23 05:26:27 +00:00
import React, {Component, PropTypes} from 'react'
2016-04-23 06:16:23 +00:00
import WorkspaceDropdown from './../containers/WorkspaceDropdown'
2016-04-07 03:34:40 +00:00
import DebouncingInput from './base/DebouncingInput'
import SidebarRequestGroupRow from './SidebarRequestGroupRow'
import SidebarRequestRow from './SidebarRequestRow'
2016-03-18 00:36:00 +00:00
2016-03-23 05:26:27 +00:00
class Sidebar extends Component {
onFilterChange (value) {
this.props.changeFilter(value);
}
_filterChildren (filter, children, extra = null) {
return children.filter(child => {
if (child.doc.type !== 'Request') {
2016-04-05 05:35:21 +00:00
return true;
}
const request = child.doc;
2016-04-05 05:35:21 +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
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
return true;
})
2016-03-24 10:20:11 +00:00
}
_renderChildren (children, requestGroup) {
2016-04-29 04:57:03 +00:00
const {
filter,
toggleRequestGroup,
addRequestToRequestGroup
} = this.props;
const filteredChildren = this._filterChildren(
filter,
children,
requestGroup && requestGroup.name
).sort((a, b) => a.doc._id > b.doc._id ? -1 : 1);
return filteredChildren.map(child => {
if (child.doc.type === 'Request') {
return (
<SidebarRequestRow
key={child.doc._id}
activateRequest={this.props.activateRequest}
isActive={child.doc._id === this.props.activeRequestId}
request={child.doc}
/>
)
2016-04-29 04:57:03 +00:00
}
// We have a RequestGroup!
2016-04-29 04:57:03 +00:00
const requestGroup = child.doc;
const isActive = !!child.children.find(c => c.doc._id === this.props.activeRequestId);
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;
}
2016-04-29 04:57:03 +00:00
return (
<SidebarRequestGroupRow
key={requestGroup._id}
isActive={isActive}
toggleRequestGroup={toggleRequestGroup}
addRequestToRequestGroup={addRequestToRequestGroup}
numChildren={child.children.length}
requestGroup={requestGroup}
>
{children}
</SidebarRequestGroupRow>
)
})
2016-03-24 10:20:11 +00:00
}
2016-03-23 05:26:27 +00:00
render () {
const {filter, children} = this.props;
2016-03-23 05:26:27 +00:00
return (
2016-04-17 22:46:17 +00:00
<section className="sidebar bg-dark grid--v section section--bordered">
<header className="header bg-brand section__header">
2016-04-15 02:13:49 +00:00
<WorkspaceDropdown />
2016-04-07 01:11:16 +00:00
</header>
<div className="grid--v grid--start grid__cell section__body">
2016-04-10 05:03:18 +00:00
<ul
className="grid--v grid--start grid__cell sidebar__scroll hover-scrollbars sidebar__request-list">
{this._renderChildren(children)}
2016-04-10 05:03:18 +00:00
</ul>
2016-04-20 17:38:27 +00:00
<div className="grid grid--center">
<div className="grid__cell form-control form-control--underlined">
<DebouncingInput
type="text"
placeholder="Filter Items"
debounceMillis={300}
value={filter}
2016-04-20 17:38:27 +00:00
onChange={this.onFilterChange.bind(this)}/>
</div>
2016-04-09 21:41:27 +00:00
</div>
2016-03-23 05:26:27 +00:00
</div>
</section>
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 = {
// Functions
2016-03-20 04:47:43 +00:00
activateRequest: PropTypes.func.isRequired,
toggleRequestGroup: PropTypes.func.isRequired,
2016-04-16 23:24:57 +00:00
addRequestToRequestGroup: PropTypes.func.isRequired,
2016-03-23 05:26:27 +00:00
changeFilter: PropTypes.func.isRequired,
// Other
children: PropTypes.array.isRequired,
workspaceId: PropTypes.string.isRequired,
// Optional
filter: PropTypes.string,
activeRequestId: PropTypes.string
2016-03-20 04:00:40 +00:00
};
2016-03-18 00:36:00 +00:00
export default Sidebar;