insomnia/app/components/Sidebar.js

171 lines
5.4 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-04 07:15:30 +00:00
import WorkspaceDropdown from './dropdowns/WorkspaceDropdown'
2016-04-03 22:54:39 +00:00
import RequestActionsDropdown from './dropdowns/RequestActionsDropdown'
2016-04-04 07:15:30 +00:00
import MethodTag from './MethodTag'
import Dropdown from './base/Dropdown'
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-03 22:54:39 +00:00
renderRequestGroupRow (requestGroup) {
2016-04-04 07:15:30 +00:00
const {activeFilter, activeRequest, addRequest, toggleRequestGroup, requests, requestGroups} = 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;
}
const toMatch = `${r.method} ::: ${r.name}`.toLowerCase();
const matchTokens = activeFilter.split(' ');
for (let i = 0; i < matchTokens.length; i++) {
if (toMatch.indexOf(matchTokens[i]) === -1) {
return false;
}
}
return true;
}
2016-03-24 10:20:11 +00:00
);
2016-04-04 07:15:30 +00:00
if (!requestGroup) {
// Grab all requests that are not children of request groups
2016-04-05 05:35:21 +00:00
// TODO: Optimize this
2016-04-04 07:15:30 +00:00
filteredRequests = filteredRequests.filter(r => {
return !requestGroups.find(rg => {
return rg.children.find(c => c.id === r.id)
})
});
return filteredRequests.map(request => this.renderRequestRow(request));
2016-04-05 05:35:21 +00:00
}
// Grab all of the children for this request group
filteredRequests = filteredRequests.filter(
r => requestGroup.children.find(c => c.id === r.id)
);
// Don't show folder if it was not in the filter
if (activeFilter && !filteredRequests.length) {
return null;
}
const isActive = activeRequest && filteredRequests.find(r => r.id == activeRequest.id);
let folderIconClass = 'fa-folder';
2016-04-05 06:08:03 +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 (
<li key={requestGroup.id}>
<div className={sidebarItemClassNames}>
<div className="sidebar__item__row">
<button onClick={e => toggleRequestGroup(requestGroup.id)}>
<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-05 05:35:21 +00:00
<button onClick={(e) => addRequest(requestGroup.id)}>
<i className="fa fa-plus-circle"></i>
</button>
<Dropdown right={true} className="tall">
<button>
<i className="fa fa-caret-down"></i>
2016-04-04 07:15:30 +00:00
</button>
2016-04-05 05:35:21 +00:00
<ul>
<li><button>Hello</button></li>
<li><button>Hello</button></li>
<li><button>Hello</button></li>
</ul>
</Dropdown>
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-07 01:11:16 +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-07 01:11:16 +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 ? (
<button onClick={() => {activateRequest(request.id)}}>
<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 01:11:16 +00:00
const {requestGroups} = this.props;
2016-03-23 05:26:27 +00:00
return (
2016-04-07 01:11:16 +00:00
<aside className="sidebar bg-dark grid--v">
<header className="header">
<h1><WorkspaceDropdown /></h1>
</header>
<div className="grid--v grid--start grid__cell">
{/*<div className="stock-height form-control form-control--outlined">
<DebouncingInput
type="text"
placeholder="Filter Requests"
debounceMillis={100}
value={activeFilter}
onChange={this.onFilterChange.bind(this)}/>
</div>*/}
<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-03-23 05:26:27 +00:00
</div>
</aside>
)
}
}
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-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;