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

Commit 8ef6150

Browse files
committed
Renamed wheel service to mousewheel and added it's tests
Closes #18
1 parent 0f26f24 commit 8ef6150

File tree

5 files changed

+296
-61
lines changed

5 files changed

+296
-61
lines changed

src/directives/snapscroll.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@
8686
});
8787
};
8888

89-
var initWheelEvents = function (wheel, scope, element) {
89+
var initWheelEvents = function (mousewheel, scope, element) {
9090
function maybePreventBubbling(e, bubbleUp) {
9191
if (!bubbleUp) {
9292
e.stopPropagation();
9393
}
9494
}
9595

96-
wheel.bind(element, {
96+
mousewheel.bind(element, {
9797
up: function (e) {
98+
e.preventDefault();
99+
98100
var bubbleUp;
99101
if (scope.snapDirection !== 'down') {
100102
if (scope.snapIndex - 1 < scope.snapIndexMin()) {
@@ -106,9 +108,12 @@
106108
});
107109
}
108110
}
111+
109112
maybePreventBubbling(e, bubbleUp);
110113
},
111114
down: function (e) {
115+
e.preventDefault();
116+
112117
var bubbleUp;
113118
if (scope.snapDirection !== 'up') {
114119
if (scope.snapIndex + 1 > scope.scopeIndexMax()) {
@@ -120,17 +125,18 @@
120125
});
121126
}
122127
}
128+
123129
maybePreventBubbling(e, bubbleUp);
124130
}
125131
});
126132

127133
scope.$on('$destroy', function () {
128-
wheel.unbind(element);
134+
mousewheel.unbind(element);
129135
});
130136
};
131137

132-
var snapscrollAsAnAttribute = ['$timeout', 'scroll', 'wheel', 'defaultSnapscrollScrollDelay', 'defaultSnapscrollSnapDuration', 'defaultSnapscrollBindScrollTimeout',
133-
function ($timeout, scroll, wheel, defaultSnapscrollScrollDelay, defaultSnapscrollSnapDuration, defaultSnapscrollBindScrollTimeout) {
138+
var snapscrollAsAnAttribute = ['$timeout', 'scroll', 'mousewheel', 'defaultSnapscrollScrollDelay', 'defaultSnapscrollSnapDuration', 'defaultSnapscrollBindScrollTimeout',
139+
function ($timeout, scroll, mousewheel, defaultSnapscrollScrollDelay, defaultSnapscrollSnapDuration, defaultSnapscrollBindScrollTimeout) {
134140
return {
135141
restrict: 'A',
136142
scope: scopeObject,
@@ -271,7 +277,7 @@
271277
scope.$on('$destroy', unbindScroll);
272278
}
273279

274-
initWheelEvents(wheel, scope, element);
280+
initWheelEvents(mousewheel, scope, element);
275281
};
276282

277283
init();

src/services/wheel.js renamed to src/services/mousewheel.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,45 @@
33

44
var snapscroll = angular.module('snapscroll');
55

6-
snapscroll.factory('wheel', [function () {
6+
snapscroll.factory('mousewheel', [function () {
77
function onWheel(e, up, down) {
88
var delta;
99

1010
if (e.originalEvent) {
1111
e = e.originalEvent;
1212
}
13-
e.preventDefault();
1413

1514
delta = Math.max(-1, Math.min(1, (e.wheelDelta || -(e.deltaY || e.detail))));
1615
if (isNaN(delta) || delta === 0) {
1716
return;
1817
}
1918

20-
if (delta < 0) {
21-
down(e);
19+
if (delta > 0) {
20+
if (up) {
21+
up(e);
22+
}
2223
} else {
23-
up(e);
24+
if (down) {
25+
down(e);
26+
}
2427
}
2528
}
2629

2730
return {
2831
bind: function (element, callbacks) {
32+
callbacks = callbacks || {};
33+
if (angular.isDefined(callbacks.up) && !angular.isFunction(callbacks.up)) {
34+
throw new Error('The \'up\' callback must be a function');
35+
}
36+
37+
if (angular.isDefined(callbacks.down) && !angular.isFunction(callbacks.down)) {
38+
throw new Error('The \'down\' callback must be a function');
39+
}
40+
41+
if (!angular.isDefined(callbacks.up) && !angular.isDefined(callbacks.down)) {
42+
throw new Error('At least one callback (\'up\' or \'down\') must be provided');
43+
}
44+
2945
function bindWheel(e) {
3046
onWheel(e, callbacks.up, callbacks.down);
3147
}
@@ -35,7 +51,7 @@
3551

3652
unbind: function (element) {
3753
var bindWheel = element.data('snapscroll-bindWheel');
38-
if (bindWheel) {
54+
if (angular.isFunction(bindWheel)) {
3955
element.data('snapscroll-bindWheel', null);
4056
element.off('wheel mousewheel onmousewheel', bindWheel);
4157
}

test/spec/directives/snapscroll.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,25 @@ describe('Directive: snapscroll', function () {
158158
element = compileElement(html, true);
159159
element.triggerHandler({
160160
type: 'wheel',
161+
wheelDelta: -120,
162+
detail: 120,
163+
deltaY: 120,
161164
preventDefault: preventDefault
162-
});
165+
}); // mousewheel down
163166
element.triggerHandler({
164167
type: 'mousewheel',
168+
wheelDelta: 120,
169+
detail: -120,
170+
deltaY: -120,
165171
preventDefault: preventDefault
166-
});
172+
}); // mousewheel up
167173
element.triggerHandler({
168174
type: 'onmousewheel',
175+
wheelDelta: -120,
176+
detail: 120,
177+
deltaY: 120,
169178
preventDefault: preventDefault
170-
});
179+
}); // mousewheel down
171180
expect(preventDefault).toHaveBeenCalled();
172181
expect(preventDefault.calls.count()).toBe(3);
173182
}

0 commit comments

Comments
 (0)