2020-08-13 18:21:48 +02:00
|
|
|
import {HtmlUtils} from "./HtmlUtils";
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
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>();
|
|
|
|
|
|
|
|
private close(): HTMLDivElement {
|
|
|
|
const cowebsiteDiv = HtmlUtils.getElementByIdOrFail<HTMLDivElement>(cowebsiteDivId);
|
|
|
|
cowebsiteDiv.classList.remove('loaded'); //edit the css class to trigger the transition
|
|
|
|
cowebsiteDiv.classList.add('hidden');
|
|
|
|
this.opened = iframeStates.closed;
|
|
|
|
return cowebsiteDiv;
|
|
|
|
}
|
|
|
|
private load(): HTMLDivElement {
|
|
|
|
const cowebsiteDiv = HtmlUtils.getElementByIdOrFail<HTMLDivElement>(cowebsiteDivId);
|
|
|
|
cowebsiteDiv.classList.remove('hidden'); //edit the css class to trigger the transition
|
|
|
|
cowebsiteDiv.classList.add('loading');
|
|
|
|
this.opened = iframeStates.loading;
|
|
|
|
return cowebsiteDiv;
|
|
|
|
}
|
|
|
|
private open(): HTMLDivElement {
|
|
|
|
const cowebsiteDiv = HtmlUtils.getElementByIdOrFail<HTMLDivElement>(cowebsiteDivId);
|
|
|
|
cowebsiteDiv.classList.remove('loading', 'hidden'); //edit the css class to trigger the transition
|
|
|
|
this.opened = iframeStates.opened;
|
|
|
|
return cowebsiteDiv;
|
|
|
|
}
|
2020-08-13 18:21:48 +02:00
|
|
|
|
2020-10-27 16:59:12 +01:00
|
|
|
public loadCoWebsite(url: string): void {
|
|
|
|
const cowebsiteDiv = this.load();
|
2020-08-13 18:21:48 +02:00
|
|
|
cowebsiteDiv.innerHTML = '';
|
|
|
|
|
|
|
|
const iframe = document.createElement('iframe');
|
|
|
|
iframe.id = 'cowebsite-iframe';
|
|
|
|
iframe.src = url;
|
2020-10-27 16:59:12 +01:00
|
|
|
const onloadPromise = new Promise((resolve) => {
|
|
|
|
iframe.onload = () => resolve();
|
|
|
|
});
|
2020-08-13 18:21:48 +02:00
|
|
|
cowebsiteDiv.appendChild(iframe);
|
2020-10-27 16:59:12 +01:00
|
|
|
const onTimeoutPromise = new Promise((resolve) => {
|
|
|
|
setTimeout(() => resolve(), 2000);
|
|
|
|
});
|
|
|
|
Promise.race([onloadPromise, onTimeoutPromise]).then(() => {
|
|
|
|
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 {
|
|
|
|
const cowebsiteDiv = this.load();
|
|
|
|
callback(cowebsiteDiv).then(() => {
|
|
|
|
this.open();
|
|
|
|
setTimeout(() => {
|
|
|
|
this.fire();
|
|
|
|
}, 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> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const cowebsiteDiv = this.close();
|
|
|
|
this.fire();
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve();
|
|
|
|
setTimeout(() => cowebsiteDiv.innerHTML = '', 500)
|
|
|
|
}, animationTime)
|
|
|
|
});
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
export const coWebsiteManager = new CoWebsiteManager();
|