scinote-web/app/assets/javascripts/assets/wopi/create_wopi_file.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-03-18 02:23:17 +08:00
// Opens create wopi modal on the click of the button
2019-03-21 15:56:31 +08:00
function applyCreateWopiFileCallback() {
$('.create-wopi-file-btn').off().on('click', function(e) {
2019-03-16 03:59:15 +08:00
var $modal = $('#new-office-file-modal');
2019-03-18 02:23:17 +08:00
$($modal).find('form').clearFormErrors();
2019-03-20 03:41:16 +08:00
$($modal).find('#new-wopi-file-name').val('');
2019-03-16 03:59:15 +08:00
// Append element info to which the new file will be attached
$modal.find('#element_id').val($(this).data('id'));
$modal.find('#element_type').val($(this).data('type'));
$modal.modal('show');
return false;
});
}
2019-03-18 02:23:17 +08:00
// Show errors on create wopi modal
function initCreateWopiFileModal() {
// Ajax actions
2019-03-21 15:56:31 +08:00
$('#new-office-file-modal form')
2019-03-18 02:23:17 +08:00
.on('ajax:success', function(ev, data) {
window.open(data.edit_url, '_blank');
window.focus();
location.reload(); // Reload current page, to display the new element
2019-03-16 03:59:15 +08:00
})
2019-03-18 02:23:17 +08:00
.on('ajax:error', function(ev, response) {
2019-03-21 15:56:31 +08:00
var element;
var msg;
2019-03-18 02:23:17 +08:00
$(this).clearFormErrors();
2019-03-20 15:00:04 +08:00
if (response.status === 400) {
2019-03-18 02:23:17 +08:00
element = $(this).find('#new-wopi-file-name');
msg = response.responseJSON.message.file.toString();
2019-03-20 15:00:04 +08:00
} else if (response.status === 403) {
element = $(this).find('#other-wopi-errors');
2019-03-18 02:23:17 +08:00
msg = I18n.t('assets.create_wopi_file.errors.forbidden');
2019-03-20 15:00:04 +08:00
} else if (response.status === 404) {
element = $(this).find('#other-wopi-errors');
2019-03-18 02:23:17 +08:00
msg = I18n.t('assets.create_wopi_file.errors.not_found');
}
renderFormError(undefined, element, msg);
});
}
2019-03-20 03:41:16 +08:00
function applyImageChangeOnButtons() {
var modal = $('#new-office-file-modal');
2019-03-20 15:00:04 +08:00
modal.find('.btn-group label').off().click(function() {
2019-03-21 15:47:52 +08:00
modal.find('img.act').hide();
modal.find('img.inactive').show();
2019-03-20 03:41:16 +08:00
$(this).find('img.act').show();
$(this).find('img.inactive').hide();
2019-03-20 15:00:04 +08:00
});
2019-03-20 03:41:16 +08:00
// Set default value
modal.find('label#word-btn').click();
}
2019-03-18 02:23:17 +08:00
$(document).ready(function() {
2019-03-16 03:59:15 +08:00
initCreateWopiFileModal();
2019-03-20 03:41:16 +08:00
applyImageChangeOnButtons();
2019-03-18 02:23:17 +08:00
});