/* global I18n DataTableHelpers */ (function(global) { 'use strict'; var DATATABLE; var CHECKED_REPORTS = []; function tableDrowCallback() { checkboxToggleCallback(); initToggleAllCheckboxes(); updateButtons(); } function appendSearchResults(data) { var items = []; if(data.hasOwnProperty('projects')){ $.each(data.projects, function(index, el) { items.push( { 'value': el.path, 'text': el.name, 'disabled': false } ) }); } return items; } function initToggleAllCheckboxes() { $('input[name="select_all"]').change(function() { if($(this).is(':checked')) { $("[data-action='toggle']").prop('checked', true); $('.report-row').addClass('selected'); addAllItems(); } else { $("[data-action='toggle']").prop('checked', false); $('.report-row').removeClass('selected'); removeAllItems(); } updateButtons(); }); } function addAllItems() { $.each($("[data-action='toggle']"), function(i, el) { CHECKED_REPORTS.push($(el).attr('data-report-id')); }) } function removeAllItems() { CHECKED_REPORTS = []; } function renderCheckboxHTML(data) { return `
`; } function renderDocxFile(data) { if (data.error) { return ` ${I18n.t('projects.reports.index.error')} `; } if (data.processing) { return ` ${I18n.t('projects.reports.index.generating')} `; } if (data.preview_url) { return ` ${I18n.t('projects.reports.index.docx')} `; } return `${I18n.t('projects.reports.index.generate')}`; } function renderPdfFile(data) { if (data.error) { return ` ${I18n.t('projects.reports.index.error')} `; } if (data.processing) { return ` ${I18n.t('projects.reports.index.generating')} `; } if (data.preview_url) { return ` ${I18n.t('projects.reports.index.pdf')} `; } return ''; } function appendEditPathToRow(row, data) { $(row).addClass('report-row') .attr('data-edit-path', data['edit']) .attr('data-id', data['0']); } function checkboxToggleCallback() { $("[data-action='toggle']").change(function() { var id = $(this).attr('data-report-id'); if($(this).is(':checked')) { $(this).closest('.report-row').addClass('selected'); CHECKED_REPORTS.push(id); } else { var index = CHECKED_REPORTS.indexOf(id); $(this).closest('.report-row').removeClass('selected'); if(index != -1) { CHECKED_REPORTS.splice(index, 1); } } updateButtons(); }); } function updateButtons() { if (CHECKED_REPORTS.length === 0) { $('.single-object-action, .multiple-object-action').addClass('disabled hidden'); } else if (CHECKED_REPORTS.length === 1) { $('.single-object-action, .multiple-object-action').removeClass('disabled hidden'); let $row = $(`.report-row[data-id=${CHECKED_REPORTS[0]}]`); let pdfProcessing = $row.has('.processing.pdf').length > 0; let docxProcessing = $row.has('.processing.docx').length > 0; let docxGenerate = $row.has('.generate-docx').length > 0; if (pdfProcessing) { $('#updatePdf').addClass('disabled'); } else { $('#updatePdf').removeClass('disabled'); } if (docxGenerate) { $('#requestDocx').removeClass('hidden'); $('#updateDocx').addClass('hidden'); } else { $('#requestDocx').addClass('hidden'); $('#updateDocx').removeClass('hidden'); if (docxProcessing) { $('#updateDocx').addClass('disabled'); } else { $('#updateDocx').removeClass('disabled'); } } } else { $('.single-object-action').removeClass('hidden').addClass('disabled'); $('.multiple-object-action').removeClass('disabled hidden'); } } // INIT function initDatatable() { var $table = $('#reports-table') DATATABLE = $table.dataTable({ dom: "Rt<'pagination-row hidden'<'pagination-info'li><'pagination-actions'p>>", order: [[2, 'desc']], sScrollX: '100%', sScrollXInner: '100%', processing: true, serverSide: true, ajax: $table.data('source'), pagingType: 'simple_numbers', colReorder: { fixedColumnsLeft: 1000000 // Disable reordering }, columnDefs: [{ targets: 0, searchable: false, orderable: false, className: 'dt-body-center', sWidth: '1%', render: renderCheckboxHTML }, { targets: 3, searchable: false, sWidth: '60', render: renderPdfFile }, { targets: 4, searchable: false, sWidth: '60', render: renderDocxFile }], oLanguage: { sSearch: I18n.t('general.filter') }, fnDrawCallback: tableDrowCallback, createdRow: appendEditPathToRow, fnInitComplete: function() { DataTableHelpers.initLengthApearance($table.closest('.dataTables_wrapper')); $('.pagination-row').removeClass('hidden'); } }); } function initEditReport() { $('#edit-report-btn').click(function(e) { e.preventDefault(); animateSpinner(); if (CHECKED_REPORTS.length === 1) { var id = CHECKED_REPORTS[0]; var row = $(".report-row[data-id='" + id + "']"); var url = row.attr('data-edit-path'); $(location).attr('href', url); } }); } function initDeleteReports() { $('#delete-reports-btn').click(function(e) { if (CHECKED_REPORTS.length > 0) { $('#report-ids').attr("value", "[" + CHECKED_REPORTS + "]"); $('#delete-reports-modal').modal("show"); } }); $("#confirm-delete-reports-btn").click(function(e) { animateLoading(); }); } initDatatable(); initEditReport(); initDeleteReports(); })(window);