Skip to content

Commit

Permalink
Support for changing share map hash (#1473)
Browse files Browse the repository at this point in the history
* Added support for changing share map hash

* Only reboot map if sharemap map arg is specified.

Co-authored-by: Stefan Forsgren <stefan@forsgren@xlent.se>
  • Loading branch information
steff-o and Stefan Forsgren authored May 11, 2022
1 parent 3c73d63 commit b8f177a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
45 changes: 32 additions & 13 deletions origo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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,
Expand All @@ -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();
}
});
};
Expand Down
1 change: 0 additions & 1 deletion src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit b8f177a

Please sign in to comment.