|
13 | 13 |
|
14 | 14 | var module = angular.module('ngCroppie', []);
|
15 | 15 |
|
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 |
| - */ |
23 | 16 | module.directive('ngCroppie', ['$timeout', function ($timeout) {
|
24 | 17 | return {
|
25 | 18 | restrict: 'AE',
|
|
40 | 33 | },
|
41 | 34 | link: function (scope, elem, attr) {
|
42 | 35 | // 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 }; |
45 | 38 | }
|
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 }; |
48 | 41 | }
|
49 | 42 |
|
50 | 43 | // catches
|
51 | 44 | if (scope.mobile === 'true') {
|
52 | 45 | scope.viewport.w = 250; scope.viewport.h = 250;
|
53 | 46 | scope.boundry.w = 300; scope.boundry.h = 300;
|
54 | 47 | } 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; |
59 | 52 | }
|
60 | 53 |
|
61 | 54 | // viewport cannot be larger than the boundaries
|
|
67 | 60 | }
|
68 | 61 |
|
69 | 62 | // 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'); |
73 | 66 |
|
74 | 67 | // define options
|
75 | 68 | var options = {
|
|
89 | 82 | enableOrientation: scope.orientation
|
90 | 83 | };
|
91 | 84 |
|
92 |
| - if (scope.update != undefined) { |
| 85 | + if (typeof scope.update !== 'undefined') { |
93 | 86 | options.update = scope.update;
|
94 | 87 | }
|
95 | 88 |
|
|
115 | 108 | }, false);
|
116 | 109 |
|
117 | 110 | // check mouseZoom property to avoid needless event listener initialization
|
| 111 | + // separated "wheel" event listener to prevent conflict with Croppie default "wheel" event listener |
118 | 112 | 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) { |
122 | 115 | 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; |
128 | 119 | });
|
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 | + |
130 | 133 | }, false);
|
131 | 134 | }
|
132 | 135 |
|
|
147 | 150 |
|
148 | 151 | // image rotation
|
149 | 152 | scope.$watch('rotation', function(newValue, oldValue) {
|
150 |
| - if (scope.orientation === 'false' || scope.orientation == undefined) { |
| 153 | + if (scope.orientation === 'false' || typeof scope.orientation === 'undefined') { |
151 | 154 | throw 'ngCroppie: Cannot rotate without \'orientation\' option';
|
152 | 155 | } else {
|
153 | 156 | c.rotate(newValue - oldValue);
|
|
161 | 164 |
|
162 | 165 | // respond to changes in src
|
163 | 166 | scope.$watch('src', function(newValue, oldValue) {
|
164 |
| - if (scope.src != undefined) { |
| 167 | + if (typeof scope.src !== 'undefined') { |
165 | 168 | c.bind(scope.src);
|
166 |
| - $timeout(function() { //delayed for ng-file-upload |
| 169 | + $timeout(function() { // delay for the ng-file-upload |
167 | 170 | c.result('canvas').then(function(img) {
|
168 | 171 | scope.$apply(function () {
|
169 | 172 | scope.ngModel = img;
|
|
0 commit comments