aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/CollectionTreeView.tsx
blob: ba72fb7b93ce75d8ede64c250e068d3462258aa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { action, computed, IReactionDisposer, observable, reaction } from "mobx";
import { observer } from "mobx-react";
import { DataSym, Doc, DocListCast, HeightSym, Opt, StrListCast, WidthSym } from '../../../fields/Doc';
import { Id } from '../../../fields/FieldSymbols';
import { InkTool } from '../../../fields/InkField';
import { listSpec } from '../../../fields/Schema';
import { ScriptField } from '../../../fields/ScriptField';
import { BoolCast, Cast, NumCast, ScriptCast, StrCast } from '../../../fields/Types';
import { TraceMobx } from '../../../fields/util';
import { emptyFunction, OmitKeys, returnEmptyDoclist, returnEmptyFilter, returnFalse, returnOne, returnTrue } from '../../../Utils';
import { DocUtils } from '../../documents/Documents';
import { CurrentUserUtils } from '../../util/CurrentUserUtils';
import { DocumentManager } from '../../util/DocumentManager';
import { DragManager, dropActionType } from "../../util/DragManager";
import { SelectionManager } from '../../util/SelectionManager';
import { SnappingManager } from '../../util/SnappingManager';
import { Transform } from '../../util/Transform';
import { undoBatch, UndoManager } from '../../util/UndoManager';
import { ContextMenu } from '../ContextMenu';
import { ContextMenuProps } from '../ContextMenuItem';
import { EditableView } from "../EditableView";
import { DocumentView } from '../nodes/DocumentView';
import { FormattedTextBox } from '../nodes/formattedText/FormattedTextBox';
import { StyleProp } from '../StyleProvider';
import { CollectionFreeFormView } from './collectionFreeForm';
import { CollectionSubView } from "./CollectionSubView";
import "./CollectionTreeView.scss";
import { TreeView } from "./TreeView";
import React = require("react");
import { FieldViewProps } from "../nodes/FieldView";
const _global = (window /* browser */ || global /* node */) as any;

export type collectionTreeViewProps = {
    treeViewExpandedView?: "fields" | "layout" | "links" | "data";
    treeViewOpen?: boolean;
    treeViewHideTitle?: boolean;
    treeViewHideHeaderFields?: boolean;
    treeViewSkipFields?: string[]; // prevents specific fields from being displayed (see LinkBox)
    onCheckedClick?: () => ScriptField;
    onChildClick?: () => ScriptField;
    // TODO: [AL] add these fields
    AddToMap?: (treeViewDoc: Doc, index: number[]) => Doc[];
    RemFromMap?: (treeViewDoc: Doc, index: number[]) => Doc[];
    hierarchyIndex?: number[];
};

export enum TreeViewType {
    outline = "outline",
    fileSystem = "fileSystem",
    default = "default"
}

@observer
export class CollectionTreeView extends CollectionSubView<Partial<collectionTreeViewProps>>() {
    private _treedropDisposer?: DragManager.DragDropDisposer;
    private _mainEle?: HTMLDivElement;
    private _titleRef?: HTMLDivElement | HTMLInputElement | null;
    private _disposers: { [name: string]: IReactionDisposer } = {};
    private _isDisposing = false; // notes that instance is in process of being disposed
    private refList: Set<any> = new Set(); // list of tree view items to monitor for height changes
    private observer: any; // observer for monitoring tree view items.
    private static expandViewLabelSize = 20;

    @computed get doc() { return this.props.Document; }
    @computed get dataDoc() { return this.props.DataDoc || this.doc; }
    @computed get treeViewtruncateTitleWidth() { return NumCast(this.doc.treeViewTruncateTitleWidth, this.panelWidth()); }
    @computed get treeChildren() { TraceMobx(); return this.props.childDocuments || this.childDocs; }
    @computed get outlineMode() { return this.doc.treeViewType === TreeViewType.outline; }
    @computed get fileSysMode() { return this.doc.treeViewType === TreeViewType.fileSystem; }
    @computed get dashboardMode() { return this.doc === CurrentUserUtils.MyDashboards; }

    @observable _explainerHeight = 0; // height of the description of the tree view

    MainEle = () => this._mainEle;

    // these should stay in synch with counterparts in DocComponent.ts ViewBoxAnnotatableComponent
    @observable _isAnyChildContentActive = false;
    whenChildContentsActiveChanged = action((isActive: boolean) => this.props.whenChildContentsActiveChanged(this._isAnyChildContentActive = isActive));
    isContentActive = (outsideReaction?: boolean) => (CurrentUserUtils.ActiveTool !== InkTool.None ||
        (this.props.isContentActive?.() || this.props.Document.forceActive ||
            this.props.isSelected(outsideReaction) || this._isAnyChildContentActive ||
            this.props.rootSelected(outsideReaction)) ? true : false)

    componentWillUnmount() {
        this._isDisposing = true;
        super.componentWillUnmount();
        this._treedropDisposer?.();
        Object.values(this._disposers).forEach(disposer => disposer?.());
    }

    componentDidMount() {
        this._disposers.autoheight = reaction(() => this.rootDoc.autoHeight,
            auto => auto && this.computeHeight(),
            { fireImmediately: true });
    }

    computeHeight = () => {
        if (!this._isDisposing) {
            const titleHeight = !this._titleRef ? this.marginTop() : Number(getComputedStyle(this._titleRef).height.replace("px", ""));
            const bodyHeight = Array.from(this.refList).reduce((p, r) => p + Number(getComputedStyle(r).height.replace("px", "")), this.marginBot());
            this.layoutDoc._autoHeightMargins = bodyHeight;
            this.props.setHeight?.(bodyHeight + titleHeight);
        }
    }
    unobserveHeight = (ref: any) => {
        this.refList.delete(ref);
        this.rootDoc.autoHeight && this.computeHeight();
    }
    observeHeight = (ref: any) => {
        if (ref) {
            this.refList.add(ref);
            this.observer = new _global.ResizeObserver(action((entries: any) => {
                if (this.rootDoc.autoHeight && ref && this.refList.size && !SnappingManager.GetIsDragging()) {
                    this.computeHeight();
                }
            }));
            this.rootDoc.autoHeight && this.computeHeight();
            this.observer.observe(ref);
        }
    }
    protected createTreeDropTarget = (ele: HTMLDivElement) => {
        this._treedropDisposer?.();
        if (this._mainEle = ele) this._treedropDisposer = DragManager.MakeDropTarget(ele, this.onInternalDrop.bind(this), this.doc, this.onInternalPreDrop.bind(this));
    }

    protected onInternalPreDrop = (e: Event, de: DragManager.DropEvent, targetAction: dropActionType) => {
        const dragData = de.complete.docDragData;
        if (dragData) {
            const isInTree = () => Doc.AreProtosEqual(dragData.treeViewDoc, this.props.Document) || dragData.draggedDocuments.some(d => d.context === this.doc && this.childDocs.includes(d));
            dragData.dropAction = targetAction && !isInTree() ? targetAction : this.doc === dragData?.treeViewDoc ? "same" : dragData.dropAction;
        }
    }

    @action
    remove = (doc: Doc | Doc[]): boolean => {
        const docs = doc instanceof Doc ? [doc] : doc;
        const targetDataDoc = this.doc[DataSym];
        const value = DocListCast(targetDataDoc[this.props.fieldKey]);
        const result = value.filter(v => !docs.includes(v));
        if ((doc instanceof Doc ? [doc] : doc).some(doc => SelectionManager.Views().some(dv => Doc.AreProtosEqual(dv.rootDoc, doc)))) SelectionManager.DeselectAll();
        if (result.length !== value.length) {
            const ind = targetDataDoc[this.props.fieldKey].indexOf(doc);
            const prev = ind && targetDataDoc[this.props.fieldKey][ind - 1];
            this.props.removeDocument?.(doc);
            if (ind > 0) {
                FormattedTextBox.SelectOnLoad = prev[Id];
                DocumentManager.Instance.getDocumentView(prev, this.props.CollectionView)?.select(false);
            }
            return true;
        }
        return false;
    }

    @action
    addDoc = (docs: Doc | Doc[], relativeTo: Opt<Doc>, before?: boolean): boolean => {
        const doAddDoc = (doc: Doc | Doc[]) =>
            (doc instanceof Doc ? [doc] : doc).reduce((flg, doc) => {
                const res = flg && Doc.AddDocToList(this.doc[DataSym], this.props.fieldKey, doc, relativeTo, before);
                res && (doc.context = this.props.Document);
                return res;
            }, true);
        if (this.doc.resolvedDataDoc instanceof Promise) return false;
        return relativeTo === undefined ? this.props.addDocument?.(docs) || false : doAddDoc(docs);
    }
    onContextMenu = (e: React.MouseEvent): void => {
        // need to test if propagation has stopped because GoldenLayout forces a parallel react hierarchy to be created for its top-level layout
        if (!Doc.noviceMode) {
            const layoutItems: ContextMenuProps[] = [];
            layoutItems.push({ description: "Make tree state " + (this.doc.treeViewOpenIsTransient ? "persistent" : "transient"), event: () => this.doc.treeViewOpenIsTransient = !this.doc.treeViewOpenIsTransient, icon: "paint-brush" });
            layoutItems.push({ description: (this.doc.treeViewHideHeaderFields ? "Show" : "Hide") + " Header Fields", event: () => this.doc.treeViewHideHeaderFields = !this.doc.treeViewHideHeaderFields, icon: "paint-brush" });
            layoutItems.push({ description: (this.doc.treeViewHideTitle ? "Show" : "Hide") + " Title", event: () => this.doc.treeViewHideTitle = !this.doc.treeViewHideTitle, icon: "paint-brush" });
            ContextMenu.Instance.addItem({ description: "Options...", subitems: layoutItems, icon: "eye" });
            const existingOnClick = ContextMenu.Instance.findByDescription("OnClick...");
            const onClicks: ContextMenuProps[] = existingOnClick && "subitems" in existingOnClick ? existingOnClick.subitems : [];
            onClicks.push({ description: "Edit onChecked Script", event: () => UndoManager.RunInBatch(() => DocUtils.makeCustomViewClicked(this.doc, undefined, "onCheckedClick"), "edit onCheckedClick"), icon: "edit" });
            !existingOnClick && ContextMenu.Instance.addItem({ description: "OnClick...", noexpand: true, subitems: onClicks, icon: "mouse-pointer" });
        }
    }
    onTreeDrop = (e: React.DragEvent, addDocs?: (docs: Doc[]) => void) => this.onExternalDrop(e, {}, addDocs);

    @undoBatch
    makeTextCollection = (childDocs: Doc[]) => {
        this.addDoc(TreeView.makeTextBullet(), childDocs.length ? childDocs[0] : undefined, true);
    }

    get editableTitle() {
        return <EditableView
            contents={this.dataDoc.title}
            display={"block"}
            maxHeight={72}
            height={"auto"}
            GetValue={() => StrCast(this.dataDoc.title)}
            SetValue={undoBatch((value: string, shift: boolean, enter: boolean) => {
                if (enter && this.props.Document.treeViewType === TreeViewType.outline) this.makeTextCollection(this.treeChildren);
                this.dataDoc.title = value;
                return true;
            })} />;
    }


    onKey = (e: React.KeyboardEvent, fieldProps: FieldViewProps) => {
        if (this.outlineMode && e.key === "Enter") {
            e.stopPropagation();
            this.makeTextCollection(this.treeChildren);
            return true;
        }
    }
    get documentTitle() {
        return <FormattedTextBox
            {...this.props}
            fieldKey={"text"}
            renderDepth={this.props.renderDepth + 1}
            isContentActive={this.isContentActive}
            isDocumentActive={this.isContentActive}
            rootSelected={returnTrue}
            forceAutoHeight={true} // needed to make the title resize even if the rest of the tree view is not autoHeight
            PanelWidth={this.documentTitleWidth}
            PanelHeight={this.documentTitleHeight}
            scaling={returnOne}
            onKey={this.onKey}
            docFilters={returnEmptyFilter}
            docRangeFilters={returnEmptyFilter}
            searchFilterDocs={returnEmptyDoclist}
            ContainingCollectionDoc={this.doc}
            ContainingCollectionView={this.props.CollectionView}
            addDocument={returnFalse}
            moveDocument={returnFalse}
            removeDocument={returnFalse}
            whenChildContentsActiveChanged={this.whenChildContentsActiveChanged}
            bringToFront={returnFalse}
        />;
    }
    childContextMenuItems = () => {
        const customScripts = Cast(this.doc.childContextMenuScripts, listSpec(ScriptField), []);
        const customFilters = Cast(this.doc.childContextMenuFilters, listSpec(ScriptField), []);
        const icons = StrListCast(this.doc.childContextMenuIcons);
        return StrListCast(this.doc.childContextMenuLabels).map((label, i) => ({ script: customScripts[i], filter: customFilters[i], icon: icons[i], label }));
    }
    @computed get treeViewElements() {
        TraceMobx();
        const dropAction = StrCast(this.doc.childDropAction) as dropActionType;
        const addDoc = (doc: Doc | Doc[], relativeTo?: Doc, before?: boolean) => this.addDoc(doc, relativeTo, before);
        const moveDoc = (d: Doc | Doc[], target: Doc | undefined, addDoc: (doc: Doc | Doc[]) => boolean) => this.props.moveDocument?.(d, target, addDoc) || false;
        return TreeView.GetChildElements(
            this.treeChildren,
            this,
            this,
            this.doc,
            this.props.DataDoc,
            this.props.ContainingCollectionDoc,
            undefined,
            addDoc,
            this.remove,
            moveDoc,
            dropAction,
            this.props.addDocTab,
            this.props.styleProvider,
            this.props.ScreenToLocalTransform,
            this.isContentActive,
            this.panelWidth,
            this.props.renderDepth,
            () => this.props.treeViewHideHeaderFields || BoolCast(this.doc.treeViewHideHeaderFields),
            [],
            this.props.onCheckedClick,
            this.onChildClick,
            this.props.treeViewSkipFields,
            true,
            this.whenChildContentsActiveChanged,
            this.props.dontRegisterView || Cast(this.props.Document.childDontRegisterViews, "boolean", null),
            this.observeHeight,
            this.unobserveHeight,
            this.childContextMenuItems(),
            //TODO: [AL] add these
            this.props.AddToMap,
            this.props.RemFromMap,
            this.props.hierarchyIndex,
        );
    }
    @computed get titleBar() {
        return this.dataDoc === null ? (null) :
            <div className="collectionTreeView-titleBar" key={this.doc[Id]}
                style={!this.outlineMode ? { paddingLeft: this.marginX(), paddingTop: this.marginTop() } : {}}
                ref={r => this._titleRef = r}>
                {this.outlineMode ? this.documentTitle : this.editableTitle}
            </div>;
    }

    @computed get noviceExplainer() {
        return !Doc.noviceMode || !this.rootDoc.explainer ? (null) :
            <div className="documentExplanation"> {this.rootDoc.explainer} </div>;
    }

    return35 = () => 35;
    @computed get buttonMenu() {
        const menuDoc = Cast(this.rootDoc.buttonMenuDoc, Doc, null);
        // To create a multibutton menu add a CollectionLinearView
        return !menuDoc ? null :
            (<div className="buttonMenu-docBtn" style={{ width: NumCast(menuDoc._width, 30), height: NumCast(menuDoc._height, 30) }}>
                <DocumentView
                    Document={menuDoc}
                    DataDoc={menuDoc}
                    isContentActive={this.props.isContentActive}
                    isDocumentActive={returnTrue}
                    addDocument={this.props.addDocument}
                    moveDocument={this.props.moveDocument}
                    addDocTab={this.props.addDocTab}
                    pinToPres={emptyFunction}
                    rootSelected={this.props.isSelected}
                    removeDocument={this.props.removeDocument}
                    ScreenToLocalTransform={Transform.Identity}
                    PanelWidth={this.return35}
                    PanelHeight={this.return35}
                    renderDepth={this.props.renderDepth + 1}
                    focus={emptyFunction}
                    styleProvider={this.props.styleProvider}
                    docViewPath={returnEmptyDoclist}
                    whenChildContentsActiveChanged={emptyFunction}
                    bringToFront={emptyFunction}
                    docFilters={this.props.docFilters}
                    docRangeFilters={this.props.docRangeFilters}
                    searchFilterDocs={this.props.searchFilterDocs}
                    ContainingCollectionView={undefined}
                    ContainingCollectionDoc={undefined}
                />
            </div>);
    }

    @computed get nativeWidth() { return Doc.NativeWidth(this.Document, undefined, true); }
    @computed get nativeHeight() { return Doc.NativeHeight(this.Document, undefined, true); }

    @computed get contentScaling() {
        const nw = this.nativeWidth;
        const nh = this.nativeHeight;
        const hscale = nh ? this.props.PanelHeight() / nh : 1;
        const wscale = nw ? this.props.PanelWidth() / nw : 1;
        return wscale < hscale ? wscale : hscale;
    }
    marginX = () => NumCast(this.doc._xMargin);
    marginTop = () => NumCast(this.doc._yMargin);
    marginBot = () => NumCast(this.doc._yMargin);
    documentTitleWidth = () => Math.min(this.layoutDoc?.[WidthSym](), this.panelWidth());
    documentTitleHeight = () => (this.layoutDoc?.[HeightSym]() || 0) - NumCast(this.layoutDoc.autoHeightMargins);
    truncateTitleWidth = () => this.treeViewtruncateTitleWidth;
    onChildClick = () => this.props.onChildClick?.() || ScriptCast(this.doc.onChildClick);
    panelWidth = () => Math.max(0, this.props.PanelWidth() - this.marginX() - CollectionTreeView.expandViewLabelSize) * (this.props.scaling?.() || 1);

    addAnnotationDocument = (doc: Doc | Doc[]) => this.props.CollectionView?.addDocument(doc, `${this.props.fieldKey}-annotations`) || false;
    remAnnotationDocument = (doc: Doc | Doc[]) => this.props.CollectionView?.removeDocument(doc, `${this.props.fieldKey}-annotations`) || false;
    moveAnnotationDocument = (doc: Doc | Doc[], targetCollection: Doc | undefined, addDocument: (document: Doc | Doc[], annotationKey?: string) => boolean) =>
        this.props.CollectionView?.moveDocument(doc, targetCollection, addDocument, `${this.props.fieldKey}-annotations`) || false

    contentFunc = () => {
        const background = () => this.props.styleProvider?.(this.doc, this.props, StyleProp.BackgroundColor);
        const pointerEvents = () => !this.props.isContentActive() && !SnappingManager.GetIsDragging() ? "none" : undefined;
        const titleBar = this.props.treeViewHideTitle || this.doc.treeViewHideTitle ? (null) : this.titleBar;
        return [
            <div className="collectionTreeView-contents" key="tree" style={{
                ...(!titleBar ? { paddingLeft: this.marginX(), paddingTop: this.marginTop() } : {}),
                overflow: "auto",
                height: "100%"//this.layoutDoc._autoHeight ? "max-content" : "100%"
            }} >
                {titleBar}
                <div className="collectionTreeView-container"
                    style={{
                        transform: this.outlineMode ? `scale(${this.contentScaling})` : "",
                        paddingLeft: `${this.marginX()}px`,
                        width: this.outlineMode ? `calc(${100 / this.contentScaling}%)` : ""
                    }}
                    onContextMenu={this.onContextMenu}>
                    {!this.buttonMenu && !this.noviceExplainer ? (null) :
                        <div className="documentButtonMenu" ref={action((r: HTMLDivElement) => r && (this._explainerHeight = r.getBoundingClientRect().height))}>
                            {this.buttonMenu}
                            {this.noviceExplainer}
                        </div>
                    }
                    <div className="collectionTreeView-dropTarget"
                        style={{
                            background: background(),
                            height: `calc(100% - ${this._explainerHeight}px)`,
                            pointerEvents: pointerEvents()
                        }}
                        onWheel={e => e.stopPropagation()}
                        onDrop={this.onTreeDrop}
                        ref={r => !this.doc.treeViewHasOverlay && r && this.createTreeDropTarget(r)}>
                        <ul className={`no-indent${this.outlineMode ? "-outline" : ""}`} >
                            {this.treeViewElements}
                        </ul>
                    </div >
                </div>
            </div>
        ];
    }
    render() {
        TraceMobx();

        return !(this.doc instanceof Doc) || !this.treeChildren ? (null) :
            this.doc.treeViewHasOverlay ?
                <CollectionFreeFormView {...OmitKeys(this.props, ["NativeWidth", "NativeHeight", "setContentView"]).omit}
                    isAnnotationOverlay={true}
                    isAnnotationOverlayScrollable={true}
                    childDocumentsActive={this.props.isDocumentActive}
                    fieldKey={this.props.fieldKey + "-annotations"}
                    dropAction={"move"}
                    select={emptyFunction}
                    addDocument={this.addAnnotationDocument}
                    removeDocument={this.remAnnotationDocument}
                    moveDocument={this.moveAnnotationDocument}
                    bringToFront={emptyFunction}
                    renderDepth={this.props.renderDepth + 1} >
                    {this.contentFunc}
                </CollectionFreeFormView> :
                this.contentFunc();
    }
}