| <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> |
| <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"; |
| |
| |
| |
| |
| @Options({ |
| components: {}, |
| }) |
| export default class ContextMenu extends Vue { |
| @Prop({ required: true }) options!: IContextMenuOption[]; |
| |
| contextMenuStyle = ""; |
| showMenu = false; |
| |
| |
| |
| |
| |
| |
| onRightClick(e: MouseEvent) { |
| |
| this.contextMenuStyle = `top: ${e.clientY - 16}px; left: ${ |
| e.clientX - 16 |
| }px; z-index: 1000;`; |
| this.showMenu = true; |
| |
| this.$emit("onMenuItemRightClick", e); |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| |
| |
| |
| |
| |
| |
| onMenuItemClick(option: IContextMenuOption) { |
| |
| option.function(); |
| this.closeMenu(); |
| } |
| |
| |
| |
| |
| closeMenu() { |
| this.showMenu = false; |
| } |
| } |
| </script> |
| |
| |
| <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> |
| |