/* global I18n HelperModule truncateLongString animateSpinner RepositoryListColumnType RepositoryStockColumnType */ /* global RepositoryDatatable RepositoryStatusColumnType RepositoryChecklistColumnType dropdownSelector RepositoryDateTimeColumnType */ /* global RepositoryDateColumnType RepositoryDatatable */ /* 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', RepositoryStockValue: 'RepositoryStockColumnType', 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(); }); } 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 status = true; var currentPartial = $('#repository-column-data-type').val(); if ($('#repository-column-name').val().length === 0) { $('#repository-column-name').parent().addClass('error'); status = false; } else { $('#repository-column-name').parent().removeClass('error'); } if (columnTypeClassNames[currentPartial]) { status = eval(columnTypeClassNames[currentPartial]).checkValidation() && status; } return status; } 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, labelHTML: true, optionLabel: function(option) { return `