// 点击外部区域触发事件
// 说明：点击外部区域触发事件，不包括点击元素本身
// 用法：v-clidk-besides="() => {}"
// 属性：bind-id，点击元素的id，用于判断是否点击了元素本身
// 属性：stop-besides="true"，"true"时候点击不触发事件
export default {
	name: 'clidk-besides',
	mounted(el, binding) {
		const call = binding.value
		const id = el.getAttribute("bind-id");
		window.addEventListener("touchstart", fun, true);
		window.addEventListener("mousedown", fun, true);
		el.clidkBesidesFun = fun
		function fun(e) {
			if (el.getAttribute("stop-besides") == "true") return
			var k = true;
			iterator(e.target);
			if (k) call && call();
			function iterator(el) {
				if (!el || el.nodeName == "#document") return;
				if (el.getAttribute("bind-id") == id) {
					k = false;
				} else {
					iterator(el.parentNode);
				}
			}
		}
	},
	unmounted(el) {
		window.removeEventListener("touchstart", el.clidkBesidesFun, true);
		window.removeEventListener("mousedown", el.clidkBesidesFun, true);
	}
};