Skip to content

Commit

Permalink
fix: clickoutside指令失效
Browse files Browse the repository at this point in the history
  • Loading branch information
yt0379 committed Sep 8, 2020
1 parent 9e981ec commit 1444ea5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/select/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ import ElOption from 'element-ui/packages/option/Option'
import ElTag from 'element-ui/packages/tag'
import ElScrollbar from 'element-ui/packages/scrollbar'
import debounce from 'throttle-debounce/debounce'
import Clickoutside from 'element-ui/src/utils/clickoutside'
import Clickoutside from 'element-ui/src/directives/clickoutside'
import {
addResizeListener,
removeResizeListener
Expand Down
82 changes: 82 additions & 0 deletions src/directives/clickoutside.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { on } from 'element-ui/src/utils/dom'

const nodeList = []
const ctx = '@@clickoutsideContext'

let startClick
let seed = 0

// !Vue.prototype.$isServer && on(document, 'mousedown', e => (startClick = e))
on(document, 'mousedown', (e) => (startClick = e))

on(document, 'mouseup', (e) => {
// !Vue.prototype.$isServer && on(document, 'mouseup', e => {
nodeList.forEach((node) => node[ctx].documentHandler(e, startClick))
})

function createDocumentHandler(el, binding, vnode) {
return function (mouseup = {}, mousedown = {}) {
if (
!vnode ||
!binding.instance ||
!mouseup.target ||
!mousedown.target ||
el.contains(mouseup.target) ||
el.contains(mousedown.target) ||
el === mouseup.target ||
(binding.instance.popperElm &&
(binding.instance.popperElm.contains(mouseup.target) ||
binding.instance.popperElm.contains(mousedown.target)))
)
return

if (
binding.expression &&
el[ctx].methodName &&
binding.instance[el[ctx].methodName]
) {
binding.instance[el[ctx].methodName]()
} else {
el[ctx].bindingFn && el[ctx].bindingFn()
}
}
}

/**
* v-clickoutside
* @desc 点击元素外面才会触发的事件
* @example
* ```vue
* <div v-element-clickoutside="handleClose">
* ```
*/
export default {
beforeMount(el, binding, vnode) {
nodeList.push(el)
const id = seed++
el[ctx] = {
id,
documentHandler: createDocumentHandler(el, binding, vnode),
methodName: binding.expression,
bindingFn: binding.value
}
},

updated(el, binding, vnode) {
el[ctx].documentHandler = createDocumentHandler(el, binding, vnode)
el[ctx].methodName = binding.expression
el[ctx].bindingFn = binding.value
},

unmounted(el) {
const len = nodeList.length

for (let i = 0; i < len; i++) {
if (nodeList[i][ctx].id === el[ctx].id) {
nodeList.splice(i, 1)
break
}
}
delete el[ctx]
}
}

0 comments on commit 1444ea5

Please sign in to comment.