2022-10-07 16:32:54 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class ExploreService extends Service {
|
|
|
|
@service router;
|
|
|
|
@service feature;
|
|
|
|
@service ghostPaths;
|
|
|
|
|
|
|
|
exploreUrl = 'https://ghost.org/explore/';
|
|
|
|
exploreRouteRoot = '#/explore';
|
|
|
|
submitRoute = 'submit';
|
|
|
|
|
|
|
|
@tracked exploreWindowOpen = false;
|
|
|
|
@tracked siteData = null;
|
|
|
|
@tracked isIframeTransition = false;
|
|
|
|
|
|
|
|
get apiUrl() {
|
|
|
|
const origin = new URL(window.location.origin);
|
|
|
|
const subdir = this.ghostPaths.subdir;
|
|
|
|
// We want the API URL without protocol
|
|
|
|
let url = this.ghostPaths.url.join(origin.host, subdir);
|
|
|
|
|
|
|
|
return url.replace(/\/$/, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
|
|
|
|
if (this.exploreUrl) {
|
|
|
|
window.addEventListener('message', (event) => {
|
|
|
|
if (event && event.data && event.data.route) {
|
|
|
|
this.handleRouteChangeInIframe(event.data.route);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRouteChangeInIframe(destinationRoute) {
|
|
|
|
if (this.exploreWindowOpen) {
|
|
|
|
let exploreRoute = this.exploreRouteRoot;
|
|
|
|
|
|
|
|
if (destinationRoute.match(/^\/explore(\/.*)?/)) {
|
|
|
|
destinationRoute = destinationRoute.replace(/\/explore/, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destinationRoute !== '/') {
|
|
|
|
exploreRoute += destinationRoute;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.location.hash !== exploreRoute) {
|
|
|
|
window.history.replaceState(window.history.state, '', exploreRoute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getIframeURL() {
|
|
|
|
let url = this.exploreUrl;
|
|
|
|
|
|
|
|
if (window.location.hash && window.location.hash.includes(this.exploreRouteRoot)) {
|
|
|
|
let destinationRoute = window.location.hash.replace(this.exploreRouteRoot, '');
|
|
|
|
|
|
|
|
// Connect is an Ember route, do not use it as iframe src
|
|
|
|
if (destinationRoute && !destinationRoute.includes('connect')) {
|
|
|
|
url += destinationRoute.replace(/^\//, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 18:48:52 +03:00
|
|
|
return url;
|
2022-10-07 16:32:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sends a route update to a child route in the BMA, because we can't control
|
|
|
|
// navigating to it otherwise
|
|
|
|
sendRouteUpdate(route) {
|
|
|
|
this.getExploreIframe().contentWindow.postMessage({
|
|
|
|
query: 'routeUpdate',
|
|
|
|
response: route
|
|
|
|
}, '*');
|
|
|
|
}
|
|
|
|
|
2023-08-09 10:46:52 +03:00
|
|
|
sendUIUpdate(data) {
|
|
|
|
this.getExploreIframe().contentWindow.postMessage({
|
|
|
|
query: 'uiUpdate',
|
|
|
|
response: data
|
|
|
|
}, '*');
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:32:54 +03:00
|
|
|
// Controls explore window modal visibility and sync of the URL visible in browser
|
|
|
|
// and the URL opened on the iframe. It is responsible to non user triggered iframe opening,
|
|
|
|
// for example: by entering "/explore" route in the URL or using history navigation (back and forward)
|
|
|
|
toggleExploreWindow(value) {
|
|
|
|
if (this.exploreWindowOpen && value) {
|
|
|
|
// don't attempt to open again
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.exploreWindowOpen = value;
|
|
|
|
}
|
|
|
|
|
2023-03-10 19:22:03 +03:00
|
|
|
openExploreWindow() {
|
2022-10-07 16:32:54 +03:00
|
|
|
if (this.exploreWindowOpen) {
|
|
|
|
// don't attempt to open again
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-07 16:31:00 +03:00
|
|
|
// Begin loading the iframe and setting the src if it's not already set
|
2023-03-10 19:22:03 +03:00
|
|
|
this.ensureIframeIsLoaded();
|
2023-03-07 16:31:00 +03:00
|
|
|
|
2022-10-07 16:32:54 +03:00
|
|
|
// Ensures correct "getIframeURL" calculation when syncing iframe location
|
|
|
|
// in toggleExploreWindow
|
2023-03-10 19:22:03 +03:00
|
|
|
window.location.hash = '/explore';
|
2022-10-07 16:32:54 +03:00
|
|
|
|
2023-03-10 19:22:03 +03:00
|
|
|
this.router.transitionTo('/explore');
|
2022-10-07 16:32:54 +03:00
|
|
|
this.toggleExploreWindow(true);
|
|
|
|
}
|
|
|
|
|
2023-03-10 19:22:03 +03:00
|
|
|
ensureIframeIsLoaded() {
|
|
|
|
if (this.getExploreIframe() && !this.getExploreIframe()?.src) {
|
|
|
|
this.getExploreIframe().src = this.getIframeURL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:32:54 +03:00
|
|
|
getExploreIframe() {
|
|
|
|
return document.getElementById('explore-frame');
|
|
|
|
}
|
|
|
|
}
|