2022-10-07 16:32:54 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
|
|
|
export default class GhExploreIframe extends Component {
|
|
|
|
@service explore;
|
|
|
|
@service router;
|
|
|
|
@service feature;
|
|
|
|
|
2023-03-10 19:22:03 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
window.addEventListener('message', this.handleIframeMessage);
|
|
|
|
}
|
|
|
|
|
2022-11-03 13:25:43 +03:00
|
|
|
willDestroy() {
|
|
|
|
super.willDestroy(...arguments);
|
|
|
|
window.removeEventListener('message', this.handleIframeMessage);
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:40:21 +03:00
|
|
|
@action
|
|
|
|
setup() {
|
|
|
|
// Only begin setup when Explore window is toggled open
|
|
|
|
// to avoid unnecessary loading of assets
|
|
|
|
if (this.explore.exploreWindowOpen) {
|
2023-03-22 17:00:00 +03:00
|
|
|
this.explore.getExploreIframe().src = this.explore.iframeURL;
|
2023-03-22 16:40:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 13:25:43 +03:00
|
|
|
@action
|
|
|
|
async handleIframeMessage(event) {
|
|
|
|
if (this.isDestroyed || this.isDestroying) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only process messages coming from the explore iframe
|
2023-03-22 17:00:00 +03:00
|
|
|
if (event?.data && this.explore.iframeURL.includes(event?.origin)) {
|
2022-11-03 13:25:43 +03:00
|
|
|
if (event.data?.request === 'apiUrl') {
|
|
|
|
this._handleUrlRequest();
|
|
|
|
}
|
2022-10-07 16:32:54 +03:00
|
|
|
|
2022-11-03 13:25:43 +03:00
|
|
|
if (event.data?.route) {
|
|
|
|
this._handleRouteUpdate(event.data);
|
|
|
|
}
|
2022-10-07 16:32:54 +03:00
|
|
|
|
2022-11-03 13:25:43 +03:00
|
|
|
if (event.data?.siteData) {
|
|
|
|
this._handleSiteDataUpdate(event.data);
|
2022-10-07 16:32:54 +03:00
|
|
|
}
|
2022-11-03 13:25:43 +03:00
|
|
|
}
|
2022-10-07 16:32:54 +03:00
|
|
|
}
|
|
|
|
|
2023-08-09 10:46:52 +03:00
|
|
|
@action
|
|
|
|
async handleDarkModeChange() {
|
|
|
|
if (this.explore.exploreWindowOpen) {
|
|
|
|
this.explore.sendUIUpdate({darkMode: this.feature.nightShift});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:32:54 +03:00
|
|
|
// 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) {
|
|
|
|
const route = data.route;
|
|
|
|
this.explore.isIframeTransition = route?.includes('/explore');
|
|
|
|
this.explore.toggleExploreWindow(this.explore.isIframeTransition);
|
|
|
|
this.router.transitionTo(route);
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleUrlRequest() {
|
|
|
|
this.explore.getExploreIframe().contentWindow.postMessage({
|
|
|
|
request: 'apiUrl',
|
|
|
|
response: {apiUrl: this.explore.apiUrl, darkMode: this.feature.nightShift}
|
|
|
|
}, '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleSiteDataUpdate(data) {
|
2022-10-21 13:48:18 +03:00
|
|
|
this.explore.siteData = data?.siteData ?? {};
|
2022-10-07 16:32:54 +03:00
|
|
|
}
|
|
|
|
}
|