/* global TinyMCE I18n animateSpinner importProtocolFromFile */ /* global HelperModule DataTableHelpers GLOBAL_CONSTANTS */ /* eslint-disable no-use-before-define, no-alert, no-restricted-globals, no-underscore-dangle */ //= require my_modules //= require protocols/import_export/import // Currently selected row in "load from protocol" modal var selectedRow = null; function initEditMyModuleDescription() { var viewObject = $('#my_module_description_view'); viewObject.on('click', function(e) { if ($(e.target).hasClass('record-info-link')) return; TinyMCE.init('#my_module_description_textarea'); }).on('click', 'a', function(e) { if ($(this).hasClass('record-info-link')) return; e.stopPropagation(); }); setTimeout(function() { TinyMCE.wrapTables(viewObject); }, 100); } function initEditProtocolDescription() { var viewObject = $('#protocol_description_view'); viewObject.on('click', function(e) { if ($(e.target).hasClass('record-info-link')) return; TinyMCE.init('#protocol_description_textarea', { afterInitCallback: refreshProtocolStatusBar }); }).on('click', 'a', function(e) { if ($(this).hasClass('record-info-link')) return; e.stopPropagation(); }); } function initCopyToRepository() { var link = "[data-action='copy-to-repository']"; var modal = '#copy-to-repository-modal'; var modalBody = '.modal-body'; var submitBtn = ".modal-footer [data-action='submit']"; $('.my-modules-protocols-index') .on('ajax:success', link, function(e, data) { $(modal).find(modalBody).html(data.html); $(modal).find(modalBody).find("[data-role='copy-to-repository']") .on('ajax:success', function(e2, data2) { if (data2.refresh !== null) { // Reload page location.reload(); } else { // Simply hide the modal $(modal).modal('hide'); } }) .on('ajax:error', function(e2, data2) { // Display errors in form $(modal).find(submitBtn)[0].disabled = false; if (data2.status === 422) { $(this).renderFormErrors('protocol', data2.responseJSON); } else { // Simply display global error alert(data2.responseJSON.message); } }); $(modal).modal('show'); $(modal).find(submitBtn)[0].disabled = false; }) .on('ajax:error', function() {}); $(modal).on('click', submitBtn, function() { // Submit the embedded form $(modal).find(submitBtn)[0].disabled = true; $(modal).find('form').submit(); }); $(modal).find(modalBody).on('click', "[data-role='link-check']", function() { var text = $(this).closest('.modal-body').find("[data-role='link-text']"); if ($(this).prop('checked')) { text.show(); } else { text.hide(); } }); $(modal).on('hidden.bs.modal', function() { $(modal).find(modalBody).find("[data-role='copy-to-repository']") .off('ajax:success ajax:error'); $(modal).find(modalBody).html(''); }); } function initLinkUpdate() { var modal = $('#confirm-link-update-modal'); var modalTitle = modal.find('.modal-title'); var modalBody = modal.find('.modal-body'); var updateBtn = modal.find(".modal-footer [data-action='submit']"); $("[data-action='unlink'], [data-action='revert'], [data-action='update-parent'], [data-action='update-self']") .on('ajax:success', function(e, data) { modalTitle.html(data.title); modalBody.html(data.message); updateBtn.text(data.btn_text); modal.attr('data-url', data.url); modal.modal('show'); }); modal.on('hidden.bs.modal', function() { modalBody.html(''); }); if (!$._data(updateBtn[0], 'events')) { updateBtn.on('click', function() { // POST via ajax $.ajax({ url: modal.attr('data-url'), type: 'POST', dataType: 'json', success: function() { // Simply reload page location.reload(); }, error: function(ev) { // Display error message in alert() alert(ev.responseJSON.message); // Hide modal modal.modal('hide'); } }); }); } $('[data-role="protocol-status-bar"] .preview-protocol').click(function(e) { e.preventDefault(); }); } function initLoadFromRepository() { var modal = $('#load-from-repository-modal'); var modalBody = modal.find('.modal-body'); var loadBtn = modal.find(".modal-footer [data-action='submit']"); $("[data-action='load-from-repository']") .on('ajax:success', function(e, data) { modalBody.html(data.html); // Disable load btn loadBtn.attr('disabled', 'disabled'); modal.modal('show'); // Init Datatable on recent tab initLoadFromRepositoryTable(modalBody.find('#load-protocols-datatable')); loadBtn.on('click', function() { loadFromRepository(); }); }); modal.on('hidden.bs.modal', function() { // Destroy the current Datatable destroyLoadFromRepositoryTable(modalBody.find('#load-protocols-datatable')); loadBtn.off('click'); modalBody.html(''); }); } function initLoadFromRepositoryTable(content) { var tableEl = content.find("[data-role='datatable']"); var datatable = tableEl.DataTable({ dom: "R<'main-actions'<'toolbar'><'protocol-filters'f>>t" + "<'pagination-row'<'pagination-info'li><'pagination-actions'p>>", sScrollX: '100%', sScrollXInner: '100%', buttons: [], processing: true, serverSide: true, responsive: true, order: [[5, 'desc']], ajax: { url: tableEl.data('source'), type: 'POST' }, colReorder: { fixedColumnsLeft: 1000000 // Disable reordering }, columnDefs: [{ targets: [0, 3, 4], searchable: true, orderable: true }, { targets: [1, 2, 5], searchable: true, orderable: true, render: function(data) { return `