Removed usage of did-insert hook and simplified openExploreWindow method

- We don't actually utilise the history handling for Explore, as it's not a full screen modal and therefore don't need passing in previous routes before opening the iframe
- Removed usage of `did-insert` livecycle hook and reverted to usage of `contructor` and `willDestroy` instead to add and remove message event listener
This commit is contained in:
Aileen Nowak 2023-03-10 16:22:03 +00:00 committed by Aileen Booker
parent a72dfdddb7
commit ec14f78ba3
5 changed files with 17 additions and 37 deletions

View File

@ -3,8 +3,6 @@
class="explore-frame"
frameborder="0"
title="Explore"
{{did-insert this.setup}}
{{did-update this.handleDarkModeChange this.feature.nightShift}}
{{did-insert this.attachMessageListener}}
...attributes
></iframe>

View File

@ -7,20 +7,16 @@ export default class GhExploreIframe extends Component {
@service router;
@service feature;
constructor() {
super(...arguments);
window.addEventListener('message', this.handleIframeMessage);
}
willDestroy() {
super.willDestroy(...arguments);
window.removeEventListener('message', this.handleIframeMessage);
}
@action
setup() {
// Only begin setup when Explore window is toggled open
// to avoid unnecessary loading of assets
if (this.explore.exploreWindowOpen) {
this.explore.getExploreIframe().src = this.explore.getIframeURL();
}
}
@action
async handleIframeMessage(event) {
if (this.isDestroyed || this.isDestroying) {
@ -50,11 +46,6 @@ export default class GhExploreIframe extends Component {
}
}
@action
attachMessageListener() {
window.addEventListener('message', this.handleIframeMessage);
}
// The iframe can send route updates to navigate to within Admin, as some routes
// have to be rendered within the iframe and others require to break out of it.
_handleRouteUpdate(data) {

View File

@ -111,7 +111,7 @@ export default class Main extends Component.extend(ShortcutsMixin) {
@action
toggleExploreWindow() {
this.explore.openExploreWindow(this.router.currentURL);
this.explore.openExploreWindow();
}
@task(function* () {

View File

@ -23,7 +23,7 @@ export default class ExploreIndexRoute extends AuthenticatedRoute {
// Ensure the explore window is set to open
if (transition.to?.localName === 'index') {
this.explore.isIframeTransition = true;
this.explore.openExploreWindow(this.router.currentURL);
this.explore.openExploreWindow();
}
}

View File

@ -6,14 +6,12 @@ export default class ExploreService extends Service {
@service feature;
@service ghostPaths;
// TODO: make this a config value
exploreUrl = 'https://ghost.org/explore/';
exploreRouteRoot = '#/explore';
submitRoute = 'submit';
@tracked exploreWindowOpen = false;
@tracked siteData = null;
@tracked previousRoute = null;
@tracked isIframeTransition = false;
get apiUrl() {
@ -97,36 +95,29 @@ export default class ExploreService extends Service {
this.exploreWindowOpen = value;
}
// Controls navigation to explore window modal which is triggered from the application UI.
// For example: pressing "View explore" link in navigation menu. It's main side effect is
// remembering the route from which the action has been triggered - "previousRoute" so it
// could be reused when closing the explore window
openExploreWindow(currentRoute, childRoute) {
openExploreWindow() {
if (this.exploreWindowOpen) {
// don't attempt to open again
return;
}
this.previousRoute = currentRoute;
// Begin loading the iframe and setting the src if it's not already set
if (this.getExploreIframe() && !this.getExploreIframe()?.src) {
this.getExploreIframe().src = this.getIframeURL();
}
// Begin loading the iframe and setting the src if it's not already set
if (this.getExploreIframe() && !this.getExploreIframe()?.src) {
this.getExploreIframe().src = this.getIframeURL();
}
this.ensureIframeIsLoaded();
// Ensures correct "getIframeURL" calculation when syncing iframe location
// in toggleExploreWindow
window.location.hash = childRoute || '/explore';
window.location.hash = '/explore';
this.router.transitionTo(childRoute || '/explore');
this.router.transitionTo('/explore');
this.toggleExploreWindow(true);
}
ensureIframeIsLoaded() {
if (this.getExploreIframe() && !this.getExploreIframe()?.src) {
this.getExploreIframe().src = this.getIframeURL();
}
}
getExploreIframe() {
return document.getElementById('explore-frame');
}