/* global I18n HelperModule truncateLongString animateSpinner RepositoryListColumnType */ /* global RepositoryDatatable RepositoryStatusColumnType RepositoryChecklistColumnType dropdownSelector */ /* eslint-disable no-restricted-globals */ //= require jquery-ui/widgets/sortable var RepositoryColumns = (function() { var TABLE_ID = ''; var TABLE = null; var columnsList = '#repository-columns-list'; var manageModal = '#manage-repository-column'; var columnTypeClassNames = { RepositoryListValue: 'RepositoryListColumnType', RepositoryStatusValue: 'RepositoryStatusColumnType', RepositoryDateValue: 'RepositoryDateColumnType', RepositoryDateTimeValue: 'RepositoryDateTimeColumnType', RepositoryTimeValue: 'RepositoryTimeColumnType', RepositoryChecklistValue: 'RepositoryChecklistColumnType', RepositoryNumberValue: 'RepositoryNumberColumnType' }; function reloadDataTablePartial() { // Append buttons for inventory datatable $('div.toolbar-filter-buttons').appendTo('.repository-show'); $('div.toolbar-filter-buttons').hide(); // destroy datatable and remove partial TABLE.destroy(); $('.repository-table').remove(); // reload datatable partial and intialize DataTable $.get($('.repository-show').data('table-url'), (response) => { $(response.html).appendTo($('.repository-show')); RepositoryDatatable.init('#' + $('.repository-table table').attr('id')); RepositoryDatatable.redrawTableOnSidebarToggle(); // show manage columns index modal setTimeout(function() { $(manageModal).find('.back-to-column-modal').trigger('click'); }, 500); }); } function initColumnTypeSelector() { var $manageModal = $(manageModal); $manageModal.on('change', '#repository-column-data-type', function() { $('.column-type').hide(); $('[data-column-type="' + $(this).val() + '"]').show(); }); } function initDeleteSubmitAction() { var $manageModal = $(manageModal); $manageModal.on('click', '#delete-repo-column-submit', function() { animateSpinner(); $.ajax({ url: $(this).data('delete-url'), type: 'DELETE', dataType: 'json', success: (result) => { reloadDataTablePartial(); animateSpinner(null, false); HelperModule.flashAlertMsg(result.message, 'success'); }, error: (result) => { animateSpinner(null, false); HelperModule.flashAlertMsg(result.responseJSON.error, 'danger'); } }); }); } function checkData() { var currentPartial = $('#repository-column-data-type').val(); if (columnTypeClassNames[currentPartial]) { return eval(columnTypeClassNames[currentPartial]) .checkValidation(); } return true; } function addSpecificParams(type, params) { var allParams = params; var columnParams; var specificParams; var currentPartial = $('#repository-column-data-type').val(); if (columnTypeClassNames[currentPartial]) { specificParams = eval(columnTypeClassNames[currentPartial]).loadParams(); columnParams = Object.assign(params.repository_column, specificParams); allParams.repository_column = columnParams; } return allParams; } function initCreateSubmitAction() { var $manageModal = $(manageModal); $manageModal.on('click', '#new-repo-column-submit', function() { var url = $('#repository-column-data-type').find(':selected').data('create-url'); var params = { repository_column: { name: $('#repository-column-name').val() } }; var selectedType = $('#repository-column-data-type').val(); params = addSpecificParams(selectedType, params); if (!checkData()) return; $.ajax({ url: url, type: 'POST', data: JSON.stringify(params), contentType: 'application/json', success: function(result) { reloadDataTablePartial(); HelperModule.flashAlertMsg(result.data.attributes.message, 'success'); }, error: function(error) { $('#new-repository-column').renderFormErrors('repository_column', error.responseJSON.repository_column, true); } }); }); } function initEditSubmitAction() { var $manageModal = $(manageModal); $manageModal.on('click', '#update-repo-column-submit', function() { var url = $('#repository-column-data-type').find(':selected').data('edit-url'); var params = { repository_column: { name: $('#repository-column-name').val() } }; var selectedType = $('#repository-column-data-type').val(); params = addSpecificParams(selectedType, params); if (!checkData()) return; $.ajax({ url: url, type: 'PUT', data: JSON.stringify(params), dataType: 'json', contentType: 'application/json', success: function(result) { reloadDataTablePartial(); HelperModule.flashAlertMsg(result.data.attributes.message, 'success'); }, error: function(error) { $('#new-repository-column').renderFormErrors('repository_column', error.responseJSON.repository_column, true); } }); }); } function initManageColumnAction() { var $manageModal = $(manageModal); $manageModal.on('click', '.manage-repo-column', function() { var button = $(this); var modalUrl = button.data('modal-url'); var columnType; var delimiterOptionsRender = function(data) { return `${data.params.icon}${data.label}`; }; var delimiterDropdownConfig = { singleSelect: true, noEmptyOption: true, selectAppearance: 'simple', closeOnSelect: true, optionClass: 'delimiter-icon-dropdown', optionLabel: delimiterOptionsRender, tagClass: 'delimiter-icon-dropdown', tagLabel: delimiterOptionsRender, disableSearch: true, labelHTML: true }; $.get(modalUrl, (data) => { $manageModal.find('.modal-content').html(data.html) .find('#repository-column-name') .focus(); if (button.data('action') !== 'destroy') { columnType = $('#repository-column-data-type').val(); dropdownSelector.init('#repository-column-data-type', { noEmptyOption: true, singleSelect: true, closeOnSelect: true, optionClass: 'custom-option', selectAppearance: 'simple', disableSearch: true }); dropdownSelector.init('.list-column-type .delimiter', delimiterDropdownConfig); RepositoryListColumnType.initListDropdown(); RepositoryListColumnType.initListPlaceholder(); dropdownSelector.init('.checklist-column-type .delimiter', delimiterDropdownConfig); RepositoryChecklistColumnType.initChecklistDropdown(); RepositoryChecklistColumnType.initChecklistPlaceholder(); $manageModal .trigger('columnModal::partialLoadedFor' + columnType); RepositoryStatusColumnType.updateLoadedEmojies(); if (button.data('action') === 'new') { $('[data-column-type="RepositoryTextValue"]').show(); $('#new-repo-column-submit').show(); } else { $('#update-repo-column-submit').show(); $('[data-column-type="' + columnType + '"]').show(); } } }).fail(function() { HelperModule.flashAlertMsg(I18n.t('libraries.repository_columns.no_permissions'), 'danger'); }); }); } function generateColumnNameTooltip(name) { var maxLength = $(TABLE_ID).data('max-dropdown-length'); if ($.trim(name).length > maxLength) { return `