Skip to content
This repository was archived by the owner on Apr 15, 2020. It is now read-only.

Commit 435d235

Browse files
committed
Added wheel service (pending tests)
1 parent 8d26fbd commit 435d235

File tree

2 files changed

+72
-45
lines changed

2 files changed

+72
-45
lines changed

src/directives/snapscroll.js

+26-45
Original file line numberDiff line numberDiff line change
@@ -86,70 +86,51 @@
8686
});
8787
};
8888

89-
var initWheelEvents = function (scope, element) {
90-
var onWheel,
91-
bindWheel,
92-
unbindWheel;
93-
94-
onWheel = function (e) {
95-
var bubbleUp,
96-
delta;
97-
98-
if (e.originalEvent) {
99-
e = e.originalEvent;
100-
}
101-
102-
e.preventDefault();
103-
104-
delta = Math.max(-1, Math.min(1, (e.wheelDelta || -(e.deltaY || e.detail))));
105-
106-
if (isNaN(delta) || delta === 0) {
107-
return;
89+
var initWheelEvents = function (wheel, scope, element) {
90+
function maybePreventBubbling(e, bubbleUp) {
91+
if (!bubbleUp) {
92+
e.stopPropagation();
10893
}
94+
}
10995

110-
if (delta < 0) {
111-
if (scope.snapDirection !== 1) {
112-
if (scope.snapIndex + 1 > scope.scopeIndexMax()) {
96+
wheel.bind(element, {
97+
up: function (e) {
98+
var bubbleUp;
99+
if (scope.snapDirection !== -1) {
100+
if (scope.snapIndex - 1 < scope.snapIndexMin()) {
113101
bubbleUp = true;
114102
} else {
115103
bubbleUp = false;
116104
scope.$apply(function () {
117-
scope.snapIndex += 1;
105+
scope.snapIndex -= 1;
118106
});
119107
}
120108
}
121-
} else {
122-
if (scope.snapDirection !== -1) {
123-
if (scope.snapIndex - 1 < scope.snapIndexMin()) {
109+
maybePreventBubbling(e, bubbleUp);
110+
},
111+
down: function (e) {
112+
var bubbleUp;
113+
if (scope.snapDirection !== 1) {
114+
if (scope.snapIndex + 1 > scope.scopeIndexMax()) {
124115
bubbleUp = true;
125116
} else {
126117
bubbleUp = false;
127118
scope.$apply(function () {
128-
scope.snapIndex -= 1;
119+
scope.snapIndex += 1;
129120
});
130121
}
131122
}
123+
maybePreventBubbling(e, bubbleUp);
132124
}
125+
});
133126

134-
if (!bubbleUp) {
135-
e.stopPropagation();
136-
}
137-
};
138-
139-
bindWheel = function () {
140-
element.on('wheel mousewheel onmousewheel', onWheel);
141-
};
142-
143-
unbindWheel = function () {
144-
element.off('wheel mousewheel onmousewheel', onWheel);
145-
};
146-
147-
bindWheel();
148-
scope.$on('$destroy', unbindWheel);
127+
scope.$on('$destroy', function () {
128+
wheel.unbind(element);
129+
});
149130
};
150131

151-
var snapscrollAsAnAttribute = ['$timeout', 'scroll', 'defaultSnapscrollScrollDelay', 'defaultSnapscrollSnapDuration', 'defaultSnapscrollBindScrollTimeout',
152-
function ($timeout, scroll, defaultSnapscrollScrollDelay, defaultSnapscrollSnapDuration, defaultSnapscrollBindScrollTimeout) {
132+
var snapscrollAsAnAttribute = ['$timeout', 'scroll', 'wheel', 'defaultSnapscrollScrollDelay', 'defaultSnapscrollSnapDuration', 'defaultSnapscrollBindScrollTimeout',
133+
function ($timeout, scroll, wheel, defaultSnapscrollScrollDelay, defaultSnapscrollSnapDuration, defaultSnapscrollBindScrollTimeout) {
153134
return {
154135
restrict: 'A',
155136
scope: scopeObject,
@@ -290,7 +271,7 @@
290271
scope.$on('$destroy', unbindScroll);
291272
}
292273

293-
initWheelEvents(scope, element);
274+
initWheelEvents(wheel, scope, element);
294275
};
295276

296277
init();

src/services/wheel.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
(function () {
2+
'use strict';
3+
4+
var snapscroll = angular.module('snapscroll');
5+
6+
snapscroll.factory('wheel', [function () {
7+
function onWheel(e, up, down) {
8+
var delta;
9+
10+
if (e.originalEvent) {
11+
e = e.originalEvent;
12+
}
13+
e.preventDefault();
14+
15+
delta = Math.max(-1, Math.min(1, (e.wheelDelta || -(e.deltaY || e.detail))));
16+
if (isNaN(delta) || delta === 0) {
17+
return;
18+
}
19+
20+
if (delta < 0) {
21+
down(e);
22+
} else {
23+
up(e);
24+
}
25+
}
26+
27+
return {
28+
bind: function (element, callbacks) {
29+
function bindWheel(e) {
30+
onWheel(e, callbacks.up, callbacks.down);
31+
}
32+
element.data('snapscroll-bindWheel', bindWheel);
33+
element.on('wheel mousewheel onmousewheel', bindWheel);
34+
},
35+
36+
unbind: function (element) {
37+
var bindWheel = element.data('snapscroll-bindWheel');
38+
if (bindWheel) {
39+
element.data('snapscroll-bindWheel', null);
40+
element.off('wheel mousewheel onmousewheel', bindWheel);
41+
}
42+
}
43+
};
44+
}]);
45+
46+
})();

0 commit comments

Comments
 (0)