Skip to content

Commit d491d4f

Browse files
committed
Merge pull request #20 from mokhov/touch
Add reaction to the swipe events on touch devices
2 parents e748934 + 4c93a55 commit d491d4f

File tree

4 files changed

+2494
-1
lines changed

4 files changed

+2494
-1
lines changed

client/core/chitalka-fb2/chitalka-fb2.bt.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = function (bt) {
33

44
bt.match('chitalka-fb2*', function (ctx) {
55
ctx.setInitOption('keyboard', true);
6+
ctx.setInitOption('touch', true);
67
ctx.setInitOption('url', ctx.getParam('url'));
78
ctx.enableAutoInit();
89

client/core/chitalka-fb2/chitalka-fb2.deps.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- hammer
12
- chitalka
23
- unzip
34
- storage

client/core/chitalka/chitalka.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ modules.define(
66
'inherit',
77
'y-extend',
88
'chitalka-ui',
9+
'hammer',
910
'storage'
1011
],
1112
function (
@@ -15,6 +16,7 @@ modules.define(
1516
inherit,
1617
extend,
1718
ChitalkaUI,
19+
Hammer,
1820
Storage
1921
) {
2022

@@ -24,6 +26,15 @@ modules.define(
2426
throw new Error('UNIMPLEMENTED METHOD: ' + method);
2527
};
2628

29+
/**
30+
* Detect if device is touch
31+
* @see http://stackoverflow.com/questions/4817029/whats-the-best-way-to-detect-a-touch-screen-device-using-javascript
32+
*/
33+
var isTouch = function () {
34+
return 'ontouchstart' in window // works on most browsers
35+
|| 'onmsgesturechange' in window; // works on ie10
36+
};
37+
2738
/**
2839
* Расширение объекта Math для вычисления медианы массива
2940
*
@@ -117,7 +128,7 @@ modules.define(
117128
}
118129

119130
if (params.touch) {
120-
this._initTouchEvents();
131+
isTouch() && this._initTouchEvents();
121132
}
122133

123134
this._fontSizeLimits = params.fontSize;
@@ -158,6 +169,22 @@ modules.define(
158169
* в функции выполняется навешивание соответствующих событий
159170
*/
160171
_initTouchEvents: function () {
172+
this._swiper = new Hammer(this.getDomNode()[0]);
173+
174+
this._swiper.on('swipe', function(e) {
175+
var direction = (e.direction === 2)? 'left' : 'right';
176+
177+
switch (direction) {
178+
case 'left':
179+
this.nextPage();
180+
break;
181+
182+
case 'right':
183+
this.previousPage();
184+
break;
185+
}
186+
}.bind(this));
187+
161188
},
162189

163190
_onKeyDown: function (e) {

0 commit comments

Comments
 (0)