import { observer } from "mobx-react"; import { CollectionViewBase } from "./CollectionViewBase"; import { Document } from "../../../fields/Document"; import { KeyStore } from "../../../fields/KeyStore"; import { ListField } from "../../../fields/ListField"; import React = require("react") import { TextField } from "../../../fields/TextField"; import { observable, action } from "mobx"; import "./CollectionTreeView.scss"; import { EditableView } from "../EditableView"; import { setupDrag } from "../../util/DragManager"; import { FieldWaiting } from "../../../fields/Field"; import { COLLECTION_BORDER_WIDTH } from "./CollectionView"; export interface TreeViewProps { document: Document; } export enum BulletType { Collapsed, Collapsible, List } @observer /** * Component that takes in a document prop and a boolean whether it's collapsed or not. */ class TreeView extends React.Component { @observable collapsed: boolean = false; // TODO this will eventually come with functions for what to attach to them renderBullet(type: BulletType) { let onClicked = action(() => this.collapsed = !this.collapsed); switch (type) { case BulletType.Collapsed: return
case BulletType.Collapsible: return
case BulletType.List: return
} } /** * Renders a single child document. If this child is a collection, it will call renderTreeView again. Otherwise, it will just append a list element. * @param childDocument The document to render. */ renderChild(childDocument: Document) { let reference = React.createRef(); var children = childDocument.GetT>(KeyStore.Data, ListField); let title = childDocument.GetT(KeyStore.Title, TextField); let onItemDown = setupDrag(reference, childDocument); // if the title hasn't loaded, immediately return the div if (!title || title === "") { return
; } // set up the title element, which will be rendered the same way for everyone let titleElement = { let title = childDocument.GetT(KeyStore.Title, TextField); if (title && title !== "") return title.Data; return ""; }} SetValue={(value: string) => { childDocument.SetData(KeyStore.Title, value, TextField); return true; }} /> // otherwise, check if it's a collection. if (children && children !== "") { // if it's not collapsed, then render the full TreeView. var subView = null; if (!this.collapsed) { subView =
  • {this.renderBullet(BulletType.Collapsible)} {titleElement}
  • } else { subView =
  • {this.renderBullet(BulletType.Collapsed)} {titleElement}
  • } return
    {subView}
    } // finally, if it's a normal document, then render it as such. else { return
  • {this.renderBullet(BulletType.List)} {titleElement}
  • ; } } render() { var children = this.props.document.GetT>(KeyStore.Data, ListField); return !children || children === FieldWaiting ? (null) : (children.Data.map(value =>
    {this.renderChild(value)}
    ) ) } } @observer export class CollectionTreeView extends CollectionViewBase { render() { let titleStr = ""; let title = this.props.Document.GetT(KeyStore.Title, TextField); if (title && title !== FieldWaiting) { titleStr = title.Data; } return (
    this.onDrop(e, {})} ref={this.createDropTarget} style={{ borderWidth: `${COLLECTION_BORDER_WIDTH}px` }}>

    { let title = this.props.Document.GetT(KeyStore.Title, TextField); if (title && title !== "") return title.Data; return ""; }} SetValue={(value: string) => { this.props.Document.SetData(KeyStore.Title, value, TextField); return true; }} />

    ); } }