This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathionic.contrib.drawer.js
189 lines (156 loc) · 4.3 KB
/
ionic.contrib.drawer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(function() {
'use strict';
/**
* The ionic-contrib-frosted-glass is a fun frosted-glass effect
* that can be used in iOS apps to give an iOS 7 frosted-glass effect
* to any element.
*/
angular.module('ionic.contrib.drawer', ['ionic'])
.controller('drawerCtrl', ['$element', '$attrs', '$ionicGesture', '$document', function($element, $attr, $ionicGesture, $document) {
var el = $element[0];
var dragging = false;
var startX, lastX, offsetX, newX;
var side;
// How far to drag before triggering
var thresholdX = 15;
// How far from edge before triggering
var edgeX = 40;
var LEFT = 0;
var RIGHT = 1;
var isTargetDrag = false;
var width = $element[0].clientWidth;
var enableAnimation = function() {
$element.addClass('animate');
};
var disableAnimation = function() {
$element.removeClass('animate');
};
// Check if this is on target or not
var isTarget = function(el) {
while(el) {
if(el === $element[0]) {
return true;
}
el = el.parentNode;
}
};
var startDrag = function(e) {
disableAnimation();
dragging = true;
offsetX = lastX - startX;
console.log('Starting drag');
console.log('Offset:', offsetX);
};
var startTargetDrag = function(e) {
disableAnimation();
dragging = true;
isTargetDrag = true;
offsetX = lastX - startX;
console.log('Starting target drag');
console.log('Offset:', offsetX);
};
var doEndDrag = function(e) {
startX = null;
lastX = null;
offsetX = null;
isTargetDrag = false;
if(!dragging) {
return;
}
dragging = false;
console.log('End drag');
enableAnimation();
ionic.requestAnimationFrame(function() {
if(newX < (-width / 2)) {
el.style.transform = el.style.webkitTransform = 'translate3d(' + -width + 'px, 0, 0)';
} else {
el.style.transform = el.style.webkitTransform = 'translate3d(0px, 0, 0)';
}
});
};
var doDrag = function(e) {
if(e.defaultPrevented) {
return;
}
if(!lastX) {
startX = e.gesture.touches[0].pageX;
}
lastX = e.gesture.touches[0].pageX;
if(!dragging) {
// Dragged 15 pixels and finger is by edge
if(Math.abs(lastX - startX) > thresholdX) {
if(isTarget(e.target)) {
startTargetDrag(e);
} else if(startX < edgeX) {
startDrag(e);
}
}
} else {
console.log(lastX, offsetX, lastX - offsetX);
newX = Math.min(0, (-width + (lastX - offsetX)));
ionic.requestAnimationFrame(function() {
el.style.transform = el.style.webkitTransform = 'translate3d(' + newX + 'px, 0, 0)';
});
}
if(dragging) {
e.gesture.srcEvent.preventDefault();
}
};
side = $attr.side == 'left' ? LEFT : RIGHT;
console.log(side);
$ionicGesture.on('drag', function(e) {
doDrag(e);
}, $document);
$ionicGesture.on('dragend', function(e) {
doEndDrag(e);
}, $document);
this.close = function() {
enableAnimation();
ionic.requestAnimationFrame(function() {
if(side === LEFT) {
el.style.transform = el.style.webkitTransform = 'translate3d(-100%, 0, 0)';
} else {
el.style.transform = el.style.webkitTransform = 'translate3d(100%, 0, 0)';
}
});
};
this.open = function() {
enableAnimation();
ionic.requestAnimationFrame(function() {
if(side === LEFT) {
el.style.transform = el.style.webkitTransform = 'translate3d(0%, 0, 0)';
} else {
el.style.transform = el.style.webkitTransform = 'translate3d(0%, 0, 0)';
}
});
};
}])
.directive('drawer', ['$rootScope', '$ionicGesture', function($rootScope, $ionicGesture) {
return {
restrict: 'E',
controller: 'drawerCtrl',
link: function($scope, $element, $attr, ctrl) {
$element.addClass($attr.side);
$scope.openDrawer = function() {
console.log('open');
ctrl.open();
};
$scope.closeDrawer = function() {
console.log('close');
ctrl.close();
};
}
}
}]);
.directive('drawerClose', ['$rootScope', function($rootScope) {
return {
restrict: 'A',
link: function($scope, $element) {
$element.bind('click', function() {
var drawerCtrl = $element.inheritedData('$drawerController');
drawerCtrl.close();
});
}
}
}]);
})();