-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
527 lines (443 loc) · 10.2 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/**
* Module dependencies.
*/
var transitionend = require('transitionend-property');
var transform = require('transform-property');
var touchAction = require('touchaction-property');
var has3d = require('has-translate3d');
var style = require('computed-style');
var Emitter = require('emitter');
var event = require('event');
var events = require('component-events');
var min = Math.min;
var max = Math.max;
/**
* Expose `Swipe`.
*/
module.exports = Swipe;
/**
* Turn `el` into a swipeable list.
*
* @param {Element} el
* @api public
*/
function Swipe(el) {
if (!(this instanceof Swipe)) return new Swipe(el);
if (!el) throw new TypeError('Swipe() requires an element');
this.child = el.children[0];
this.touchAction('none');
this.currentEl = this.children().visible[0];
this.currentVisible = 0;
this.current = 0;
this.el = el;
this.refresh();
this.interval(5000);
this.duration(300);
this.fastThreshold(200);
this.threshold(0.5);
this.show(0, 0, { silent: true });
this.bind();
}
/**
* Mixin `Emitter`.
*/
Emitter(Swipe.prototype);
/**
* Set the swipe threshold to `n`.
*
* This is the factor required for swipe
* to detect when a slide has passed the
* given threshold, and may display the next
* or previous slide. For example the default
* of `.5` means that the user must swipe _beyond_
* half of the side width.
*
* @param {Number} n
* @api public
*/
Swipe.prototype.threshold = function(n){
this._threshold = n;
};
/**
* Set the "fast" swipe threshold to `ms`.
*
* This is the amount of time in milliseconds
* which determines if a swipe was "fast" or not. When
* the swipe's duration is less than `ms` only 1/10th of
* the slide's width must be exceeded to display the previous
* or next slide.
*
* @param {Number} n
* @api public
*/
Swipe.prototype.fastThreshold = function(ms){
this._fastThreshold = ms;
};
/**
* Refresh sizing data.
*
* @api public
*/
Swipe.prototype.refresh = function(){
var children = this.children();
var visible = children.visible.length;
var prev = this.visible || visible;
var i = indexOf(children.visible, this.currentEl);
// we removed/added item(s), update current
if (visible < prev && i <= this.currentVisible && i >= 0) {
this.currentVisible -= this.currentVisible - i;
} else if (visible > prev && i > this.currentVisible) {
this.currentVisible += i - this.currentVisible;
}
this.visible = visible;
this.childWidth = this.el.getBoundingClientRect().width;
this.width = Math.ceil(this.childWidth * visible);
this.child.style.width = this.width + 'px';
this.child.style.height = this.height + 'px';
this.show(this.currentVisible, 0, { silent: true });
};
/**
* Bind event handlers.
*
* @api public
*/
Swipe.prototype.bind = function(){
this.events = events(this.child, this);
this.docEvents = events(document, this);
// standard mouse click events
this.events.bind('mousedown', 'ontouchstart');
this.events.bind('mousemove', 'ontouchmove');
this.docEvents.bind('mouseup', 'ontouchend');
// W3C touch events
this.events.bind('touchstart');
this.events.bind('touchmove');
this.docEvents.bind('touchend');
// MS IE touch events
this.events.bind('PointerDown', 'ontouchstart');
this.events.bind('PointerMove', 'ontouchmove');
this.docEvents.bind('PointerUp', 'ontouchstart');
};
/**
* Unbind event handlers.
*
* @api public
*/
Swipe.prototype.unbind = function(){
this.events.unbind();
this.docEvents.unbind();
};
/**
* Handle touchstart.
*
* @api private
*/
Swipe.prototype.ontouchstart = function(e){
this.transitionDuration(0);
this.dx = 0;
this.updown = null;
var touch = this.getTouch(e);
this.down = {
x: touch.pageX,
y: touch.pageY,
at: new Date()
};
};
/**
* Handle touchmove.
*
* For the first and last slides
* we apply some resistence to help
* indicate that you're at the edges.
*
* @api private
*/
Swipe.prototype.ontouchmove = function(e){
if (!this.down || this.updown) return;
var touch = this.getTouch(e);
// TODO: ignore more than one finger
if (!touch) return;
var down = this.down;
var x = touch.pageX;
var w = this.childWidth;
var i = this.currentVisible;
this.dx = x - down.x;
// determine dy and the slope
if (null == this.updown) {
var y = touch.pageY;
var dy = y - down.y;
var slope = dy / this.dx;
// if is greater than 1 or -1, we're swiping up/down
if (slope > 1 || slope < -1) {
this.updown = true;
return;
} else {
this.updown = false;
}
}
e.preventDefault();
var dir = this.dx < 0 ? 1 : 0;
if (this.isFirst() && 0 == dir) this.dx /= 2;
if (this.isLast() && 1 == dir) this.dx /= 2;
this.translate((i * w) + -this.dx);
};
/**
* Handle touchend.
*
* @api private
*/
Swipe.prototype.ontouchend = function(e){
e.stopPropagation();
if (!this.down) return;
var touch = this.getTouch(e);
// setup
var dx = this.dx;
var x = touch.pageX;
var w = this.childWidth;
// < 200ms swipe
var ms = new Date() - this.down.at;
var threshold = ms < this._fastThreshold ? w / 10 : w * this._threshold;
var dir = dx < 0 ? 1 : 0;
var half = Math.abs(dx) >= threshold;
// clear
this.down = null;
// first -> next
if (this.isFirst() && 1 == dir && half) return this.next();
// first -> first
if (this.isFirst()) return this.prev();
// last -> last
if (this.isLast() && 1 == dir) return this.next();
// N -> N + 1
if (1 == dir && half) return this.next();
// N -> N - 1
if (0 == dir && half) return this.prev();
// N -> N
this.show(this.currentVisible);
};
/**
* Set transition duration to `ms`.
*
* @param {Number} ms
* @return {Swipe} self
* @api public
*/
Swipe.prototype.duration = function(ms){
this._duration = ms;
return this;
};
/**
* Set cycle interval to `ms`.
*
* @param {Number} ms
* @return {Swipe} self
* @api public
*/
Swipe.prototype.interval = function(ms){
this._interval = ms;
return this;
};
/**
* Play through all the elements.
*
* @return {Swipe} self
* @api public
*/
Swipe.prototype.play = function(){
if (this.timer) return;
this.timer = setInterval(this.cycle.bind(this), this._interval);
return this;
};
/**
* Stop playing.
*
* @return {Swipe} self
* @api public
*/
Swipe.prototype.stop = function(){
clearInterval(this.timer);
this.timer = null;
return this;
};
/**
* Show the next slide, when the end
* is reached start from the beginning.
*
* @api public
*/
Swipe.prototype.cycle = function(){
if (this.isLast()) {
this.currentVisible = -1;
this.next();
} else {
this.next();
}
};
/**
* Check if we're on the first visible slide.
*
* @return {Boolean}
* @api public
*/
Swipe.prototype.isFirst = function(){
return this.currentVisible == 0;
};
/**
* Check if we're on the last visible slide.
*
* @return {Boolean}
* @api public
*/
Swipe.prototype.isLast = function(){
return this.currentVisible == this.visible - 1;
};
/**
* Show the previous slide, if any.
*
* @return {Swipe} self
* @api public
*/
Swipe.prototype.prev = function(){
this.show(this.currentVisible - 1);
return this;
};
/**
* Show the next slide, if any.
*
* @return {Swipe} self
* @api public
*/
Swipe.prototype.next = function(){
this.show(this.currentVisible + 1);
return this;
};
/**
* Show slide `i`.
*
* Emits `show `event
*
* @param {Number} i
* @return {Swipe} self
* @api public
*/
Swipe.prototype.show = function(i, ms, options){
options = options || {};
if (null == ms) ms = this._duration;
var self = this;
var children = this.children();
i = max(0, min(i, children.visible.length - 1));
this.currentVisible = i;
this.currentEl = children.visible[i];
this.current = indexOf(children.all, this.currentEl);
this.transitionDuration(ms);
this.translate(this.childWidth * i);
if (!options.silent) {
this.emit('showing', this.current, this.currentEl);
if (!ms) return this;
event.bind(this.child, transitionend, function shown() {
if (self.current == i) self.emit('show', self.current, self.currentEl);
event.unbind(self.child, transitionend, shown);
});
}
return this;
};
/**
* Return children categorized by visibility.
*
* @return {Object}
* @api private
*/
Swipe.prototype.children = function(){
var els = this.child.children;
var ret = {
all: els,
visible: [],
hidden: []
};
for (var i = 0; i < els.length; i++) {
var el = els[i];
if (visible(el)) {
ret.visible.push(el);
} else {
ret.hidden.push(el);
}
}
return ret;
};
/**
* Set transition duration.
*
* @api private
*/
Swipe.prototype.transitionDuration = function(ms){
var s = this.child.style;
s.webkitTransition = ms + 'ms -webkit-transform';
s.MozTransition = ms + 'ms -moz-transform';
s.msTransition = ms + 'ms -ms-transform';
s.OTransition = ms + 'ms -o-transform';
s.transition = ms + 'ms transform';
};
/**
* Translate to `x`.
*
* TODO: use translate component
*
* @api private
*/
Swipe.prototype.translate = function(x){
var s = this.child.style;
x = -x;
if (has3d) {
s[transform] = 'translate3d(' + x + 'px, 0, 0)';
} else {
s[transform] = 'translateX(' + x + 'px)';
}
};
/**
* Sets the "touchAction" CSS style property to `value`.
*
* @api private
*/
Swipe.prototype.touchAction = function(value){
var s = this.child.style;
if (touchAction) {
s[touchAction] = value;
}
};
/**
* Gets the appropriate "touch" object for the `e` event. The event may be from
* a "mouse", "touch", or "Pointer" event, so the normalization happens here.
*
* @api private
*/
Swipe.prototype.getTouch = function(e){
// "mouse" and "Pointer" events just use the event object itself
var touch = e;
if (e.changedTouches && e.changedTouches.length > 0) {
// W3C "touch" events use the `changedTouches` array
touch = e.changedTouches[0];
}
return touch;
};
/**
* Return index of `el` in `els`.
*
* @param {Array} els
* @param {Element} el
* @return {Number}
* @api private
*/
function indexOf(els, el) {
for (var i = 0; i < els.length; i++) {
if (els[i] == el) return i;
}
return -1;
}
/**
* Check if `el` is visible.
*
* @param {Element} el
* @return {Boolean}
* @api private
*/
function visible(el) {
return style(el).display != 'none';
}