diff --git a/origo.js b/origo.js index baf60b6b7..7c3802e77 100644 --- a/origo.js +++ b/origo.js @@ -25,8 +25,11 @@ import { renderSvgIcon } from './src/utils/legendmaker'; import SelectedItem from './src/models/SelectedItem'; import 'elm-pep'; import 'pepjs'; +import permalink from './src/permalink/permalink'; const Origo = function Origo(configPath, options = {}) { + /** Reference to the returned Component */ + let origo; let viewer; const origoConfig = { controls: [], @@ -92,6 +95,33 @@ const Origo = function Origo(configPath, options = {}) { const api = () => viewer; const getConfig = () => origoConfig; + /** Helper that initialises a new viewer */ + const initViewer = () => { + const defaultConfig = Object.assign({}, origoConfig, options); + loadResources(configPath, defaultConfig) + .then((data) => { + const viewerOptions = data.options; + viewerOptions.controls = initControls(viewerOptions.controls); + viewerOptions.extensions = initExtensions(viewerOptions.extensions || []); + const target = viewerOptions.target; + viewer = Viewer(target, viewerOptions); + viewer.on('loaded', () => { + // Inform listeners that there is a new Viewer in town + origo.dispatch('load', viewer); + }); + }) + .catch(error => console.error(error)); + }; + // Add a listener to handle a new sharemap when using hash format. + window.addEventListener('hashchange', (ev) => { + const newParams = permalink.parsePermalink(ev.newURL); + + if (newParams.map) { + // "Reboot" the application by creating a new viewer instance using the original configuration and the new sharemap state + initViewer(); + } + }); + return ui.Component({ api, getConfig, @@ -100,19 +130,8 @@ const Origo = function Origo(configPath, options = {}) { const base = document.createElement('base'); base.href = defaultConfig.baseUrl; document.getElementsByTagName('head')[0].appendChild(base); - loadResources(configPath, defaultConfig) - .then((data) => { - const viewerOptions = data.options; - viewerOptions.controls = initControls(viewerOptions.controls); - viewerOptions.extensions = initExtensions(viewerOptions.extensions || []); - const target = viewerOptions.target; - viewer = Viewer(target, viewerOptions); - const origo = this; - viewer.on('loaded', () => { - origo.dispatch('load', viewer); - }); - }) - .catch(error => console.error(error)); + origo = this; + initViewer(); } }); }; diff --git a/src/viewer.js b/src/viewer.js index 025e07a38..8ef52d62c 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -14,7 +14,6 @@ import Layer from './layer'; import Main from './components/main'; import Footer from './components/footer'; import flattenGroups from './utils/flattengroups'; -import getAttributes from './getattributes'; import getcenter from './geometry/getcenter'; import isEmbedded from './utils/isembedded';