aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/collectionLinear/CollectionLinearView.tsx
blob: 8d3947653e81abae140f99b5e075d6ad5d171c0c (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
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Tooltip } from '@mui/material';
import { Toggle, ToggleType, Type } from '@dash/components';
import { Property } from 'csstype';
import { IReactionDisposer, action, makeObservable, reaction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { ClientUtils, returnTrue } from '../../../../ClientUtils';
import { emptyFunction } from '../../../../Utils';
import { Doc, Opt } from '../../../../fields/Doc';
import { Height, Width } from '../../../../fields/DocSymbols';
import { Id } from '../../../../fields/FieldSymbols';
import { BoolCast, Cast, DocCast, NumCast, ScriptCast, StrCast } from '../../../../fields/Types';
import { CollectionViewType } from '../../../documents/DocumentTypes';
import { BranchingTrailManager } from '../../../util/BranchingTrailManager';
import { DragManager } from '../../../util/DragManager';
import { dropActionType } from '../../../util/DropActionTypes';
import { SettingsManager } from '../../../util/SettingsManager';
import { Transform } from '../../../util/Transform';
import { UndoStack } from '../../UndoStack';
import { DocumentLinksButton } from '../../nodes/DocumentLinksButton';
import { DocumentView } from '../../nodes/DocumentView';
import { LinkDescriptionPopup } from '../../nodes/LinkDescriptionPopup';
import { CollectionSubView, SubCollectionViewProps } from '../CollectionSubView';
import './CollectionLinearView.scss';

/**
 * CollectionLinearView is the class for rendering the horizontal collection
 * of documents, it useful for horizontal menus. It can either be expandable
 * or not using the linearView_Expandable field.
 * It is used in the following locations:
 * - It is used in the popup menu on the bottom left (see docButtons() in MainView.tsx)
 * - It is used for the context sensitive toolbar at the top (see contMenuButtons() in CollectionMenu.tsx)
 */
@observer
export class CollectionLinearView extends CollectionSubView() {
    private _dropDisposer?: DragManager.DragDropDisposer;
    private _widthDisposer?: IReactionDisposer;
    private _selectedDisposer?: IReactionDisposer;

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

    componentWillUnmount() {
        this._dropDisposer?.();
        this._widthDisposer?.();
        this._selectedDisposer?.();
        this.childLayoutPairs.map(pair => ScriptCast(DocCast(pair.layout.proto)?.onPointerUp)?.script.run({ this: pair.layout.proto }, console.log));
    }

    componentDidMount() {
        this._widthDisposer = reaction(
            () => 5 + NumCast(this.dataDoc.linearBtnWidth, this.dimension()) + (this.layoutDoc.linearView_IsOpen ? this.childDocs.filter(doc => !doc.hidden).reduce((tot, doc) => (NumCast(doc._width) || this.dimension()) + tot + 4, 0) : 0),
            width => {
                this.childDocs.length && (this.layoutDoc._width = width);
            },
            { fireImmediately: true }
        );
    }
    protected createDashEventsTarget = (ele: HTMLDivElement | null) => {
        this._dropDisposer?.();
        if (ele) this._dropDisposer = DragManager.MakeDropTarget(ele, this.onInternalDrop.bind(this), this.layoutDoc);
    };

    dimension = () => NumCast(this.layoutDoc._height);
    getTransform = (ele: Opt<HTMLDivElement>) => {
        if (!ele) return Transform.Identity();
        const { translateX, translateY } = ClientUtils.GetScreenTransform(ele);
        return new Transform(-translateX, -translateY, 1);
    };

    @action
    exitLongLinks = () => {
        if (DocumentLinksButton.StartLink?.Document) {
            action(() => Doc.UnBrushDoc(DocumentLinksButton.StartLink?.Document as Doc));
        }
        DocumentLinksButton.StartLink = undefined;
        DocumentLinksButton.StartLinkView = undefined;
    };

    @action
    changeDescriptionSetting = () => {
        if (LinkDescriptionPopup.Instance.showDescriptions) {
            if (LinkDescriptionPopup.Instance.showDescriptions === 'ON') {
                LinkDescriptionPopup.Instance.showDescriptions = 'OFF';
                LinkDescriptionPopup.Instance.display = false;
            } else {
                LinkDescriptionPopup.Instance.showDescriptions = 'ON';
            }
        } else {
            LinkDescriptionPopup.Instance.showDescriptions = 'OFF';
            LinkDescriptionPopup.Instance.display = false;
        }
    };

    myContextMenu = (e: React.MouseEvent) => {
        e.stopPropagation();
        e.preventDefault();
    };

    getLinkUI = () =>
        !DocumentLinksButton.StartLink ? null : (
            <span key="-link-ui-" className="bottomPopup-background" style={{ pointerEvents: 'all' }} onPointerDown={e => e.stopPropagation()}>
                <span className="bottomPopup-text">
                    Creating link from:{' '}
                    <b>
                        {(DocumentLinksButton.AnnotationId ? 'Annotation in ' : ' ') +
                            (StrCast(DocumentLinksButton.StartLink.title).length < 51 ? DocumentLinksButton.StartLink.title : StrCast(DocumentLinksButton.StartLink.title).slice(0, 50) + '...')}
                    </b>
                </span>

                <Tooltip title={<div className="dash-tooltip">Toggle description pop-up </div>} placement="top">
                    <span className="bottomPopup-descriptions" onClick={this.changeDescriptionSetting}>
                        Labels: {LinkDescriptionPopup.Instance.showDescriptions ? LinkDescriptionPopup.Instance.showDescriptions : 'ON'}
                    </span>
                </Tooltip>

                <Tooltip title={<div className="dash-tooltip">Exit linking mode</div>} placement="top">
                    <span className="bottomPopup-exit" onClick={this.exitLongLinks}>
                        Stop
                    </span>
                </Tooltip>
            </span>
        );
    getCurrentlyPlayingUI = () =>
        !DocumentView.CurrentlyPlaying?.length ? null : (
            <span key="-currently-playing-" className="bottomPopup-background">
                <span className="bottomPopup-text">
                    Currently playing:
                    {DocumentView.CurrentlyPlaying.map((clip, i) => (
                        <>
                            <span className="audio-title" onPointerDown={() => DocumentView.showDocument(clip.Document, { willZoomCentered: true })}>
                                {clip.Document.title + (i === DocumentView.CurrentlyPlaying.length - 1 ? '  ' : ',')}
                            </span>
                            <FontAwesomeIcon icon={!clip.ComponentView?.IsPlaying?.() ? 'play' : 'pause'} size="lg" onPointerDown={() => clip.ComponentView?.TogglePause?.()} />{' '}
                            <FontAwesomeIcon icon="times" size="lg" onPointerDown={() => clip.ComponentView?.Pause?.()} />{' '}
                        </>
                    ))}
                </span>
            </span>
        );

    getDisplayDoc = (doc: Doc, preview: boolean = false) => {
        // hack to avoid overhead of making UndoStack,etc into DocumentView style Boxes. If the UndoStack is ever intended to become part of the persisten state of the dashboard, then this would have to change.
        // prettier-ignore
        switch (doc.layout) {
            case '<LinkingUI>':          return this.getLinkUI();
            case '<CurrentlyPlayingUI>': return this.getCurrentlyPlayingUI();
            case '<UndoStack>':          return <UndoStack key={doc[Id]}/>;
            case '<Branching>':          return Doc.UserDoc().isBranchingMode  ? <BranchingTrailManager key={doc[Id]} /> : null;
            default: 
        }

        const nested = doc._type_collection === CollectionViewType.Linear;
        const hidden = doc.hidden === true;

        let dref: Opt<HTMLDivElement>;
        const docXf = () => this.getTransform(dref);
        // const scalable = pair.layout.onClick || pair.layout.onDragStart;
        return hidden ? null : (
            <div
                className={preview ? 'preview' : `collectionLinearView-docBtn`}
                key={doc[Id]}
                ref={r => {
                    dref = r || undefined;
                }}
                style={{
                    pointerEvents: 'all',
                    width: NumCast(doc._width),
                    height: NumCast(doc._height),
                    marginLeft: 2,
                    marginRight: 2,
                    // width: NumCast(pair.layout._width),
                    // height: NumCast(pair.layout._height),
                }}>
                <DocumentView
                    Document={doc}
                    isContentActive={this._props.isContentActive}
                    isDocumentActive={returnTrue}
                    addDocument={this._props.addDocument}
                    moveDocument={this._props.moveDocument}
                    addDocTab={this._props.addDocTab}
                    pinToPres={emptyFunction}
                    dragAction={(this.layoutDoc.childDragAction ?? this._props.childDragAction) as dropActionType}
                    rootSelected={this.rootSelected}
                    removeDocument={this._props.removeDocument}
                    ScreenToLocalTransform={docXf}
                    PanelWidth={doc[Width]}
                    PanelHeight={nested || doc._height ? doc[Height] : this.dimension}
                    renderDepth={this._props.renderDepth + 1}
                    dontRegisterView={BoolCast(this.Document.childDontRegisterViews)}
                    focus={emptyFunction}
                    styleProvider={this._props.styleProvider}
                    containerViewPath={this.childContainerViewPath}
                    whenChildContentsActiveChanged={emptyFunction}
                    childFilters={this._props.childFilters}
                    childFiltersByRanges={this._props.childFiltersByRanges}
                    searchFilterDocs={this._props.searchFilterDocs}
                    hideResizeHandles
                />
            </div>
        );
    };

    render() {
        const flexDir = StrCast(this.Document.flexDirection); // Specify direction of linear view content
        const flexGap = NumCast(this.Document.flexGap); // Specify the gap between linear view content
        const isExpanded = BoolCast(this.layoutDoc.linearView_IsOpen);

        const menuOpener = (
            <Toggle
                text={Cast(this.Document.icon, 'string', null)}
                icon={Cast(this.Document.icon, 'string', null) ? undefined : <FontAwesomeIcon color={SettingsManager.userColor} icon={isExpanded ? 'minus' : 'plus'} />}
                color={SettingsManager.userColor}
                background={SettingsManager.userVariantColor}
                type={Type.TERT}
                onPointerDown={e => e.stopPropagation()}
                toggleType={ToggleType.BUTTON}
                toggleStatus={BoolCast(this.layoutDoc.linearView_IsOpen)}
                onClick={() => {
                    this.layoutDoc.linearView_IsOpen = !isExpanded;
                    ScriptCast(this.Document.onClick)?.script.run({ this: this.Document }, console.log);
                }}
                tooltip={isExpanded ? 'Close' : 'Open'}
                fillWidth
                align="center"
            />
        );

        return (
            <div className={`collectionLinearView-outer ${this.layoutDoc.linearView_SubMenu}`} style={{ backgroundColor: this.layoutDoc.linearView_IsOpen ? undefined : 'transparent' }}>
                <div className="collectionLinearView" ref={this.createDashEventsTarget} onContextMenu={this.myContextMenu} style={{ minHeight: this.dimension(), pointerEvents: 'all' }}>
                    {!this.layoutDoc.linearView_Expandable ? null : menuOpener}
                    {!this.layoutDoc.linearView_IsOpen ? null : (
                        <div
                            className="collectionLinearView-content"
                            style={{
                                height: this.dimension(),
                                flexDirection: flexDir as Property.FlexDirection,
                                gap: flexGap,
                            }}>
                            {this.childLayoutPairs.map(pair => this.getDisplayDoc(pair.layout))}
                        </div>
                    )}
                </div>
            </div>
        );
    }
}