summaryrefslogtreecommitdiff
path: root/frontend/app/views/tile/handles/tile.video.js
blob: 001cce23ca441309e5caf8761e990c860908b987 (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
import React, { Component } from 'react'

import { generateTransform, generateVideoStyle } from 'app/views/tile/tile.utils'
import { timestampToSeconds } from 'app/utils'

export default class TileVideo extends Component {
  constructor(props) {
    super(props)
    this.videoRef = React.createRef()
    this.handleTimeUpdate = this.handleTimeUpdate.bind(this)
    this.handleEnded = this.handleEnded.bind(this)
  }
  componentDidMount() {
    this.videoRef.current.addEventListener('ended', this.handleEnded)
    this.videoRef.current.addEventListener('timeupdate', this.handleTimeUpdate)
  }
  componentWillUnmount() {
    this.videoRef.current.removeEventListener('timeupdate', this.handleTimeUpdate)
    this.videoRef.current.removeEventListener('ended', this.handleEnded)
  }
  handleTimeUpdate() {
    if (this.props.tile.settings.loop && this.props.tile.settings.loop_section) {
      const loop_start = timestampToSeconds(this.props.tile.settings.loop_start) || 0
      const loop_end = timestampToSeconds(this.props.tile.settings.loop_end) || this.videoRef.current.duration
      if (this.videoRef.current.currentTime > loop_end) {
        this.videoRef.current.currentTime = loop_start
      }
    }
  }
  handleEnded() {
    this.props.onPlaybackEnded(this.props.tile)
    if (this.props.tile.settings.loop && this.props.tile.settings.loop_section) {
      const loop_start = timestampToSeconds(this.props.tile.settings.loop_start) || 0
      this.videoRef.current.currentTime = loop_start
    }
  }
  render() {
    let { tile, bounds, box, viewing, onMouseDown, onDoubleClick } = this.props
    // console.log(tile)
    const style = {
      transform: generateTransform(tile, box),
      opacity: tile.settings.opacity,
    }
    let className = ['tile', tile.type].join(' ')
    if (tile.target_page_id || (viewing && tile.href)) {
      className += ' ' + (tile.settings.cursor || 'hand_up')
    }
    // console.log(tile.settings)
    if (!tile.settings.url) {
      return null
    }
    className += ' ' + tile.settings.align
    return (
      <div
        className={className}
        onMouseDown={onMouseDown}
        onDoubleClick={onDoubleClick}
        style={style}
      >
        <video
          ref={this.videoRef}
          src={tile.settings.url}
          autoPlay={true}
          controls={false}
          loop={tile.settings.loop}
          muted={tile.settings.muted}
          style={generateVideoStyle(tile, bounds)}
        />
      </div>
    )
  }
}