-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
106 lines (99 loc) · 4.26 KB
/
main.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
// transform cropper dataURI output to a Blob which Dropzone accepts
var dataURItoBlob = function (dataURI) {
var byteString = atob(dataURI.split(',')[1]);
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], {type: 'image/jpeg'});
};
Dropzone.autoDiscover = false;
var c = 0;
var myDropzone = new Dropzone("#my-dropzone-container", {
addRemoveLinks: true,
parallelUploads: 10,
uploadMultiple: false,
maxFiles: 10,
init: function () {
this.on('success', function (file) {
var $button = $('<a href="#" class="js-open-cropper-modal" data-file-name="' + file.name + '">Crop & Upload</a>');
$(file.previewElement).append($button);
});
}
});
$('#my-dropzone-container').on('click', '.js-open-cropper-modal', function (e) {
e.preventDefault();
var fileName = $(this).data('file-name');
var modalTemplate =
'<div class="modal fade" tabindex="-1" role="dialog">' +
'<div class="modal-dialog modal-lg" role="document">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>' +
'<h4 class="modal-title">Crop</h4>' +
'</div>' +
'<div class="modal-body">' +
'<div class="image-container">' +
'<img id="img-' + ++c + '" src="uploads/' + fileName + '">' +
'</div>' +
'</div>' +
'<div class="modal-footer">' +
'<button type="button" class="btn btn-warning rotate-left"><span class="fa fa-rotate-left"></span></button>' +
'<button type="button" class="btn btn-warning rotate-right"><span class="fa fa-rotate-right"></span></button>' +
'<button type="button" class="btn btn-warning scale-x" data-value="-1"><span class="fa fa-arrows-h"></span></button>' +
'<button type="button" class="btn btn-warning scale-y" data-value="-1"><span class="fa fa-arrows-v"></span></button>' +
'<button type="button" class="btn btn-warning reset"><span class="fa fa-refresh"></span></button>' +
'<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>' +
'<button type="button" class="btn btn-primary crop-upload">Crop & upload</button>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
var $cropperModal = $(modalTemplate);
$cropperModal.modal('show').on("shown.bs.modal", function () {
var cropper = new Cropper(document.getElementById('img-' + c), {
autoCropArea: 1,
movable: false,
cropBoxResizable: true,
rotatable: true
});
var $this = $(this);
$this
.on('click', '.crop-upload', function () {
// get cropped image data
var blob = cropper.getCroppedCanvas().toDataURL();
// transform it to Blob object
var croppedFile = dataURItoBlob(blob);
croppedFile.name = fileName;
var files = myDropzone.getAcceptedFiles();
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file.name === fileName) {
myDropzone.removeFile(file);
}
}
myDropzone.addFile(croppedFile);
$this.modal('hide');
})
.on('click', '.rotate-right', function () {
cropper.rotate(90);
})
.on('click', '.rotate-left', function () {
cropper.rotate(-90);
})
.on('click', '.reset', function () {
cropper.reset();
})
.on('click', '.scale-x', function () {
var $this = $(this);
cropper.scaleX($this.data('value'));
$this.data('value', -$this.data('value'));
})
.on('click', '.scale-y', function () {
var $this = $(this);
cropper.scaleY($this.data('value'));
$this.data('value', -$this.data('value'));
});
});
});