forked from Mango/slideout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
223 lines (190 loc) · 5.59 KB
/
index.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'use strict';
/**
* Module dependencies
*/
var decouple = require('decouple');
/**
* Privates
*/
var scrollTimeout;
var scrolling = false;
var doc = window.document;
var html = doc.documentElement;
var msPointerSupported = window.navigator.msPointerEnabled;
var touch = {
'start': msPointerSupported ? 'MSPointerDown' : 'touchstart',
'move': msPointerSupported ? 'MSPointerMove' : 'touchmove',
'end': msPointerSupported ? 'MSPointerUp' : 'touchend'
};
var prefix = (function prefix() {
var regex = /^(Webkit|Khtml|Moz|ms|O)(?=[A-Z])/;
var styleDeclaration = doc.getElementsByTagName('script')[0].style;
for (var prop in styleDeclaration) {
if (regex.test(prop)) {
return '-' + prop.match(regex)[0].toLowerCase() + '-';
}
}
// Nothing found so far? Webkit does not enumerate over the CSS properties of the style object.
// However (prop in style) returns the correct value, so we'll have to test for
// the precence of a specific property
if ('WebkitOpacity' in styleDeclaration) { return '-webkit-'; }
if ('KhtmlOpacity' in styleDeclaration) { return '-khtml-'; }
return '';
}());
/**
* Slideout constructor
*/
function Slideout(options) {
options = options || {};
// Sets default values
this._startOffsetX = 0;
this._currentOffsetX = 0;
this._opening = false;
this._moved = false;
this._opened = false;
this._preventOpen = false;
// Sets panel
this.panel = options.panel;
this.menu = options.menu;
// Sets classnames
this.panel.className += ' slideout-panel';
this.menu.className += ' slideout-menu';
// Sets options
this._fx = options.fx || 'ease';
this._duration = parseInt(options.duration, 10) || 300;
this._tolerance = parseInt(options.tolerance, 10) || 70;
this._padding = parseInt(options.padding, 10) || 256;
// Init touch events
this._initTouchEvents();
}
/**
* Opens the slideout menu.
*/
Slideout.prototype.open = function() {
var self = this;
if (html.className.search('slideout-open') === -1) { html.className += ' slideout-open'; }
this._setTransition();
this._translateXTo(this._padding);
this._opened = true;
setTimeout(function() {
self.panel.style.transition = self.panel.style['-webkit-transition'] = '';
}, this._duration + 50);
return this;
};
/**
* Closes slideout menu.
*/
Slideout.prototype.close = function() {
var self = this;
if (!this.isOpen() && !this._opening) { return this; }
this._setTransition();
this._translateXTo(0);
this._opened = false;
setTimeout(function() {
html.className = html.className.replace(/ slideout-open/, '');
self.panel.style.transition = self.panel.style['-webkit-transition'] = '';
}, this._duration + 50);
return this;
};
/**
* Toggles (open/close) slideout menu.
*/
Slideout.prototype.toggle = function() {
return this.isOpen() ? this.close() : this.open();
};
/**
* Returns true if the slideout is currently open, and false if it is closed.
*/
Slideout.prototype.isOpen = function() {
return this._opened;
};
/**
* Translates panel and updates currentOffset with a given X point
*/
Slideout.prototype._translateXTo = function(translateX) {
this._currentOffsetX = translateX;
this.panel.style[prefix + 'transform'] = this.panel.style.transform = 'translate3d(' + translateX + 'px, 0, 0)';
};
/**
* Set transition properties
*/
Slideout.prototype._setTransition = function() {
this.panel.style[prefix + 'transition'] = this.panel.style.transition = prefix + 'transform ' + this._duration + 'ms ' + this._fx;
};
/**
* Initializes touch event
*/
Slideout.prototype._initTouchEvents = function() {
var self = this;
/**
* Decouple scroll event
*/
decouple(doc, 'scroll', function() {
if (!self._moved) {
clearTimeout(scrollTimeout);
scrolling = true;
scrollTimeout = setTimeout(function() {
scrolling = false;
}, 250);
}
});
/**
* Prevents touchmove event if slideout is moving
*/
doc.addEventListener(touch.move, function(eve) {
if (self._moved) {
eve.preventDefault();
}
});
/**
* Resets values on touchstart
*/
this.panel.addEventListener(touch.start, function(eve) {
self._moved = false;
self._opening = false;
self._startOffsetX = eve.touches[0].pageX;
self._preventOpen = (!self.isOpen() && self.menu.clientWidth !== 0);
});
/**
* Resets values on touchcancel
*/
this.panel.addEventListener('touchcancel', function() {
self._moved = false;
self._opening = false;
});
/**
* Toggles slideout on touchend
*/
this.panel.addEventListener(touch.end, function() {
if (self._moved) {
(self._opening && Math.abs(self._currentOffsetX) > self._tolerance) ? self.open() : self.close();
}
self._moved = false;
});
/**
* Translates panel on touchmove
*/
this.panel.addEventListener(touch.move, function(eve) {
if (scrolling || self._preventOpen) { return; }
var dif_x = eve.touches[0].clientX - self._startOffsetX;
var translateX = self._currentOffsetX = dif_x;
if (Math.abs(translateX) > self._padding) { return; }
if (Math.abs(dif_x) > 20) {
self._opening = true;
if (self._opened && dif_x > 0 || !self._opened && dif_x < 0) { return; }
if (!self._moved && html.className.search('slideout-open') === -1) {
html.className += ' slideout-open';
}
if (dif_x <= 0) {
translateX = dif_x + self._padding;
self._opening = false;
}
self.panel.style[prefix + 'transform'] = self.panel.style.transform = 'translate3d(' + translateX + 'px, 0, 0)';
self._moved = true;
}
});
};
/**
* Expose Slideout
*/
module.exports = Slideout;