|
| 1 | +<template> |
| 2 | + <div |
| 3 | + ref="dragWrapper" |
| 4 | + class="org-tree-drag-wrapper" |
| 5 | + @mousedown="mousedownView" |
| 6 | + @contextmenu="handleDocumentContextmenu" |
| 7 | + > |
| 8 | + <div class="org-tree-wrapper" :style="orgTreeStyle"> |
| 9 | + <v-org-tree |
| 10 | + v-if="data" |
| 11 | + :data="data" |
| 12 | + :node-render="nodeRender" |
| 13 | + :expand-all="true" |
| 14 | + @on-node-click="handleNodeClick" |
| 15 | + collapsable |
| 16 | + ></v-org-tree> |
| 17 | + </div> |
| 18 | + </div> |
| 19 | +</template> |
| 20 | + |
| 21 | +<script> |
| 22 | +import { on, off } from '@/libs/tools' |
| 23 | +const menuList = [ |
| 24 | + { |
| 25 | + key: 'edit', |
| 26 | + label: '编辑部门' |
| 27 | + }, |
| 28 | + { |
| 29 | + key: 'detail', |
| 30 | + label: '查看部门' |
| 31 | + }, |
| 32 | + { |
| 33 | + key: 'new', |
| 34 | + label: '新增子部门' |
| 35 | + }, |
| 36 | + { |
| 37 | + key: 'delete', |
| 38 | + label: '删除部门' |
| 39 | + } |
| 40 | +] |
| 41 | +export default { |
| 42 | + name: 'OrgView', |
| 43 | + props: { |
| 44 | + zoomHandled: { |
| 45 | + type: Number, |
| 46 | + default: 1 |
| 47 | + }, |
| 48 | + data: Object |
| 49 | + }, |
| 50 | + data () { |
| 51 | + return { |
| 52 | + currentContextMenuId: '', |
| 53 | + orgTreeOffsetLeft: 0, |
| 54 | + orgTreeOffsetTop: 0, |
| 55 | + initPageX: 0, |
| 56 | + initPageY: 0, |
| 57 | + oldMarginLeft: 0, |
| 58 | + oldMarginTop: 0, |
| 59 | + canMove: false |
| 60 | + } |
| 61 | + }, |
| 62 | + computed: { |
| 63 | + orgTreeStyle () { |
| 64 | + return { |
| 65 | + transform: `translate(-50%, -50%) scale(${this.zoomHandled}, ${ |
| 66 | + this.zoomHandled |
| 67 | + })`, |
| 68 | + marginLeft: `${this.orgTreeOffsetLeft}px`, |
| 69 | + marginTop: `${this.orgTreeOffsetTop}px` |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + methods: { |
| 74 | + handleNodeClick (e, data, expand) { |
| 75 | + expand() |
| 76 | + }, |
| 77 | + closeMenu () { |
| 78 | + this.currentContextMenuId = '' |
| 79 | + }, |
| 80 | + getBgColor (data) { |
| 81 | + return this.currentContextMenuId === data.id |
| 82 | + ? data.isRoot |
| 83 | + ? '#0d7fe8' |
| 84 | + : '#5d6c7b' |
| 85 | + : '' |
| 86 | + }, |
| 87 | + nodeRender (h, data) { |
| 88 | + return ( |
| 89 | + <div |
| 90 | + class={[ |
| 91 | + 'custom-org-node', |
| 92 | + data.children && data.children.length ? 'has-children-label' : '' |
| 93 | + ]} |
| 94 | + on-mousedown={event => event.stopPropagation()} |
| 95 | + on-contextmenu={this.contextmenu.bind(this, data)} |
| 96 | + > |
| 97 | + {data.label} |
| 98 | + <dropdown |
| 99 | + trigger="custom" |
| 100 | + class="context-menu" |
| 101 | + visible={this.currentContextMenuId === data.id} |
| 102 | + nativeOn-click={this.handleDropdownClick} |
| 103 | + on-on-click={this.handleContextMenuClick.bind(this, data)} |
| 104 | + style={{ |
| 105 | + transform: `scale(${1 / this.zoomHandled}, ${1 / |
| 106 | + this.zoomHandled})` |
| 107 | + }} |
| 108 | + v-click-outside={this.closeMenu} |
| 109 | + > |
| 110 | + <dropdown-menu slot="list"> |
| 111 | + {menuList.map(item => { |
| 112 | + return ( |
| 113 | + <dropdown-item name={item.key}>{item.label}</dropdown-item> |
| 114 | + ) |
| 115 | + })} |
| 116 | + </dropdown-menu> |
| 117 | + </dropdown> |
| 118 | + </div> |
| 119 | + ) |
| 120 | + }, |
| 121 | + contextmenu (data, $event) { |
| 122 | + let event = $event || window.event |
| 123 | + event.preventDefault |
| 124 | + ? event.preventDefault() |
| 125 | + : (event.returnValue = false) |
| 126 | + this.currentContextMenuId = data.id |
| 127 | + }, |
| 128 | + setDepartmentData (data) { |
| 129 | + data.isRoot = true |
| 130 | + this.departmentData = data |
| 131 | + }, |
| 132 | + mousedownView (event) { |
| 133 | + this.canMove = true |
| 134 | + this.initPageX = event.pageX |
| 135 | + this.initPageY = event.pageY |
| 136 | + this.oldMarginLeft = this.orgTreeOffsetLeft |
| 137 | + this.oldMarginTop = this.orgTreeOffsetTop |
| 138 | + on(document, 'mousemove', this.mousemoveView) |
| 139 | + on(document, 'mouseup', this.mouseupView) |
| 140 | + }, |
| 141 | + mousemoveView (event) { |
| 142 | + if (!this.canMove) return |
| 143 | + const { pageX, pageY } = event |
| 144 | + this.orgTreeOffsetLeft = this.oldMarginLeft + pageX - this.initPageX |
| 145 | + this.orgTreeOffsetTop = this.oldMarginTop + pageY - this.initPageY |
| 146 | + }, |
| 147 | + mouseupView () { |
| 148 | + this.canMove = false |
| 149 | + off(document, 'mousemove', this.mousemoveView) |
| 150 | + off(document, 'mouseup', this.mouseupView) |
| 151 | + }, |
| 152 | + handleDropdownClick (event) { |
| 153 | + event.stopPropagation() |
| 154 | + }, |
| 155 | + handleDocumentContextmenu () { |
| 156 | + this.canMove = false |
| 157 | + }, |
| 158 | + handleContextMenuClick (data, key) { |
| 159 | + this.$emit('on-menu-click', { data, key }) |
| 160 | + } |
| 161 | + }, |
| 162 | + mounted () { |
| 163 | + on(document, 'mousedown', this.mousedownView) |
| 164 | + on(document, 'contextmenu', this.handleDocumentContextmenu) |
| 165 | + }, |
| 166 | + beforeDestroy () { |
| 167 | + off(document, 'mousedown', this.mousedownView) |
| 168 | + off(document, 'contextmenu', this.handleDocumentContextmenu) |
| 169 | + } |
| 170 | +} |
| 171 | +</script> |
| 172 | + |
| 173 | +<style> |
| 174 | +</style> |
0 commit comments