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; deleteDoc: (doc: Document) => void; } 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; delete = () => { this.props.deleteDoc(this.props.document); } @action remove = (document: Document) => { var children = this.props.document.GetT>(KeyStore.Data, ListField); if (children && children !== FieldWaiting) { children.Data.splice(children.Data.indexOf(document), 1); } } 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 the EditableView title element for placement into the tree. */ renderTitle() { let title = this.props.document.GetT(KeyStore.Title, TextField); // if the title hasn't loaded, immediately return the div if (!title || title === "") { return
; } return
{ 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; }} />
x
} render() { var children = this.props.document.GetT>(KeyStore.Data, ListField); let reference = React.createRef(); let onItemDown = setupDrag(reference, () => this.props.document); let titleElement = this.renderTitle(); // check if this document is a collection if (children && children !== FieldWaiting) { let subView; // if uncollapsed, then add the children elements if (!this.collapsed) { // render all children elements let childrenElement = (children.Data.map(value => ) ) subView =
  • {this.renderBullet(BulletType.Collapsible)} {titleElement}
      {childrenElement}
  • } else { subView =
  • {this.renderBullet(BulletType.Collapsed)} {titleElement}
  • } return
    {subView}
    } // otherwise this is a normal leaf node else { return
  • {this.renderBullet(BulletType.List)} {titleElement}
  • ; } } } @observer export class CollectionTreeView extends CollectionViewBase { @action remove = (document: Document) => { var children = this.props.Document.GetT>(KeyStore.Data, ListField); if (children && children !== FieldWaiting) { children.Data.splice(children.Data.indexOf(document), 1); } } render() { let titleStr = ""; let title = this.props.Document.GetT(KeyStore.Title, TextField); if (title && title !== FieldWaiting) { titleStr = title.Data; } var children = this.props.Document.GetT>(KeyStore.Data, ListField); let childrenElement = !children || children === FieldWaiting ? (null) : (children.Data.map(value => ) ) return (
    this.onDrop(e, {})} ref={this.createDropTarget} style={{ borderWidth: `${COLLECTION_BORDER_WIDTH}px` }}>

    { return this.props.Document.Title; }} SetValue={(value: string) => { this.props.Document.SetData(KeyStore.Title, value, TextField); return true; }} />

      {childrenElement}
    ); } }