Vue.directive(
'tap'
,{
bind:function(el,binding){
var startTx, startTy,endTx,endTy,longClick,timeOutEvent,
longMethod=binding.value.longMethod,
method = binding.value.method,
params = binding.value.params,
propagation=binding.value.propagation;
el.addEventListener(
"touchstart"
,function(e){
var touch=e.touches[
0
];
startTx = touch.clientX;
startTy = touch.clientY;
if
(longMethod && typeof longMethod===
'function'
){
longClick=
0
;
timeOutEvent =setTimeout(function(){
longClick=
1
;
longMethod(e, params);
},
500
)
}
if
(!propagation){
if
(e.stopImmediatePropagation) {
e.stopImmediatePropagation();
}
else
{
e.propagationStopped =
true
;
}
}
},
false
);
el.addEventListener(
"touchmove"
,function(e){
if
(longMethod && timeOutEvent){
clearTimeout(timeOutEvent);
timeOutEvent =
0
;
}
},
false
);
el.addEventListener(
"touchend"
,function(e){
var touch = e.changedTouches[
0
];
endTx = touch.clientX;
endTy = touch.clientY;
if
(longMethod && timeOutEvent){
clearTimeout(timeOutEvent);
}
if
((timeOutEvent!=
0
&& longClick==
0
)||!longMethod){
if
( Math.abs(startTx - endTx) <
6
&& Math.abs(startTy - endTy) <
6
){
if
(params
instanceof
Array){
method(...params);
}
else
method(params);
}
var focusInput=document.querySelector(
'input:focus'
);
if
(focusInput)focusInput.blur();
var tagName=el.tagName.toLowerCase();
if
(tagName===
'input'
||tagName===
'textarea'
)el.focus();
if
(!propagation){
if
(e.stopImmediatePropagation) {
e.stopImmediatePropagation();
}
else
{
e.propagationStopped =
true
;
}
e.stopPropagation();
return
false
;
}
}
},
false
);
}
})