-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtabs-swipable.js
98 lines (87 loc) · 4.77 KB
/
tabs-swipable.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
angular.module('ionic')
.directive('tabsSwipable', ['$ionicGesture', function($ionicGesture) {
return {
restrict: 'A',
require: 'ionTabs',
link: function(scope, elm, attrs, tabsCtrl) {
var swipeGesture = {};
var swipeLoop = false;
if (attrs.tabsSwipableLoop!=undefined) {
if (["true", "false"].indexOf(attrs.tabsSwipableLoop) == -1) {
console.error("tabsSwipable: tabsSwipableLoop value should be true or false, '" + attrs.tabsSwipableLoop + "' is given.");
}
else {
swipeLoop = (attrs.tabsSwipableLoop == "true");
}
}
var onSwipeEnd = function(event) {
document.contains(document.getElementById('ionTabsAnimatedStyle')) && document.getElementById('ionTabsAnimatedStyle').remove();
for (var i = 0; i < tabsCtrl.tabs.length; i++) {
ionView = angular.element(document.querySelector("ion-nav-view[name='" + tabsCtrl.tabs[i].navViewName+"']"));
ionView.css({'left': 'initial'})
if (i!=tabsCtrl.selectedIndex()) {
ionView.attr("nav-view", "cached");
}
}
if ((event.gesture.distance > window.screen.width/1.6) || (event.gesture.distance/(event.gesture.timeStamp-event.gesture.startEvent.timeStamp)>0.1)
) {
var nextTarget = tabsCtrl.selectedIndex();
event.gesture.direction == "right" && (nextTarget--);
event.gesture.direction == "left" && (nextTarget++);
if (nextTarget>=0 && nextTarget<tabsCtrl.tabs.length) {
nextTarget!=tabsCtrl.selectedIndex() && (scope.$apply(tabsCtrl.select(nextTarget)));
}
else if (swipeLoop) {
if (nextTarget==-1) {
nextTarget!=tabsCtrl.selectedIndex() && (scope.$apply(tabsCtrl.select(tabsCtrl.tabs.length - 1)));
}
else if (nextTarget==tabsCtrl.tabs.length) {
nextTarget!=tabsCtrl.selectedIndex() && (scope.$apply(tabsCtrl.select(0)));
}
}
}
};
var onSwipe = function(event) {
var target = nextTarget = tabsCtrl.selectedIndex();
var ionTabView = angular.element(document.querySelector("ion-nav-view[name='" + tabsCtrl.tabs[target].navViewName+"']"));
var nextIonNavView = null;
var activeTab = document.querySelector("[tabs-swipable] .tabs .tab-item.tab-item-active");
var activeTabWidth = activeTab.offsetWidth;
var styleTag = document.getElementById('ionTabsAnimatedStyle') || document.createElement("style");
styleTag.setAttribute("id", 'ionTabsAnimatedStyle');
styleTag.innerHTML = ".tab-item-active::after{position: absolute;width: "+activeTabWidth+"px;}";
styleTag.innerHTML += ".tab-item-active::after{transform: translateX( "+(-event.gesture.deltaX/5)+"px);}"
if (target<0) {return;}
event.gesture.direction == "right" && (nextTarget--);
event.gesture.direction == "left" && (nextTarget++);
if (nextTarget>=0 && nextTarget<tabsCtrl.tabs.length) {
nextIonNavView = angular.element(document.querySelector("ion-nav-view[name='" + tabsCtrl.tabs[nextTarget].navViewName+"']"));
}
else if (swipeLoop) {
if (nextTarget==-1) {
nextIonNavView = angular.element(document.querySelector("ion-nav-view[name='" + tabsCtrl.tabs[tabsCtrl.tabs.length - 1].navViewName+"']"));
}
else if (nextTarget==tabsCtrl.tabs.length) {
nextIonNavView = angular.element(document.querySelector("ion-nav-view[name='" + tabsCtrl.tabs[0].navViewName+"']"));
}
}
if (swipeLoop || (event.gesture.direction == "right" && nextTarget >= 0) || (event.gesture.direction == "left" && nextTarget < tabsCtrl.tabs.length)) {
var style = {};
style["left"] = (event.gesture.deltaX) + "px";
ionTabView.css(style);
event.gesture.direction == "left" && (style["left"] = ( window.screen.width+event.gesture.deltaX) + "px");
event.gesture.direction == "right" && (style["left"] = ( event.gesture.deltaX-window.screen.width) + "px");
nextIonNavView.attr("nav-view", "active");
nextIonNavView.css(style);
document.getElementsByTagName('head')[0].append(styleTag);
}
};
swipeGesture.drag = $ionicGesture.on('drag', onSwipe, elm);
swipeGesture.dragend = $ionicGesture.on('dragend', onSwipeEnd, elm);
scope.$on('$destroy', function() {
$ionicGesture.off(swipeGesture.drag, 'drag', onSwipe);
$ionicGesture.off(swipeGesture.dragend, 'dragend', onSwipeEnd);
});
}
};
}]);