|
|
|
@ -1,84 +1,95 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/* globals define */
|
|
|
|
|
/* globals define, templates, translator */
|
|
|
|
|
|
|
|
|
|
define('uploader', ['csrf'], function(csrf) {
|
|
|
|
|
|
|
|
|
|
var module = {};
|
|
|
|
|
|
|
|
|
|
module.open = function(route, params, fileSize, callback) {
|
|
|
|
|
var uploadModal = $('#upload-picture-modal');
|
|
|
|
|
uploadModal.modal('show').removeClass('hide');
|
|
|
|
|
module.hideAlerts();
|
|
|
|
|
var uploadForm = $('#uploadForm');
|
|
|
|
|
uploadForm[0].reset();
|
|
|
|
|
uploadForm.attr('action', route);
|
|
|
|
|
uploadForm.find('#params').val(JSON.stringify(params));
|
|
|
|
|
|
|
|
|
|
if (fileSize) {
|
|
|
|
|
uploadForm.find('#file-size-block')
|
|
|
|
|
.translateText('[[uploads:maximum-file-size, ' + fileSize + ']]')
|
|
|
|
|
.removeClass('hide');
|
|
|
|
|
} else {
|
|
|
|
|
uploadForm.find('#file-size-block').addClass('hide');
|
|
|
|
|
}
|
|
|
|
|
parseModal(function(uploadModal) {
|
|
|
|
|
uploadModal = $(uploadModal);
|
|
|
|
|
|
|
|
|
|
$('#pictureUploadSubmitBtn').off('click').on('click', function() {
|
|
|
|
|
uploadForm.submit();
|
|
|
|
|
});
|
|
|
|
|
uploadModal.modal('show');
|
|
|
|
|
uploadModal.on('hidden.bs.modal', function() {
|
|
|
|
|
uploadModal.remove();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
uploadForm.off('submit').submit(function() {
|
|
|
|
|
var uploadForm = uploadModal.find('#uploadForm');
|
|
|
|
|
uploadForm.attr('action', route);
|
|
|
|
|
uploadForm.find('#params').val(JSON.stringify(params));
|
|
|
|
|
|
|
|
|
|
function showAlert(type, message) {
|
|
|
|
|
module.hideAlerts();
|
|
|
|
|
uploadModal.find('#alert-' + type).translateText(message).removeClass('hide');
|
|
|
|
|
if (fileSize) {
|
|
|
|
|
uploadForm.find('#file-size-block')
|
|
|
|
|
.translateText('[[uploads:maximum-file-size, ' + fileSize + ']]')
|
|
|
|
|
.removeClass('hide');
|
|
|
|
|
} else {
|
|
|
|
|
uploadForm.find('#file-size-block').addClass('hide');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showAlert('status', '[[uploads:uploading-file]]');
|
|
|
|
|
uploadModal.find('#pictureUploadSubmitBtn').off('click').on('click', function() {
|
|
|
|
|
uploadForm.submit();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
uploadModal.find('#upload-progress-bar').css('width', '0%');
|
|
|
|
|
uploadModal.find('#upload-progress-box').show().removeClass('hide');
|
|
|
|
|
uploadForm.off('submit').submit(function() {
|
|
|
|
|
|
|
|
|
|
if (!$('#userPhotoInput').val()) {
|
|
|
|
|
showAlert('error', '[[uploads:select-file-to-upload]]');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
function showAlert(type, message) {
|
|
|
|
|
module.hideAlerts(uploadModal);
|
|
|
|
|
uploadModal.find('#alert-' + type).translateText(message).removeClass('hide');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$(this).ajaxSubmit({
|
|
|
|
|
headers: {
|
|
|
|
|
'x-csrf-token': csrf.get()
|
|
|
|
|
},
|
|
|
|
|
error: function(xhr) {
|
|
|
|
|
xhr = maybeParse(xhr);
|
|
|
|
|
showAlert('error', xhr.responseJSON ? xhr.responseJSON.error : 'error uploading, code : ' + xhr.status);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
uploadProgress: function(event, position, total, percent) {
|
|
|
|
|
uploadModal.find('#upload-progress-bar').css('width', percent + '%');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
success: function(response) {
|
|
|
|
|
response = maybeParse(response);
|
|
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
|
showAlert('error', response.error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
showAlert('status', '[[uploads:uploading-file]]');
|
|
|
|
|
|
|
|
|
|
callback(response[0].url);
|
|
|
|
|
uploadModal.find('#upload-progress-bar').css('width', '0%');
|
|
|
|
|
uploadModal.find('#upload-progress-box').show().removeClass('hide');
|
|
|
|
|
|
|
|
|
|
showAlert('success', '[[uploads:upload-success]]');
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
module.hideAlerts();
|
|
|
|
|
uploadModal.modal('hide');
|
|
|
|
|
}, 750);
|
|
|
|
|
if (!uploadModal.find('#userPhotoInput').val()) {
|
|
|
|
|
showAlert('error', '[[uploads:select-file-to-upload]]');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
$(this).ajaxSubmit({
|
|
|
|
|
headers: {
|
|
|
|
|
'x-csrf-token': csrf.get()
|
|
|
|
|
},
|
|
|
|
|
error: function(xhr) {
|
|
|
|
|
xhr = maybeParse(xhr);
|
|
|
|
|
showAlert('error', xhr.responseJSON ? xhr.responseJSON.error : 'error uploading, code : ' + xhr.status);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
uploadProgress: function(event, position, total, percent) {
|
|
|
|
|
uploadModal.find('#upload-progress-bar').css('width', percent + '%');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
success: function(response) {
|
|
|
|
|
response = maybeParse(response);
|
|
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
|
showAlert('error', response.error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(response[0].url);
|
|
|
|
|
|
|
|
|
|
showAlert('success', '[[uploads:upload-success]]');
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
module.hideAlerts();
|
|
|
|
|
uploadModal.modal('hide');
|
|
|
|
|
}, 750);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function parseModal(callback) {
|
|
|
|
|
templates.parse('partials/modals/upload_picture_modal', {}, function(html) {
|
|
|
|
|
translator.translate(html, callback);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function maybeParse(response) {
|
|
|
|
|
if (typeof response === 'string') {
|
|
|
|
|
try {
|
|
|
|
@ -90,8 +101,8 @@ define('uploader', ['csrf'], function(csrf) {
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.hideAlerts = function() {
|
|
|
|
|
$('#upload-picture-modal').find('#alert-status, #alert-success, #alert-error, #upload-progress-box').addClass('hide');
|
|
|
|
|
module.hideAlerts = function(modal) {
|
|
|
|
|
modal.find('#alert-status, #alert-success, #alert-error, #upload-progress-box').addClass('hide');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return module;
|
|
|
|
|