summaryrefslogtreecommitdiff
path: root/frontend/site/viewer/viewer.container.js
blob: 6f6b8509cb4a318b6b2b009b91128b34c7a8fc7f (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
import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import { history } from 'site/store'
import actions from 'site/actions'
import { Loader } from 'app/common/loader.component'
import TileHandle from 'app/views/tile/components/tile.handle'

import 'app/views/page/page.css'

class ViewerContainer extends Component {
  state = {
    page: {},
    bounds: { width: window.innerWidth, height: window.innerHeight },
    roadblock: false,
  }

  constructor(props) {
    super(props)
    this.pageRef = React.createRef()
    this.handleMouseDown = this.handleMouseDown.bind(this)
    this.handleResize = this.handleResize.bind(this)
    this.removeRoadblock = this.removeRoadblock.bind(this)
    window.addEventListener('resize', this.handleResize)
  }

  componentDidUpdate(prevProps) {
    // console.log('didUpdate', this.props.graph !== prevProps.graph, this.props.location.pathname !== prevProps.location.pathname)
    if (this.props.graph !== prevProps.graph || this.props.location.pathname !== prevProps.location.pathname) {
      this.load()
    }
  }

  componentWillUnmount() {
    window.removeEventListener('resize', this.handleResize)
    actions.site.interact()
  }

  load() {
    const { graph_name, page_name } = this.props.match.params
    const page_path = ["", graph_name, page_name].join('/')
    const { pages, home_page } = this.props.graph
    const page = pages[page_path] || pages[home_page]
    if (!this.props.interactive && hasAutoplay(page)) {
      this.setState({ page, roadblock: true })
    } else {
      this.setState({ page, roadblock: false })
      actions.site.interact()
      this.props.audio.player.playPage(page)
    }
  }

  handleResize() {
    this.setState({
      bounds: {
        width: window.innerWidth,
        height: window.innerHeight,
      }
    })
  }

  handleMouseDown(e, tile) {
    if (tile.href) {
      if (tile.href.indexOf('http') === 0) {
        window.location.href = tile.href
        return
      }
      else if (!tile.settings.navigate_when_audio_finishes) {
        history.push(tile.href)
      }
    }
    if (tile.settings.audio_on_click_id > 0) {
      this.props.audio.player.playTile({
        type: "click",
        tile,
      })
    }
  }

  handlePlaybackEnded(tile) {
    if (tile.href && tile.settings.autoadvance) {
      history.push(tile.href)
    }
  }

  render() {
    const { page, audio } = this.state
    if (this.state.roadblock) {
      return this.renderRoadblock()
    }
    if (this.props.graph.loading || !page.id) {
      return (
        <div>
          <div className='body'>
            <div className='page loading'>
              <Loader />
            </div>
          </div>
        </div>
      )
    }
    const { settings } = page
    const pageStyle = { backgroundColor: settings ? settings.background_color : '#000000' }
    // console.log(page)
    return (
      <div className='body'>
        <div className='page' ref={this.pageRef} style={pageStyle}>
          {page.tiles.map(tile => {
            return (
              <TileHandle
                viewing
                key={tile.id}
                tile={tile}
                audio={audio}
                bounds={this.state.bounds}
                onMouseDown={e => this.handleMouseDown(e, tile)}
                onPlaybackEnded={e => this.handlePlaybackEnded(e, tile)}
                onDoubleClick={e => {}}
              />
            )
          })}
        </div>
      </div>
    )
  }

  removeRoadblock() {
    console.log("remove roadblock")
    actions.site.interact()
    this.setState({ roadblock: false })
    this.props.audio.player.playPage(this.state.page)
  }
  
  renderRoadblock() {
    const { title } = this.props.graph
    return (
      <div className='roadblock' onClick={this.removeRoadblock}>
        <div>
          <h2>{title}</h2>
          <button>Enter</button>
        </div>
      </div>
    )
  }
}

const hasAutoplay = page => {
  const hasAutoplayVideo = page.tiles.some(tile => {
    return tile.type === 'video' && !tile.settings.muted
  })
  const hasAutoplayAudio = page.settings.background_audio_id > 0
  return hasAutoplayAudio || hasAutoplayVideo
}

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

const mapDispatchToProps = dispatch => ({
})

export default connect(mapStateToProps, mapDispatchToProps)(ViewerContainer)