workadventure/front/src/Api/iframe/nav.ts

106 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-10-12 10:38:49 +02:00
import { IframeApiContribution, sendToWorkadventure, queryWorkadventure } from "./IframeApiContribution";
2021-10-25 18:42:51 +02:00
export class CoWebsite {
constructor(private readonly id: string, public readonly position: number) {}
close() {
return queryWorkadventure({
type: "closeCoWebsite",
data: this.id,
});
}
}
export class WorkadventureNavigationCommands extends IframeApiContribution<WorkadventureNavigationCommands> {
callbacks = [];
openTab(url: string): void {
sendToWorkadventure({
type: "openTab",
data: {
url,
},
});
}
goToPage(url: string): void {
sendToWorkadventure({
type: "goToPage",
data: {
url,
},
});
}
goToRoom(url: string): void {
2021-06-17 11:32:59 +02:00
sendToWorkadventure({
type: "loadPage",
data: {
url,
},
2021-06-17 11:32:59 +02:00
});
}
2021-10-12 10:38:49 +02:00
/**
* @deprecated Use openCoWebsite instead
*/
openCoWebSite(url: string, allowApi?: boolean, allowPolicy?: string) {
return queryWorkadventure({
type: "openCoWebsite",
data: {
url,
allowApi,
allowPolicy,
2021-10-12 10:38:49 +02:00
position: undefined,
},
});
}
2021-10-25 18:42:51 +02:00
async openCoWebsite(url: string, allowApi?: boolean, allowPolicy?: string, position?: number): Promise<CoWebsite> {
const result = await queryWorkadventure({
2021-10-12 10:38:49 +02:00
type: "openCoWebsite",
data: {
url,
allowApi,
allowPolicy,
position,
},
});
2021-10-25 18:42:51 +02:00
return new CoWebsite(result.id, result.position);
2021-10-12 10:38:49 +02:00
}
2021-10-25 18:42:51 +02:00
async getCoWebsites(): Promise<CoWebsite[]> {
const result = await queryWorkadventure({
2021-10-12 10:38:49 +02:00
type: "getCoWebsites",
data: undefined
});
2021-10-25 18:42:51 +02:00
return result.map((cowebsiteEvent) => new CoWebsite(cowebsiteEvent.id, cowebsiteEvent.position));
2021-10-12 10:38:49 +02:00
}
/**
* @deprecated Use closeCoWebsites instead to close all co-websites
*/
closeCoWebSite() {
return queryWorkadventure({
type: "closeCoWebsites",
data: undefined,
});
}
closeCoWebsite(coWebsiteId: string) {
return queryWorkadventure({
type: "closeCoWebsite",
data: coWebsiteId,
});
}
closeCoWebsites() {
return queryWorkadventure({
type: "closeCoWebsites",
data: undefined,
});
}
}
export default new WorkadventureNavigationCommands();