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

import actions from 'app/actions'
import { ROMAN_NUMERALS } from 'app/constants'
import { Arrow } from './viewer.icons'
import NavPlayer from './nav.player'
import { thumbnailURL, sectionProgressPercentage, sectionOffsetSeconds } from 'app/utils/annotation.utils'

class NavParent extends Component {
  state = {
    hoveringNext: false,
    hoveringNav: false,
    suppressHover: false,
  }
  constructor(props){
    super(props)
    this.navbarRef = React.createRef()
    this.handleMouseMove = this.handleMouseMove.bind(this)
    this.handleMouseEnter = this.handleMouseEnter.bind(this)
    this.handleMouseLeave = this.handleMouseLeave.bind(this)
    this.handleMouseEnterNext = this.handleMouseEnterNext.bind(this)
    this.handleMouseLeaveNext = this.handleMouseLeaveNext.bind(this)
    this.handleNavBarClick = this.handleNavBarClick.bind(this)
    this.handleSectionLinkClick = this.handleSectionLinkClick.bind(this)
    this.goToNextSection = this.goToNextSection.bind(this)
  }
  handleMouseEnter(){
    // console.log('mouse enter')
    if (this.state.suppressHover) return
    this.setState({ hoveringNext: false, hoveringNav: true })
  }
  handleMouseEnterNext(){
    // console.log('mouse enter next', this.state.suppressHover)
    if (this.state.suppressHover) return
    this.setState({ hoveringNext: true, hoveringNav: false })
  }
  handleMouseLeave(){
    // console.log('mouse leave')
    this.setState({ hoveringNav: false })
    clearTimeout(this.timeout)
    this.timeout = setTimeout(() => {
      this.setState({ hoveringNext: false, suppressHover: false })
    }, 100)
  }
  handleMouseLeaveNext(){
    // console.log('mouse leave next')
    this.setState({ hoveringNext: false, suppressHover: true, })
    clearTimeout(this.timeout)
    this.timeout = setTimeout(() => {
      this.setState({ suppressHover: false })
    }, 100)
  }
  handleNavBarClick(e) {
    e && e.stopPropagation()
    // console.log('>> CLICK NAV')
    // actions.viewer.toggleComponent('nav')
    const percent = (e.pageX / this.navbarRef.current.offsetWidth)
    const seconds = sectionOffsetSeconds(this.props.currentSection, percent)
    console.log(e.pageX, this.navbarRef.current.offsetWidth, percent)
    console.log(this.props.currentSection)
    console.log(">>", seconds)
    actions.viewer.seekToTimestamp(seconds)
  }
  handleSectionLinkClick(e) {
    e && e.stopPropagation()
    actions.viewer.toggleComponent('nav')
  }
  handleMouseMove(e) {
    e && e.stopPropagation()
  }
  goToNextSection(e) {
    e && e.preventDefault()
    e && e.stopPropagation()
    const { viewer } = this.props
    // console.log('>> SEEK', viewer.nextSection)
    if (viewer.nextSection) {
      actions.viewer.seekToSection(viewer.nextSection)
    } else {
      actions.viewer.showNavComponent('credits')
      // actions.viewer.seekToBeginning()
    }
    this.setState({ hoveringNext: false, hoveringNav: false, suppressHover: true })
  }
  render() {
    const { viewer, currentSection, play_ts, started, playing } = this.props
    let containerClassName = "viewer-nav " + viewer.navStyle
    let navClassName = 'nav-row main-nav'
    if (this.state.hoveringNav || !started) containerClassName += ' hovering-nav'
    if ((this.state.hoveringNext || (viewer.atEndOfSection && !playing)) && !viewer.nav && viewer.nextSection) containerClassName += ' hovering-next'
    return (
      <div ref={this.navbarRef} className={containerClassName} onMouseMove={this.handleMouseMove} onMouseLeave={this.handleMouseLeave}>
        <div className={navClassName} onClick={this.handleNavBarClick}>
          <div className='nav-section-name' onMouseEnter={this.handleMouseEnter}>
            <span className="section-link link">
              <Arrow type={viewer.nav ? 'down' : 'up'} onClick={this.handleSectionLinkClick} />
              {currentSection && (
                viewer.mediaTitle
                ? <span><i>{viewer.mediaTitle}</i></span>
                : <span>
                    {ROMAN_NUMERALS[currentSection.index]}
                    {'. '}
                    {currentSection.title}
                  </span>
              )}
            </span>
          </div>
          <div className='nav-player-container' onMouseEnter={this.handleMouseEnter}>
            <NavPlayer />
          </div>
          <div
            className='nav-next'
            onMouseEnter={this.handleMouseEnter}
          >
            <span
              className="next-link link"
              onMouseEnter={this.handleMouseEnterNext}
              onClick={this.goToNextSection}
            >
              {viewer.nextSection
                ? "Next"
                : <span className="replay-link">{'Credits'}</span>
              }
              <Arrow type={'right'} />
            </span>
          </div>
          <div className="nav-progress-percentage" style={{
            width: currentSection ? sectionProgressPercentage(currentSection, play_ts) : 0
          }} />
        </div>
        {viewer.nextSection &&
          <div className="next-section-thumbnail"
            onClick={this.goToNextSection}
            style={{
              backgroundImage: (
                viewer.nextSection.media.length
                && 'url(' + thumbnailURL(viewer.nextSection.media[0].media) + ')'
              ),
            }}
          />
        }
      </div>
    )
  }
}

const mapStateToProps = state => ({
  viewer: state.viewer,
  currentSection: state.viewer.currentSection,
  started: state.audio.started,
  playing: state.audio.playing,
  play_ts: state.audio.play_ts,
})

export default connect(mapStateToProps)(NavParent)