-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Angular] The page is redirected to the home page of the website afte…
…r clicking the "Change associated content" button in the Experience Editor in angular application (#835) * Add workaround for Angular not handling EE onclick * Change import to angluar pkg * Move code to utils in angular package * Add comment to sample * Refactor based on comments * More refactoring - Use ExperienceEditor class for active editor check - Change null check for targetNode * Use isExperienceEditorActive method for release/18.0.0 branch (cherry picked from commit 103b7a5)
- Loading branch information
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { isExperienceEditorActive } from '@sitecore-jss/sitecore-jss'; | ||
|
||
/** | ||
* @description in Experience Editor with an Angular sample app, anchor tags | ||
* with both onclick and href attributes will use the href, blocking the onclick from firing. | ||
* This function makes it so the anchor tags function as intended in an Angular sample when using Experience Editor | ||
* | ||
* The Mutation Observer API is used to observe changes to the body, then select all elements with href="#" and an onclick, | ||
* and replaces the # value with javascript:void(0); which prevents the anchor tag from blocking the onclick event handler. | ||
* @see Mutation Observer API: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver | ||
*/ | ||
export const handleEditorAnchors = () => { | ||
// Angular gives the href attribute priority over the onclick attribute if both are present, so we must replace | ||
// the href attribute to avoid overriding the onclick in Experience Editor | ||
if (!window || !isExperienceEditorActive()) { | ||
return; | ||
} | ||
const targetNode = document.querySelector('body'); | ||
const callback = (mutationList: MutationRecord[]) => { | ||
mutationList.forEach((mutation: MutationRecord) => { | ||
const btns: NodeListOf<HTMLAnchorElement> = document.querySelectorAll( | ||
'.scChromeDropDown > a[href="#"], a[onclick]' | ||
); | ||
if (mutation.type === 'childList') { | ||
btns.forEach((link: HTMLAnchorElement) => { | ||
link.href = 'javascript:void(0);'; | ||
}); | ||
} | ||
return; | ||
}); | ||
}; | ||
const observer: MutationObserver = new MutationObserver(callback); | ||
const observerOptions = { | ||
childList: true, | ||
subtree: true, | ||
}; | ||
if (targetNode) { | ||
observer.observe(targetNode, observerOptions); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters