calckey/src/client/app/desktop/views/components/context-menu.vue

91 lines
1.8 KiB
Vue
Raw Normal View History

2018-02-18 03:35:18 +00:00
<template>
2018-06-08 02:46:45 +00:00
<div class="context-menu" @contextmenu.prevent="() => {}">
2018-02-20 16:39:51 +00:00
<x-menu :menu="menu" @x="click"/>
2018-02-18 03:35:18 +00:00
</div>
</template>
<script lang="ts">
import Vue from 'vue';
2019-01-18 04:06:11 +00:00
import anime from 'animejs';
2018-02-18 03:35:18 +00:00
import contains from '../../../common/scripts/contains';
2018-02-20 16:39:51 +00:00
import XMenu from './context-menu.menu.vue';
2018-02-18 03:35:18 +00:00
export default Vue.extend({
2018-02-19 07:18:18 +00:00
components: {
2018-02-20 16:39:51 +00:00
XMenu
2018-02-19 07:18:18 +00:00
},
2018-02-18 03:35:18 +00:00
props: ['x', 'y', 'menu'],
mounted() {
this.$nextTick(() => {
2018-06-08 02:46:45 +00:00
const width = this.$el.offsetWidth;
const height = this.$el.offsetHeight;
let x = this.x;
let y = this.y;
2018-06-09 18:27:10 +00:00
if (x + width - window.pageXOffset > window.innerWidth) {
x = window.innerWidth - width + window.pageXOffset;
2018-06-08 02:46:45 +00:00
}
2018-06-09 18:27:10 +00:00
if (y + height - window.pageYOffset > window.innerHeight) {
y = window.innerHeight - height + window.pageYOffset;
2018-06-08 02:46:45 +00:00
}
this.$el.style.left = x + 'px';
this.$el.style.top = y + 'px';
for (const el of Array.from(document.querySelectorAll('body *'))) {
2018-02-18 03:35:18 +00:00
el.addEventListener('mousedown', this.onMousedown);
}
2018-02-18 03:35:18 +00:00
this.$el.style.display = 'block';
anime({
targets: this.$el,
opacity: [0, 1],
duration: 100,
easing: 'linear'
});
});
},
methods: {
onMousedown(e) {
e.preventDefault();
if (!contains(this.$el, e.target) && (this.$el != e.target)) this.close();
return false;
},
click(item) {
2018-06-08 02:46:45 +00:00
if (item.action) item.action();
2018-02-18 03:35:18 +00:00
this.close();
},
close() {
for (const el of Array.from(document.querySelectorAll('body *'))) {
2018-02-18 03:35:18 +00:00
el.removeEventListener('mousedown', this.onMousedown);
}
2018-02-18 03:35:18 +00:00
this.$emit('closed');
2018-09-15 12:53:04 +00:00
this.destroyDom();
2018-02-18 03:35:18 +00:00
}
}
});
</script>
<style lang="stylus" scoped>
2018-09-28 10:59:19 +00:00
.context-menu
2018-02-18 03:35:18 +00:00
$width = 240px
$item-height = 38px
$padding = 10px
position fixed
top 0
left 0
z-index 4096
width $width
font-size 0.8em
2018-09-28 10:59:19 +00:00
background var(--popupBg)
2018-02-18 03:35:18 +00:00
border-radius 0 4px 4px 4px
2018-04-28 23:51:17 +00:00
box-shadow 2px 2px 8px rgba(#000, 0.2)
2018-02-18 03:35:18 +00:00
opacity 0
</style>