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, trace } from "mobx"; import { observer } from "mobx-react"; import { Document } from "../../../fields/Document"; import { FieldWaiting } from "../../../fields/Field"; import { KeyStore } from "../../../fields/KeyStore"; import { ListField } from "../../../fields/ListField"; import { setupDrag, DragManager } from "../../util/DragManager"; import { EditableView } from "../EditableView"; import "./CollectionTreeView.scss"; import { CollectionView } from "./CollectionView"; import { CollectionSubView } from "./CollectionSubView"; import React = require("react"); import { COLLECTION_BORDER_WIDTH } from './CollectionBaseView'; import { props } from 'bluebird'; export interface TreeViewProps { document: Document; deleteDoc: (doc: Document) => 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 = this.props.document.GetT>(KeyStore.Data, ListField); if (children && children !== FieldWaiting) { children.Data.splice(children.Data.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) => ( this.props.document.Title} SetValue={(value: string) => { this.props.document.SetText(KeyStore.Title, value); return true; }} />); return (
{editableView(this.props.document.Title)}
); } render() { let bulletType = BulletType.List; let childElements: JSX.Element | undefined = undefined; var children = this.props.document.GetT>(KeyStore.Data, ListField); if (children && children !== FieldWaiting) { // add children for a collection if (!this._collapsed) { bulletType = BulletType.Collapsible; childElements =
    {children.Data.map(value => )}
; } else bulletType = BulletType.Collapsed; } return
  • {this.renderBullet(bulletType)} {this.renderTitle()} {childElements ? childElements : (null)}
  • ; } } @observer export class CollectionTreeView extends CollectionSubView { @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 children = this.props.Document.GetT>(KeyStore.Data, ListField); let copyOnDrag = this.props.Document.GetBoolean(KeyStore.CopyDraggedItems, false); let childrenElement = !children || children === FieldWaiting ? (null) : (children.Data.map(value => ) ); return (
    e.stopPropagation()} onDrop={(e: React.DragEvent) => this.onDrop(e, {})} ref={this.createDropTarget} style={{ borderWidth: `${COLLECTION_BORDER_WIDTH}px` }}>
    this.props.Document.Title} SetValue={(value: string) => { this.props.Document.SetText(KeyStore.Title, value); return true; }} />

      {childrenElement}
    ); } }