workadventure/front/src/Components/Menu/Menu.svelte

205 lines
6.2 KiB
Svelte
Raw Normal View History

2021-07-12 17:39:16 +02:00
<script lang="typescript">
2021-12-06 16:12:37 +01:00
import { fly } from "svelte/transition";
2021-08-09 14:49:17 +02:00
import SettingsSubMenu from "./SettingsSubMenu.svelte";
import ProfileSubMenu from "./ProfileSubMenu.svelte";
import AboutRoomSubMenu from "./AboutRoomSubMenu.svelte";
import GlobalMessageSubMenu from "./GlobalMessagesSubMenu.svelte";
import ContactSubMenu from "./ContactSubMenu.svelte";
2021-12-06 16:12:37 +01:00
import CustomSubMenu from "./CustomSubMenu.svelte";
import GuestSubMenu from "./GuestSubMenu.svelte";
import {
checkSubMenuToShow,
customMenuIframe,
menuVisiblilityStore,
SubMenusInterface,
2021-12-06 16:12:37 +01:00
subMenusStore,
} from "../../Stores/MenuStore";
2022-01-21 17:06:03 +01:00
import type { MenuItem } from "../../Stores/MenuStore";
2021-12-06 16:12:37 +01:00
import { onDestroy, onMount } from "svelte";
import type { Unsubscriber } from "svelte/store";
import { sendMenuClickedEvent } from "../../Api/iframe/Ui/MenuItem";
2022-01-21 17:06:03 +01:00
import LL from "../../i18n/i18n-svelte";
2022-01-21 17:06:03 +01:00
let activeSubMenu: MenuItem = $subMenusStore[0];
let activeComponent: typeof ProfileSubMenu | typeof CustomSubMenu = ProfileSubMenu;
2021-12-06 16:12:37 +01:00
let props: { url: string; allowApi: boolean };
let unsubscriberSubMenuStore: Unsubscriber;
onMount(() => {
unsubscriberSubMenuStore = subMenusStore.subscribe(() => {
2022-01-21 17:06:03 +01:00
if (!$subMenusStore.includes(activeSubMenu)) {
switchMenu($subMenusStore[0]);
}
2021-12-06 16:12:37 +01:00
});
checkSubMenuToShow();
2022-01-21 17:06:03 +01:00
switchMenu($subMenusStore[0]);
2021-12-06 16:12:37 +01:00
});
2021-07-12 17:39:16 +02:00
onDestroy(() => {
2021-12-06 16:12:37 +01:00
if (unsubscriberSubMenuStore) {
unsubscriberSubMenuStore();
}
2021-12-06 16:12:37 +01:00
});
2022-01-21 17:06:03 +01:00
function switchMenu(menu: MenuItem) {
if (menu.type === "translated") {
activeSubMenu = menu;
2022-01-21 17:06:03 +01:00
switch (menu.key) {
case SubMenusInterface.settings:
activeComponent = SettingsSubMenu;
break;
case SubMenusInterface.profile:
activeComponent = ProfileSubMenu;
break;
case SubMenusInterface.invite:
activeComponent = GuestSubMenu;
break;
case SubMenusInterface.aboutRoom:
activeComponent = AboutRoomSubMenu;
break;
case SubMenusInterface.globalMessages:
activeComponent = GlobalMessageSubMenu;
break;
case SubMenusInterface.contact:
activeComponent = ContactSubMenu;
break;
}
2022-01-21 17:06:03 +01:00
} else {
const customMenu = customMenuIframe.get(menu.label);
if (customMenu !== undefined) {
2022-02-07 17:25:55 +01:00
activeSubMenu = menu;
2022-01-21 17:06:03 +01:00
props = { url: customMenu.url, allowApi: customMenu.allowApi };
activeComponent = CustomSubMenu;
} else {
sendMenuClickedEvent(menu.label);
menuVisiblilityStore.set(false);
}
}
2021-07-12 17:39:16 +02:00
}
2021-07-13 12:00:09 +02:00
2021-08-09 14:49:17 +02:00
function closeMenu() {
menuVisiblilityStore.set(false);
}
2021-12-19 16:01:51 +01:00
2021-12-06 16:12:37 +01:00
function onKeyDown(e: KeyboardEvent) {
if (e.key === "Escape") {
2021-08-09 14:49:17 +02:00
closeMenu();
}
2021-07-13 12:00:09 +02:00
}
2021-12-19 16:01:51 +01:00
2022-01-21 17:06:03 +01:00
function translateMenuName(menu: MenuItem) {
if (menu.type === "scripting") {
return menu.label;
}
// Bypass the proxy of typesafe for getting the menu name : https://github.com/ivanhofer/typesafe-i18n/issues/156
const getMenuName = $LL.menu.sub[menu.key];
2021-12-19 16:01:51 +01:00
2022-01-21 17:06:03 +01:00
return getMenuName();
2021-12-19 16:01:51 +01:00
}
2021-07-12 17:39:16 +02:00
</script>
2021-12-06 16:12:37 +01:00
<svelte:window on:keydown={onKeyDown} />
2021-08-09 14:49:17 +02:00
<div class="menu-container-main">
2021-12-06 16:12:37 +01:00
<div class="menu-nav-sidebar nes-container is-rounded" transition:fly={{ x: -1000, duration: 500 }}>
2022-01-21 17:06:03 +01:00
<h2>{$LL.menu.title()}</h2>
2021-07-12 17:39:16 +02:00
<nav>
{#each $subMenusStore as submenu}
2021-12-06 16:12:37 +01:00
<button
type="button"
class="nes-btn {activeSubMenu === submenu ? 'is-disabled' : ''}"
on:click|preventDefault={() => switchMenu(submenu)}
>
2021-12-19 16:01:51 +01:00
{translateMenuName(submenu)}
</button>
2021-08-09 14:49:17 +02:00
{/each}
2021-07-12 17:39:16 +02:00
</nav>
2021-08-09 14:49:17 +02:00
</div>
2021-12-06 16:12:37 +01:00
<div class="menu-submenu-container nes-container is-rounded" transition:fly={{ y: -1000, duration: 500 }}>
<button type="button" class="nes-btn is-error close" on:click={closeMenu}>&times</button>
2021-12-19 16:01:51 +01:00
<h2>{translateMenuName(activeSubMenu)}</h2>
2021-12-06 16:12:37 +01:00
<svelte:component this={activeComponent} {...props} />
2021-08-09 14:49:17 +02:00
</div>
</div>
2021-07-12 17:39:16 +02:00
<style lang="scss">
2022-01-05 10:27:40 +01:00
@import "../../../style/breakpoints.scss";
2021-12-06 16:12:37 +01:00
.nes-container {
padding: 5px;
2021-07-12 17:39:16 +02:00
}
2021-12-06 16:12:37 +01:00
div.menu-container-main {
--size-first-columns-grid: 200px;
2021-08-09 14:49:17 +02:00
2021-12-06 16:12:37 +01:00
font-family: "Press Start 2P";
pointer-events: auto;
height: 80%;
width: 75%;
2022-01-05 10:27:40 +01:00
top: 4%;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
2021-12-06 16:12:37 +01:00
2022-01-05 10:27:40 +01:00
position: absolute;
z-index: 900;
2021-12-06 16:12:37 +01:00
display: grid;
grid-template-columns: var(--size-first-columns-grid) calc(100% - var(--size-first-columns-grid));
grid-template-rows: 100%;
h2 {
text-align: center;
margin-bottom: 20px;
}
div.menu-nav-sidebar {
background-color: #333333;
color: whitesmoke;
nav button {
width: calc(100% - 10px);
margin-bottom: 10px;
}
}
2021-07-12 17:39:16 +02:00
2021-12-06 16:12:37 +01:00
div.menu-submenu-container {
background-color: #333333;
color: whitesmoke;
2021-12-06 16:12:37 +01:00
.nes-btn.is-error.close {
position: absolute;
top: -20px;
right: -20px;
}
}
2021-08-09 14:49:17 +02:00
}
2021-08-17 14:44:43 +02:00
2022-01-05 10:27:40 +01:00
@include media-breakpoint-up(md) {
2021-12-06 16:12:37 +01:00
div.menu-container-main {
--size-first-columns-grid: 120px;
height: 70%;
top: 55px;
2022-01-05 10:27:40 +01:00
width: 95%;
2021-12-06 16:12:37 +01:00
font-size: 0.5em;
div.menu-nav-sidebar {
overflow-y: auto;
}
div.menu-submenu-container {
.nes-btn.is-error.close {
position: absolute;
top: -35px;
right: 0;
}
}
}
2021-08-17 14:44:43 +02:00
}
2021-12-06 16:12:37 +01:00
</style>