This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shower-touch.js
127 lines (104 loc) · 3.69 KB
/
shower-touch.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
/**
* @fileOverview
* Touch events plugin for shower.
*/
shower.modules.define('shower-touch', [
'util.extend'
], function (provide, extend) {
var INTERACTIVE_ELEMENTS = [
'VIDEO', 'AUDIO',
'A', 'BUTTON', 'INPUT'
];
/**
* @class
* Touch events plugin for shower.
* @name plugin.Touch
* @param {Shower} shower
* @param {Object} [options] Plugin options.
* @constructor
*/
function Touch (shower, options) {
options = options || {};
this._shower = shower;
this._setupListeners();
}
extend(Touch.prototype, /** @lends plugin.Touch.prototype */{
destroy: function () {
this._clearListeners();
this._shower = null;
},
_setupListeners: function () {
var shower = this._shower;
this._showerListeners = shower.events.group()
.on('add', this._onSlideAdd, this);
this._bindedTouchStart = this._onTouchStart.bind(this);
this._bindedTouchMove = this._onTouchMove.bind(this);
this._shower.getSlides().forEach(this._addTouchStartListener, this);
document.addEventListener('touchmove', this._bindedTouchMove, true);
},
_clearListeners: function () {
this._showerListeners.offAll();
this._shower.getSlides().forEach(this._removeTouchStartListener, this);
document.removeEventListener('touchmove', this._bindedTouchMove, false);
},
_onSlideAdd: function (event) {
var slide = event.get('slide');
this._addTouchStartListener(slide);
},
_addTouchStartListener: function (slide) {
var element = slide.layout.getElement();
element.addEventListener('touchstart', this._bindedTouchStart, false);
},
_removeTouchStartListener: function (slide) {
var element = slide.layout.getElement();
element.removeEventListener('touchstart', this._bindedTouchStart, false);
},
_onTouchStart: function (e) {
var shower = this._shower;
var isSlideMode = shower.container.isSlideMode();
var element = e.target;
var slide = this._getSlideByElement(e.currentTarget);
var x;
if (slide) {
if (isSlideMode && !this._isInteractiveElement(element)) {
e.preventDefault();
x = e.touches[0].pageX;
if (x > window.innerWidth / 2) {
shower.player.next();
} else {
shower.player.prev();
}
}
if (!isSlideMode) {
// Go && turn on slide mode.
slide.activate();
}
}
},
_onTouchMove: function (e) {
if (this._shower.container.isSlideMode()) {
e.preventDefault();
}
},
_getSlideByElement: function (element) {
var slides = this._shower.getSlides();
var result = null;
for (var i = 0, k = slides.length; i < k; i++) {
if (element.id === slides[i].getId()) {
result = this._shower.get(i);
break;
}
}
return result;
},
_isInteractiveElement: function (element) {
return INTERACTIVE_ELEMENTS.some(function (elName) {
return elName === element.tagName;
});
}
});
provide(Touch);
});
shower.modules.require(['shower'], function (sh) {
sh.plugins.add('shower-touch');
});