You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/public/src/modules/pictureCropper.js

79 lines
2.5 KiB
JavaScript

'use strict';
/* globals define, socket, app, templates */
define('pictureCropper', ['translator', 'cropper'], function (translator, cropper) {
8 years ago
var module = {};
module.handleImageCrop = function (data, callback) {
8 years ago
$('#crop-picture-modal').remove();
templates.parse('modals/crop_picture', {url: data.url}, function (cropperHtml) {
translator.translate(cropperHtml, function (translated) {
var cropperModal = $(translated);
cropperModal.modal('show');
var img = document.getElementById('cropped-image');
var cropperTool = new cropper.default(img, {
aspectRatio: data.aspectRatio,
viewMode: 1
});
cropperModal.find('.rotate').on('click', function () {
var degrees = this.getAttribute("data-degrees");
cropperTool.rotate(degrees);
});
cropperModal.find('.flip').on('click', function () {
var option = this.getAttribute("data-option");
var method = this.getAttribute("data-method");
method === 'scaleX' ? cropperTool.scaleX(option) : cropperTool.scaleY(option);
this.setAttribute("data-option", option * -1);
});
cropperModal.find('.reset').on('click', function () {
cropperTool.reset();
});
cropperModal.find('.crop-btn').on('click', function () {
$(this).addClass('disabled');
var imageData = data.imageType ? cropperTool.getCroppedCanvas().toDataURL(data.imageType) : cropperTool.getCroppedCanvas().toDataURL();
8 years ago
cropperModal.find('#upload-progress-bar').css('width', '100%');
cropperModal.find('#upload-progress-box').show().removeClass('hide');
8 years ago
var socketData = {};
socketData[data.paramName] = data.paramValue;
socketData['imageData'] = imageData;
8 years ago
socket.emit(data.socketMethod, socketData, function (err, imageData) {
if (err) {
cropperModal.find('#upload-progress-box').hide();
cropperModal.find('.upload-btn').removeClass('disabled');
cropperModal.find('.crop-btn').removeClass('disabled');
return app.alertError(err.message);
}
8 years ago
callback(imageData.url);
cropperModal.modal('hide');
});
8 years ago
});
cropperModal.find('.upload-btn').on('click', function () {
$(this).addClass('disabled');
cropperTool.destroy();
cropperTool = new cropper.default(img, {
viewMode: 1,
autoCropArea: 1
});
8 years ago
cropperModal.find('.crop-btn').trigger('click');
});
});
8 years ago
});
};
return module;
});