forked from allenJynRoyston/ngCroppie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-croppie.js
179 lines (162 loc) · 7.27 KB
/
ng-croppie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*!
* Angular Croppie Tool (ngCroppie)
* An awesome image cropping and rotating module for AngularJS.
*
* Credits: https://github.com/allenRoyston/ngCroppie/graphs/contributors
* Inspired by Croppie.js https://github.com/Foliotek/Croppie
*
* Version: 1.0.3
* License: MIT
*/
(function () {
'use strict';
var module = angular.module('ngCroppie', []);
/**
* Crops, rotates and zooms the image to this element
*
* 20170724 orif-jr - Added mobile support
* 20170720 orif-jr - Enhanced watchers for src and rotation
* 20170406 orif-jr - Added modularized code procedure
*/
module.directive('ngCroppie', ['$timeout', function ($timeout) {
return {
restrict: 'AE',
scope: {
src: '=',
rotation: '=',
viewport: '=',
boundry: '=',
type: '@',
zoom: '@',
mousezoom: '@',
zoomslider: '@',
exif: '@',
orientation: '@',
update: '=',
ngModel: '=',
mobile: '@'
},
link: function (scope, elem, attr) {
// defaults
if (scope.viewport == undefined) {
scope.viewport = {w: null, h: null};
}
if (scope.boundry == undefined) {
scope.boundry = {w: null, h: null};
}
// catches
if (scope.mobile === 'true') {
scope.viewport.w = 250; scope.viewport.h = 250;
scope.boundry.w = 300; scope.boundry.h = 300;
} else {
scope.viewport.w = (scope.viewport.w != undefined) ? scope.viewport.w : 300;
scope.viewport.h = (scope.viewport.h != undefined) ? scope.viewport.h : 300;
scope.boundry.w = (scope.boundry.w != undefined) ? scope.boundry.w : 400;
scope.boundry.h = (scope.boundry.h != undefined) ? scope.boundry.h : 400;
}
// viewport cannot be larger than the boundaries
if (scope.viewport.w > scope.boundry.w) {
scope.viewport.w = scope.boundry.w
}
if (scope.viewport.h > scope.boundry.h) {
scope.viewport.h = scope.boundry.h
}
// convert string to Boolean
var zoom = (scope.zoom === 'true' || typeof scope.zoom == 'undefined'),
mouseZoom = (scope.mousezoom === 'true' || typeof scope.mousezoom == 'undefined'),
zoomSlider = (scope.zoomslider === 'true' || typeof scope.zoomslider == 'undefined');
// define options
var options = {
viewport: {
width: scope.viewport.w,
height: scope.viewport.h,
type: scope.type || 'square'
},
boundary: {
width: scope.boundry.w,
height: scope.boundry.h
},
enableZoom: zoom,
mouseWheelZoom: mouseZoom,
showZoomer: zoomSlider,
enableExif: scope.exif,
enableOrientation: scope.orientation
};
if (scope.update != undefined) {
options.update = scope.update;
}
// create new croppie and settime for updates
var c = new Croppie(elem[0], options);
// get Croppie elements for further calculations
var croppieBody = angular.element(elem[0])[0];
var croppieCanvas = angular.element(elem[0].getElementsByClassName('cr-boundary'))[0];
var intervalID;
var croppieCanvasRectangle = croppieCanvas.getBoundingClientRect();
// initialize interval only if action registered within ngCroppie container
croppieBody.addEventListener('mousedown', function() {
intervalID = window.setInterval(function() {
c.result('canvas').then(function(img) {
scope.$apply(function() {
scope.ngModel = img;
});
});
}, 250);
}, false);
// check mouseZoom property to avoid needless event listener initialization
if (mouseZoom) {
// separated "wheel" event listener to prevent conflict with Croppie default "wheel" event listener
croppieBody.addEventListener('wheel', function(evt) {
console.log('Wheel event called');
evt.preventDefault();
if ((evt.clientX > croppieCanvasRectangle.left) && (evt.clientX < croppieCanvasRectangle.right) && (evt.clientY < croppieCanvasRectangle.bottom) && (evt.clientY > croppieCanvasRectangle.top)) {
c.result('canvas').then(function(img) {
scope.$apply(function() {
scope.ngModel = img;
});
});
}
}, false);
}
// destroy all created intervals
croppieBody.addEventListener('mouseup', function() {
clearInterval(intervalID);
}, false);
croppieBody.addEventListener('mouseleave', function() {
clearInterval(intervalID);
}, false);
croppieBody.addEventListener('mouseout', function() {
clearInterval(intervalID);
}, false);
scope.$on('$destroy', function (event) {
clearInterval(intervalID);
});
// image rotation
scope.$watch('rotation', function(newValue, oldValue) {
if (scope.orientation === 'false' || scope.orientation == undefined) {
throw 'ngCroppie: Cannot rotate without \'orientation\' option';
} else {
c.rotate(newValue - oldValue);
c.result('canvas').then(function(img) {
scope.$apply(function () {
scope.ngModel = img;
});
});
}
});
// respond to changes in src
scope.$watch('src', function(newValue, oldValue) {
if (scope.src != undefined) {
c.bind(scope.src);
$timeout(function() { //delayed for ng-file-upload
c.result('canvas').then(function(img) {
scope.$apply(function () {
scope.ngModel = img;
});
});
}, 250);
}
});
}
};
}]);
}());