workadventure/front/src/WebRtc/CoWebsiteManager.ts

720 lines
24 KiB
TypeScript
Raw Normal View History

import { HtmlUtils } from "./HtmlUtils";
import { Subject } from "rxjs";
import { waScaleManager } from "../Phaser/Services/WaScaleManager";
import { coWebsites, coWebsitesNotAsleep, mainCoWebsite } from "../Stores/CoWebsiteStore";
2022-02-10 19:26:46 +01:00
import { get, Readable, Writable, writable } from "svelte/store";
2022-01-05 10:30:27 +01:00
import { embedScreenLayout, highlightedEmbedScreen } from "../Stores/EmbedScreensStore";
import { isMediaBreakpointDown } from "../Utils/BreakpointsUtils";
import { LayoutMode } from "./LayoutManager";
2022-02-10 11:40:36 +01:00
import type { CoWebsite } from "./CoWebsite/CoWesbite";
import type CancelablePromise from "cancelable-promise";
2022-02-07 17:09:52 +01:00
export enum iframeStates {
closed = 1,
loading, // loading an iframe can be slow, so we show some placeholder until it is ready
opened,
}
2021-10-07 14:44:15 +02:00
const cowebsiteDomId = "cowebsite"; // the id of the whole container.
2022-01-05 10:30:27 +01:00
const gameOverlayDomId = "game-overlay";
2021-10-07 14:44:15 +02:00
const cowebsiteBufferDomId = "cowebsite-buffer"; // the id of the container who contains cowebsite iframes.
const cowebsiteAsideHolderDomId = "cowebsite-aside-holder";
2022-01-05 10:30:27 +01:00
const cowebsiteLoaderDomId = "cowebsite-loader";
const cowebsiteCloseButtonId = "cowebsite-close";
const cowebsiteFullScreenButtonId = "cowebsite-fullscreen";
const cowebsiteOpenFullScreenImageId = "cowebsite-fullscreen-open";
const cowebsiteCloseFullScreenImageId = "cowebsite-fullscreen-close";
2022-01-17 10:04:54 +01:00
const cowebsiteSwipeButtonId = "cowebsite-swipe";
2022-01-05 10:30:27 +01:00
const cowebsiteSlotBaseDomId = "cowebsite-slot-";
const animationTime = 500; //time used by the css transitions, in ms.
interface TouchMoveCoordinates {
x: number;
y: number;
}
class CoWebsiteManager {
2022-02-10 19:26:46 +01:00
private openedMain: Writable<iframeStates> = writable(iframeStates.closed);
private _onResize: Subject<void> = new Subject();
public onResize = this._onResize.asObservable();
2021-10-07 14:44:15 +02:00
private cowebsiteDom: HTMLDivElement;
private resizing: boolean = false;
2022-01-05 10:30:27 +01:00
private gameOverlayDom: HTMLDivElement;
2021-10-07 14:44:15 +02:00
private cowebsiteBufferDom: HTMLDivElement;
private cowebsiteAsideHolderDom: HTMLDivElement;
2022-01-05 10:30:27 +01:00
private cowebsiteLoaderDom: HTMLDivElement;
private previousTouchMoveCoordinates: TouchMoveCoordinates | null = null; //only use on touchscreens to track touch movement
2022-01-05 10:30:27 +01:00
private loaderAnimationInterval: {
interval: NodeJS.Timeout | undefined;
trails: number[] | undefined;
};
2021-10-07 14:44:15 +02:00
2021-11-10 15:20:04 +01:00
private resizeObserver = new ResizeObserver((entries) => {
2021-10-07 14:44:15 +02:00
this.resizeAllIframes();
});
2022-02-07 17:09:52 +01:00
public getMainState() {
2022-02-10 19:26:46 +01:00
return get(this.openedMain);
}
public getMainStateSubscriber(): Readable<iframeStates> {
2022-02-07 17:09:52 +01:00
return this.openedMain;
}
2021-03-17 11:52:41 +01:00
get width(): number {
2021-10-07 14:44:15 +02:00
return this.cowebsiteDom.clientWidth;
2021-03-17 11:52:41 +01:00
}
set width(width: number) {
2021-10-07 14:44:15 +02:00
this.cowebsiteDom.style.width = width + "px";
2021-03-17 11:52:41 +01:00
}
get maxWidth(): number {
let maxWidth = 75 * window.innerWidth;
if (maxWidth !== 0) {
maxWidth = Math.round(maxWidth / 100);
}
return maxWidth;
}
get height(): number {
2021-10-07 14:44:15 +02:00
return this.cowebsiteDom.clientHeight;
}
set height(height: number) {
2021-10-07 14:44:15 +02:00
this.cowebsiteDom.style.height = height + "px";
}
get maxHeight(): number {
let maxHeight = 60 * window.innerHeight;
if (maxHeight !== 0) {
maxHeight = Math.round(maxHeight / 100);
}
return maxHeight;
}
get verticalMode(): boolean {
return window.innerWidth < window.innerHeight;
}
get isFullScreen(): boolean {
2021-03-24 15:51:18 +01:00
return this.verticalMode ? this.height === window.innerHeight : this.width === window.innerWidth;
}
2020-11-16 16:15:21 +01:00
constructor() {
2021-10-07 14:44:15 +02:00
this.cowebsiteDom = HtmlUtils.getElementByIdOrFail<HTMLDivElement>(cowebsiteDomId);
2022-01-05 10:30:27 +01:00
this.gameOverlayDom = HtmlUtils.getElementByIdOrFail<HTMLDivElement>(gameOverlayDomId);
2021-10-07 14:44:15 +02:00
this.cowebsiteBufferDom = HtmlUtils.getElementByIdOrFail<HTMLDivElement>(cowebsiteBufferDomId);
this.cowebsiteAsideHolderDom = HtmlUtils.getElementByIdOrFail<HTMLDivElement>(cowebsiteAsideHolderDomId);
2022-01-05 10:30:27 +01:00
this.cowebsiteLoaderDom = HtmlUtils.getElementByIdOrFail<HTMLDivElement>(cowebsiteLoaderDomId);
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
this.loaderAnimationInterval = {
interval: undefined,
trails: undefined,
};
this.holderListeners();
this.transitionListeners();
2021-03-17 11:52:41 +01:00
2022-01-05 10:30:27 +01:00
this.resizeObserver.observe(this.cowebsiteDom);
this.resizeObserver.observe(this.gameOverlayDom);
2021-03-17 11:52:41 +01:00
2022-01-17 10:04:54 +01:00
const buttonCloseCoWebsite = HtmlUtils.getElementByIdOrFail(cowebsiteCloseButtonId);
buttonCloseCoWebsite.addEventListener("click", () => {
2022-01-05 10:30:27 +01:00
const coWebsite = this.getMainCoWebsite();
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
if (!coWebsite) {
throw new Error("Undefined main co-website on closing");
2021-10-07 14:44:15 +02:00
}
2022-02-10 11:40:36 +01:00
if (coWebsite.isClosable()) {
this.closeCoWebsite(coWebsite);
2022-01-05 10:30:27 +01:00
} else {
2022-02-10 11:40:36 +01:00
this.unloadCoWebsite(coWebsite).catch((err) => {
console.error("Cannot unload co-website on click on close button", err);
});
2022-01-05 10:30:27 +01:00
}
2021-03-17 11:52:41 +01:00
});
2021-06-03 20:05:39 +02:00
const buttonFullScreenFrame = HtmlUtils.getElementByIdOrFail(cowebsiteFullScreenButtonId);
buttonFullScreenFrame.addEventListener("click", () => {
2021-06-03 20:05:39 +02:00
buttonFullScreenFrame.blur();
2021-03-17 18:57:00 +01:00
this.fullscreen();
});
2022-01-17 10:04:54 +01:00
const buttonSwipe = HtmlUtils.getElementByIdOrFail(cowebsiteSwipeButtonId);
highlightedEmbedScreen.subscribe((value) => {
if (!value || value.type !== "cowebsite") {
buttonSwipe.style.display = "none";
return;
}
buttonSwipe.style.display = "block";
});
buttonSwipe.addEventListener("click", () => {
const highlightedEmbed = get(highlightedEmbedScreen);
if (highlightedEmbed?.type === "cowebsite") {
this.goToMain(highlightedEmbed.embed);
}
});
2021-03-17 11:52:41 +01:00
}
2022-01-05 10:30:27 +01:00
public getCoWebsiteBuffer(): HTMLDivElement {
return this.cowebsiteBufferDom;
}
2021-10-07 14:44:15 +02:00
public getDevicePixelRatio(): number {
//on chrome engines, movementX and movementY return global screens coordinates while other browser return pixels
//so on chrome-based browser we need to adjust using 'devicePixelRatio'
return window.navigator.userAgent.includes("Firefox") ? 1 : window.devicePixelRatio;
}
2022-01-05 10:30:27 +01:00
private holderListeners() {
const movecallback = (event: MouseEvent | TouchEvent) => {
let x, y;
if (event.type === "mousemove") {
x = (event as MouseEvent).movementX / this.getDevicePixelRatio();
y = (event as MouseEvent).movementY / this.getDevicePixelRatio();
} else {
const touchEvent = (event as TouchEvent).touches[0];
const last = { x: touchEvent.pageX, y: touchEvent.pageY };
const previous = this.previousTouchMoveCoordinates as TouchMoveCoordinates;
this.previousTouchMoveCoordinates = last;
x = last.x - previous.x;
y = last.y - previous.y;
}
2022-01-05 10:30:27 +01:00
if (this.verticalMode) {
const tempValue = this.height + y;
if (tempValue < this.cowebsiteAsideHolderDom.offsetHeight) {
this.height = this.cowebsiteAsideHolderDom.offsetHeight;
} else if (tempValue > this.maxHeight) {
this.height = this.maxHeight;
2022-01-05 10:30:27 +01:00
} else {
this.height = tempValue;
}
} else {
const tempValue = this.width - x;
if (tempValue < this.cowebsiteAsideHolderDom.offsetWidth) {
this.width = this.cowebsiteAsideHolderDom.offsetWidth;
} else if (tempValue > this.maxWidth) {
this.width = this.maxWidth;
2022-01-05 10:30:27 +01:00
} else {
this.width = tempValue;
}
}
this.fire();
};
this.cowebsiteAsideHolderDom.addEventListener("mousedown", (event) => {
if (this.isFullScreen) return;
2022-01-05 10:30:27 +01:00
const coWebsite = this.getMainCoWebsite();
if (!coWebsite) {
this.closeMain();
return;
}
2022-02-10 11:40:36 +01:00
const iframe = coWebsite.getIframe();
if (iframe) {
iframe.style.display = "none";
}
this.resizing = true;
document.addEventListener("mousemove", movecallback);
});
document.addEventListener("mouseup", (event) => {
if (!this.resizing || this.isFullScreen) return;
document.removeEventListener("mousemove", movecallback);
2022-01-05 10:30:27 +01:00
const coWebsite = this.getMainCoWebsite();
if (!coWebsite) {
this.resizing = false;
this.closeMain();
return;
}
2022-02-10 11:40:36 +01:00
const iframe = coWebsite.getIframe();
if (iframe) {
iframe.style.display = "flex";
}
this.resizing = false;
});
2021-03-17 11:52:41 +01:00
this.cowebsiteAsideHolderDom.addEventListener("touchstart", (event) => {
if (this.isFullScreen) return;
2022-01-05 10:30:27 +01:00
const coWebsite = this.getMainCoWebsite();
if (!coWebsite) {
this.closeMain();
return;
}
2022-02-10 11:40:36 +01:00
const iframe = coWebsite.getIframe();
if (iframe) {
iframe.style.display = "none";
}
this.resizing = true;
const touchEvent = event.touches[0];
this.previousTouchMoveCoordinates = { x: touchEvent.pageX, y: touchEvent.pageY };
document.addEventListener("touchmove", movecallback);
});
document.addEventListener("touchend", (event) => {
if (!this.resizing || this.isFullScreen) return;
this.previousTouchMoveCoordinates = null;
document.removeEventListener("touchmove", movecallback);
2022-01-05 10:30:27 +01:00
const coWebsite = this.getMainCoWebsite();
if (!coWebsite) {
this.closeMain();
this.resizing = false;
return;
}
2022-02-10 11:40:36 +01:00
const iframe = coWebsite.getIframe();
if (iframe) {
iframe.style.display = "flex";
}
this.resizing = false;
2022-01-05 10:30:27 +01:00
});
}
private transitionListeners() {
this.cowebsiteDom.addEventListener("transitionend", (event) => {
if (this.cowebsiteDom.classList.contains("loading")) {
this.fire();
}
if (this.cowebsiteDom.classList.contains("closing")) {
this.cowebsiteDom.classList.remove("closing");
if (this.loaderAnimationInterval.interval) {
clearInterval(this.loaderAnimationInterval.interval);
}
this.loaderAnimationInterval.trails = undefined;
}
});
2020-11-16 16:15:21 +01:00
}
2022-02-07 17:09:52 +01:00
public displayMain() {
const coWebsite = this.getMainCoWebsite();
if (coWebsite) {
2022-02-10 11:40:36 +01:00
const iframe = coWebsite.getIframe();
if (iframe) {
iframe.style.display = "block";
}
2022-02-07 17:09:52 +01:00
}
2022-02-10 11:40:36 +01:00
this.loadMain(coWebsite?.getWidthPercent());
2022-02-07 17:09:52 +01:00
this.openMain();
this.fire();
}
public hideMain() {
const coWebsite = this.getMainCoWebsite();
if (coWebsite) {
2022-02-10 11:40:36 +01:00
const iframe = coWebsite.getIframe();
if (iframe) {
iframe.style.display = "none";
}
2022-02-07 17:09:52 +01:00
}
this.cowebsiteDom.classList.add("closing");
this.cowebsiteDom.classList.remove("opened");
2022-02-10 19:26:46 +01:00
this.openedMain.set(iframeStates.closed);
2022-02-07 17:09:52 +01:00
this.fire();
}
2021-10-07 14:44:15 +02:00
private closeMain(): void {
2022-01-05 10:30:27 +01:00
this.toggleFullScreenIcon(true);
this.cowebsiteDom.classList.add("closing");
this.cowebsiteDom.classList.remove("opened");
2022-02-10 19:26:46 +01:00
this.openedMain.set(iframeStates.closed);
2021-10-07 14:44:15 +02:00
this.resetStyleMain();
2022-01-05 10:30:27 +01:00
this.fire();
2021-10-07 14:44:15 +02:00
}
2022-01-05 10:30:27 +01:00
private loadMain(openingWidth?: number): void {
2022-01-05 10:30:27 +01:00
this.loaderAnimationInterval.interval = setInterval(() => {
if (!this.loaderAnimationInterval.trails) {
this.loaderAnimationInterval.trails = [0, 1, 2];
}
for (let trail = 1; trail < this.loaderAnimationInterval.trails.length + 1; trail++) {
for (let state = 0; state < 4; state++) {
// const newState = this.loaderAnimationInterval.frames + trail -1;
const stateDom = this.cowebsiteLoaderDom.querySelector(
`#trail-${trail}-state-${state}`
) as SVGPolygonElement;
if (!stateDom) {
continue;
}
stateDom.style.visibility =
this.loaderAnimationInterval.trails[trail - 1] !== 0 &&
this.loaderAnimationInterval.trails[trail - 1] >= state
? "visible"
: "hidden";
}
}
this.loaderAnimationInterval.trails = this.loaderAnimationInterval.trails.map((trail) =>
trail === 3 ? 0 : trail + 1
);
}, 200);
if (!this.verticalMode && openingWidth) {
let newWidth = 50;
if (openingWidth > 100) {
newWidth = 100;
} else if (openingWidth > 1) {
newWidth = openingWidth;
}
newWidth = Math.round((newWidth * this.maxWidth) / 100);
if (newWidth < this.cowebsiteAsideHolderDom.offsetWidth) {
newWidth = this.cowebsiteAsideHolderDom.offsetWidth;
}
this.width = newWidth;
}
2022-01-05 10:30:27 +01:00
this.cowebsiteDom.classList.add("opened");
2022-02-10 19:26:46 +01:00
this.openedMain.set(iframeStates.loading);
2021-10-07 14:44:15 +02:00
}
2022-01-05 10:30:27 +01:00
2021-10-07 14:44:15 +02:00
private openMain(): void {
this.cowebsiteDom.addEventListener("transitionend", () => {
this.resizeAllIframes();
});
2022-02-10 19:26:46 +01:00
this.openedMain.set(iframeStates.opened);
2021-10-07 14:44:15 +02:00
}
public resetStyleMain() {
this.cowebsiteDom.style.width = "";
this.cowebsiteDom.style.height = "";
}
2022-01-05 10:30:27 +01:00
public getCoWebsites(): CoWebsite[] {
return get(coWebsites);
}
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
public getCoWebsiteById(coWebsiteId: string): CoWebsite | undefined {
2022-02-10 11:40:36 +01:00
return get(coWebsites).find((coWebsite: CoWebsite) => {
return coWebsite.getId() === coWebsiteId;
});
2022-01-05 10:30:27 +01:00
}
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
private getCoWebsiteByPosition(position: number): CoWebsite | undefined {
let i = 0;
return get(coWebsites).find((coWebsite: CoWebsite) => {
if (i === position) {
return coWebsite;
2021-10-07 14:44:15 +02:00
}
2022-01-05 10:30:27 +01:00
i++;
return false;
2021-10-07 14:44:15 +02:00
});
2020-11-16 16:15:21 +01:00
}
2022-01-05 10:30:27 +01:00
private getMainCoWebsite(): CoWebsite | undefined {
return get(mainCoWebsite);
}
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
private getPositionByCoWebsite(coWebsite: CoWebsite): number {
2022-02-10 11:40:36 +01:00
return get(coWebsites).findIndex((currentCoWebsite) => {
return currentCoWebsite.getId() === coWebsite.getId();
});
}
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
private getSlotByCowebsite(coWebsite: CoWebsite): HTMLDivElement | undefined {
const index = this.getPositionByCoWebsite(coWebsite);
if (index === -1) {
return undefined;
}
2021-03-17 11:52:41 +01:00
2022-01-05 10:30:27 +01:00
let id = cowebsiteSlotBaseDomId;
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
if (index === 0) {
id += "main";
2021-10-25 18:43:17 +02:00
} else {
2022-02-10 11:40:36 +01:00
id += coWebsite.getId();
2021-10-25 18:43:17 +02:00
}
2021-03-17 11:52:41 +01:00
2022-01-05 10:30:27 +01:00
const slot = HtmlUtils.getElementById<HTMLDivElement>(id);
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
return slot;
2021-10-07 14:44:15 +02:00
}
2022-01-05 10:30:27 +01:00
private setIframeOffset(coWebsite: CoWebsite) {
const coWebsiteSlot = this.getSlotByCowebsite(coWebsite);
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
if (!coWebsiteSlot) {
2021-10-07 14:44:15 +02:00
return;
}
2022-01-05 10:30:27 +01:00
const bounding = coWebsiteSlot.getBoundingClientRect();
2021-10-07 14:44:15 +02:00
2022-02-10 11:40:36 +01:00
const iframe = coWebsite.getIframe();
if (iframe) {
iframe.style.top = bounding.top + "px";
iframe.style.left = bounding.left + "px";
iframe.style.width = bounding.right - bounding.left + "px";
iframe.style.height = bounding.bottom - bounding.top + "px";
}
2022-01-05 10:30:27 +01:00
}
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
public resizeAllIframes() {
const mainCoWebsite = this.getCoWebsiteByPosition(0);
2022-02-10 11:40:36 +01:00
const mainIframe = mainCoWebsite?.getIframe();
2022-01-05 10:30:27 +01:00
const highlightEmbed = get(highlightedEmbedScreen);
2021-10-07 14:44:15 +02:00
2022-02-10 11:40:36 +01:00
get(coWebsites).forEach((coWebsite: CoWebsite) => {
const iframe = coWebsite.getIframe();
if (!iframe) {
return;
}
const notMain = !mainCoWebsite || (mainCoWebsite && mainIframe && mainIframe.id !== iframe.id);
2022-01-05 10:30:27 +01:00
const notHighlighEmbed =
!highlightEmbed ||
(highlightEmbed &&
(highlightEmbed.type !== "cowebsite" ||
2022-02-10 11:40:36 +01:00
(highlightEmbed.type === "cowebsite" && highlightEmbed.embed.getId() !== coWebsite.getId())));
2021-10-25 18:43:17 +02:00
2022-02-10 11:40:36 +01:00
if (iframe.classList.contains("main") && notMain) {
iframe.classList.remove("main");
2022-01-05 10:30:27 +01:00
}
2021-10-07 14:44:15 +02:00
2022-02-10 11:40:36 +01:00
if (iframe.classList.contains("highlighted") && notHighlighEmbed) {
iframe.classList.remove("highlighted");
iframe.classList.add("pixel");
iframe.style.top = "-1px";
iframe.style.left = "-1px";
2022-01-05 10:30:27 +01:00
}
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
if (notMain && notHighlighEmbed) {
2022-02-10 11:40:36 +01:00
iframe.classList.add("pixel");
iframe.style.top = "-1px";
iframe.style.left = "-1px";
2022-01-05 10:30:27 +01:00
}
2021-11-17 15:26:58 +01:00
2022-01-05 10:30:27 +01:00
this.setIframeOffset(coWebsite);
});
2021-10-07 14:44:15 +02:00
2022-02-10 11:40:36 +01:00
if (mainIframe) {
mainIframe.classList.add("main");
mainIframe.classList.remove("pixel");
2022-01-05 10:30:27 +01:00
}
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
if (highlightEmbed && highlightEmbed.type === "cowebsite") {
2022-02-10 11:40:36 +01:00
const highlightEmbedIframe = highlightEmbed.embed.getIframe();
if (highlightEmbedIframe) {
highlightEmbedIframe.classList.add("highlighted");
highlightEmbedIframe.classList.remove("pixel");
}
2022-01-05 10:30:27 +01:00
}
2021-10-07 14:44:15 +02:00
}
2022-01-05 10:30:27 +01:00
private removeHighlightCoWebsite(coWebsite: CoWebsite) {
const highlighted = get(highlightedEmbedScreen);
2022-02-10 11:40:36 +01:00
if (highlighted && highlighted.type === "cowebsite" && highlighted.embed.getId() === coWebsite.getId()) {
2022-01-05 10:30:27 +01:00
highlightedEmbedScreen.removeHighlight();
2021-11-17 15:26:58 +01:00
}
}
2022-01-05 10:30:27 +01:00
private removeCoWebsiteFromStack(coWebsite: CoWebsite) {
this.removeHighlightCoWebsite(coWebsite);
coWebsites.remove(coWebsite);
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
if (get(coWebsites).length < 1) {
this.closeMain();
2021-10-07 14:44:15 +02:00
}
2022-01-05 10:30:27 +01:00
2022-02-10 11:40:36 +01:00
coWebsite.unload().catch((err) => {
console.error("Cannot unload cowebsite on remove from stack");
});
2021-10-07 14:44:15 +02:00
}
2022-01-05 10:30:27 +01:00
public goToMain(coWebsite: CoWebsite) {
const mainCoWebsite = this.getMainCoWebsite();
coWebsites.remove(coWebsite);
coWebsites.add(coWebsite, 0);
if (
isMediaBreakpointDown("lg") &&
get(embedScreenLayout) === LayoutMode.Presentation &&
mainCoWebsite &&
2022-02-10 11:40:36 +01:00
mainCoWebsite.getId() !== coWebsite.getId() &&
mainCoWebsite.getState() !== "asleep"
2022-01-05 10:30:27 +01:00
) {
2022-02-10 19:26:46 +01:00
highlightedEmbedScreen.removeHighlight();
2021-10-07 14:44:15 +02:00
}
2022-01-05 10:30:27 +01:00
this.resizeAllIframes();
}
2021-10-07 14:44:15 +02:00
2022-02-10 11:40:36 +01:00
public addCoWebsiteToStore(coWebsite: CoWebsite, position: number | undefined) {
2022-01-05 10:30:27 +01:00
const coWebsitePosition = position === undefined ? get(coWebsites).length : position;
coWebsites.add(coWebsite, coWebsitePosition);
2021-10-07 14:44:15 +02:00
}
2022-02-10 11:40:36 +01:00
public generateUniqueId() {
2022-01-05 10:30:27 +01:00
let id = undefined;
do {
id = "cowebsite-iframe-" + (Math.random() + 1).toString(36).substring(7);
} while (this.getCoWebsiteById(id));
2021-10-07 14:44:15 +02:00
2022-01-05 10:30:27 +01:00
return id;
}
2021-10-07 14:44:15 +02:00
2022-02-10 11:40:36 +01:00
public loadCoWebsite(coWebsite: CoWebsite): CancelablePromise<void> {
2022-01-05 10:30:27 +01:00
if (get(coWebsitesNotAsleep).length < 1) {
coWebsites.remove(coWebsite);
coWebsites.add(coWebsite, 0);
2022-02-10 11:40:36 +01:00
this.loadMain(coWebsite.getWidthPercent());
2022-01-05 10:30:27 +01:00
}
2021-10-07 14:44:15 +02:00
2022-02-07 17:09:52 +01:00
// Check if the main is hide
2022-02-10 19:26:46 +01:00
if (this.getMainCoWebsite() && this.getMainState() === iframeStates.closed) {
2022-02-07 17:09:52 +01:00
this.displayMain();
}
2022-02-10 11:40:36 +01:00
const coWebsiteLloading = coWebsite
.load()
.then(() => {
const mainCoWebsite = this.getMainCoWebsite();
if (mainCoWebsite && mainCoWebsite.getId() === coWebsite.getId()) {
this.openMain();
2021-10-07 14:44:15 +02:00
2022-02-10 11:40:36 +01:00
setTimeout(() => {
this.fire();
}, animationTime);
}
this.resizeAllIframes();
})
.catch((err) => {
console.error("Error on co-website loading => ", err);
this.removeCoWebsiteFromStack(coWebsite);
2022-01-05 10:30:27 +01:00
});
2022-02-10 11:40:36 +01:00
return coWebsiteLloading;
2020-08-31 12:18:00 +02:00
}
2022-02-10 11:40:36 +01:00
public unloadCoWebsite(coWebsite: CoWebsite): Promise<void> {
this.removeHighlightCoWebsite(coWebsite);
2022-01-05 10:30:27 +01:00
2022-02-10 11:40:36 +01:00
return coWebsite
.unload()
.then(() => {
coWebsites.remove(coWebsite);
const mainCoWebsite = this.getMainCoWebsite();
2021-10-07 14:44:15 +02:00
2022-02-10 11:40:36 +01:00
if (mainCoWebsite) {
this.removeHighlightCoWebsite(mainCoWebsite);
this.goToMain(mainCoWebsite);
this.resizeAllIframes();
} else {
this.closeMain();
}
2022-01-05 10:30:27 +01:00
2022-02-10 11:40:36 +01:00
coWebsites.add(coWebsite, get(coWebsites).length);
})
.catch(() => {
console.error();
});
}
public closeCoWebsite(coWebsite: CoWebsite): void {
if (get(coWebsites).length === 1) {
this.fire();
}
2021-10-07 14:44:15 +02:00
this.removeCoWebsiteFromStack(coWebsite);
2022-01-17 10:04:54 +01:00
const mainCoWebsite = this.getMainCoWebsite();
2022-01-17 10:04:54 +01:00
if (mainCoWebsite) {
this.removeHighlightCoWebsite(mainCoWebsite);
this.goToMain(mainCoWebsite);
this.resizeAllIframes();
} else {
this.closeMain();
}
}
public closeCoWebsites(): void {
get(coWebsites).forEach((coWebsite: CoWebsite) => {
this.closeCoWebsite(coWebsite);
});
2021-10-07 14:44:15 +02:00
}
public getGameSize(): { width: number; height: number } {
2022-02-10 19:26:46 +01:00
if (this.getMainState() === iframeStates.closed) {
return {
width: window.innerWidth,
height: window.innerHeight,
};
}
if (!this.verticalMode) {
return {
2021-03-17 11:52:41 +01:00
width: window.innerWidth - this.width,
height: window.innerHeight,
};
} else {
return {
width: window.innerWidth,
height: window.innerHeight - this.height,
};
}
}
private fire(): void {
this._onResize.next();
waScaleManager.applyNewSize();
waScaleManager.refreshFocusOnTarget();
}
2021-03-17 18:57:00 +01:00
private fullscreen(): void {
if (this.isFullScreen) {
2022-01-05 10:30:27 +01:00
this.toggleFullScreenIcon(true);
2021-10-07 14:44:15 +02:00
this.resetStyleMain();
2021-03-24 15:51:18 +01:00
this.fire();
2021-03-17 18:57:00 +01:00
//we don't trigger a resize of the phaser game since it won't be visible anyway.
2022-01-05 10:30:27 +01:00
} else {
this.toggleFullScreenIcon(false);
this.verticalMode ? (this.height = window.innerHeight) : (this.width = window.innerWidth);
//we don't trigger a resize of the phaser game since it won't be visible anyway.
}
}
private toggleFullScreenIcon(visible: boolean) {
const openFullscreenImage = HtmlUtils.getElementByIdOrFail(cowebsiteOpenFullScreenImageId);
const closeFullScreenImage = HtmlUtils.getElementByIdOrFail(cowebsiteCloseFullScreenImageId);
if (visible) {
this.cowebsiteAsideHolderDom.style.visibility = "visible";
openFullscreenImage.style.display = "inline";
closeFullScreenImage.style.display = "none";
2021-03-17 18:57:00 +01:00
} else {
this.cowebsiteAsideHolderDom.style.visibility = "hidden";
openFullscreenImage.style.display = "none";
closeFullScreenImage.style.display = "inline";
}
}
}
export const coWebsiteManager = new CoWebsiteManager();