2020-08-13 18:21:48 +02:00
|
|
|
import {HtmlUtils} from "./HtmlUtils";
|
2021-03-06 16:00:07 +01:00
|
|
|
import {iframeListener} from "../Api/IframeListener";
|
2020-08-13 18:21:48 +02:00
|
|
|
|
|
|
|
export type CoWebsiteStateChangedCallback = () => void;
|
|
|
|
|
2020-10-27 16:59:12 +01:00
|
|
|
enum iframeStates {
|
|
|
|
closed = 1,
|
|
|
|
loading, // loading an iframe can be slow, so we show some placeholder until it is ready
|
|
|
|
opened,
|
|
|
|
}
|
|
|
|
|
|
|
|
const cowebsiteDivId = "cowebsite"; // the id of the parent div of the iframe.
|
|
|
|
const animationTime = 500; //time used by the css transitions, in ms.
|
|
|
|
|
|
|
|
class CoWebsiteManager {
|
2021-03-06 16:00:07 +01:00
|
|
|
|
|
|
|
private opened: iframeStates = iframeStates.closed;
|
2020-08-13 18:21:48 +02:00
|
|
|
|
2020-10-27 16:59:12 +01:00
|
|
|
private observers = new Array<CoWebsiteStateChangedCallback>();
|
2020-11-10 15:22:30 +01:00
|
|
|
/**
|
|
|
|
* Quickly going in and out of an iframe trigger can create conflicts between the iframe states.
|
|
|
|
* So we use this promise to queue up every cowebsite state transition
|
|
|
|
*/
|
2020-11-16 16:15:21 +01:00
|
|
|
private currentOperationPromise: Promise<void> = Promise.resolve();
|
2021-03-06 16:00:07 +01:00
|
|
|
private cowebsiteDiv: HTMLDivElement;
|
|
|
|
|
2020-11-16 16:15:21 +01:00
|
|
|
constructor() {
|
|
|
|
this.cowebsiteDiv = HtmlUtils.getElementByIdOrFail<HTMLDivElement>(cowebsiteDivId);
|
|
|
|
}
|
2021-03-06 16:00:07 +01:00
|
|
|
|
2020-11-16 16:15:21 +01:00
|
|
|
private close(): void {
|
|
|
|
this.cowebsiteDiv.classList.remove('loaded'); //edit the css class to trigger the transition
|
|
|
|
this.cowebsiteDiv.classList.add('hidden');
|
2020-10-27 16:59:12 +01:00
|
|
|
this.opened = iframeStates.closed;
|
|
|
|
}
|
2020-11-16 16:15:21 +01:00
|
|
|
private load(): void {
|
|
|
|
this.cowebsiteDiv.classList.remove('hidden'); //edit the css class to trigger the transition
|
|
|
|
this.cowebsiteDiv.classList.add('loading');
|
2020-10-27 16:59:12 +01:00
|
|
|
this.opened = iframeStates.loading;
|
|
|
|
}
|
2020-11-16 16:15:21 +01:00
|
|
|
private open(): void {
|
|
|
|
this.cowebsiteDiv.classList.remove('loading', 'hidden'); //edit the css class to trigger the transition
|
2020-10-27 16:59:12 +01:00
|
|
|
this.opened = iframeStates.opened;
|
|
|
|
}
|
2020-08-13 18:21:48 +02:00
|
|
|
|
2021-03-06 16:00:07 +01:00
|
|
|
public loadCoWebsite(url: string, base: string, allowApi?: boolean, allowPolicy?: string): void {
|
2020-11-16 16:15:21 +01:00
|
|
|
this.load();
|
2020-11-17 18:03:44 +01:00
|
|
|
this.cowebsiteDiv.innerHTML = `<button class="close-btn" id="cowebsite-close">
|
|
|
|
<img src="resources/logos/close.svg">
|
|
|
|
</button>`;
|
2020-12-17 16:36:01 +01:00
|
|
|
setTimeout(() => {
|
2020-12-17 16:39:11 +01:00
|
|
|
HtmlUtils.getElementByIdOrFail('cowebsite-close').addEventListener('click', () => {
|
|
|
|
this.closeCoWebsite();
|
|
|
|
});
|
2020-12-17 16:36:01 +01:00
|
|
|
}, 100);
|
2020-08-13 18:21:48 +02:00
|
|
|
|
|
|
|
const iframe = document.createElement('iframe');
|
|
|
|
iframe.id = 'cowebsite-iframe';
|
2021-01-30 17:51:07 +01:00
|
|
|
iframe.src = (new URL(url, base)).toString();
|
2020-12-29 23:17:16 +01:00
|
|
|
if (allowPolicy) {
|
2021-03-06 16:00:07 +01:00
|
|
|
iframe.allow = allowPolicy;
|
2020-12-29 23:17:16 +01:00
|
|
|
}
|
2020-10-27 16:59:12 +01:00
|
|
|
const onloadPromise = new Promise((resolve) => {
|
|
|
|
iframe.onload = () => resolve();
|
|
|
|
});
|
2021-03-06 16:00:07 +01:00
|
|
|
if (allowApi) {
|
|
|
|
iframeListener.registerIframe(iframe);
|
|
|
|
}
|
2020-11-16 16:15:21 +01:00
|
|
|
this.cowebsiteDiv.appendChild(iframe);
|
2020-10-27 16:59:12 +01:00
|
|
|
const onTimeoutPromise = new Promise((resolve) => {
|
|
|
|
setTimeout(() => resolve(), 2000);
|
|
|
|
});
|
2020-11-10 15:22:30 +01:00
|
|
|
this.currentOperationPromise = this.currentOperationPromise.then(() =>Promise.race([onloadPromise, onTimeoutPromise])).then(() => {
|
2020-10-27 16:59:12 +01:00
|
|
|
this.open();
|
|
|
|
setTimeout(() => {
|
|
|
|
this.fire();
|
|
|
|
}, animationTime)
|
2020-11-09 11:16:54 +01:00
|
|
|
}).catch(() => this.closeCoWebsite());
|
2020-08-31 12:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just like loadCoWebsite but the div can be filled by the user.
|
|
|
|
*/
|
2020-10-27 16:59:12 +01:00
|
|
|
public insertCoWebsite(callback: (cowebsite: HTMLDivElement) => Promise<void>): void {
|
2020-11-16 16:15:21 +01:00
|
|
|
this.load();
|
|
|
|
this.currentOperationPromise = this.currentOperationPromise.then(() => callback(this.cowebsiteDiv)).then(() => {
|
2020-10-27 16:59:12 +01:00
|
|
|
this.open();
|
|
|
|
setTimeout(() => {
|
|
|
|
this.fire();
|
2020-11-16 16:15:21 +01:00
|
|
|
}, animationTime);
|
2020-11-09 11:16:54 +01:00
|
|
|
}).catch(() => this.closeCoWebsite());
|
2020-08-13 18:21:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 16:59:12 +01:00
|
|
|
public closeCoWebsite(): Promise<void> {
|
2020-11-10 15:22:30 +01:00
|
|
|
this.currentOperationPromise = this.currentOperationPromise.then(() => new Promise((resolve, reject) => {
|
|
|
|
if(this.opened === iframeStates.closed) resolve(); //this method may be called twice, in case of iframe error for example
|
2020-11-16 16:15:21 +01:00
|
|
|
this.close();
|
2020-10-27 16:59:12 +01:00
|
|
|
this.fire();
|
2021-03-06 16:00:07 +01:00
|
|
|
const iframe = this.cowebsiteDiv.querySelector('iframe');
|
|
|
|
if (iframe) {
|
|
|
|
iframeListener.unregisterIframe(iframe);
|
|
|
|
}
|
2020-10-27 16:59:12 +01:00
|
|
|
setTimeout(() => {
|
2020-11-17 18:03:44 +01:00
|
|
|
this.cowebsiteDiv.innerHTML = `<button class="close-btn" id="cowebsite-close">
|
|
|
|
<img src="resources/logos/close.svg">
|
|
|
|
</button>`;
|
2020-10-27 16:59:12 +01:00
|
|
|
resolve();
|
|
|
|
}, animationTime)
|
2020-11-10 15:22:30 +01:00
|
|
|
}));
|
|
|
|
return this.currentOperationPromise;
|
2020-08-13 18:21:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 16:59:12 +01:00
|
|
|
public getGameSize(): {width: number, height: number} {
|
|
|
|
if (this.opened !== iframeStates.opened) {
|
2020-08-13 18:21:48 +02:00
|
|
|
return {
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (window.innerWidth >= window.innerHeight) {
|
|
|
|
return {
|
|
|
|
width: window.innerWidth / 2,
|
|
|
|
height: window.innerHeight
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight / 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 16:00:07 +01:00
|
|
|
//todo: is it still useful to allow any kind of observers?
|
2020-10-27 16:59:12 +01:00
|
|
|
public onStateChange(observer: CoWebsiteStateChangedCallback) {
|
|
|
|
this.observers.push(observer);
|
2020-08-13 18:21:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 16:59:12 +01:00
|
|
|
private fire(): void {
|
|
|
|
for (const callback of this.observers) {
|
2020-08-13 18:21:48 +02:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 16:59:12 +01:00
|
|
|
|
2021-03-06 16:00:07 +01:00
|
|
|
export const coWebsiteManager = new CoWebsiteManager();
|