Skip to content

Commit 855b571

Browse files
committed
fix scroll wheel in firefox and improve conditional checks
1 parent 7ff04e7 commit 855b571

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

ng-croppie.js

+34-31
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313

1414
var module = angular.module('ngCroppie', []);
1515

16-
/**
17-
* Crops, rotates and zooms the image to this element
18-
*
19-
* 20170724 orif-jr - Added mobile support
20-
* 20170720 orif-jr - Enhanced watchers for src and rotation
21-
* 20170406 orif-jr - Added modularized code procedure
22-
*/
2316
module.directive('ngCroppie', ['$timeout', function ($timeout) {
2417
return {
2518
restrict: 'AE',
@@ -40,22 +33,22 @@
4033
},
4134
link: function (scope, elem, attr) {
4235
// defaults
43-
if (scope.viewport == undefined) {
44-
scope.viewport = {w: null, h: null};
36+
if (typeof scope.viewport === 'undefined') {
37+
scope.viewport = { w: null, h: null };
4538
}
46-
if (scope.boundry == undefined) {
47-
scope.boundry = {w: null, h: null};
39+
if (typeof scope.boundry === 'undefined') {
40+
scope.boundry = { w: null, h: null };
4841
}
4942

5043
// catches
5144
if (scope.mobile === 'true') {
5245
scope.viewport.w = 250; scope.viewport.h = 250;
5346
scope.boundry.w = 300; scope.boundry.h = 300;
5447
} else {
55-
scope.viewport.w = (scope.viewport.w != undefined) ? scope.viewport.w : 300;
56-
scope.viewport.h = (scope.viewport.h != undefined) ? scope.viewport.h : 300;
57-
scope.boundry.w = (scope.boundry.w != undefined) ? scope.boundry.w : 400;
58-
scope.boundry.h = (scope.boundry.h != undefined) ? scope.boundry.h : 400;
48+
scope.viewport.w = (typeof scope.viewport.w !== 'undefined') ? scope.viewport.w : 300;
49+
scope.viewport.h = (typeof scope.viewport.h !== 'undefined') ? scope.viewport.h : 300;
50+
scope.boundry.w = (typeof scope.boundry.w !== 'undefined') ? scope.boundry.w : 400;
51+
scope.boundry.h = (typeof scope.boundry.h !== 'undefined') ? scope.boundry.h : 400;
5952
}
6053

6154
// viewport cannot be larger than the boundaries
@@ -67,9 +60,9 @@
6760
}
6861

6962
// convert string to Boolean
70-
var zoom = (scope.zoom === 'true' || typeof scope.zoom == 'undefined'),
71-
mouseZoom = (scope.mousezoom === 'true' || typeof scope.mousezoom == 'undefined'),
72-
zoomSlider = (scope.zoomslider === 'true' || typeof scope.zoomslider == 'undefined');
63+
var zoom = (scope.zoom === 'true' || typeof scope.zoom === 'undefined'),
64+
mouseZoom = (scope.mousezoom === 'true' || typeof scope.mousezoom === 'undefined'),
65+
zoomSlider = (scope.zoomslider === 'true' || typeof scope.zoomslider === 'undefined');
7366

7467
// define options
7568
var options = {
@@ -89,7 +82,7 @@
8982
enableOrientation: scope.orientation
9083
};
9184

92-
if (scope.update != undefined) {
85+
if (typeof scope.update !== 'undefined') {
9386
options.update = scope.update;
9487
}
9588

@@ -115,18 +108,28 @@
115108
}, false);
116109

117110
// check mouseZoom property to avoid needless event listener initialization
111+
// separated "wheel" event listener to prevent conflict with Croppie default "wheel" event listener
118112
if (mouseZoom) {
119-
// separated "wheel" event listener to prevent conflict with Croppie default "wheel" event listener
120-
croppieBody.addEventListener('wheel', function(evt) {
121-
console.log('Wheel event called');
113+
// IE9, Chrome, Opera, Safari
114+
croppieBody.addEventListener('mousewheel', function(evt) {
122115
evt.preventDefault();
123-
if ((evt.clientX > croppieCanvasRectangle.left) && (evt.clientX < croppieCanvasRectangle.right) && (evt.clientY < croppieCanvasRectangle.bottom) && (evt.clientY > croppieCanvasRectangle.top)) {
124-
c.result('canvas').then(function(img) {
125-
scope.$apply(function() {
126-
scope.ngModel = img;
127-
});
116+
c.result('canvas').then(function(img) {
117+
scope.$apply(function() {
118+
scope.ngModel = img;
128119
});
129-
}
120+
});
121+
122+
}, false);
123+
124+
// Firefox
125+
croppieBody.addEventListener('DOMMouseScroll', function(evt) {
126+
evt.preventDefault();
127+
c.result('canvas').then(function(img) {
128+
scope.$apply(function() {
129+
scope.ngModel = img;
130+
});
131+
});
132+
130133
}, false);
131134
}
132135

@@ -147,7 +150,7 @@
147150

148151
// image rotation
149152
scope.$watch('rotation', function(newValue, oldValue) {
150-
if (scope.orientation === 'false' || scope.orientation == undefined) {
153+
if (scope.orientation === 'false' || typeof scope.orientation === 'undefined') {
151154
throw 'ngCroppie: Cannot rotate without \'orientation\' option';
152155
} else {
153156
c.rotate(newValue - oldValue);
@@ -161,9 +164,9 @@
161164

162165
// respond to changes in src
163166
scope.$watch('src', function(newValue, oldValue) {
164-
if (scope.src != undefined) {
167+
if (typeof scope.src !== 'undefined') {
165168
c.bind(scope.src);
166-
$timeout(function() { //delayed for ng-file-upload
169+
$timeout(function() { // delay for the ng-file-upload
167170
c.result('canvas').then(function(img) {
168171
scope.$apply(function () {
169172
scope.ngModel = img;

ng-croppie.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)