2021-03-06 15:26:07 +01:00
|
|
|
import {ChatEvent, isChatEvent} from "./Api/Events/ChatEvent";
|
|
|
|
import {isIframeEventWrapper} from "./Api/Events/IframeEvent";
|
|
|
|
import {isUserInputChatEvent, UserInputChatEvent} from "./Api/Events/UserInputChatEvent";
|
|
|
|
import {Subject} from "rxjs";
|
2021-03-08 18:57:59 +01:00
|
|
|
import {EnterLeaveEvent, isEnterLeaveEvent} from "./Api/Events/EnterLeaveEvent";
|
2021-03-09 16:21:14 +01:00
|
|
|
import {OpenPopupEvent} from "./Api/Events/OpenPopupEvent";
|
2021-03-09 18:05:07 +01:00
|
|
|
import {isButtonClickedEvent} from "./Api/Events/ButtonClickedEvent";
|
2021-03-05 16:50:54 +01:00
|
|
|
|
2021-03-04 19:00:00 +01:00
|
|
|
interface WorkAdventureApi {
|
|
|
|
sendChatMessage(message: string, author: string): void;
|
2021-03-05 16:50:54 +01:00
|
|
|
onChatMessage(callback: (message: string) => void): void;
|
2021-03-08 18:57:59 +01:00
|
|
|
onEnterZone(name: string, callback: () => void): void;
|
|
|
|
onLeaveZone(name: string, callback: () => void): void;
|
2021-03-09 16:21:14 +01:00
|
|
|
openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): number;
|
2021-03-04 19:00:00 +01:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:50:54 +01:00
|
|
|
declare global {
|
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var WA: WorkAdventureApi
|
|
|
|
}
|
2021-03-04 19:00:00 +01:00
|
|
|
|
2021-03-06 15:26:07 +01:00
|
|
|
type ChatMessageCallback = (message: string) => void;
|
|
|
|
|
|
|
|
const userInputChatStream: Subject<UserInputChatEvent> = new Subject();
|
2021-03-08 18:57:59 +01:00
|
|
|
const enterStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>();
|
|
|
|
const leaveStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>();
|
2021-03-09 18:05:07 +01:00
|
|
|
const popupCallbacks: Map<number, Map<number, () => void>> = new Map<number, Map<number, () => void>>();
|
|
|
|
|
2021-03-09 16:21:14 +01:00
|
|
|
let popupId = 0;
|
|
|
|
interface ButtonDescriptor {
|
|
|
|
/**
|
|
|
|
* The label of the button
|
|
|
|
*/
|
|
|
|
label: string,
|
|
|
|
/**
|
|
|
|
* The type of the button. Can be one of "normal", "primary", "success", "warning", "error", "disabled"
|
|
|
|
*/
|
|
|
|
className?: "normal"|"primary"|"success"|"warning"|"error"|"disabled",
|
|
|
|
/**
|
|
|
|
* Callback called if the button is pressed
|
|
|
|
*/
|
|
|
|
callback?: () => void,
|
|
|
|
/**
|
|
|
|
* If set to true, the popup is closed when the button is clicked
|
|
|
|
*/
|
|
|
|
closeOnClick?: boolean
|
|
|
|
}
|
2021-03-06 15:26:07 +01:00
|
|
|
|
|
|
|
|
2021-03-04 19:00:00 +01:00
|
|
|
window.WA = {
|
|
|
|
/**
|
2021-03-05 16:50:54 +01:00
|
|
|
* Send a message in the chat.
|
2021-03-04 19:00:00 +01:00
|
|
|
* Only the local user will receive this message.
|
|
|
|
*/
|
|
|
|
sendChatMessage(message: string, author: string) {
|
|
|
|
window.parent.postMessage({
|
|
|
|
'type': 'chat',
|
|
|
|
'data': {
|
|
|
|
'message': message,
|
|
|
|
'author': author
|
2021-03-05 16:50:54 +01:00
|
|
|
} as ChatEvent
|
2021-03-04 19:00:00 +01:00
|
|
|
}, '*');
|
2021-03-05 16:50:54 +01:00
|
|
|
},
|
2021-03-09 16:21:14 +01:00
|
|
|
openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): number {
|
|
|
|
popupId++;
|
|
|
|
window.parent.postMessage({
|
|
|
|
'type': 'openPopup',
|
|
|
|
'data': {
|
|
|
|
popupId,
|
|
|
|
targetObject,
|
|
|
|
message,
|
|
|
|
buttons: buttons.map((button) => {
|
|
|
|
return {
|
|
|
|
label: button.label,
|
|
|
|
className: button.className,
|
|
|
|
closeOnClick: button.closeOnClick
|
|
|
|
};
|
|
|
|
})
|
|
|
|
} as OpenPopupEvent
|
|
|
|
}, '*');
|
|
|
|
return popupId;
|
|
|
|
},
|
2021-03-05 16:50:54 +01:00
|
|
|
/**
|
|
|
|
* Listen to messages sent by the local user, in the chat.
|
|
|
|
*/
|
2021-03-06 15:26:07 +01:00
|
|
|
onChatMessage(callback: ChatMessageCallback): void {
|
|
|
|
userInputChatStream.subscribe((userInputChatEvent) => {
|
|
|
|
callback(userInputChatEvent.message);
|
|
|
|
});
|
2021-03-08 18:57:59 +01:00
|
|
|
},
|
|
|
|
onEnterZone(name: string, callback: () => void): void {
|
|
|
|
let subject = enterStreams.get(name);
|
|
|
|
if (subject === undefined) {
|
|
|
|
subject = new Subject<EnterLeaveEvent>();
|
|
|
|
enterStreams.set(name, subject);
|
|
|
|
}
|
|
|
|
subject.subscribe(callback);
|
|
|
|
},
|
|
|
|
onLeaveZone(name: string, callback: () => void): void {
|
|
|
|
let subject = leaveStreams.get(name);
|
|
|
|
if (subject === undefined) {
|
|
|
|
subject = new Subject<EnterLeaveEvent>();
|
|
|
|
leaveStreams.set(name, subject);
|
|
|
|
}
|
|
|
|
subject.subscribe(callback);
|
|
|
|
},
|
2021-03-04 19:00:00 +01:00
|
|
|
}
|
2021-03-06 15:26:07 +01:00
|
|
|
|
|
|
|
window.addEventListener('message', message => {
|
|
|
|
if (message.source !== window.parent) {
|
|
|
|
return; // Skip message in this event listener
|
|
|
|
}
|
|
|
|
|
|
|
|
const payload = message.data;
|
2021-03-08 18:57:59 +01:00
|
|
|
|
|
|
|
console.log(payload);
|
|
|
|
|
2021-03-06 15:26:07 +01:00
|
|
|
if (isIframeEventWrapper(payload)) {
|
2021-03-08 18:57:59 +01:00
|
|
|
const payloadData = payload.data;
|
|
|
|
if (payload.type === 'userInputChat' && isUserInputChatEvent(payloadData)) {
|
|
|
|
userInputChatStream.next(payloadData);
|
|
|
|
} else if (payload.type === 'enterEvent' && isEnterLeaveEvent(payloadData)) {
|
|
|
|
enterStreams.get(payloadData.name)?.next();
|
|
|
|
} else if (payload.type === 'leaveEvent' && isEnterLeaveEvent(payloadData)) {
|
|
|
|
leaveStreams.get(payloadData.name)?.next();
|
2021-03-09 18:05:07 +01:00
|
|
|
} else if (payload.type === 'buttonClickedEvent' && isButtonClickedEvent(payloadData)) {
|
|
|
|
const callback = popupCallbacks.get(payloadData.popupId)?.get(payloadData.buttonId);
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
2021-03-06 15:26:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...
|
|
|
|
});
|