summaryrefslogtreecommitdiff
path: root/frontend/site/projects/museum/views/petros.nav.js
blob: 9ccfdffece55b35e8aec77b12255f7ad984d2532 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
 * Navigation for Petros Moris "Oracle"
 */

import React, { Component } from 'react'
import { connect } from 'react-redux'

import './petros.nav.css'

import { history } from "site/store"
import { preloadImage } from "app/utils"
import actions from "site/actions"

const RESET_STATE = {
  textActive: false,
  textDone: false,
  ready: false,
}

const MOVEMENT = "movement"
const LOOP = "loop"

const INITIAL_VIEW = {
  1: MOVEMENT,
  2: LOOP,
  3: LOOP,
  4: MOVEMENT,
  5: MOVEMENT,
  6: MOVEMENT,
  7: LOOP,
}

const LOOP_TIMEOUT = 100
const MOVEMENT_TIMEOUT = 40000
const TEXT_LOAD_TIMEOUT = 15000

class PetrosNav extends Component {
  state = {
    ...RESET_STATE
  }

  constructor(props) {
    super(props)
    this.handleEnter = this.handleEnter.bind(this)
    this.handleLeave = this.handleLeave.bind(this)
    this.handleClickText = this.handleClickText.bind(this)
    this.navigate = this.navigate.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(prevProps.match && prevProps.match.params)
    }
  }

  load(lastParams) {
    const { page_name } = this.props.match.params
    const page_partz = page_name.split("-")
    const isPetros = page_partz[0] === 'petros'
    const index = parseInt(page_partz[1])

    if (!isPetros) {
      clearTimeout(this.readyTimeout)
      this.setState({
        ...RESET_STATE,
      })
      return      
    }
    preloadImage(`/thelastmuseum/static/media/last-museum/petros-moris/OracleTextButton${index}-White.png`)
    preloadImage(`/thelastmuseum/static/media/last-museum/petros-moris/OracleTextButton${index}.png`)
    preloadImage(`/thelastmuseum/static/media/last-museum/petros-moris/NavBW${index}.png`)
    preloadImage(`/thelastmuseum/static/media/last-museum/petros-moris/NavB${index}.png`)
    this.setState({
      ...RESET_STATE,
      index
    })
    this.readyTimeout = setTimeout(() => {
      this.setState({
        ready: true,
      })
    }, INITIAL_VIEW[index] === MOVEMENT ? MOVEMENT_TIMEOUT : LOOP_TIMEOUT)
  }

  handleEnter(event) {
    const side = event.target.className.split('-')
    this.setState({ hovering: side })
  }
  handleLeave(event) {
    this.setState({ hovering: false })
  }

  /** Start text generation sequence */
  handleClickText() {
    if (this.state.textActive) return
    this.setState({
      textActive: true,
      hovering: false
    })
    // Turn on the glyph
    actions.site.setPopups({
      ...this.props.popups,
      glyph: true,
    })
    // Fetch the subtitles
    this.textTimeout = setTimeout(() => {
      this.setState({
        textActive: false,
        textDone: true,
      })

      // Turn off the glyph
      actions.site.setPopups({
        ...this.props.popups,
        glyph: false,
      })
    }, TEXT_LOAD_TIMEOUT)
  }

  /** Navigate using the links at the bottom */
  navigate() {
    const { index, textActive } = this.state
    const leftIndex = Math.max(1, index - 1)
    const rightIndex = Math.min(7, index + 1)
    if (textActive) return
    const side = event.target.className.split('-')
    // if navigating to the left, just navigate.
    console.log(side, index)
    if (side === "left") {
      if (index !== leftIndex) {
        history.push(`/thelastmuseum/petros-${leftIndex}`)
      }
    } else if (side === "right") {
      // if this page starts with the loop, switch to movement
      // this will autoadvance when the video is done (set in tile settings)
      console.log(INITIAL_VIEW[index])
      if (INITIAL_VIEW[index] === LOOP) {
        actions.site.setPopups({
          ...this.props.popups,
          movement: true,
        })
        this.setState({ ready: false })
      } else {
        // otherwise, just advance immediately.
        history.push(`/thelastmuseum/petros-${rightIndex}`)
      }
    }
  }

  render() {
    const { index, ready, hovering, textActive, textDone } = this.state
    if (!this.props.interactive || (!index)) return null
    const leftIndex = Math.max(1, index - 1)
    const rightIndex = Math.min(7, index + 1)
    return (
      <div>
        {ready && (
          <img
            className="petros-text"
            src={(hovering === "text" || textActive)
              ? `/thelastmuseum/static/media/last-museum/petros-moris/OracleTextButton${index}-White.png`
              : `/thelastmuseum/static/media/last-museum/petros-moris/OracleTextButton${index}.png`
            }
            style={{
              cursor: `url(/thelastmuseum/static/media/last-museum/petros-moris/OracleTextButton${index}-White.png), pointer`
            }}
            onMouseEnter={this.handleEnter}
            onMouseLeave={this.handleLeave}
            onClick={this.handleClickText}
          />
        )}
        {ready && textDone && (
          <img
            className="petros-left"
            src={(hovering === "left" || textActive || index === leftIndex)
              ? `/thelastmuseum/static/media/last-museum/petros-moris/NavBW${leftIndex}.png`
              : `/thelastmuseum/static/media/last-museum/petros-moris/NavB${leftIndex}.png`
            }
            style={{
              cursor: `url(/thelastmuseum/static/media/last-museum/petros-moris/NavBW${leftIndex}-White.png), pointer`
            }}
            onMouseEnter={this.handleEnter}
            onMouseLeave={this.handleLeave}
            onClick={this.navigate}
          />
        )}
        {ready && textDone && (
          <img
            className="petros-right"
            src={(hovering === "right" || textActive)
              ? `/thelastmuseum/static/media/last-museum/petros-moris/NavBW${rightIndex}.png`
              : `/thelastmuseum/static/media/last-museum/petros-moris/NavB${rightIndex}.png`
            }
            style={{
              cursor: `url(/thelastmuseum/static/media/last-museum/petros-moris/NavBW${rightIndex}-White.png), pointer`
            }}
            onMouseEnter={this.handleEnter}
            onMouseLeave={this.handleLeave}
            onClick={this.navigate}
          />
        )}
      </div>
    )
  }
}

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

export default connect(mapStateToProps)(PetrosNav)