2021-06-21 18:41:41 +02:00
|
|
|
import { registeredCallbacks } from "./Api/iframe/registeredCallbacks";
|
2021-06-18 17:22:56 +02:00
|
|
|
import {
|
|
|
|
IframeResponseEvent,
|
|
|
|
IframeResponseEventMap,
|
|
|
|
isIframeResponseEventWrapper,
|
|
|
|
TypedMessageEvent
|
|
|
|
} from "./Api/Events/IframeEvent";
|
|
|
|
import chat from "./Api/iframe/chat";
|
2021-06-21 18:39:02 +02:00
|
|
|
import type { IframeCallback } from './Api/iframe/IframeApiContribution';
|
|
|
|
import nav from "./Api/iframe/nav";
|
2021-06-21 18:41:41 +02:00
|
|
|
import controls from "./Api/iframe/controls";
|
2021-06-21 18:39:02 +02:00
|
|
|
import ui from "./Api/iframe/ui";
|
2021-06-21 18:41:41 +02:00
|
|
|
import sound from "./Api/iframe/sound";
|
|
|
|
import room from "./Api/iframe/room";
|
2021-06-23 11:32:11 +02:00
|
|
|
import player from "./Api/iframe/player";
|
2021-06-21 18:39:02 +02:00
|
|
|
import type { ButtonDescriptor } from "./Api/iframe/Ui/ButtonDescriptor";
|
|
|
|
import type { Popup } from "./Api/iframe/Ui/Popup";
|
2021-06-21 18:41:41 +02:00
|
|
|
import type { Sound } from "./Api/iframe/Sound/Sound";
|
2021-06-23 12:02:29 +02:00
|
|
|
import {sendToWorkadventure} from "./Api/iframe/IframeApiContribution";
|
2021-06-18 17:22:56 +02:00
|
|
|
|
|
|
|
const wa = {
|
|
|
|
ui,
|
|
|
|
nav,
|
|
|
|
controls,
|
|
|
|
chat,
|
|
|
|
sound,
|
|
|
|
room,
|
2021-06-23 11:32:11 +02:00
|
|
|
player,
|
2021-06-18 17:22:56 +02:00
|
|
|
|
|
|
|
// All methods below are deprecated and should not be used anymore.
|
|
|
|
// They are kept here for backward compatibility.
|
2021-03-05 16:50:54 +01:00
|
|
|
|
2021-03-09 16:21:14 +01:00
|
|
|
/**
|
2021-06-18 17:22:56 +02:00
|
|
|
* @deprecated Use WA.chat.sendChatMessage instead
|
2021-03-09 16:21:14 +01:00
|
|
|
*/
|
2021-06-18 17:22:56 +02:00
|
|
|
sendChatMessage(message: string, author: string): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.sendChatMessage is deprecated. Please use WA.chat.sendChatMessage instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
chat.sendChatMessage(message, author);
|
2021-05-27 17:06:39 +02:00
|
|
|
},
|
|
|
|
|
2021-03-04 19:00:00 +01:00
|
|
|
/**
|
2021-06-18 17:22:56 +02:00
|
|
|
* @deprecated Use WA.chat.disablePlayerControls instead
|
2021-03-04 19:00:00 +01:00
|
|
|
*/
|
2021-05-10 12:14:31 +02:00
|
|
|
disablePlayerControls(): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.disablePlayerControls is deprecated. Please use WA.controls.disablePlayerControls instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
controls.disablePlayerControls();
|
2021-03-12 16:39:29 +01:00
|
|
|
},
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.controls.restorePlayerControls instead
|
|
|
|
*/
|
2021-05-10 12:14:31 +02:00
|
|
|
restorePlayerControls(): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.restorePlayerControls is deprecated. Please use WA.controls.restorePlayerControls instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
controls.restorePlayerControls();
|
2021-03-12 16:39:29 +01:00
|
|
|
},
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.ui.displayBubble instead
|
|
|
|
*/
|
2021-05-09 21:30:29 +02:00
|
|
|
displayBubble(): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.displayBubble is deprecated. Please use WA.ui.displayBubble instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
ui.displayBubble();
|
2021-03-12 16:39:29 +01:00
|
|
|
},
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.ui.removeBubble instead
|
|
|
|
*/
|
2021-05-09 21:30:29 +02:00
|
|
|
removeBubble(): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.removeBubble is deprecated. Please use WA.ui.removeBubble instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
ui.removeBubble();
|
2021-03-12 16:39:29 +01:00
|
|
|
},
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.nav.openTab instead
|
|
|
|
*/
|
2021-05-21 16:24:48 +02:00
|
|
|
openTab(url: string): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.openTab is deprecated. Please use WA.nav.openTab instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
nav.openTab(url);
|
2021-05-21 16:24:48 +02:00
|
|
|
},
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.sound.loadSound instead
|
|
|
|
*/
|
2021-06-21 18:39:02 +02:00
|
|
|
loadSound(url: string): Sound {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.loadSound is deprecated. Please use WA.sound.loadSound instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
return sound.loadSound(url);
|
2021-04-21 16:47:19 +02:00
|
|
|
},
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.nav.goToPage instead
|
|
|
|
*/
|
2021-06-21 18:39:02 +02:00
|
|
|
goToPage(url: string): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.goToPage is deprecated. Please use WA.nav.goToPage instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
nav.goToPage(url);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated Use WA.nav.goToRoom instead
|
|
|
|
*/
|
|
|
|
goToRoom(url: string): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.goToRoom is deprecated. Please use WA.nav.goToRoom instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
nav.goToRoom(url);
|
2021-05-21 16:24:48 +02:00
|
|
|
},
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.nav.openCoWebSite instead
|
|
|
|
*/
|
2021-05-10 21:27:17 +02:00
|
|
|
openCoWebSite(url: string): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.openCoWebSite is deprecated. Please use WA.nav.openCoWebSite instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
nav.openCoWebSite(url);
|
2021-03-25 17:12:53 +01:00
|
|
|
},
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.nav.closeCoWebSite instead
|
|
|
|
*/
|
2021-05-09 21:30:29 +02:00
|
|
|
closeCoWebSite(): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.closeCoWebSite is deprecated. Please use WA.nav.closeCoWebSite instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
nav.closeCoWebSite();
|
2021-03-25 17:12:53 +01:00
|
|
|
},
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.controls.restorePlayerControls instead
|
|
|
|
*/
|
2021-03-09 18:51:30 +01:00
|
|
|
openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.openPopup is deprecated. Please use WA.ui.openPopup instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
return ui.openPopup(targetObject, message, buttons);
|
2021-04-27 11:59:22 +02:00
|
|
|
},
|
2021-03-05 16:50:54 +01:00
|
|
|
/**
|
2021-06-18 17:22:56 +02:00
|
|
|
* @deprecated Use WA.chat.onChatMessage instead
|
2021-03-05 16:50:54 +01:00
|
|
|
*/
|
2021-06-18 17:22:56 +02:00
|
|
|
onChatMessage(callback: (message: string) => void): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.onChatMessage is deprecated. Please use WA.chat.onChatMessage instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
chat.onChatMessage(callback);
|
2021-03-08 18:57:59 +01:00
|
|
|
},
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.room.onEnterZone instead
|
|
|
|
*/
|
2021-03-08 18:57:59 +01:00
|
|
|
onEnterZone(name: string, callback: () => void): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.onEnterZone is deprecated. Please use WA.room.onEnterZone instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
room.onEnterZone(name, callback);
|
2021-03-08 18:57:59 +01:00
|
|
|
},
|
2021-06-18 17:22:56 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use WA.room.onLeaveZone instead
|
|
|
|
*/
|
2021-03-08 18:57:59 +01:00
|
|
|
onLeaveZone(name: string, callback: () => void): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
console.warn('Method WA.onLeaveZone is deprecated. Please use WA.room.onLeaveZone instead');
|
2021-06-18 17:22:56 +02:00
|
|
|
room.onLeaveZone(name, callback);
|
2021-03-08 18:57:59 +01:00
|
|
|
},
|
2021-06-18 17:22:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export type WorkAdventureApi = typeof wa;
|
2021-03-04 19:00:00 +01:00
|
|
|
|
2021-03-05 16:50:54 +01:00
|
|
|
declare global {
|
2021-03-25 17:12:53 +01:00
|
|
|
|
2021-05-25 13:47:41 +02:00
|
|
|
interface Window {
|
|
|
|
WA: WorkAdventureApi
|
|
|
|
}
|
|
|
|
let WA: WorkAdventureApi
|
2021-03-04 19:00:00 +01:00
|
|
|
}
|
2021-03-06 15:26:07 +01:00
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
window.WA = wa;
|
2021-05-28 01:44:38 +02:00
|
|
|
|
2021-05-28 02:28:11 +02:00
|
|
|
window.addEventListener('message', <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
|
2021-03-06 15:26:07 +01:00
|
|
|
if (message.source !== window.parent) {
|
|
|
|
return; // Skip message in this event listener
|
|
|
|
}
|
|
|
|
const payload = message.data;
|
2021-05-09 21:30:29 +02:00
|
|
|
console.debug(payload);
|
2021-03-08 18:57:59 +01:00
|
|
|
|
2021-05-09 21:30:29 +02:00
|
|
|
if (isIframeResponseEventWrapper(payload)) {
|
2021-03-08 18:57:59 +01:00
|
|
|
const payloadData = payload.data;
|
2021-05-25 13:47:41 +02:00
|
|
|
|
2021-05-28 02:28:11 +02:00
|
|
|
const callback = registeredCallbacks[payload.type] as IframeCallback<T> | undefined
|
|
|
|
if (callback?.typeChecker(payloadData)) {
|
|
|
|
callback?.callback(payloadData)
|
2021-03-06 15:26:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...
|
|
|
|
});
|