右键菜单

这是一个右键菜单的组件

<template>
<div id="dropMenu"
:style="style"
style="display: block;"
v-show="show"
@mousedown.stop <!--阻止冒泡事件-->
@contextmenu.prevent><!--阻止右键的默认事件-->
<slot></slot>
</div>
</template>
<script>
export default {
data() {
return {
triggerShowFn: () => {},
triggerHideFn: () => {},
x: null,
y: null,
style: {},
binded: false
}
},
props: {
target: null,
show: Boolean
},
mounted() {
this.bindEvents()
},
watch: {
show(show) {
if (show) {
this.bindHideEvents()
} else {
this.unbindHideEvents()
}
},
target(target) {
this.bindEvents()
}
},
methods: {
// 初始化事件
bindEvents() {
this.$nextTick(() => {
var that = this
if (!this.target || this.binded) return
this.triggerShowFn = this.contextMenuHandler.bind(this)
this.target.addEventListener('contextmenu', this.triggerShowFn)
//this.binded = true
})
},
// 取消绑定事件
unbindEvents() {
if (!this.target) return
this.target.removeEventListener('contextmenu', this.triggerShowFn)
},
// 绑定隐藏菜单事件
bindHideEvents() {
this.triggerHideFn = this.clickDocumentHandler.bind(this)
document.addEventListener('mousedown', this.triggerHideFn)
document.addEventListener('mousewheel', this.triggerHideFn)
},
// 取消绑定隐藏菜单事件
unbindHideEvents() {
document.removeEventListener('mousedown', this.triggerHideFn)
document.removeEventListener('mousewheel', this.triggerHideFn)
},
// 鼠标按压事件处理器
clickDocumentHandler(e) {
this.$emit('update:show', false) //隐藏
},
// 右键事件事件处理
contextMenuHandler(e) {
e.target.click()//这个是因为我需要获取tree节点的数据,所以我通过点击事件去获取数据
this.x = e.clientX -
this.y = e.clientY -
this.layout()
this.$emit('update:show', true) //显示
e.preventDefault()
e.stopPropagation() this.$emit('targetElement', e.target) //我还要获取右键的DOM元素进行操作
},
// 布局
layout() {
this.style = {
left: this.x + 'px',
top: this.y + 'px'
}
}
}
}
</script>
<style lang="scss">
#dropMenu {
position: absolute;
margin: ;
padding: ;
width: 80px;
height: auto;
border: 1px solid #ccc;
border-radius: 4px;
ul {
list-style: none;
margin: ;
padding: ;
li {
width: %;
text-align: center;
height: 30px;
line-height: 30px;
background: #eee;
margin-bottom: 1px;
cursor: pointer;
}
}
}
</style>

调用组件

 <menu-context :target="contextMenuTarget"
:show="isShowDrop"
@update:show="(show) => isShowDrop = show"
@targetElement="getTargetElement">
<ul>
        <!--按需展示按钮-->
<li @click="deleteOne"
v-show="isShowDelete">删除</li>
<li @click="add"
v-show="isShowAdd">添加</li>
<li @click="editNode"
v-show="isShowEdit">编辑</li>
<li @click="linkModels"
v-show="isShowLink">关联</li>
<li @click="move"
v-show="isShowMove">转移</li>
</ul>
</menu-context>

引入组件

import menuContext from './components/menuContext.vue'
export default {
components: {
menuContext
},
data() {
return {
//空间树数据
spaceTreeData: [],
//默认树结构的输出格式
defaultProps: {
children: 'children',
label: 'name'
},
isShowDrop: false, //右键菜单的是否显示
contextMenuTarget: null, //右键菜单的DOM对象
thisformdata: {}, //右键菜单的点击的节点对象
targetElement: {}, //右键点击的目标对象
isShowDelete: true, //是否显示菜单中的删除按钮
isShowEdit: true, //是否显示菜单中的 编辑按钮
isShowAdd: true, //是否显示菜单中的 添加按钮
isShowMove: true, //是否显示菜单中的 转移按钮
isShowLink: true //是否显示菜单中的 关联按钮
}
},

初始化

 mounted() {
  //dom加载完,进行目标dom的设置,直接在data中赋值,会找不到dom
this.contextMenuTarget = document.querySelector('#modelTree')
},

方法

methods: {
getTargetElement(v) {
this.targetElement = v
},
deleteOne() {
var that = this
var nodeIds = [this.thisformdata.nodeId]
this.$confirm('此操作将删除该节点及其子节点, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
center: true
})
.then(() => {
deleteTreeNode(that.spaceTreeId, nodeIds).then(function(res) {
console.log('deleteTreeNode', res)
if (res.success) {
that.$message('删除成功!')
that.refreshSpaceTree()
}
})
})
.catch(() => {})
},
add() {
this.append(this.thisformdata)
this.isShowDrop = false
},
editNode() {
var data = this.thisformdata
var str = "<input type='text' name='label' value='" + data.name + "'>"
var e = window.event
var text = this.targetElement
text.innerHTML = str
this.isShowDrop = false
text.lastChild.onblur = function() {
data.name = text.lastChild.value
text.innerHTML = text.lastChild.value
var that = this
var space = {
nodeId: data.nodeId,
orgnazitionalTreeID: data.orgnazitionalTreeID,
parentNodeId: data.parentNodeId,
name: data.name,
nodeType: data.nodeType
}
//发送请求修改内容=>?有问题明天要修改
modifyTreeNode(data.orgnazitionalTreeID, space).then(function(res) {
console.log('modifyTreeNode', res)
})
}
},
move() {
if (this.thisformdata.nodeType == ) {
this.$refs.modelLinkSpace.Open([this.thisformdata])
}
this.isShowDrop = false
},
linkModels() {
this.linkModel({}, this.thisformdata)
},
handleNodeClick(data) {
console.log('handleNodeClick', data)
// !核心 : 节点数据被获得
this.thisformdata = data
    //按需展示不同的按钮
if (data.nodeType == && !data.children) {
this.isShowAdd = true
this.isShowLink = true
this.isShowMove = true
} else if (data.nodeType == ) {
this.isShowAdd = false
this.isShowLink = false
this.isShowMove = true
} else if (data.nodeType == && data.children && data.children[].nodeType == ) {
this.isShowAdd = false
this.isShowLink = true
this.isShowMove = true
} else if (data.nodeType == && data.children && data.children[].nodeType == ) {
this.isShowAdd = true
this.isShowLink = false
this.isShowMove = false
}
},

 element 右键菜单-LMLPHPelement 右键菜单-LMLPHPelement 右键菜单-LMLPHPelement 右键菜单-LMLPHP

05-23 22:07