-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobile_touch.js
62 lines (57 loc) · 2.32 KB
/
mobile_touch.js
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
/*
* this directives replaces ng-click with tap event to avoid the 300ms delay on dom for detecting
* if a move event is hapening. since this is a non isolating directive a scope is not defined
*/
.directive('mobileTouch',['$timeout','$location','$rootScope',function($timeout,$location,$rootScope){
return{
restrict:'C',
link:function(scope,element,attrs){
scope.tapped=false;
scope.touchstarttime=null;
$(element).off('touchstart');
element.bind('touchstart',function($event){
scope.tapped=true;
scope.touchstarttime=Date.now();
});
$(element).bind('touchmove',function($event){
scope.tapped=false;
});
$(element).bind('touchend',function($event){
if(!scope.tapped){
scope.touchstarttime=null;
return;
}
scope.tapped=false;
if(attrs.prevent && attrs.prevent==='true'){
$event.preventDefault();
}
$event.stopImmediatePropagation();
$event.stopPropagation();
var str=null;
if(attrs.message){
str=attrs.message;
}
var link='';
var path=$location.path();
if(attrs.href){
str=str||'Getting There';
link=attrs.href.replace(/#/,'');
if(link===path)
return;
}
if(str){
$rootScope.$broadcast('toast',str,5000)
}
$timeout(
function(){
if(attrs.href){
$timeout(function(){$location.path(link);},900);
}
if(attrs.ngClick){
$timeout(function(){scope.$apply(attrs.ngClick);},100);
}
},100);
});
}
};
}])