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';
|
2021-05-24 13:02:26 +03:00
|
|
|
import {task} from 'ember-concurrency-decorators';
|
|
|
|
import {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-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() {
|
|
|
|
return this.args.src || `${this.config.get('blogUrl')}/`;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
if (iframe.contentWindow.location.href !== this.srcUrl) {
|
|
|
|
iframe.contentWindow.location = this.srcUrl;
|
|
|
|
} else {
|
|
|
|
iframe.contentWindow.location.reload();
|
|
|
|
}
|
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 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-18 18:36:18 +03:00
|
|
|
}
|
|
|
|
this.args.onLoad?.(event);
|
|
|
|
}
|
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) => {
|
|
|
|
const srcURL = new URL(this.srcUrl);
|
|
|
|
const originURL = new URL(event.origin);
|
|
|
|
|
|
|
|
if (originURL.origin === srcURL.origin) {
|
|
|
|
if (event.data === this.args.invisibleUntilLoaded) {
|
|
|
|
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-18 18:08:03 +03:00
|
|
|
}
|