2021-05-18 18:08:03 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
2019-03-21 20:55:58 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2021-05-18 18:36:18 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2019-03-21 20:55:58 +03:00
|
|
|
|
2021-05-18 18:08:03 +03:00
|
|
|
export default class GhSiteIframeComponent extends Component {
|
|
|
|
@service config;
|
2021-01-18 20:48:11 +03:00
|
|
|
|
2021-05-18 18:36:18 +03:00
|
|
|
@tracked isInvisible = this.args.invisibleUntilLoaded;
|
|
|
|
|
2021-05-18 20:26:07 +03:00
|
|
|
willDestroy() {
|
2021-07-15 17:27:29 +03:00
|
|
|
super.willDestroy?.(...arguments);
|
|
|
|
|
2021-05-24 16:03:08 +03:00
|
|
|
if (this.messageListener) {
|
|
|
|
window.removeEventListener('message', this.messageListener);
|
|
|
|
}
|
2021-05-18 20:26:07 +03:00
|
|
|
this.args.onDestroyed?.();
|
|
|
|
}
|
|
|
|
|
2021-05-18 18:08:03 +03:00
|
|
|
get srcUrl() {
|
2021-05-24 19:27:52 +03:00
|
|
|
const srcUrl = new URL(this.args.src || `${this.config.get('blogUrl')}/`);
|
|
|
|
|
|
|
|
if (this.args.guid) {
|
|
|
|
srcUrl.searchParams.set('v', this.args.guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return srcUrl.href;
|
2021-05-18 18:08:03 +03:00
|
|
|
}
|
2021-01-18 20:48:11 +03:00
|
|
|
|
2021-05-18 18:08:03 +03:00
|
|
|
@action
|
|
|
|
resetSrcAttribute(iframe) {
|
2021-05-19 22:24:13 +03:00
|
|
|
// reset the src attribute and force reload each time the guid changes
|
|
|
|
// - allows for a click on the navigation item to reset back to the homepage
|
|
|
|
// or a portal preview modal to force a reload so it can fetch server-side data
|
2021-05-18 18:08:03 +03:00
|
|
|
if (this.args.guid !== this._lastGuid) {
|
2019-03-21 20:55:58 +03:00
|
|
|
if (iframe) {
|
2021-05-19 22:24:13 +03:00
|
|
|
if (this.args.invisibleUntilLoaded) {
|
|
|
|
this.isInvisible = true;
|
|
|
|
}
|
2021-06-18 15:08:11 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (iframe.contentWindow.location.href !== this.srcUrl) {
|
|
|
|
iframe.contentWindow.location = this.srcUrl;
|
|
|
|
} else {
|
|
|
|
iframe.contentWindow.location.reload();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (e.name === 'SecurityError') {
|
|
|
|
iframe.src = this.srcUrl;
|
|
|
|
}
|
2021-05-19 22:24:13 +03:00
|
|
|
}
|
2019-03-21 20:55:58 +03:00
|
|
|
}
|
|
|
|
}
|
2021-05-18 18:08:03 +03:00
|
|
|
this._lastGuid = this.args.guid;
|
2019-03-21 20:55:58 +03:00
|
|
|
}
|
2021-05-18 18:36:18 +03:00
|
|
|
|
|
|
|
@action
|
|
|
|
onLoad(event) {
|
2021-05-24 19:27:52 +03:00
|
|
|
this.iframe = event.target;
|
|
|
|
|
2021-05-24 16:03:08 +03:00
|
|
|
if (this.args.invisibleUntilLoaded && typeof this.args.invisibleUntilLoaded === 'boolean') {
|
2021-05-24 13:02:26 +03:00
|
|
|
this.makeVisible.perform();
|
2021-05-24 19:27:52 +03:00
|
|
|
} else {
|
|
|
|
this.args.onLoad?.(this.iframe);
|
2021-05-18 18:36:18 +03:00
|
|
|
}
|
|
|
|
}
|
2021-05-24 13:02:26 +03:00
|
|
|
|
2021-05-24 16:03:08 +03:00
|
|
|
@action
|
|
|
|
attachMessageListener() {
|
|
|
|
if (typeof this.args.invisibleUntilLoaded === 'string') {
|
|
|
|
this.messageListener = (event) => {
|
2021-05-27 15:20:37 +03:00
|
|
|
if (this.isDestroying || this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:03:08 +03:00
|
|
|
const srcURL = new URL(this.srcUrl);
|
|
|
|
const originURL = new URL(event.origin);
|
|
|
|
|
|
|
|
if (originURL.origin === srcURL.origin) {
|
2021-05-27 15:20:37 +03:00
|
|
|
if (event.data === this.args.invisibleUntilLoaded || event.data.type === this.args.invisibleUntilLoaded) {
|
2021-05-24 16:03:08 +03:00
|
|
|
this.makeVisible.perform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('message', this.messageListener, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:02:26 +03:00
|
|
|
@task
|
|
|
|
*makeVisible() {
|
|
|
|
// give any scripts a bit of time to render before making visible
|
|
|
|
// allows portal to render it's overlay and prevent site background flashes
|
|
|
|
yield timeout(100);
|
|
|
|
this.isInvisible = false;
|
2021-05-24 19:27:52 +03:00
|
|
|
this.args.onLoad?.(this.iframe);
|
2021-05-24 13:02:26 +03:00
|
|
|
}
|
2021-05-18 18:08:03 +03:00
|
|
|
}
|