-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathng-croppie.js
186 lines (167 loc) · 7.52 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
180
181
182
183
184
185
186
/*!
* 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.2.2
* License: MIT
*/
(function () {
'use strict';
var module = angular.module('ngCroppie', []);
module.directive('ngCroppie', ['$timeout', function ($timeout) {
return {
restrict: 'AE',
scope: {
src: '=',
rotation: '=',
viewport: '=',
boundry: '=',
type: '@',
zoom: '@',
mousezoom: '@',
zoomslider: '@',
exif: '@',
enforceBoundary: '@',
orientation: '@',
update: '=',
ngModel: '=',
mobile: '@'
},
link: function (scope, elem, attr) {
// defaults
if (typeof scope.viewport === 'undefined') {
scope.viewport = { w: null, h: null };
}
if (typeof 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 = (typeof scope.viewport.w !== 'undefined') ? scope.viewport.w : 300;
scope.viewport.h = (typeof scope.viewport.h !== 'undefined') ? scope.viewport.h : 300;
scope.boundry.w = (typeof scope.boundry.w !== 'undefined') ? scope.boundry.w : 400;
scope.boundry.h = (typeof 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,
enforceBoundary: scope.enforceBoundary,
enableOrientation: scope.orientation
};
if (typeof 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;
// TODO: needs for investigation
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;
});
});
}, 100);
}, false);
// check mouseZoom property to avoid needless event listener initialization
// separated "wheel" event listener to prevent conflict with Croppie default "wheel" event listener
if (mouseZoom) {
// IE9, Chrome, Opera, Safari
croppieBody.addEventListener('mousewheel', function(evt) {
evt.preventDefault();
c.result('canvas').then(function(img) {
scope.$apply(function() {
scope.ngModel = img;
});
});
}, false);
// Firefox
croppieBody.addEventListener('DOMMouseScroll', function(evt) {
evt.preventDefault();
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' || typeof 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 (typeof scope.src === 'undefined') {
throw 'ngCroppie: Image source undefined!'
} else {
c.bind(scope.src);
window.setInterval(function() { // force delay for the ng-file-upload
c.result('canvas').then(function(img) {
scope.$apply(function () {
scope.ngModel = img;
});
});
}, 0);
}
});
}
};
}]);
}());