2020-12-22 22:25:30 +08:00
|
|
|
/* global windowScrollEvents HelperModule I18n PdfPreview */
|
2020-11-04 20:08:40 +08:00
|
|
|
$(document).on('click', '.asset-context-menu .change-preview-type', function(e) {
|
|
|
|
var viewModeBtn = $(this);
|
|
|
|
var viewMode = viewModeBtn.data('preview-type');
|
|
|
|
var toggleUrl = viewModeBtn.closest('.dropdown-menu').data('toggle-view-url');
|
|
|
|
var assetId = viewModeBtn.closest('.dropdown-menu').data('asset-id');
|
2020-10-28 18:45:47 +08:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
$.ajax({
|
|
|
|
url: toggleUrl,
|
|
|
|
type: 'PATCH',
|
|
|
|
dataType: 'json',
|
|
|
|
data: { asset: { view_mode: viewMode } },
|
|
|
|
success: function(data) {
|
2020-11-04 20:08:40 +08:00
|
|
|
viewModeBtn.closest('.dropdown-menu').find('.change-preview-type').removeClass('selected');
|
|
|
|
viewModeBtn.addClass('selected');
|
2020-10-28 18:45:47 +08:00
|
|
|
$(`.asset[data-asset-id=${assetId}]`).replaceWith(data.html);
|
2020-12-22 22:25:30 +08:00
|
|
|
PdfPreview.initCanvas();
|
2020-10-28 18:45:47 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-11-04 20:08:40 +08:00
|
|
|
|
|
|
|
$(document).on('click', '.asset .delete-asset', function(e) {
|
|
|
|
var asset = $(this).closest('.asset');
|
2020-11-25 19:50:34 +08:00
|
|
|
var fileName = $(this).data('file-name');
|
2020-11-04 20:08:40 +08:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
$.ajax({
|
|
|
|
url: $(this).attr('href'),
|
|
|
|
type: 'DELETE',
|
|
|
|
dataType: 'json',
|
2020-11-25 19:50:34 +08:00
|
|
|
beforeSend: function(element, ajaxSettings) {
|
|
|
|
var deleteModal = $('.modal-file-delete');
|
|
|
|
var ajax = ajaxSettings;
|
|
|
|
ajax.beforeSend = null;
|
|
|
|
deleteModal.find('.asset-confirmation-description')
|
|
|
|
.html(I18n.t('assets.delete_file_modal.description_1_html', { file_name: fileName }));
|
2020-11-26 21:37:12 +08:00
|
|
|
deleteModal.find('.confirm-button').attr('disabled', false);
|
2020-11-25 19:50:34 +08:00
|
|
|
deleteModal.modal('show');
|
2020-11-26 21:37:12 +08:00
|
|
|
deleteModal.off('click', '.confirm-button').one('click', '.confirm-button', { ajax: ajax }, function(button) {
|
|
|
|
$(this).attr('disabled', true);
|
2020-11-26 21:04:26 +08:00
|
|
|
$.ajax(button.data.ajax);
|
2020-11-25 19:50:34 +08:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
},
|
2020-11-04 20:08:40 +08:00
|
|
|
success: function(result) {
|
|
|
|
asset.remove();
|
|
|
|
HelperModule.flashAlertMsg(result.flash, 'success');
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-23 19:40:23 +08:00
|
|
|
$(document).on('turbolinks:before-visit', (e) => {
|
|
|
|
if ($('.uploading-attachment-container:not(.error)').length) {
|
|
|
|
if (confirm(I18n.t('protocols.steps.attachments.new.leaving_warning'))) {
|
2022-05-25 21:04:14 +08:00
|
|
|
return true;
|
2022-05-23 19:40:23 +08:00
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-25 21:04:14 +08:00
|
|
|
return true;
|
2022-05-23 19:40:23 +08:00
|
|
|
});
|
|
|
|
|
2020-11-04 20:08:40 +08:00
|
|
|
var InlineAttachments = (function() {
|
|
|
|
function elementVisible(element) {
|
|
|
|
var elementRect = element.getBoundingClientRect();
|
|
|
|
var elementHeight = $(element).height();
|
|
|
|
return elementRect.top + (elementHeight / 2) >= 0
|
|
|
|
&& elementRect.bottom <= (window.innerHeight || document.documentElement.clientHeight) + (elementHeight / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function showElement(element) {
|
|
|
|
setTimeout(() => {
|
|
|
|
var iframeUrl = $(element).find('.iframe-placeholder').data('iframe-url');
|
|
|
|
if (elementVisible(element) && iframeUrl) {
|
|
|
|
$(element).find('.iframe-placeholder')
|
|
|
|
.replaceWith(`<iframe class="active-iframe-preview" src="${iframeUrl}"></iframe>`);
|
|
|
|
$(element).addClass('active').attr('data-created-at', new Date().getTime());
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hideElement(element) {
|
|
|
|
var iframeUrl = $(element).find('.active-iframe-preview').attr('src');
|
|
|
|
if (!elementVisible(element) && iframeUrl) {
|
|
|
|
$(element).find('iframe')
|
|
|
|
.replaceWith(`<div class="iframe-placeholder" data-iframe-url="${iframeUrl}"></div>`);
|
|
|
|
$(element).removeClass('active').attr('data-created-at', null);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkForAttachmentsState() {
|
|
|
|
$.each($('.inline-attachment-container'), function(i, element) {
|
|
|
|
showElement(element);
|
|
|
|
});
|
|
|
|
if ($('.active-iframe-preview').length > 5) {
|
|
|
|
let sortedIframes = $('.inline-attachment-container.active').sort(function(a, b) {
|
|
|
|
return +a.dataset.createdAt - +b.dataset.createdAt;
|
|
|
|
});
|
|
|
|
$.each(sortedIframes, function(i, element) {
|
|
|
|
if (hideElement(element)) return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 19:55:05 +08:00
|
|
|
function initReloadButtons() {
|
|
|
|
$(document).on('ajax:success', '.asset .reload-asset', function(e, data) {
|
|
|
|
$(this).closest('.asset').replaceWith(data.html);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:08:40 +08:00
|
|
|
return {
|
|
|
|
init: () => {
|
|
|
|
windowScrollEvents.InlineAttachments = InlineAttachments.scrollEvent;
|
|
|
|
},
|
2021-03-22 19:55:05 +08:00
|
|
|
initReloadButtons: () => {
|
|
|
|
initReloadButtons();
|
|
|
|
},
|
2020-11-04 20:08:40 +08:00
|
|
|
scrollEvent: () => {
|
|
|
|
checkForAttachmentsState();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
$(document).on('turbolinks:load', function() {
|
|
|
|
InlineAttachments.init();
|
|
|
|
InlineAttachments.scrollEvent();
|
|
|
|
});
|
2021-03-22 19:55:05 +08:00
|
|
|
InlineAttachments.initReloadButtons();
|