2019-11-18 21:04:53 +08:00
|
|
|
/*
|
2022-05-26 19:19:38 +08:00
|
|
|
globals HelperModule animateSpinner SmartAnnotation AssetColumnHelper GLOBAL_CONSTANTS I18n
|
2019-11-18 21:04:53 +08:00
|
|
|
*/
|
2022-05-26 19:19:38 +08:00
|
|
|
/* eslint-disable no-alert no-unused-vars */
|
|
|
|
/* eslint-disable no-alert */
|
2019-11-18 21:04:53 +08:00
|
|
|
|
|
|
|
var RepositoryDatatableRowEditor = (function() {
|
|
|
|
const NAME_COLUMN_ID = 'row-name';
|
|
|
|
const TABLE_ROW = '<tr></tr>';
|
|
|
|
const TABLE_CELL = '<td></td>';
|
2020-11-03 05:19:33 +08:00
|
|
|
const EDIT_FORM_CLASS_NAME = 'repository-row-edit-form';
|
2019-11-18 21:04:53 +08:00
|
|
|
|
|
|
|
var TABLE;
|
|
|
|
|
|
|
|
// Initialize SmartAnnotation
|
|
|
|
function initSmartAnnotation($row) {
|
|
|
|
$row.find('[data-object="repository_cell"]').each(function(el) {
|
|
|
|
if (el.data('atwho')) {
|
2022-11-29 20:47:52 +08:00
|
|
|
SmartAnnotation.init(el, true);
|
2019-11-18 21:04:53 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-15 16:14:26 +08:00
|
|
|
function validateAndSubmit($table, $submitButton) {
|
2023-04-25 20:35:45 +08:00
|
|
|
let $forms = $table.find(`.${EDIT_FORM_CLASS_NAME}`);
|
|
|
|
let $row = $forms.closest('tr');
|
2019-12-04 21:52:52 +08:00
|
|
|
let valid = true;
|
|
|
|
let directUrl = $table.data('direct-upload-url');
|
2022-11-21 18:47:46 +08:00
|
|
|
let $files = $table.find('input[type=file]');
|
2019-12-04 21:52:52 +08:00
|
|
|
$row.find('.has-error').removeClass('has-error').find('span').remove();
|
2019-11-18 21:04:53 +08:00
|
|
|
|
2019-12-04 21:52:52 +08:00
|
|
|
// Validations here
|
|
|
|
$row.find('input').each(function() {
|
|
|
|
let dataType = $(this).data('type');
|
|
|
|
if (!dataType) return;
|
|
|
|
|
|
|
|
valid = $.fn.dataTable.render[dataType + 'Validator']($(this));
|
|
|
|
if (!valid) return false;
|
|
|
|
});
|
2019-11-18 21:04:53 +08:00
|
|
|
|
2019-12-04 21:52:52 +08:00
|
|
|
if (!valid) return false;
|
2022-07-15 16:14:26 +08:00
|
|
|
$submitButton.attr('disabled', true);
|
2020-01-21 17:26:24 +08:00
|
|
|
animateSpinner($table, true);
|
2019-12-04 21:52:52 +08:00
|
|
|
// DirectUpload here
|
2020-02-06 21:43:17 +08:00
|
|
|
let uploadPromise = AssetColumnHelper.uploadFiles($files, directUrl);
|
2019-11-18 21:04:53 +08:00
|
|
|
|
2019-12-04 21:52:52 +08:00
|
|
|
// Submission here
|
|
|
|
uploadPromise
|
|
|
|
.then(function() {
|
2023-04-25 20:35:45 +08:00
|
|
|
$forms.submit();
|
|
|
|
|
2019-12-04 21:52:52 +08:00
|
|
|
return false;
|
|
|
|
}).catch((reason) => {
|
2022-05-26 19:19:38 +08:00
|
|
|
if (reason.includes('Status: 403')) {
|
|
|
|
HelperModule.flashAlertMsg(I18n.t('activerecord.errors.storage.limit_reached'), 'danger');
|
|
|
|
} else alert(reason);
|
|
|
|
animateSpinner($table, false);
|
2019-12-04 21:52:52 +08:00
|
|
|
return false;
|
2019-11-18 21:04:53 +08:00
|
|
|
});
|
|
|
|
|
2019-12-04 21:52:52 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function initAssetCellActions($row) {
|
|
|
|
let fileInputs = $row.find('input[type=file]');
|
|
|
|
let deleteButtons = $row.find('.file-upload-button>span.delete-action');
|
|
|
|
|
|
|
|
fileInputs.on('change', function() {
|
|
|
|
let $input = $(this);
|
|
|
|
let $fileBtn = $input.next('.file-upload-button');
|
2020-02-06 21:43:17 +08:00
|
|
|
let $label = $fileBtn.find('label');
|
|
|
|
|
|
|
|
if ($input[0].files[0]) {
|
|
|
|
$label.text($input[0].files[0].name);
|
|
|
|
$fileBtn.removeClass('new-file');
|
|
|
|
} else {
|
|
|
|
$label.text('');
|
|
|
|
$fileBtn.addClass('new-file');
|
|
|
|
}
|
2020-01-16 20:49:34 +08:00
|
|
|
$fileBtn.removeClass('error');
|
2019-11-18 21:04:53 +08:00
|
|
|
});
|
|
|
|
|
2019-12-04 21:52:52 +08:00
|
|
|
|
|
|
|
deleteButtons.on('click', function() {
|
|
|
|
let $fileBtn = $(this).parent();
|
|
|
|
let $input = $fileBtn.prev('input[type=file]');
|
2020-02-06 21:43:17 +08:00
|
|
|
let $label = $fileBtn.find('label');
|
2019-12-04 21:52:52 +08:00
|
|
|
|
|
|
|
$fileBtn.addClass('new-file');
|
|
|
|
$label.text('');
|
|
|
|
$input.val('');
|
2020-01-16 20:49:34 +08:00
|
|
|
$fileBtn.removeClass('error');
|
2019-12-04 21:52:52 +08:00
|
|
|
|
|
|
|
if (!$input.data('is-empty')) { // set hidden field for deletion only if original value has been set on rendering
|
|
|
|
$input
|
|
|
|
.prev('.file-hidden-field-container')
|
2019-12-18 18:55:12 +08:00
|
|
|
.html(`<input type="hidden"
|
|
|
|
form="${$input.attr('form')}"
|
|
|
|
name="repository_cells[${$input.data('col-id')}]"
|
2019-12-10 21:18:00 +08:00
|
|
|
value=""/>`);
|
2019-12-04 21:52:52 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initFormSubmitAction(table) {
|
|
|
|
TABLE = table;
|
|
|
|
let $table = $(TABLE.table().node());
|
|
|
|
|
2020-02-06 23:44:55 +08:00
|
|
|
$table.on('ajax:success', `.${EDIT_FORM_CLASS_NAME}`, function(ev, data) {
|
2023-04-25 20:35:45 +08:00
|
|
|
HelperModule.flashAlertMsg(data.flash, 'success');
|
2019-11-18 21:04:53 +08:00
|
|
|
});
|
|
|
|
|
2020-02-06 23:44:55 +08:00
|
|
|
$table.on('ajax:error', `.${EDIT_FORM_CLASS_NAME}`, function(ev, data) {
|
2020-06-05 04:42:48 +08:00
|
|
|
$(TABLE.nodes()).find('.spinner').remove();
|
2020-01-15 22:23:03 +08:00
|
|
|
HelperModule.flashAlertMsg(data.responseJSON.flash, 'danger');
|
2019-11-18 21:04:53 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addNewRow(table) {
|
|
|
|
TABLE = table;
|
|
|
|
let $row = $(TABLE_ROW);
|
|
|
|
const formId = 'repositoryNewRowForm';
|
|
|
|
let actionUrl = $(TABLE.table().node()).data('createRecord');
|
|
|
|
let rowForm = $(`
|
2019-11-20 22:05:44 +08:00
|
|
|
<td>
|
|
|
|
<form id="${formId}"
|
2020-11-03 05:19:33 +08:00
|
|
|
class="${EDIT_FORM_CLASS_NAME} ${GLOBAL_CONSTANTS.HAS_UNSAVED_DATA_CLASS_NAME}"
|
2019-11-20 22:05:44 +08:00
|
|
|
action="${actionUrl}"
|
|
|
|
method="post"
|
|
|
|
data-remote="true">
|
|
|
|
</form>
|
|
|
|
</td>
|
2019-11-18 21:04:53 +08:00
|
|
|
`);
|
|
|
|
|
|
|
|
// First two columns are always present and visible
|
2019-11-20 22:05:44 +08:00
|
|
|
$row.append(rowForm);
|
2020-01-27 21:39:45 +08:00
|
|
|
$row.addClass('editing');
|
2019-11-18 21:04:53 +08:00
|
|
|
$row.append($(TABLE_CELL));
|
|
|
|
|
2019-12-05 16:19:19 +08:00
|
|
|
$(TABLE.table().node()).find('tbody').prepend($row);
|
|
|
|
|
2019-11-18 21:04:53 +08:00
|
|
|
table.columns().every(function() {
|
|
|
|
let column = this;
|
|
|
|
let $header = $(column.header());
|
|
|
|
|
|
|
|
if (column.index() < 2) return;
|
|
|
|
if (!column.visible()) return;
|
|
|
|
|
|
|
|
let columnId = $header.attr('id');
|
|
|
|
|
2019-12-05 16:19:19 +08:00
|
|
|
let $cell = $(TABLE_CELL).appendTo($row);
|
|
|
|
|
2019-11-18 21:04:53 +08:00
|
|
|
if (columnId === NAME_COLUMN_ID) {
|
2019-12-05 16:19:19 +08:00
|
|
|
$.fn.dataTable.render.newRowName(formId, $cell);
|
2019-11-18 21:04:53 +08:00
|
|
|
} else {
|
|
|
|
let dataType = $header.data('type');
|
|
|
|
if (dataType) {
|
2019-12-18 18:55:12 +08:00
|
|
|
$.fn.dataTable.render['new' + dataType](formId, columnId, $cell, $header);
|
2019-11-18 21:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
initSmartAnnotation($row);
|
2020-01-16 20:49:34 +08:00
|
|
|
initAssetCellActions($row);
|
2019-11-18 21:04:53 +08:00
|
|
|
|
2023-05-29 04:36:52 +08:00
|
|
|
$(`#${formId}`).on('ajax:complete', function() {
|
|
|
|
$('html, body').animate({ scrollLeft: 0 }, 300);
|
|
|
|
TABLE.ajax.reload(() => {
|
|
|
|
animateSpinner(null, false);
|
|
|
|
}, false);
|
2023-05-29 15:55:31 +08:00
|
|
|
});
|
2023-05-29 04:36:52 +08:00
|
|
|
|
2019-11-18 21:04:53 +08:00
|
|
|
TABLE.columns.adjust();
|
|
|
|
}
|
|
|
|
|
|
|
|
function switchRowToEditMode(row) {
|
|
|
|
let $row = $(row.node());
|
|
|
|
let itemId = row.id();
|
|
|
|
let formId = `repositoryRowForm${itemId}`;
|
2019-11-29 19:52:27 +08:00
|
|
|
let requestUrl = $(TABLE.table().node()).data('current-uri');
|
2019-11-18 21:04:53 +08:00
|
|
|
let rowForm = $(`
|
|
|
|
<form id="${formId}"
|
2020-11-03 05:19:33 +08:00
|
|
|
class="${EDIT_FORM_CLASS_NAME} ${GLOBAL_CONSTANTS.HAS_UNSAVED_DATA_CLASS_NAME}"
|
2019-11-18 21:04:53 +08:00
|
|
|
action="${row.data().recordUpdateUrl}"
|
|
|
|
method="patch"
|
|
|
|
data-row-id="${itemId}">
|
|
|
|
<input name="id" type="hidden" value="${itemId}" />
|
2019-11-29 19:52:27 +08:00
|
|
|
<input name="request_url" type="hidden" value="${requestUrl}" />
|
2019-11-18 21:04:53 +08:00
|
|
|
</form>
|
|
|
|
`);
|
|
|
|
|
2019-11-20 22:05:44 +08:00
|
|
|
$row.find('td').first().append(rowForm);
|
2020-01-27 21:39:45 +08:00
|
|
|
$row.addClass('editing');
|
2019-11-18 21:04:53 +08:00
|
|
|
|
|
|
|
TABLE.cells(row.index(), row.columns().eq(0)).every(function() {
|
2019-11-29 19:52:27 +08:00
|
|
|
let $header = $(TABLE.columns(this.index().column).header());
|
|
|
|
let columnId = $header.attr('id');
|
|
|
|
let dataType = $header.data('type');
|
2019-11-18 21:04:53 +08:00
|
|
|
let cell = this;
|
|
|
|
|
2019-12-02 15:08:49 +08:00
|
|
|
if (!cell.column(cell.index().column).visible()) return true; // return if cell is not visible
|
|
|
|
|
2019-11-18 21:04:53 +08:00
|
|
|
if (columnId === NAME_COLUMN_ID) {
|
|
|
|
$.fn.dataTable.render.editRowName(formId, cell);
|
2019-11-29 19:52:27 +08:00
|
|
|
} else if (dataType) {
|
2019-12-18 18:55:12 +08:00
|
|
|
$.fn.dataTable.render['edit' + dataType](formId, columnId, cell, $header);
|
2019-11-18 21:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
initSmartAnnotation($row);
|
2019-12-04 21:52:52 +08:00
|
|
|
initAssetCellActions($row);
|
2019-11-18 21:04:53 +08:00
|
|
|
|
|
|
|
TABLE.columns.adjust();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.freeze({
|
2020-02-06 23:44:55 +08:00
|
|
|
EDIT_FORM_CLASS_NAME: EDIT_FORM_CLASS_NAME,
|
2019-11-18 21:04:53 +08:00
|
|
|
initFormSubmitAction: initFormSubmitAction,
|
2019-12-04 21:52:52 +08:00
|
|
|
validateAndSubmit: validateAndSubmit,
|
2019-11-18 21:04:53 +08:00
|
|
|
switchRowToEditMode: switchRowToEditMode,
|
|
|
|
addNewRow: addNewRow
|
|
|
|
});
|
|
|
|
}());
|