All files / src/directives v-close-popover.js

0% Statements 0/36
0% Branches 0/18
0% Functions 0/9
0% Lines 0/34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                                                                                                                                           
import { supportsPassive } from '../utils'
 
function addListeners (el) {
	el.addEventListener('click', onClick)
	el.addEventListener('touchstart', onTouchStart, supportsPassive ? {
		passive: true,
	} : false)
}
 
function removeListeners (el) {
	el.removeEventListener('click', onClick)
	el.removeEventListener('touchstart', onTouchStart)
	el.removeEventListener('touchend', onTouchEnd)
	el.removeEventListener('touchcancel', onTouchCancel)
}
 
function onClick (event) {
	const el = event.currentTarget
	event.closePopover = !el.$_vclosepopover_touch
}
 
function onTouchStart (event) {
	if (event.changedTouches.length === 1) {
		const el = event.currentTarget
		el.$_vclosepopover_touch = true
		const touch = event.changedTouches[0]
		el.$_vclosepopover_touchPoint = touch
		el.addEventListener('touchend', onTouchEnd)
		el.addEventListener('touchcancel', onTouchCancel)
	}
}
 
function onTouchEnd (event) {
	const el = event.currentTarget
	el.$_vclosepopover_touch = false
	if (event.changedTouches.length === 1) {
		const touch = event.changedTouches[0]
		const firstTouch = el.$_vclosepopover_touchPoint
		event.closePopover = (
			Math.abs(touch.screenY - firstTouch.screenY) < 20 &&
			Math.abs(touch.screenX - firstTouch.screenX) < 20
		)
	}
}
 
function onTouchCancel (event) {
	const el = event.currentTarget
	el.$_vclosepopover_touch = false
}
 
export default {
	bind (el, { value }) {
		if (typeof value === 'undefined' || value) {
			addListeners(el)
		}
	},
	update (el, { value, oldValue }) {
		if (value !== oldValue) {
			if (typeof value === 'undefined' || value) {
				addListeners(el)
			} else {
				removeListeners(el)
			}
		}
	},
	unbind (el) {
		removeListeners(el)
	},
}