summaryrefslogtreecommitdiff
path: root/frontend/site/projects/museum/views/titles.overlay.js
blob: 95c272d4510d8a890b3c784cf9441917a11074cd (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
import React, { Component } from 'react'
import { connect } from 'react-redux'

import { SUBTITLES } from '../subtitles.js'

import './titles.css'

import { history } from "site/store"

const HEADPHONES_SHOW_DELAY = 500
const HEADPHONES_HIDE_DELAY = 3000
const TITLE_SHOW_DELAY = 1000
const TITLE_HIDE_DELAY = 6000
const FIRST_SUBTITLE_DELAY = 3000
const SUBTITLE_DELAY = 3500
const LAST_SUBTITLE_DELAY = 5000

class TitlesOverlay extends Component {
  state = {
    content: null,
    showText: false,
    showVideo: false,
  }

  constructor(props) {
    super(props)
    this.titleRef = React.createRef()
    this.headphonesRef = React.createRef()
    this.textRef = React.createRef()
    this.showTitle = this.showTitle.bind(this)
    this.showHeadphones = this.showHeadphones.bind(this)
    this.advance = this.advance.bind(this)
  }

  componentDidMount() {
    if (this.props.interactive) {
      this.load()
    }
  }

  componentDidUpdate(prevProps) {
    if (
      (this.props.interactive && this.props.interactive !== prevProps.interactive)
      || this.props.location.pathname !== prevProps.location.pathname
    ) {
      this.load()
    }
    if (
      this.props.popups !== prevProps.popups
      && this.state.content
      && this.state.content.popup
    ) {
      if (this.props.popups[this.state.content.popup]) {
        this.setState({ showVideo: true })
      } else if (this.state.showVideo) {
        this.setState({ showText: true })
        this.startHeartbeat()
      }
    }
  }

  load() {
    const { page_name } = this.props.match.params
    clearTimeout(this.titleTimeout)
    this.props.audio.player.stop("text-overlay")
    if (SUBTITLES[page_name]) {
      this.setState({
        content: SUBTITLES[page_name],
        showText: false,
        showVideo: false,
      })
      setTimeout(SUBTITLES[page_name].headphones ? this.showHeadphones : this.showTitle, 0)
    } else {
      this.setState({
        content: null,
        showText: false,
        showVideo: false,
      })
    }
  }

  startHeartbeat() {
    if (this.state.content.audio_url) {
      this.props.audio.player.stop("text-overlay")
      this.props.audio.player.playURL({
        id: "text-overlay",
        url: this.state.content.audio_url,
      })
    }
  }

  showHeadphones() {
    if (!this.headphonesRef.current) return
    this.titleTimeout = setTimeout(() => {
      this.headphonesRef.current.style.opacity = 1
      this.titleTimeout = setTimeout(() => {
        this.headphonesRef.current.style.opacity = 0
        setTimeout(this.showTitle, TITLE_SHOW_DELAY)
      }, HEADPHONES_HIDE_DELAY)
    }, HEADPHONES_SHOW_DELAY)
  }

  showTitle() {
    if (!this.titleRef.current) return
    this.titleRef.current.innerHTML = ""
    this.titleRef.current.style.color = this.state.content.color || "rgba(255, 121, 13, 1.0)"
    this.titleRef.current.style.opacity = 0
    this.titleTimeout = setTimeout(() => {
      this.titleRef.current.innerHTML = this.state.content.title
      this.titleRef.current.style.opacity = 1
      this.titleTimeout = setTimeout(() => {
        this.titleRef.current.style.opacity = 0
      }, TITLE_HIDE_DELAY)
    }, TITLE_SHOW_DELAY)
  }

  advance(){
    if (this.advancing === this.state.content.next) return
    this.advancing = this.state.content.next
    this.textRef.current.classList.remove("visible")
    Array.from(document.querySelectorAll("video")).forEach(video => {
      video.style.transition = "opacity 0.5s"
    })
    setTimeout(() => {
      Array.from(document.querySelectorAll("video")).forEach(video => {
        video.style.opacity = 0
      })
    }, 500)
    setTimeout(() => {
      history.push(`/last-museum/${this.state.content.next}/`)
    }, 1000)
  }

  render() {
    const { content, showText } = this.state
    const { popups, interactive } = this.props
    if (!interactive || !content) return null
    return (
      <div>
        <div
          ref={this.headphonesRef}
          className="chapter-headphones"
        >
          {"Headphones recommended"}
        </div>
        <div
          ref={this.titleRef}
          className="chapter-title"
        >
          {this.state.content.title}
        </div>
        <div
          ref={this.textRef}
          className={showText ? "charles-text visible" : "charles-text"}
          style={{
            cursor: `url(${this.state.content.cursor}) 50 50, pointer`
          }}
          onClick={this.advance}
        >
          {showText && (
            <span>
              {this.state.content.text}
              <br/><br/>
              <div className="charles-credit">{this.state.content.credit}</div>
            </span>
          )}
        </div>
      </div>
    )
  }
}

const mapStateToProps = state => ({
  audio: state.audio,
  popups: state.site.popups,
  interactive: state.site.interactive,
})

export default connect(mapStateToProps)(TitlesOverlay)