Skip to content

Commit

Permalink
Reload microfrontend when clicking on selected node (SAP#385)
Browse files Browse the repository at this point in the history
* triggering iframe reload when the same route is accessed again
  • Loading branch information
maxmarkus authored Jan 31, 2019
1 parent fe051b4 commit e7e5677
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
6 changes: 6 additions & 0 deletions core/src/services/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,9 @@ export const navigateIframe = (config, component, node) => {
}, iframeNavFallbackTimeout);
}
};

export const reloadActiveIframe = () => {
IframeHelpers.getVisibleIframes()
.pop()
.contentDocument.location.reload(true);
};
17 changes: 11 additions & 6 deletions core/src/services/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ export const concatenatePath = (basePath, relativePath) => {

/**
navigateTo used for navigation
Triggers a frame reload if we are on the same route (eg. if we click on same navigation item again)
@param route string absolute path of the new route
*/
export const navigateTo = async route => {
if (LuigiConfig.getConfigValue('routing.useHashRouting')) {
window.location.hash = route;
const windowPath = GenericHelpers.trimLeadingSlash(getWindowPath());
if (windowPath === GenericHelpers.trimLeadingSlash(route)) {
Iframe.reloadActiveIframe();
return;
}

// Avoid infinite loop on logout + login whith path routing
if (window.location.pathname === route) {
if (LuigiConfig.getConfigValue('routing.useHashRouting')) {
window.location.hash = route;
return;
}

Expand All @@ -71,10 +73,13 @@ export const navigateTo = async route => {
window.dispatchEvent(event);
};

export const buildFromRelativePath = node => {
let windowPath = LuigiConfig.getConfigValue('routing.useHashRouting')
const getWindowPath = () =>
LuigiConfig.getConfigValue('routing.useHashRouting')
? GenericHelpers.getPathWithoutHash(window.location.hash)
: window.location.pathname;

export const buildFromRelativePath = node => {
let windowPath = getWindowPath();
if (node.parent && node.parent.pathSegment) {
// use only this part of the current path that refers to the parent of the node (remove additional parts refering to the sibiling)
// remove everything that is after the parents pathSegment 'parent/keepSelectedForChildren/something' -> 'parent'
Expand Down
6 changes: 6 additions & 0 deletions core/src/utilities/helpers/iframe-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ export const getLocation = url => {
element.href = url;
return element.origin;
};

export const getVisibleIframes = () => {
return Array.prototype.slice
.call(document.querySelectorAll('iframe'))
.filter(item => item.style.display !== 'none');
};

0 comments on commit e7e5677

Please sign in to comment.