insomnia/app/components/Sidebar.js

170 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-03-23 05:26:27 +00:00
import React, {Component, PropTypes} from 'react'
2016-04-05 05:35:21 +00:00
import classnames from 'classnames'
2016-04-23 06:16:23 +00:00
import WorkspaceDropdown from './../containers/WorkspaceDropdown'
import RequestActionsDropdown from './../containers/RequestActionsDropdown'
import RequestGroupActionsDropdown from './../containers/RequestGroupActionsDropdown'
2016-04-07 03:34:40 +00:00
import DebouncingInput from './base/DebouncingInput'
2016-04-04 07:15:30 +00:00
import MethodTag from './MethodTag'
2016-04-20 17:38:27 +00:00
import * as db from '../database'
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);
}
2016-04-16 23:24:57 +00:00
renderRequestGroupRow (requestGroup = null) {
2016-04-07 03:09:14 +00:00
const {
activeFilter,
activeRequest,
2016-04-16 23:24:57 +00:00
addRequestToRequestGroup,
2016-04-07 03:09:14 +00:00
toggleRequestGroup,
requests
2016-04-07 03:09:14 +00:00
} = this.props;
2016-03-24 10:20:11 +00:00
2016-04-04 07:15:30 +00:00
let filteredRequests = requests.filter(
2016-04-05 05:35:21 +00:00
r => {
// TODO: Move this to a lib file
if (!activeFilter) {
return true;
}
2016-04-20 17:38:27 +00:00
const requestGroupName = requestGroup ? requestGroup.name : '';
const toMatch = `${requestGroupName}${r.method}${r.name}`.toLowerCase();
2016-04-10 06:37:22 +00:00
const matchTokens = activeFilter.toLowerCase().split(' ');
2016-04-05 05:35:21 +00:00
for (let i = 0; i < matchTokens.length; i++) {
2016-04-20 17:38:27 +00:00
let token = `${matchTokens[i]}`;
if (toMatch.indexOf(token) === -1) {
2016-04-05 05:35:21 +00:00
return false;
}
}
return true;
}
2016-03-24 10:20:11 +00:00
);
2016-04-04 07:15:30 +00:00
if (!requestGroup) {
2016-04-16 23:24:57 +00:00
filteredRequests = filteredRequests.filter(r => !r.parent);
2016-04-04 07:15:30 +00:00
return filteredRequests.map(request => this.renderRequestRow(request));
2016-04-05 05:35:21 +00:00
}
// Grab all of the children for this request group
2016-04-16 23:24:57 +00:00
filteredRequests = filteredRequests.filter(r => r.parent === requestGroup._id);
2016-04-05 05:35:21 +00:00
// Don't show folder if it was not in the filter
if (activeFilter && !filteredRequests.length) {
return null;
}
2016-04-16 23:24:57 +00:00
const isActive = activeRequest && filteredRequests.find(r => r._id == activeRequest._id);
2016-04-05 05:35:21 +00:00
let folderIconClass = 'fa-folder';
2016-04-20 17:38:27 +00:00
let expanded = !requestGroup.collapsed;
2016-04-05 05:35:21 +00:00
folderIconClass += !expanded ? '' : '-open';
folderIconClass += isActive ? '' : '-o';
2016-04-07 01:11:16 +00:00
2016-04-05 05:35:21 +00:00
const sidebarItemClassNames = classnames(
'sidebar__item',
'sidebar__item--bordered',
{'sidebar__item--active': isActive}
);
2016-04-07 01:11:16 +00:00
2016-04-05 05:35:21 +00:00
return (
2016-04-16 23:24:57 +00:00
<li key={requestGroup._id}>
2016-04-05 05:35:21 +00:00
<div className={sidebarItemClassNames}>
2016-04-17 22:46:17 +00:00
<div className="sidebar__item__row sidebar__item__row--heading">
2016-04-16 23:24:57 +00:00
<button onClick={e => toggleRequestGroup(requestGroup)}>
2016-04-05 05:35:21 +00:00
<i className={'fa ' + folderIconClass}></i>
&nbsp;&nbsp;&nbsp;{requestGroup.name}
</button>
</div>
2016-04-07 01:11:16 +00:00
<div className="sidebar__item__btn grid">
2016-04-16 23:24:57 +00:00
<button onClick={(e) => addRequestToRequestGroup(requestGroup)}>
2016-04-05 05:35:21 +00:00
<i className="fa fa-plus-circle"></i>
</button>
2016-04-10 05:03:18 +00:00
<RequestGroupActionsDropdown
requestGroup={requestGroup}
right={true}
className="tall"/>
2016-03-24 10:20:11 +00:00
</div>
2016-04-05 05:35:21 +00:00
</div>
<ul>
2016-04-07 01:11:16 +00:00
{expanded && !filteredRequests.length ? this.renderRequestRow() : null}
2016-04-05 05:35:21 +00:00
{!expanded ? null : filteredRequests.map(request => this.renderRequestRow(request, requestGroup))}
</ul>
</li>
);
2016-03-24 10:20:11 +00:00
}
2016-04-07 01:11:16 +00:00
renderRequestRow (request = null, requestGroup = null) {
2016-03-24 10:20:11 +00:00
const {activeRequest, activateRequest} = this.props;
2016-04-16 23:24:57 +00:00
const isActive = request && activeRequest && request._id === activeRequest._id;
2016-04-03 22:54:39 +00:00
2016-03-24 10:20:11 +00:00
return (
2016-04-16 23:24:57 +00:00
<li key={request ? request._id : 'none'}>
2016-04-04 07:15:30 +00:00
<div className={'sidebar__item ' + (isActive ? 'sidebar__item--active' : '')}>
<div className="sidebar__item__row">
2016-04-07 01:11:16 +00:00
{request ? (
2016-04-16 23:24:57 +00:00
<button onClick={() => {activateRequest(request)}}>
2016-04-07 01:11:16 +00:00
<MethodTag method={request.method}/> {request.name}
</button>
) : (
<button className="italic">No Requests</button>
)}
2016-04-04 07:15:30 +00:00
</div>
2016-04-07 01:11:16 +00:00
{request ? (
<RequestActionsDropdown
className="sidebar__item__btn"
right={true}
request={request}
requestGroup={requestGroup}
/>
) : null}
2016-04-04 01:05:34 +00:00
</div>
2016-03-24 10:20:11 +00:00
</li>
);
}
2016-03-23 05:26:27 +00:00
render () {
2016-04-07 03:34:40 +00:00
const {activeFilter, requestGroups} = 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.renderRequestGroupRow(null)}
{requestGroups.map(requestGroup => this.renderRequestGroupRow(requestGroup))}
</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={activeFilter}
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 = {
2016-03-20 04:47:43 +00:00
activateRequest: 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,
2016-04-04 07:15:30 +00:00
toggleRequestGroup: PropTypes.func.isRequired,
2016-03-23 05:26:27 +00:00
activeFilter: PropTypes.string,
2016-03-20 23:20:00 +00:00
requests: PropTypes.array.isRequired,
2016-03-24 10:20:11 +00:00
requestGroups: PropTypes.array.isRequired,
2016-04-04 07:15:30 +00:00
activeRequest: PropTypes.object
2016-03-20 04:00:40 +00:00
};
2016-03-18 00:36:00 +00:00
export default Sidebar;