-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-modal.js
107 lines (88 loc) · 2.92 KB
/
upload-modal.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
(function () {
'use strict';
angular.module('upload-modal', [])
.directive('uploadModal', ['$http', function ($http) {
return {
restrict: 'E',
compile: function(elem, attrs) {
var $targetImage = elem.next('img');
var $dropzoneModal = $('#dropzone-modal');
var dropzone;
var jcrop;
$targetImage.attr('data-toggle', 'modal');
$targetImage.attr('data-target', '#dropzone-modal');
$targetImage.css('cursor', 'pointer');
return function(scope, elem, attrs) {
dropzone = new Dropzone("#dropzone-form", {
url: '/upload',
maxFilesize: 4, // MB
clickable: '#dropzone-button',
autoProcessQueue: false,
previewTemplate: '<div><img id="dropzone-img" data-dz-thumbnail></div>',
previewsContainer: "#dropzone-preview",
thumbnailWidth: null,
thumbnailHeight: null,
maxFiles: 1,
maxfilesexceeded: function(file) {
this.removeFile(file);
},
init: function() {
this.on('thumbnail', function(file) {
dropzone.disable();
scope.added = true;
scope.$apply();
attachJcrop();
});
}
});
var attachJcrop = function() {
$('#dropzone-img').Jcrop({
bgOpacity: 0.25,
bgColor: 'white',
addClass: 'jcrop-light',
aspectRatio: 1,
minSize: [100, 100],
keySupport: false
}, function() {
jcrop = this;
jcrop.animateTo([50,50,300,300]);
});
};
$dropzoneModal.on('hidden.bs.modal', function () {
scope.resetDropzone();
scope.$apply();
});
$dropzoneModal.mousedown(function (e) {
var $this = $(this);
if( ! $this.has(e.target).length) {
$this.modal('hide');
}
});
scope.uploadImage = function () {
var params = {
selection: jcrop.tellSelect(),
image: $('#dropzone-img').prop('src'),
size: jcrop.getBounds()
};
$http.post(dropzone.options.url, params).then(function (response) {
if (response.data == 0) {
// TODO show error alert
return;
}
$targetImage.attr('src', $targetImage.attr('src') + '?' + (new Date).getTime());
});
$dropzoneModal.modal('hide');
};
scope.resetDropzone = function () {
if(!jcrop) return;
jcrop.destroy();
dropzone.removeAllFiles();
dropzone.enable();
scope.added = false;
};
}
},
templateUrl: '/templates/upload-modal.html'
};
}]);
})();