-
Notifications
You must be signed in to change notification settings - Fork 1
/
leap-slide.js
128 lines (107 loc) · 3.89 KB
/
leap-slide.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
!function ($) {
/* SLIDE_CONTROLLER PUBLIC CLASS DEFINITION
* ================================ */
var LeapSlide = function (element, options) {
this.$element = $(element);
this.options = $.extend({}, $.fn.leapSlide.defaults, options);
this._init();
// Change the event binding way (in 'slides.js') to jQuery style
document.removeEventListener(this.options.event, handleBodyKeyDown, false);
$(document).on(this.options.event, handleBodyKeyDown);
};
LeapSlide.prototype = {
constructor: LeapSlide
, _init: function () {
// Listen to Leap Motion
var gestureHandler = $.proxy(this.gestureHandler, this);
Leap.loop({enableGestures: true}, function(obj) {
obj.gestures.forEach(gestureHandler);
});
this.arr = [0];
this.transitioning = false;
this.timer = undefined;
$.support.transition = {end: 'webkitTransitionEnd'};
this.createArrow();
}
, createArrow: function () {
var $arrow = $("<div class='arrow'>")
.append("<div class='arrow-triangle'>")
.append("<div class='arrow-rectangle'>")
.append("<div class='arrow-trailing'>")
.append("<div class='arrow-trailing'>");
$arrow.appendTo("body");
}
, navigate: function (direction) {
if (this.locked()) return;
if (this.onBoundary(direction)) return
if (this.swipeBusted(direction)) return;
this.resetSwipeBuster(direction);
this.lock(direction);
this.$element.trigger($.Event(this.options.event, { keyCode: 38 + direction } ));
}
, locked: function () {
return this.transitioning;
}
, onBoundary: function (direction) {
return (direction == -1 && curSlide <= 0) || (direction == 1 && curSlide >= slideEls.length - 1)
}
, swipeBusted: function (direction) {
return direction == -this.lastDirection;
}
, resetSwipeBuster: function (direction) {
var $arrow = $('.arrow');
this.lastDirection = direction;
clearTimeout(this.timer);
$arrow.stop(true, true);
$arrow.show().addClass(direction == 1 ? 'arrow-left' : 'arrow-right').appendTo('.current');
this.timer = window.setTimeout($.proxy(function () {
this.lastDirection = null;
}, this), this.options.timeout);
$arrow.fadeOut(this.options.timeout, function () {
$(this).removeClass('arrow-right arrow-left');
});
}
, lock: function (direction) {
this.transitioning = true;
$('.current').one($.support.transition.end, $.proxy(function () {
this.transitioning = false;
}, this));
}
, gestureHandler: function (gesture) {
if (gesture.type != 'swipe')
return;
switch (gesture.state) {
case 'start': this.arr = [0]; break;
case 'update': this.arr.push(gesture.direction[0]); break;
case 'stop':
var dir = sum(this.arr) > 0 ? -1 : 1;
this.navigate(this.options.reversed ? -dir : dir);
break;
}
function sum (arr) {
return arr.reduce(function (a, b) {return a+b; });
}
}
};
/* SLIDE_CONTROLLER PLUGIN DEFINITION
* ========================== */
$.fn.leapSlide = function (option) {
var args = Array.prototype.slice.call(arguments);
return this.each(function () {
var $this = $(this)
, data = $this.data('leapSlide')
, options = $.extend({}, $.fn.leapSlide.defaults, $this.data(), typeof option == 'object' && option);
if (!data) $this.data('leapSlide', (data = new LeapSlide(this, options)));
if (typeof option == 'string') data[option]();
});
};
$.fn.leapSlide.defaults = {
timeout: 1500,
reversed: false,
event: 'keydown'
};
$.fn.leapSlide.Constructor = LeapSlide;
}(window.jQuery);
$(function () {
$(document).leapSlide();
});