2017-09-18 06:45:40 +00:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import { observable } from 'mobx';
|
|
|
|
import { observer, inject } from 'mobx-react';
|
|
|
|
import Flex from 'components/Flex';
|
|
|
|
import styled from 'styled-components';
|
2017-09-25 03:03:03 +00:00
|
|
|
import { color, fontWeight } from 'styles/constants';
|
2017-09-18 06:45:40 +00:00
|
|
|
|
|
|
|
import SidebarLink from './SidebarLink';
|
|
|
|
import DropToImport from 'components/DropToImport';
|
|
|
|
import Icon from 'components/Icon';
|
|
|
|
import CollectionMenu from 'menus/CollectionMenu';
|
|
|
|
|
|
|
|
import CollectionsStore from 'stores/CollectionsStore';
|
|
|
|
import UiStore from 'stores/UiStore';
|
|
|
|
import Document from 'models/Document';
|
|
|
|
import { type NavigationNode } from 'types';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
history: Object,
|
|
|
|
collections: CollectionsStore,
|
|
|
|
activeDocument: ?Document,
|
|
|
|
onCreateCollection: Function,
|
|
|
|
ui: UiStore,
|
|
|
|
};
|
|
|
|
|
|
|
|
@observer class SidebarCollections extends React.PureComponent {
|
|
|
|
props: Props;
|
|
|
|
|
|
|
|
render() {
|
2017-09-25 05:38:53 +00:00
|
|
|
const { history, collections, activeDocument, ui } = this.props;
|
2017-09-18 06:45:40 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Flex column>
|
|
|
|
<Header>Collections</Header>
|
|
|
|
{collections.data.map(collection => (
|
|
|
|
<CollectionLink
|
|
|
|
key={collection.id}
|
2017-09-25 05:38:53 +00:00
|
|
|
history={history}
|
2017-09-18 06:45:40 +00:00
|
|
|
collection={collection}
|
|
|
|
activeDocument={activeDocument}
|
|
|
|
ui={ui}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
|
|
|
|
{collections.isLoaded &&
|
|
|
|
<SidebarLink onClick={this.props.onCreateCollection}>
|
|
|
|
<Icon type="Plus" /> Add new collection
|
|
|
|
</SidebarLink>}
|
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@observer class CollectionLink extends React.Component {
|
|
|
|
@observable menuOpen = false;
|
|
|
|
|
|
|
|
render() {
|
2017-09-25 05:38:53 +00:00
|
|
|
const { history, collection, activeDocument, ui } = this.props;
|
|
|
|
|
2017-09-18 06:45:40 +00:00
|
|
|
return (
|
2017-09-28 04:04:13 +00:00
|
|
|
<StyledDropToImport
|
2017-09-18 06:45:40 +00:00
|
|
|
key={collection.id}
|
|
|
|
history={history}
|
|
|
|
collectionId={collection.id}
|
2017-09-25 05:38:53 +00:00
|
|
|
activeClassName="activeDropZone"
|
2017-09-28 04:04:13 +00:00
|
|
|
menuOpen={this.menuOpen}
|
2017-09-18 06:45:40 +00:00
|
|
|
>
|
|
|
|
<SidebarLink key={collection.id} to={collection.url}>
|
|
|
|
<Flex justify="space-between">
|
|
|
|
{collection.name}
|
|
|
|
|
2017-09-28 04:04:13 +00:00
|
|
|
<CollectionAction>
|
|
|
|
<CollectionMenu
|
|
|
|
history={history}
|
|
|
|
collection={collection}
|
|
|
|
onShow={() => (this.menuOpen = true)}
|
|
|
|
onClose={() => (this.menuOpen = false)}
|
|
|
|
open={this.menuOpen}
|
|
|
|
/>
|
|
|
|
</CollectionAction>
|
2017-09-18 06:45:40 +00:00
|
|
|
</Flex>
|
2017-09-27 07:21:40 +00:00
|
|
|
|
2017-09-18 06:45:40 +00:00
|
|
|
{collection.id === ui.activeCollectionId &&
|
2017-09-27 07:21:40 +00:00
|
|
|
<Children column>
|
|
|
|
{collection.documents.map(document => (
|
|
|
|
<DocumentLink
|
|
|
|
key={document.id}
|
|
|
|
history={history}
|
|
|
|
document={document}
|
|
|
|
activeDocument={activeDocument}
|
|
|
|
depth={0}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Children>}
|
2017-09-18 06:45:40 +00:00
|
|
|
</SidebarLink>
|
2017-09-28 04:04:13 +00:00
|
|
|
</StyledDropToImport>
|
2017-09-18 06:45:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DocumentLinkProps = {
|
|
|
|
document: NavigationNode,
|
2017-09-25 05:38:53 +00:00
|
|
|
history: Object,
|
2017-09-18 06:45:40 +00:00
|
|
|
activeDocument: ?Document,
|
|
|
|
depth: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
const DocumentLink = observer((props: DocumentLinkProps) => {
|
2017-09-27 07:21:40 +00:00
|
|
|
const { document, activeDocument, depth } = props;
|
|
|
|
|
|
|
|
const showChildren =
|
|
|
|
activeDocument &&
|
|
|
|
(activeDocument.pathToDocument
|
|
|
|
.map(entry => entry.id)
|
|
|
|
.includes(document.id) ||
|
|
|
|
activeDocument.id === document.id);
|
2017-09-18 06:45:40 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Flex column key={document.id}>
|
2017-09-27 07:21:40 +00:00
|
|
|
<DropToImport
|
|
|
|
history={history}
|
|
|
|
documentId={document.id}
|
|
|
|
activeStyle="activeDropZone"
|
|
|
|
>
|
|
|
|
<SidebarLink
|
|
|
|
to={document.url}
|
|
|
|
hasChildren={document.children.length > 0}
|
|
|
|
expanded={showChildren}
|
2017-09-18 06:45:40 +00:00
|
|
|
>
|
2017-09-27 07:21:40 +00:00
|
|
|
{document.title}
|
|
|
|
</SidebarLink>
|
|
|
|
</DropToImport>
|
|
|
|
|
|
|
|
{showChildren &&
|
2017-09-18 06:45:40 +00:00
|
|
|
<Children column>
|
|
|
|
{document.children &&
|
|
|
|
document.children.map(childDocument => (
|
|
|
|
<DocumentLink
|
|
|
|
key={childDocument.id}
|
2017-09-25 05:38:53 +00:00
|
|
|
history={history}
|
2017-09-18 06:45:40 +00:00
|
|
|
document={childDocument}
|
2017-09-27 07:21:40 +00:00
|
|
|
activeDocument={activeDocument}
|
2017-09-18 06:45:40 +00:00
|
|
|
depth={depth + 1}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Children>}
|
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const CollectionAction = styled.a`
|
|
|
|
color: ${color.slate};
|
|
|
|
svg { opacity: .75; }
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
svg { opacity: 1; }
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2017-09-28 04:04:13 +00:00
|
|
|
const StyledDropToImport = styled(DropToImport)`
|
|
|
|
${CollectionAction} {
|
|
|
|
display: ${props => (props.menuOpen ? 'inline' : 'none')};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
${CollectionAction} {
|
|
|
|
display: inline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Header = styled(Flex)`
|
|
|
|
font-size: 11px;
|
|
|
|
font-weight: ${fontWeight.semiBold};
|
|
|
|
text-transform: uppercase;
|
|
|
|
color: ${color.slate};
|
|
|
|
letter-spacing: 0.04em;
|
|
|
|
`;
|
|
|
|
|
2017-09-18 06:45:40 +00:00
|
|
|
const Children = styled(Flex)`
|
|
|
|
margin-left: 20px;
|
|
|
|
`;
|
|
|
|
|
|
|
|
export default inject('collections', 'ui')(SidebarCollections);
|