import { IconProp, library } from '@fortawesome/fontawesome-svg-core'; import { faCaretDown, faCaretRight, faTrashAlt } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { action, observable } from "mobx"; import { observer } from "mobx-react"; import { DragManager, SetupDrag } from "../../util/DragManager"; import { EditableView } from "../EditableView"; import { CollectionSubView } from "./CollectionSubView"; import "./CollectionTreeView.scss"; import React = require("react"); import { Document, listSpec } from '../../../new_fields/Schema'; import { Cast, StrCast, BoolCast } from '../../../new_fields/Types'; import { Doc } from '../../../new_fields/Doc'; import { Id } from '../../../new_fields/RefField'; export interface TreeViewProps { document: Doc; deleteDoc: (doc: Doc) => void; moveDocument: DragManager.MoveFunction; copyOnDrag: boolean; } export enum BulletType { Collapsed, Collapsible, List } library.add(faTrashAlt); library.add(faCaretDown); library.add(faCaretRight); @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 = true; delete = () => this.props.deleteDoc(this.props.document); @action remove = (document: Document) => { var children = Cast(this.props.document.data, listSpec(Doc)); if (children) { children.splice(children.indexOf(document), 1); } } @action move: DragManager.MoveFunction = (document, target, addDoc) => { if (this.props.document === target) { return true; } //TODO This should check if it was removed this.remove(document); return addDoc(document); } renderBullet(type: BulletType) { let onClicked = action(() => this._collapsed = !this._collapsed); let bullet: IconProp | undefined = undefined; switch (type) { case BulletType.Collapsed: bullet = "caret-right"; break; case BulletType.Collapsible: bullet = "caret-down"; break; } return
{bullet ? : ""}
; } /** * Renders the EditableView title element for placement into the tree. */ renderTitle() { let reference = React.createRef(); let onItemDown = SetupDrag(reference, () => this.props.document, this.props.moveDocument, this.props.copyOnDrag); let editableView = (titleString: string) => ( StrCast(this.props.document.title)} SetValue={(value: string) => { this.props.document.title = value; return true; }} />); return (
{editableView(StrCast(this.props.document.title))}
); } render() { let bulletType = BulletType.List; let childElements: JSX.Element | undefined = undefined; var children = Cast(this.props.document.data, listSpec(Doc)); if (children) { // add children for a collection if (!this._collapsed) { bulletType = BulletType.Collapsible; childElements =
    {children.map(value => )}
; } else bulletType = BulletType.Collapsed; } return
  • {this.renderBullet(bulletType)} {this.renderTitle()} {childElements ? childElements : (null)}
  • ; } } @observer export class CollectionTreeView extends CollectionSubView(Document) { @action remove = (document: Document) => { const children = this.children; if (children) { children.splice(children.indexOf(document), 1); } } render() { const children = this.children; let copyOnDrag = BoolCast(this.props.Document.copyDraggedItems, false); let childrenElement = !children ? (null) : (children.map(value => ) ); return (
    e.stopPropagation()} onDrop={(e: React.DragEvent) => this.onDrop(e, {})} ref={this.createDropTarget}>
    StrCast(this.props.Document.title)} SetValue={(value: string) => { this.props.Document.title = value; return true; }} />

      {childrenElement}
    ); } }