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, 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.handleMouseEnter = this.handleMouseEnter.bind(this) this.handleMouseLeave = this.handleMouseLeave.bind(this) this.removeRoadblock = this.removeRoadblock.bind(this) this.updateTimer = this.updateTimer.bind(this) window.addEventListener('resize', this.handleResize) } componentDidMount() { if (this.props.graph && !this.props.graph.loading) { this.load() } } componentDidUpdate(prevProps) { // console.log('didUpdate', this.props.graph !== prevProps.graph, this.props.location.pathname !== prevProps.location.pathname) // console.log(this.props.location.pathname, prevProps.location.pathname, this.props.interactive, prevProps.interactive) if ( this.props.graph !== prevProps.graph || this.props.location.pathname !== prevProps.location.pathname || this.props.interactive !== prevProps.interactive ) { this.load() } else 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 console.log("load", graph_name, page_name) 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] document.querySelector("title").innerText = page.title // console.log(pages, page) // console.log("show page", page.id) if (!this.props.interactive && hasAutoplay(page)) { this.setState({ page, hidden: {}, roadblock: true, unloaded: false }) actions.site.setPopups({}) } else { this.setState({ page, hidden: {}, roadblock: false, unloaded: false }) actions.site.setPopups({}) 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) { const popups = { ...this.props.popups } let refreshPopups = false if (tile.href) { if (tile.href.indexOf('http') === 0) { window.location.href = tile.href return } else if (tile.href === '__open_popup') { popups[tile.settings.target_popup] = true refreshPopups = true } else if (tile.href === '__close_popup') { popups[tile.settings.target_popup] = false refreshPopups = true } else if (tile.href === '__toggle_popup') { popups[tile.settings.target_popup] = !this.props.popups[tile.settings.target_popup] refreshPopups = true } 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, } }) if (tile.settings.show_popup_on_hover && tile.settings.on_hover_popup) { popups[tile.settings.on_hover_popup] = false refreshPopups = true } } if (refreshPopups) { actions.site.setPopups(popups) } } handleMouseEnter(e, tile) { if (tile.settings.audio_on_hover_id > 0) { this.props.audio.player.playTile({ type: "hover", tile, restart: true, }) } if (tile.settings.show_popup_on_hover && tile.settings.on_hover_popup) { actions.site.setPopups({ ...this.props.popups, [tile.settings.on_hover_popup]: true, }) } } handleMouseLeave(e, tile) { if (tile.settings.show_popup_on_hover && tile.settings.on_hover_popup) { actions.site.setPopups({ ...this.props.popups, [tile.settings.on_hover_popup]: false, }) } } handlePlaybackEnded(tile) { if (tile.href && tile.settings.autoadvance) { history.push(tile.href) } } render() { const { page, hidden, time } = this.state const { popups, audio } = this.props if (this.state.unloaded) { return null } if (this.state.roadblock) { return this.renderRoadblock() } if (this.props.graph.loading || !page.id) { return (