index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <div
  3. class="vue-portal-target"
  4. :style="{ left: left, top: top, display: display }"
  5. @mousedown.stop=""
  6. >
  7. <div
  8. class="v-menu"
  9. style="height: auto; width: auto; z-index: 15000"
  10. v-if="selectList.length"
  11. >
  12. <div class="v-trap-focus-indicator"></div>
  13. <div class="v-trap-focus-body">
  14. <div
  15. class="v-menu-item"
  16. :class="select.cls"
  17. v-for="(select, selectIndex) in selectList"
  18. :key="select.id"
  19. @mouseenter="mouseenterSelect(selectIndex)"
  20. @mouseleave="mouseleaveSelect(selectIndex)"
  21. @click="clickSelect(select)"
  22. >
  23. {{ select.text }}
  24. </div>
  25. </div>
  26. <div class="v-trap-focus-indicator"></div>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. export default {
  32. mounted() {
  33. this.$bus.on("showPortalTarget", this.showPortalTarget);
  34. this.$bus.on("hiddenPortalTarget", this.hiddenPortalTarget);
  35. },
  36. data() {
  37. return {
  38. left: "457px",
  39. top: "184px",
  40. display: "none",
  41. selectList: [],
  42. selectedItem: {},
  43. };
  44. },
  45. methods: {
  46. // 显示本页面
  47. showPortalTarget(obj) {
  48. this.left = obj.left + "px";
  49. this.top = obj.top + "px";
  50. this.selectList = obj.selectList;
  51. this.selectedItem = JSON.parse(JSON.stringify(obj.item));
  52. this.selectList.forEach((item, index) => {
  53. if (index === 0) {
  54. item.cls = "active";
  55. } else {
  56. item.cls = "";
  57. }
  58. });
  59. if (this.display === "none") {
  60. this.display = "block";
  61. window.addEventListener('mousedown',this.hiddenPortalTarget)
  62. }
  63. },
  64. // 隐藏本页面
  65. hiddenPortalTarget() {
  66. console.log(1)
  67. if (this.display === "block") {
  68. this.display = "none";
  69. }
  70. window.removeEventListener('mousedown',this.hiddenPortalTarget)
  71. },
  72. // 移入选择项改变背景颜色
  73. mouseenterSelect(selectIndex) {
  74. this.selectList.forEach((item, index) => {
  75. if (index === selectIndex) {
  76. item.cls = "active";
  77. } else {
  78. item.cls = "";
  79. }
  80. });
  81. },
  82. // 移出选择项改变背景颜色
  83. mouseleaveSelect(selectIndex) {
  84. this.selectList[selectIndex].cls = "";
  85. },
  86. // 点击选择项
  87. clickSelect(select) {
  88. this.selectedItem.selectList.forEach((item) => {
  89. if ((item.text = "添加到桌面")) {
  90. item.text = "删除快捷方式";
  91. }
  92. });
  93. if (select.text === "添加到桌面") {
  94. this.$bus.emit("pushShortcutList", this.selectedItem);
  95. }
  96. if (select.text === "删除快捷方式") {
  97. this.$bus.emit("spliceShortcutList", this.selectedItem);
  98. }
  99. },
  100. },
  101. components: {},
  102. name: "PortalTarget",
  103. };
  104. </script>