summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/viewer/player/player.container.js
blob: 39a08dc1187f4b2d35f0c2dc1deca807c45815c9 (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
import React, { Component } from 'react'
import { connect } from 'react-redux'

import actions from 'app/actions'
import { floatInRange, clamp } from 'app/utils'
import { preloadSectionImages } from 'app/utils/image.utils'

import PlayerTranscript from './player.transcript'
import PlayerFullscreen from './player.fullscreen'
import VideoModal from '../modals/modals.video'

class PlayerContainer extends Component {
  constructor(props) {
    super(props)
    this.handleKeyDown = this.handleKeyDown.bind(this)
  }
  
  componentDidMount() {
    // console.log(this.props.sections)
    const { sections } = this.props
    actions.viewer.setCurrentSection(sections[0], sections[1])
    document.addEventListener('keydown', this.handleKeyDown)
  }

  componentWillUnmount() {
    document.removeEventListener('keydown', this.handleKeyDown)
  }

  handleKeyDown(e) {
    if (document.activeElement !== document.body) {
      return
    }
    const { currentSection, audio, viewer } = this.props
    const { play_ts } = audio
    let start_ts = 0
    let end_ts = 0
    if (currentSection) {
      start_ts = currentSection.start_ts
      end_ts = currentSection.end_ts
    }
    // console.log(e.keyCode)
    switch (e.keyCode) {
      case 32: // spacebar
        e.preventDefault()
        actions.audio.toggle()
        break
      case 27:
        e.preventDefault()
        if (viewer.vitrineModal.open) {
          actions.viewer.closeVitrineModal()
        }
        break
      case 37: // left
      case 38: // up
        e.preventDefault()
        if (viewer.vitrineModal.open) {
          actions.viewer.vitrineGo(-1)
        } else {
          actions.audio.seek(clamp(play_ts - 5.0, start_ts, end_ts))
        }
        break
      case 39: // right
      case 40: // down
        e.preventDefault()
        if (viewer.vitrineModal.open) {
          actions.viewer.vitrineGo(+1)
        } else {
          actions.audio.seek(clamp(play_ts + 5.0, start_ts, end_ts))
        }
        break
    }
  }

  componentDidUpdate(prevProps) {
    if (this.props.audio.play_ts !== prevProps.audio.play_ts) {
      this.handleTimeUpdate()
    }
    if (this.props.currentSection !== prevProps.currentSection) {
      this.preloader && this.preloader.cancel()
      this.preloader = preloadSectionImages(this.props.currentSection, this.props.mediaLookup)
    }
  }

  handleTimeUpdate() {
    const { audio, currentSection, autoAdvance } = this.props
    const { play_ts } = audio
    // const { play_ts, seek_ts } = audio
    // const didSeek = floatEQ(play_ts, seek_ts)
    const inCurrentSection = floatInRange(currentSection.start_ts, play_ts, currentSection.end_ts)
    // console.log('inCurrentSection?', inCurrentSection)
    // console.log('didSeek?', didSeek)
    // if the current TS isn't in the same section as the current one...
    // console.log(currentSection.start_ts, play_ts, currentSection.end_ts)
    if (inCurrentSection) {
      return
    }
    // at end of section ()
    if (autoAdvance) {
      actions.viewer.setSectionFromTimestamp(play_ts)
    }
    else {
      console.log(">> Reached end of section")
      actions.viewer.reachedEndOfSection(currentSection)
    }
  }
  
  render() {
    if (!this.props.currentSection) { return <div /> }
    // console.log(currentSection)
    return (
      <div className='viewer-container'>
        <PlayerTranscript />
        <PlayerFullscreen />
        <VideoModal />
      </div>
    )
  }
}

const mapStateToProps = state => ({
  viewer: state.viewer,
  audio: state.audio,
  mediaLookup: state.media.index.lookup,
  sections: state.viewer.sections,
  currentSection: state.viewer.currentSection,
  autoAdvance: state.viewer.autoAdvance,
})

export default connect(mapStateToProps)(PlayerContainer)