aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/CollectionCarousel3DView.tsx
blob: 01f41869e46f8e72f918c83c35bea45961df1ca9 (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
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { computed } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc } from '../../../fields/Doc';
import { Id } from '../../../fields/FieldSymbols';
import { NumCast, ScriptCast, StrCast } from '../../../fields/Types';
import { OmitKeys, returnFalse, Utils } from '../../../Utils';
import { DragManager } from '../../util/DragManager';
import { DocumentView } from '../nodes/DocumentView';
import { StyleProp } from '../StyleProvider';
import "./CollectionCarousel3DView.scss";
import { CollectionSubView } from './CollectionSubView';

@observer
export class CollectionCarousel3DView extends CollectionSubView() {
    @computed get scrollSpeed() {
        return this.layoutDoc._autoScrollSpeed ? NumCast(this.layoutDoc._autoScrollSpeed) : 1000; //default scroll speed
    }

    private _dropDisposer?: DragManager.DragDropDisposer;

    componentWillUnmount() { this._dropDisposer?.(); }

    protected createDashEventsTarget = (ele: HTMLDivElement | null) => { //used for stacking and masonry view
        this._dropDisposer?.();
        if (ele) {
            this._dropDisposer = DragManager.MakeDropTarget(ele, this.onInternalDrop.bind(this), this.layoutDoc);
        }
    }

    panelWidth = () => this.props.PanelWidth() / 3;
    panelHeight = () => this.props.PanelHeight() * 0.6;
    onChildDoubleClick = () => ScriptCast(this.layoutDoc.onChildDoubleClick);
    @computed get content() {
        const currentIndex = NumCast(this.layoutDoc._itemIndex);
        const displayDoc = (childPair: { layout: Doc, data: Doc }) => {
            return <DocumentView  {...OmitKeys(this.props, ["NativeWidth", "NativeHeight", "childLayoutTemplate", "childLayoutString"]).omit}
                onDoubleClick={this.onChildDoubleClick}
                renderDepth={this.props.renderDepth + 1}
                LayoutTemplate={this.props.childLayoutTemplate}
                LayoutTemplateString={this.props.childLayoutString}
                Document={childPair.layout}
                DataDoc={childPair.data}
                PanelWidth={this.panelWidth}
                PanelHeight={this.panelHeight}
                bringToFront={returnFalse}
            />;
        };

        return (this.childLayoutPairs.map((childPair, index) => {
            return (
                <div key={childPair.layout[Id]}
                    className={`collectionCarousel3DView-item${index === currentIndex ? "-active" : ""} ${index}`}
                    style={index === currentIndex ?
                        { opacity: '1', transform: 'scale(1.3)', width: this.panelWidth() } :
                        { opacity: '0.5', transform: 'scale(0.6)', userSelect: 'none', width: this.panelWidth() }}>
                    {displayDoc(childPair)}
                </div>);
        }));
    }

    changeSlide = (direction: number) => {
        this.layoutDoc._itemIndex = (NumCast(this.layoutDoc._itemIndex) + direction + this.childLayoutPairs.length) % this.childLayoutPairs.length;
    }

    onArrowClick = (e: React.MouseEvent, direction: number) => {
        e.stopPropagation();
        this.changeSlide(direction);
        !this.layoutDoc.autoScrollOn && (this.layoutDoc.showScrollButton = (direction === 1) ? "fwd" : "back"); // while autoscroll is on, keep the other autoscroll button hidden
        !this.layoutDoc.autoScrollOn && this.fadeScrollButton(); // keep pause button visible while autoscroll is on
    }

    interval?: number;
    startAutoScroll = (direction: number) => {
        this.interval = window.setInterval(() => {
            this.changeSlide(direction);
        }, this.scrollSpeed);
    }

    stopAutoScroll = () => {
        window.clearInterval(this.interval);
        this.interval = undefined;
        this.fadeScrollButton();
    }

    toggleAutoScroll = (direction: number) => {
        this.layoutDoc.autoScrollOn = this.layoutDoc.autoScrollOn ? false : true;
        this.layoutDoc.autoScrollOn ? this.startAutoScroll(direction) : this.stopAutoScroll();
    }

    fadeScrollButton = () => {
        window.setTimeout(() => {
            !this.layoutDoc.autoScrollOn && (this.layoutDoc.showScrollButton = "none"); //fade away after 1.5s if it's not clicked.
        }, 1500);
    }

    @computed get buttons() {
        if (!this.props.isContentActive()) return null;
        return <div className="arrow-buttons" >
            <div key="back" className="carousel3DView-back" style={{ background: `${StrCast(this.props.Document.backgroundColor)}` }}
                onClick={(e) => this.onArrowClick(e, -1)}
            >
                <FontAwesomeIcon icon={"angle-left"} size={"2x"} />
            </div>
            <div key="fwd" className="carousel3DView-fwd" style={{ background: `${StrCast(this.props.Document.backgroundColor)}` }}
                onClick={(e) => this.onArrowClick(e, 1)}
            >
                <FontAwesomeIcon icon={"angle-right"} size={"2x"} />
            </div>
            {this.autoScrollButton}
        </div>;
    }

    @computed get autoScrollButton() {
        const whichButton = this.layoutDoc.showScrollButton;
        return <>
            <div className={`carousel3DView-back-scroll${whichButton === "back" ? "" : "-hidden"}`} style={{ background: `${StrCast(this.props.Document.backgroundColor)}` }}
                onClick={() => this.toggleAutoScroll(-1)}>
                {this.layoutDoc.autoScrollOn ? <FontAwesomeIcon icon={"pause"} size={"1x"} /> : <FontAwesomeIcon icon={"angle-double-left"} size={"1x"} />}
            </div>
            <div className={`carousel3DView-fwd-scroll${whichButton === "fwd" ? "" : "-hidden"}`} style={{ background: `${StrCast(this.props.Document.backgroundColor)}` }}
                onClick={() => this.toggleAutoScroll(1)}>
                {this.layoutDoc.autoScrollOn ? <FontAwesomeIcon icon={"pause"} size={"1x"} /> : <FontAwesomeIcon icon={"angle-double-right"} size={"1x"} />}
            </div>
        </>;
    }

    @computed get dots() {
        return (this.childLayoutPairs.map((_child, index) =>
            <div key={Utils.GenerateGuid()} className={`dot${index === NumCast(this.layoutDoc._itemIndex) ? "-active" : ""}`}
                onClick={() => this.layoutDoc._itemIndex = index} />));
    }

    render() {
        const index = NumCast(this.layoutDoc._itemIndex);
        const translateX = this.panelWidth() * (1 - index);

        return <div className="collectionCarousel3DView-outer" ref={this.createDashEventsTarget}
            style={{
                background: this.props.styleProvider?.(this.layoutDoc, this.props, StyleProp.BackgroundColor),
                color: this.props.styleProvider?.(this.layoutDoc, this.props, StyleProp.Color),
            }}  >
            <div className="carousel-wrapper" style={{ transform: `translateX(${translateX}px)` }}>
                {this.content}
            </div>
            {this.props.Document._chromeHidden ? (null) : this.buttons}
            <div className="dot-bar">
                {this.dots}
            </div>
        </div>;
    }
}