aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/CollectionPileView.tsx
blob: 9d68c621b6f1385e5133713d5dd40dc1bc58583c (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
import { action, computed, IReactionDisposer, makeObservable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc, DocListCast } from '../../../fields/Doc';
import { ScriptField } from '../../../fields/ScriptField';
import { NumCast, StrCast } from '../../../fields/Types';
import { emptyFunction, returnFalse, setupMoveUpEvents } from '../../../Utils';
import { DocUtils } from '../../documents/Documents';
import { dropActionType } from '../../util/DragManager';
import { SelectionManager } from '../../util/SelectionManager';
import { undoBatch, UndoManager } from '../../util/UndoManager';
import { OpenWhere } from '../nodes/DocumentView';
import { computePassLayout, computeStarburstLayout } from './collectionFreeForm';
import { CollectionFreeFormView } from './collectionFreeForm/CollectionFreeFormView';
import './CollectionPileView.scss';
import { CollectionSubView } from './CollectionSubView';

@observer
export class CollectionPileView extends CollectionSubView() {
    _originalChrome: any = '';
    _disposers: { [name: string]: IReactionDisposer } = {};

    constructor(props: any) {
        super(props);
        makeObservable(this);
    }

    componentDidMount() {
        if (this.layoutEngine() !== computePassLayout.name && this.layoutEngine() !== computeStarburstLayout.name) {
            this.Document._freeform_pileEngine = computePassLayout.name;
        }
        this._originalChrome = this.layoutDoc._chromeHidden;
        this.layoutDoc._chromeHidden = true;
    }
    componentWillUnmount() {
        this.layoutDoc._chromeHidden = this._originalChrome;
        Object.values(this._disposers).forEach(disposer => disposer?.());
    }

    layoutEngine = () => StrCast(this.Document._freeform_pileEngine);

    @undoBatch
    addPileDoc = (doc: Doc | Doc[]) => {
        (doc instanceof Doc ? [doc] : doc).map(d => DocUtils.iconify(d));
        return this._props.addDocument?.(doc) || false;
    };

    @undoBatch
    removePileDoc = (doc: Doc | Doc[], targetCollection: Doc | undefined, addDoc: (doc: Doc | Doc[]) => boolean) => {
        (doc instanceof Doc ? [doc] : doc).forEach(d => Doc.deiconifyView(d));
        const ret = this._props.moveDocument?.(doc, targetCollection, addDoc) || false;
        if (ret && !DocListCast(this.dataDoc[this.fieldKey ?? 'data']).length) this.DocumentView?.()._props.removeDocument?.(this.Document);
        return ret;
    };

    @computed get toggleIcon() {
        return ScriptField.MakeScript('documentView.iconify()', { documentView: 'any' });
    }
    @computed get contentEvents() {
        const isStarburst = this.layoutEngine() === computeStarburstLayout.name;
        return this._props.isContentActive() && isStarburst ? undefined : 'none';
    }

    // returns the contents of the pileup in a CollectionFreeFormView
    @computed get contents() {
        return (
            <div className="collectionPileView-innards" style={{ pointerEvents: this.contentEvents }}>
                <CollectionFreeFormView
                    {...this._props} //
                    layoutEngine={this.layoutEngine}
                    addDocument={this.addPileDoc}
                    moveDocument={this.removePileDoc}
                    // pile children never have their contents active, but will be document active whenever the entire pile is.
                    childContentsActive={returnFalse}
                    childDocumentsActive={this._props.isDocumentActive}
                    childDragAction={dropActionType.move}
                    childClickScript={this.toggleIcon}
                />
            </div>
        );
    }

    // toggles the pileup between starburst to compact
    toggleStarburst = action(() => {
        this.layoutDoc._freeform_scale = undefined;
        if (this.layoutEngine() === computeStarburstLayout.name) {
            if (NumCast(this.layoutDoc._width) !== NumCast(this.Document._starburstDiameter, 500)) {
                this.Document._starburstDiameter = NumCast(this.layoutDoc._width);
            }
            const defaultSize = 110;
            this.Document.x = NumCast(this.Document.x) + NumCast(this.layoutDoc._width) / 2 - NumCast(this.layoutDoc._freeform_pileWidth, defaultSize) / 2;
            this.Document.y = NumCast(this.Document.y) + NumCast(this.layoutDoc._height) / 2 - NumCast(this.layoutDoc._freeform_pileHeight, defaultSize) / 2;
            this.layoutDoc._width = NumCast(this.layoutDoc._freeform_pileWidth, defaultSize);
            this.layoutDoc._height = NumCast(this.layoutDoc._freeform_pileHeight, defaultSize);
            DocUtils.pileup(this.childDocs, undefined, undefined, NumCast(this.layoutDoc._width) / 2, false);
            this.layoutDoc._freeform_panX = 0;
            this.layoutDoc._freeform_panY = -10;
            this.Document._freeform_pileEngine = computePassLayout.name;
        } else {
            const defaultSize = NumCast(this.Document._starburstDiameter, 400);
            this.Document.x = NumCast(this.Document.x) + NumCast(this.layoutDoc._width) / 2 - defaultSize / 2;
            this.Document.y = NumCast(this.Document.y) + NumCast(this.layoutDoc._height) / 2 - defaultSize / 2;
            this.layoutDoc._freeform_pileWidth = NumCast(this.layoutDoc._width);
            this.layoutDoc._freeform_pileHeight = NumCast(this.layoutDoc._height);
            this.layoutDoc._freeform_panX = this.layoutDoc._freeform_panY = 0;
            this.layoutDoc._width = this.layoutDoc._height = defaultSize;
            this.layoutDoc.background;
            this.Document._freeform_pileEngine = computeStarburstLayout.name;
        }
    });

    // for dragging documents out of the pileup view
    _undoBatch: UndoManager.Batch | undefined;
    pointerDown = (e: React.PointerEvent) => {
        let dist = 0;
        setupMoveUpEvents(
            this,
            e,
            (e: PointerEvent, down: number[], delta: number[]) => {
                if (this.layoutEngine() === 'pass' && this.childDocs.length && e.shiftKey) {
                    dist += Math.sqrt(delta[0] * delta[0] + delta[1] * delta[1]);
                    if (dist > 100) {
                        if (!this._undoBatch) {
                            this._undoBatch = UndoManager.StartBatch('layout pile');
                        }
                        const doc = this.childDocs[0];
                        doc.x = e.clientX;
                        doc.y = e.clientY;
                        this._props.addDocTab(doc, OpenWhere.inParentFromScreen) && (this._props.removeDocument?.(doc) || false);
                        dist = 0;
                    }
                }
                return false;
            },
            () => {
                this._undoBatch?.end();
                this._undoBatch = undefined;
            },
            emptyFunction,
            e.shiftKey && this.layoutEngine() === computePassLayout.name,
            this.layoutEngine() === computePassLayout.name && e.shiftKey
        ); // this sets _doubleTap
    };

    // onClick for toggling the pileup view
    @undoBatch
    onClick = (e: React.MouseEvent) => {
        if (e.button === 0) {
            SelectionManager.DeselectAll();
            this.toggleStarburst();
            e.stopPropagation();
        }
    };

    render() {
        return (
            <div className={`collectionPileView`} onClick={this.onClick} onPointerDown={this.pointerDown} style={{ width: this._props.PanelWidth(), height: '100%' }}>
                {this.contents}
            </div>
        );
    }
}