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, unloaded: false, popups: {}, hidden: {}, time: 0, maxDeferTime: 0, } 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) this.updateTimer = this.updateTimer.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() } if (this.props.interactive && (this.props.interactive !== prevProps.interactive)) { this.setState({ roadblock: false }) this.props.audio.player.playPage(this.state.page) this.resetTimer(this.state.page) } } componentWillUnmount() { window.removeEventListener('resize', this.handleResize) actions.site.interact() } load() { const { page_name } = this.props.match.params const { pages, home_page, path: graph_name } = this.props.graph const page_path = ["", graph_name, page_name].join('/') // if ((!page_path in pages) || page_name === 'index.html') { // this.setState({ unloaded: true }) // return // } console.log(this.props.interactive) const page = pages[page_path] || pages[home_page] if (!this.props.interactive && hasAutoplay(page)) { this.setState({ page, popups: {}, hidden: {}, roadblock: true, unloaded: false }) } else { this.setState({ page, popups: {}, hidden: {}, roadblock: false, unloaded: false }) actions.site.interact() this.props.audio.player.playPage(page) this.resetTimer(page) } } resetTimer(page) { clearTimeout(this.timeout) const maxDeferTime = page.tiles.reduce((max_time, tile) => Math.max(tile.settings.appear_after || 0, max_time), 0) if (maxDeferTime) { this.setState({ time: 0, maxDeferTime }) this.timeout = setTimeout(this.updateTimer, 500) } } updateTimer() { clearTimeout(this.timeout) this.setState({ time: this.state.time + 0.500 }) if (this.state.time < this.state.maxDeferTime) { this.timeout = setTimeout(this.updateTimer, 500) } } 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.href === '__open_popup') { this.setState({ popups: { ...this.state.popups, [tile.settings.target_popup]: true, }, }) } else if (tile.href === '__close_popup') { this.setState({ popups: { ...this.state.popups, [tile.settings.target_popup]: false, }, }) } 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, }) } if (tile.settings.hide_on_click) { this.setState({ hidden: { ...this.state.hidden, [tile.id]: true, } }) } } handleMouseEnter(e, tile) { if (tile.settings.audio_on_hover_id > 0) { this.props.audio.player.playTile({ type: "hover", tile, restart: true, }) } } handlePlaybackEnded(tile) { if (tile.href && tile.settings.autoadvance) { history.push(tile.href) } } render() { const { page, audio, popups, hidden, time } = this.state if (this.state.unloaded) { return null } if (this.state.roadblock) { return this.renderRoadblock() } if (this.props.graph.loading || !page.id) { return (