2021-08-24 17:35:06 +02:00
|
|
|
import { get, writable } from "svelte/store";
|
2021-07-29 18:02:36 +02:00
|
|
|
import Timeout = NodeJS.Timeout;
|
2021-08-26 12:01:07 +02:00
|
|
|
import { userIsAdminStore } from "./GameStore";
|
|
|
|
import { CONTACT_URL } from "../Enum/EnvironmentVariable";
|
2021-09-16 18:12:51 +02:00
|
|
|
import { analyticsClient } from "../Administration/AnalyticsClient";
|
2022-01-21 17:06:03 +01:00
|
|
|
import type { Translation } from "../i18n/i18n-types";
|
2021-05-12 18:32:55 +02:00
|
|
|
|
2021-08-09 14:49:17 +02:00
|
|
|
export const menuIconVisiblilityStore = writable(false);
|
|
|
|
export const menuVisiblilityStore = writable(false);
|
2021-09-15 11:50:25 +02:00
|
|
|
menuVisiblilityStore.subscribe((value) => {
|
|
|
|
if (value) analyticsClient.openedMenu();
|
2021-09-16 18:12:51 +02:00
|
|
|
});
|
2021-08-09 14:49:17 +02:00
|
|
|
export const menuInputFocusStore = writable(false);
|
2021-09-05 18:17:49 +02:00
|
|
|
export const userIsConnected = writable(false);
|
2021-07-29 18:02:36 +02:00
|
|
|
|
|
|
|
let warningContainerTimeout: Timeout | null = null;
|
|
|
|
function createWarningContainerStore() {
|
|
|
|
const { subscribe, set } = writable<boolean>(false);
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
|
|
|
activateWarningContainer() {
|
|
|
|
set(true);
|
|
|
|
if (warningContainerTimeout) clearTimeout(warningContainerTimeout);
|
|
|
|
warningContainerTimeout = setTimeout(() => {
|
|
|
|
set(false);
|
|
|
|
warningContainerTimeout = null;
|
|
|
|
}, 120000);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const warningContainerStore = createWarningContainerStore();
|
2021-08-11 14:07:34 +02:00
|
|
|
|
|
|
|
export enum SubMenusInterface {
|
2022-01-21 17:06:03 +01:00
|
|
|
settings = "settings",
|
|
|
|
profile = "profile",
|
|
|
|
invite = "invite",
|
|
|
|
aboutRoom = "credit",
|
|
|
|
globalMessages = "globalMessages",
|
|
|
|
contact = "contact",
|
2021-08-11 14:07:34 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 17:06:03 +01:00
|
|
|
type MenuKeys = keyof Translation["menu"]["sub"];
|
|
|
|
|
|
|
|
interface TranslatedMenu {
|
|
|
|
type: "translated";
|
|
|
|
key: MenuKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A menu item from the scripting API
|
|
|
|
*/
|
|
|
|
interface ScriptingMenu {
|
|
|
|
type: "scripting";
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type MenuItem = TranslatedMenu | ScriptingMenu;
|
|
|
|
|
2021-08-11 14:07:34 +02:00
|
|
|
function createSubMenusStore() {
|
2022-01-21 17:06:03 +01:00
|
|
|
const { subscribe, update } = writable<MenuItem[]>([
|
|
|
|
{
|
|
|
|
type: "translated",
|
|
|
|
key: SubMenusInterface.profile,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "translated",
|
|
|
|
key: SubMenusInterface.globalMessages,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "translated",
|
|
|
|
key: SubMenusInterface.contact,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "translated",
|
|
|
|
key: SubMenusInterface.settings,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "translated",
|
|
|
|
key: SubMenusInterface.invite,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "translated",
|
|
|
|
key: SubMenusInterface.aboutRoom,
|
|
|
|
},
|
2021-08-11 14:07:34 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
2022-01-21 17:06:03 +01:00
|
|
|
addTranslatedMenu(menuCommand: MenuKeys) {
|
|
|
|
update((menuList) => {
|
|
|
|
if (!menuList.find((menu) => menu.type === "translated" && menu.key === menuCommand)) {
|
|
|
|
menuList.push({
|
|
|
|
type: "translated",
|
|
|
|
key: menuCommand,
|
|
|
|
});
|
2021-08-11 14:07:34 +02:00
|
|
|
}
|
|
|
|
return menuList;
|
|
|
|
});
|
|
|
|
},
|
2022-01-21 17:06:03 +01:00
|
|
|
removeTranslatedMenu(menuCommand: MenuKeys) {
|
|
|
|
update((menuList) => {
|
|
|
|
const index = menuList.findIndex((menu) => menu.type === "translated" && menu.key === menuCommand);
|
|
|
|
if (index !== -1) {
|
|
|
|
menuList.splice(index, 1);
|
|
|
|
}
|
|
|
|
return menuList;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
addScriptingMenu(menuCommand: string) {
|
|
|
|
update((menuList) => {
|
|
|
|
if (!menuList.find((menu) => menu.type === "scripting" && menu.label === menuCommand)) {
|
|
|
|
menuList.push({
|
|
|
|
type: "scripting",
|
|
|
|
label: menuCommand,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return menuList;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
removeScriptingMenu(menuCommand: string) {
|
|
|
|
update((menuList) => {
|
|
|
|
const index = menuList.findIndex((menu) => menu.type === "scripting" && menu.label === menuCommand);
|
2021-08-11 14:07:34 +02:00
|
|
|
if (index !== -1) {
|
|
|
|
menuList.splice(index, 1);
|
|
|
|
}
|
|
|
|
return menuList;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const subMenusStore = createSubMenusStore();
|
2021-08-26 12:01:07 +02:00
|
|
|
|
2021-09-21 20:24:53 +02:00
|
|
|
export const contactPageStore = writable<string | undefined>(CONTACT_URL);
|
|
|
|
|
2021-09-08 15:43:46 +02:00
|
|
|
export function checkSubMenuToShow() {
|
2022-01-21 17:06:03 +01:00
|
|
|
subMenusStore.removeTranslatedMenu(SubMenusInterface.globalMessages);
|
|
|
|
subMenusStore.removeTranslatedMenu(SubMenusInterface.contact);
|
2021-09-21 20:24:53 +02:00
|
|
|
|
|
|
|
if (get(userIsAdminStore)) {
|
2022-01-21 17:06:03 +01:00
|
|
|
subMenusStore.addTranslatedMenu(SubMenusInterface.globalMessages);
|
2021-08-26 12:01:07 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 20:24:53 +02:00
|
|
|
if (get(contactPageStore) !== undefined) {
|
2022-01-21 17:06:03 +01:00
|
|
|
subMenusStore.addTranslatedMenu(SubMenusInterface.contact);
|
2021-08-26 12:01:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:34:03 +02:00
|
|
|
export const customMenuIframe = new Map<string, { url: string; allowApi: boolean }>();
|
2021-08-24 17:35:06 +02:00
|
|
|
|
|
|
|
export function handleMenuRegistrationEvent(
|
|
|
|
menuName: string,
|
|
|
|
iframeUrl: string | undefined = undefined,
|
2021-08-27 10:34:03 +02:00
|
|
|
source: string | undefined = undefined,
|
|
|
|
options: { allowApi: boolean }
|
2021-08-24 17:35:06 +02:00
|
|
|
) {
|
2022-01-21 17:06:03 +01:00
|
|
|
if (get(subMenusStore).find((item) => item.type === "scripting" && item.label === menuName)) {
|
2021-08-24 17:35:06 +02:00
|
|
|
console.warn("The menu " + menuName + " already exist.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:06:03 +01:00
|
|
|
subMenusStore.addScriptingMenu(menuName);
|
2021-08-24 17:35:06 +02:00
|
|
|
|
|
|
|
if (iframeUrl !== undefined) {
|
|
|
|
const url = new URL(iframeUrl, source);
|
2021-08-27 10:34:03 +02:00
|
|
|
customMenuIframe.set(menuName, { url: url.toString(), allowApi: options.allowApi });
|
2021-08-24 17:35:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleMenuUnregisterEvent(menuName: string) {
|
2022-01-21 17:06:03 +01:00
|
|
|
subMenusStore.removeScriptingMenu(menuName);
|
2021-08-24 17:35:06 +02:00
|
|
|
customMenuIframe.delete(menuName);
|
|
|
|
}
|