aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/Timeline.tsx
blob: 90907861f78dcf344c18c4cd38951f1c58e32902 (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
import * as React from "react"; 
import * as ReactDOM from "react-dom"; 
import "./Timeline.scss"; 
import { CollectionSubView } from "../collections/CollectionSubView";
import { Document, listSpec, createSchema, makeInterface, defaultSpec } from "../../../new_fields/Schema";
import { observer} from "mobx-react";
import { Track } from "./Track";
import { observable, reaction, action, IReactionDisposer, observe, IObservableArray, computed, toJS, Reaction } from "mobx";
import { Cast } from "../../../new_fields/Types";
import { SelectionManager } from "../../util/SelectionManager";
import { List } from "../../../new_fields/List";
import { Self } from "../../../new_fields/FieldSymbols";
import { Doc, DocListCast } from "../../../new_fields/Doc";
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCircle, faPlayCircle, faBackward, faForward } from "@fortawesome/free-solid-svg-icons";
import { DocumentContentsView } from "./DocumentContentsView";



@observer
export class Timeline extends CollectionSubView(Document){

    @observable private _scrubberbox = React.createRef<HTMLDivElement>()
    @observable private _trackbox = React.createRef<HTMLDivElement>(); 
    @observable private _currentBarX:number = 0; 
    @observable private _windSpeed:number = 1; 
    @observable private _isPlaying:boolean = false; 
    @observable private _boxLength:number = 0; 
    @observable private _nodes:List<Doc> = new List<Doc>(); 
    @observable private _time = 100000; //DEFAULT

    @observable private _infoContainer = React.createRef<HTMLDivElement>(); 
    @observable private _panX = 0; 
    @observable private _ticks: number[] = []; 
    private _reactionDisposers: IReactionDisposer[] = [];


    @action
    componentDidMount(){
        let scrubber = this._scrubberbox.current!; 
        this._boxLength = scrubber.getBoundingClientRect().width; 
        let children = Cast(this.props.Document[this.props.fieldKey], listSpec(Doc));
        if (!children) {
            return;
        }
        let childrenList = ((children[Self] as any).__fields) as List<Doc>;
        this._nodes = (childrenList) as List<Doc>;


        //check if this is a video frame 

        let boxWidth = scrubber.getBoundingClientRect().width; 
        for (let i = 0; i < this._time; ) {
            this._ticks.push(i); 
            i += 1000; 
        }
    }

    componentDidUpdate(){
        let infoContainer = this._infoContainer.current!; 
        let trackbox = this._trackbox.current!; 
        this._boxLength = infoContainer.scrollWidth; 
        trackbox.style.width = `${this._boxLength}`; 
    }

    componentWillUnmount(){
        
    }

    //for playing
    @action
    onPlay = async (e: React.MouseEvent) => {
        if (this._isPlaying) {
            this._isPlaying = false;
        } else {
            this._isPlaying = true;
            this.changeCurrentX();
        }
    }

    @action
    changeCurrentX = () => { 
        console.log(this._currentBarX); 
            console.log(this._boxLength); 
        if (this._currentBarX === this._boxLength && this._isPlaying) {
            this._currentBarX = 0;
        }
        if (this._currentBarX <= this._boxLength && this._isPlaying) {
            this._currentBarX = this._currentBarX + this._windSpeed;
            setTimeout(this.changeCurrentX, 15);
        }
    }

    @action
    windForward = (e: React.MouseEvent) => {
        if (this._windSpeed < 64) { //max speed is 32
            this._windSpeed = this._windSpeed * 2;
        }
    }

    @action
    windBackward = (e: React.MouseEvent) => {
        if (this._windSpeed > 1 / 16) { // min speed is 1/8
            this._windSpeed = this._windSpeed / 2;
        }
    }

    //for scrubber action 
    @action 
    onScrubberDown = (e:React.PointerEvent) => {
        e.preventDefault(); 
        e.stopPropagation(); 
        let scrubberbox = this._scrubberbox.current!;
        //let left = scrubberbox.getBoundingClientRect().left;
        
        //let offsetX = Math.round(e.clientX - left);
        let mouse = e.nativeEvent; 
        this._currentBarX = mouse.offsetX; 
        document.addEventListener("pointermove", this.onScrubberMove); 
        document.addEventListener("pointerup", this.onScrubberFinished); 
     }

    @action 
    onScrubberMove = (e: PointerEvent) => {
        e.preventDefault(); 
        e.stopPropagation(); 
        let scrubberbox = this._scrubberbox.current!;
        let left = scrubberbox.getBoundingClientRect().left;
        let offsetX = Math.round(e.clientX - left);
        this._currentBarX = offsetX; 
    }

    onScrubberFinished = (e: PointerEvent) => {
        e.preventDefault(); 
        e.stopPropagation(); 
        let scrubberbox = this._scrubberbox.current!; 
        document.removeEventListener("pointermove", this.onScrubberMove);       
    }

    toTime = (time:number):string => {
        const inSeconds = time / 1000; 
        let min:(string|number) = Math.floor(inSeconds / 60); 
        let sec:(string|number) = inSeconds % 60; 

        if (Math.floor(sec/10) === 0){
            sec = "0" + sec; 
        }
        return `${min}:${sec}`; 
        
    }


    @action 
    onPanDown = (e:React.PointerEvent) => {
        e.preventDefault(); 
        e.stopPropagation(); 
        document.addEventListener("pointermove", this.onPanMove); 
        document.addEventListener("pointerup", this.onPanUp);
    }

    @action 
    onPanMove = (e:PointerEvent) => {
        e.preventDefault(); 
        e.stopPropagation(); 
        let infoContainer = this._infoContainer.current!; 
        infoContainer.scrollLeft = infoContainer.scrollLeft - e.movementX; 
    }

    @action 
    onPanUp = (e:PointerEvent) => {
        e.preventDefault(); 
        e.stopPropagation(); 
        document.removeEventListener("pointermove", this.onPanMove); 
    }

    render(){
        return (
            <div className="timeline-container">
                <div className="toolbox">
                    <div onClick={this.windBackward}> <FontAwesomeIcon icon={faBackward} size="lg"/> </div>  
                    <div onClick={this.onPlay}> <FontAwesomeIcon icon={faPlayCircle} size="lg"/> </div>
                    <div onClick={this.windForward}> <FontAwesomeIcon icon={faForward} size="lg"/> </div>
                </div>  
                <div className="info-container" ref ={this._infoContainer} onPointerDown={this.onPanDown}> 
                    <div className="scrubberbox" ref ={this._scrubberbox}>
                        {this._ticks.map(element => {return <div className="tick" style={{transform:`translate(${element / 20}px)`, position:"absolute"}}> <p>{this.toTime(element)}</p></div>})}
                    </div>
                    <div className="scrubber" onPointerDown = {this.onScrubberDown} style={{transform:`translate(${this._currentBarX}px)`}}>
                        <FontAwesomeIcon className="scrubberhead" icon={faCircle}/>;
                    </div>
                    <div className="trackbox" ref={this._trackbox}>  
                        {this._nodes.map(doc => {return <Track node={(doc as any).value() as Doc}/>;})}
                    </div> 
                </div>
            </div>
        ); 
    }

}