submenu closes when click outside

This commit is contained in:
Ali Gator 2022-12-10 10:45:34 +01:00
parent c4db938d8e
commit 9b83b88c54
4 changed files with 46 additions and 30 deletions

View File

@ -1,9 +1,9 @@
<template> <template>
<div id="main-menu"> <div id="main-menu">
<MenuGeneralOptions :active="active" @update-active="updateActive" /> <MenuGeneralOptions />
<MenuCellProperties :active="active" @update-active="updateActive" /> <MenuCellProperties />
<MenuElementaryCA :active="active" @update-active="updateActive" /> <MenuElementaryCA />
<Menu2dCA :active="active" @update-active="updateActive" /> <Menu2dCA />
</div> </div>
</template> </template>
@ -20,21 +20,6 @@
MenuElementaryCA, MenuElementaryCA,
Menu2dCA, Menu2dCA,
}, },
data() {
return {
active: "",
};
},
methods: {
// set the opened submenu. If already active, reset it so it can close
updateActive(menu) {
if (menu === this.active) {
this.active = "";
} else {
this.active = menu;
}
},
},
}; };
</script> </script>
@ -43,7 +28,6 @@
display: flex; display: flex;
flex-direction: row; flex-direction: row;
width: 100%; width: 100%;
flex: 1; flex: 1;
} }

View File

@ -38,7 +38,7 @@
import { globalStore } from "../stores/index.js"; import { globalStore } from "../stores/index.js";
import MenuRow from "./MenuRow.vue"; import MenuRow from "./MenuRow.vue";
export default { export default {
name: "MainMenu", name: "MenuCellProperties",
components: { components: {
MenuRow, MenuRow,
}, },

View File

@ -1,15 +1,17 @@
<template> <template>
<div class="menu-row"> <div class="menu-row">
<h2 :id="rowTitle" @click="$emit('update-active', rowTitle)"> <h2 :id="rowTitle" @click.stop="storeActiveSubMenu">
{{ rowTitle }} {{ rowTitle }}
</h2> </h2>
<div v-if="isActive" class="menu-row-content"> <div v-if="isActive" ref="content" class="menu-row-content">
<slot /> <slot />
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import { mapActions, mapState } from "pinia";
import { globalStore } from "../stores/index.js";
export default { export default {
name: "MenuRow", name: "MenuRow",
props: { props: {
@ -17,15 +19,37 @@
type: String, type: String,
default: "", default: "",
}, },
active: {
type: String,
default: "",
}, },
},
emits: ["update-active"],
computed: { computed: {
...mapState(globalStore, ["activeSubMenu"]),
isActive() { isActive() {
return this.rowTitle == this.active; return this.rowTitle == this.activeSubMenu;
},
},
beforeUnmount() {
window.removeEventListener("click", this.onWindowClick);
},
methods: {
...mapActions(globalStore, ["setActiveSubMenu"]),
onKeyDown: function (event) {
// escape
if (event.keyCode == 27) {
this.setActiveSubMenu("");
}
},
storeActiveSubMenu() {
window.addEventListener("click", this.onWindowClick);
this.setActiveSubMenu(this.rowTitle);
},
// hides submenu when click is detected outside from it
onWindowClick(event) {
const form = this.$refs.content;
if (form != null) {
if (!form.contains(event.target)) {
this.setActiveSubMenu("");
}
return;
}
}, },
}, },
}; };

View File

@ -34,6 +34,7 @@ export const globalStore = defineStore("globalStore", {
draw2dLast: false, draw2dLast: false,
reset: false, reset: false,
canDraw: true, canDraw: true,
activeSubMenu: "",
}; };
}, },
actions: { actions: {
@ -46,14 +47,17 @@ export const globalStore = defineStore("globalStore", {
); );
}, },
toggleDraw1d() { toggleDraw1d() {
this.setActiveSubMenu("");
this.draw1d = true; this.draw1d = true;
}, },
toggleDraw2d() { toggleDraw2d() {
this.setActiveSubMenu("");
this.toggleStop(); this.toggleStop();
this.canDraw = true; this.canDraw = true;
this.draw2d = true; this.draw2d = true;
}, },
toggleDraw2dLast() { toggleDraw2dLast() {
this.setActiveSubMenu("");
this.toggleStop(); this.toggleStop();
this.canDraw = true; this.canDraw = true;
this.draw2dLast = true; this.draw2dLast = true;
@ -68,5 +72,9 @@ export const globalStore = defineStore("globalStore", {
this.draw2dLast = false; this.draw2dLast = false;
this.canDraw = false; this.canDraw = false;
}, },
setActiveSubMenu(data) {
if (this.activeSubMenu == data) this.activeSubMenu = "";
else this.activeSubMenu = data;
},
}, },
}); });