Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function closeConditional () {
  2.   return false
  3. }
  4.  
  5. function directive (e, el, binding, vnode) {
  6.   // Args may not always be supplied
  7.   binding.args = binding.args || {}
  8.  
  9.   // If no closeConditional was supplied assign a default
  10.   // const isActive = (binding.args.closeConditional || closeConditional)
  11.   // const isActive = (vnode.data.attrs['close-conditional'] || closeConditional)
  12.   const isActive = (binding.value.condition || closeConditional)
  13.  
  14.   // The include element callbacks below can be expensive
  15.   // so we should avoid calling them when we're not active.
  16.   // Explicitly check for false to allow fallback compatibility
  17.   // with non-toggleable components
  18.   if (!e || isActive(e) === false) return
  19.  
  20.   // If click was triggered programmaticaly (domEl.click()) then
  21.   // it shouldn't be treated as click-outside
  22.   // Chrome/Firefox support isTrusted property
  23.   // IE/Edge support pointerType property (empty if not triggered
  24.   // by pointing device)
  25.   if (('isTrusted' in e && !e.isTrusted) ||
  26.     ('pointerType' in e && !e.pointerType)
  27.   ) return
  28.  
  29.   // Check if additional elements were passed to be included in check
  30.   // (click must be outside all included elements, if any)
  31.   // const elements = (binding.args.include || (() => []))()
  32.   const elements = (binding.value.include || (() => []))()
  33.   // Add the root element for the component this directive was defined on
  34.   elements.push(el)
  35.  
  36.   // Check if it's a click outside our elements, and then if our callback returns true.
  37.   // Non-toggleable components should take action in their callback and return falsy.
  38.   // Toggleable can return true if it wants to deactivate.
  39.   // Note that, because we're in the capture phase, this callback will occure before
  40.   // the bubbling click event on any outside elements.
  41.   !clickedInEls(e, elements) && setTimeout(() => {
  42.     // isActive(e) && binding.value(e)
  43.     isActive(e) && binding.value.cb(e)
  44.   }, 0)
  45. }
  46.  
  47. function clickedInEls (e, elements) {
  48.   // Get position of click
  49.   const { clientX: x, clientY: y } = e
  50.   // Loop over all included elements to see if click was in any of them
  51.   for (const el of elements) {
  52.     if (clickedInEl(el, x, y)) return true
  53.   }
  54.  
  55.   return false
  56. }
  57.  
  58. function clickedInEl (el, x, y) {
  59.   if (!el) return false
  60.   // Get bounding rect for element
  61.   // (we're in capturing event and we want to check for multiple elements,
  62.   //  so can't use target.)
  63.   const b = el.getBoundingClientRect()
  64.   // Check if the click was in the element's bounding rect
  65.  
  66.   return x >= b.left && x <= b.right && y >= b.top && y <= b.bottom
  67. }
  68.  
  69. function inserted (el, binding, vnode) {
  70.   const onClick = (e) => directive(e, el, binding, vnode)
  71.   // iOS does not recognize click events on document
  72.   // or body, this is the entire purpose of the v-app
  73.   // component and [data-app], stop removing this
  74.   const app = document.querySelector('#app') || document.body // This is only for unit tests
  75.   app.addEventListener('click', onClick, true)
  76.   el._clickOutside = onClick
  77. }
  78.  
  79. function unbind (el) {
  80.   if (!el._clickOutside) return
  81.  
  82.   const app = document.querySelector('#app') || document.body // This is only for unit tests
  83.   app && app.removeEventListener('click', el._clickOutside, true)
  84.   delete el._clickOutside
  85. }
  86.  
  87.  
  88. export default {
  89.   name: 'click-outside',
  90.   inserted,
  91.   unbind
  92. }
  93.  
  94. /* function bind (el, binding, vnode) {
  95.   el.event = function(event) {
  96.     if (!(el === event.target || el.contains(event.target))) {
  97.       vnode.context[binding.expression](event)
  98.     }
  99.   }
  100.   document.body.addEventListener('click', el.event)
  101.   document.body.addEventListener('touchstart', el.event)
  102. }
  103. function unbind (el) {
  104.   document.body.removeEventListener('click', el.event)
  105.   document.body.removeEventListener('touchstart', el.event)
  106. }
  107.  
  108.  
  109. export default {
  110.   name: 'click-outside',
  111.   bind,
  112.   unbind
  113. } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement