File size: 2,858 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<template>
    <span>
        <div
            v-if="showMenu"
            class="p-1 dropdown-menu contextMenu"
            :style="contextMenuStyle"
            @mouseleave="closeMenu"
        >
            <span v-for="option of options" :key="option">
                <hr
                    v-if="option.text === 'separator'"
                    class="m-1 my-half dropdown-divider"
                />
                <a
                    v-else
                    @click="onMenuItemClick(option)"
                    :class="
                        'p-1 py-half dropdown-item' +
                        (option.enabled ? '' : ' disabled')
                    "
                    >{{ option.text }}</a
                >
            </span>

            <!-- <div class="p-1 py-half dropdown-divider"></div> -->
        </div>
        <div @click.right.prevent="onRightClick">
            <slot></slot>
        </div>
    </span>
</template>

<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { Prop } from "vue-property-decorator";
import { IContextMenuOption } from "./ContextMenuInterfaces";

/**
 * ContextMenu component
 */
@Options({
    components: {},
})
export default class ContextMenu extends Vue {
    @Prop({ required: true }) options!: IContextMenuOption[];

    contextMenuStyle = "";
    showMenu = false;

    /**
     * Runs when user right clicks.
     * 
     * @param {MouseEvent} e  The mouse event.
     */
    onRightClick(e: MouseEvent) {
        // Set position of the context menu
        this.contextMenuStyle = `top: ${e.clientY - 16}px; left: ${
            e.clientX - 16
        }px; z-index: 1000;`;
        this.showMenu = true;

        this.$emit("onMenuItemRightClick", e);

        // // Show the menu
        // contextMenu.classList.add('show');

        // // To make the dropdown visible above other elements, adjust the `z-index`
        // if (contextMenu.classList.contains('show')) {
        //     contextMenu.classList.remove('show');
        // }
    }

    /**
     * Runs when user clicks a menu item.
     * 
     * @param {IContextMenuOption} option  The menu item.
     */
    onMenuItemClick(option: IContextMenuOption) {
        // this.$emit("onMenuItemClick", option);
        option.function();
        this.closeMenu();
    }

    /**
     * Closes the menu.
     */
    closeMenu() {
        this.showMenu = false;
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.contextMenu {
    // position: absolute;
    position: fixed;
    transform: translate3d(0, 0, 0);
    will-change: transform;
    display: inline-block;
}

.py-half {
    padding-top: 1px !important;
    padding-bottom: 1px !important;
}

.my-half {
    margin-top: 2px !important;
    margin-bottom: 1px !important;
}
</style>