summaryrefslogtreecommitdiff
path: root/src/client/app/desktop/views/components/context-menu.vue
blob: afb6838eb6b04dfeaf295e8cfbad5170d226519a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<template>
<div class="context-menu" @contextmenu.prevent="() => {}">
	<x-menu :menu="menu" @x="click"/>
</div>
</template>

<script lang="ts">
import Vue from 'vue';
import * as anime from 'animejs';
import contains from '../../../common/scripts/contains';
import XMenu from './context-menu.menu.vue';

export default Vue.extend({
	components: {
		XMenu
	},
	props: ['x', 'y', 'menu'],
	mounted() {
		this.$nextTick(() => {
			const width = this.$el.offsetWidth;
			const height = this.$el.offsetHeight;

			let x = this.x;
			let y = this.y;

			if (x + width - window.pageXOffset > window.innerWidth) {
				x = window.innerWidth - width + window.pageXOffset;
			}

			if (y + height - window.pageYOffset > window.innerHeight) {
				y = window.innerHeight - height + window.pageYOffset;
			}

			this.$el.style.left = x + 'px';
			this.$el.style.top = y + 'px';

			Array.from(document.querySelectorAll('body *')).forEach(el => {
				el.addEventListener('mousedown', this.onMousedown);
			});

			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) {
			if (item.action) item.action();
			this.close();
		},
		close() {
			Array.from(document.querySelectorAll('body *')).forEach(el => {
				el.removeEventListener('mousedown', this.onMousedown);
			});

			this.$emit('closed');
			this.$destroy();
		}
	}
});
</script>

<style lang="stylus" scoped>
root(isDark)
	$width = 240px
	$item-height = 38px
	$padding = 10px

	position fixed
	top 0
	left 0
	z-index 4096
	width $width
	font-size 0.8em
	background isDark ? #282c37 : #fff
	border-radius 0 4px 4px 4px
	box-shadow 2px 2px 8px rgba(#000, 0.2)
	opacity 0

.context-menu[data-darkmode]
	root(true)

.context-menu:not([data-darkmode])
	root(false)

</style>