import React, { PropTypes } from 'react'; import { toJS } from 'mobx'; import { Link, browserHistory } from 'react-router'; import { observer } from 'mobx-react'; import DocumentSceneStore, { DOCUMENT_PREFERENCES } from './DocumentSceneStore'; import Layout, { HeaderAction } from 'components/Layout'; import AtlasPreviewLoading from 'components/AtlasPreviewLoading'; import CenteredContent from 'components/CenteredContent'; import Document from 'components/Document'; import DropdownMenu, { MenuItem, MoreIcon } from 'components/DropdownMenu'; import Flex from 'components/Flex'; import Tree from 'components/Tree'; import styles from './DocumentScene.scss'; import classNames from 'classnames/bind'; const cx = classNames.bind(styles); import treeStyles from 'components/Tree/Tree.scss'; @observer(['ui']) class DocumentScene extends React.Component { static propTypes = { ui: PropTypes.object.isRequired, } static store; state = { didScroll: false, } constructor(props) { super(props); this.store = new DocumentSceneStore(JSON.parse(localStorage[DOCUMENT_PREFERENCES] || "{}")); } componentDidMount = () => { const { id } = this.props.routeParams; this.store.fetchDocument(id); } componentWillReceiveProps = (nextProps) => { // Reload on url change const oldId = this.props.params.id; const newId = nextProps.params.id; if (oldId !== newId) { this.store.fetchDocument(newId, true); } // Scroll to anchor after loading, and only once const { hash } = this.props.location; if (nextProps.doc && hash && !this.state.didScroll) { const name = hash.split('#')[1]; setTimeout(() => { this.setState({ didScroll: true }); const element = doc.getElementsByName(name)[0]; if (element) element.scrollIntoView() }, 0); } } onEdit = () => { const url = `/documents/${this.store.document.id}/edit`; browserHistory.push(url); } onCreate = () => { const url = `/documents/${this.store.document.id}/new`; browserHistory.push(url); } onDelete = () => { let msg; if (this.store.document.atlas.type === 'atlas') { msg = 'Are you sure you want to delete this document and all it\'s child documents (if any)?' } else { msg = "Are you sure you want to delete this document?"; } if (confirm(msg)) { this.store.deleteDocument(); }; } renderNode = (node) => { return ( {node.module.name} ); } render() { const { sidebar, toggleSidebar, } = this.props.ui; const doc = this.store.document; const allowDelete = doc && doc.atlas.type === 'atlas' && doc.id !== doc.atlas.navigationTree.id; let title; let titleText; let actions; if (doc) { actions = (