2019-04-25 17:14:55 +08:00
|
|
|
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "initInlineEditing" }]*/
|
2020-09-08 21:42:30 +08:00
|
|
|
/* global SmartAnnotation HelperModule I18n */
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2023-09-19 19:13:52 +08:00
|
|
|
let inlineEditing = (function() {
|
2020-01-06 23:07:23 +08:00
|
|
|
const SIDEBAR_ITEM_TYPES = ['project', 'experiment', 'my_module', 'repository'];
|
|
|
|
|
2023-09-19 19:13:52 +08:00
|
|
|
const editBlocks = '.inline-init-handler';
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
function appendAfterLabel(container) {
|
|
|
|
if (container.data('label-after')) {
|
|
|
|
$(container.data('label-after')).appendTo(container.find('.view-mode'));
|
2019-05-15 20:59:15 +08:00
|
|
|
}
|
2023-03-31 22:30:26 +08:00
|
|
|
|
2023-04-11 22:47:01 +08:00
|
|
|
if ($(container).data('params-group') === 'protocol' && $(container).hasClass('inline-editing-container')) {
|
2023-04-06 20:31:07 +08:00
|
|
|
$('.view-mode').text(I18n.t('protocols.draft_name', { name: $('.view-mode').text() }));
|
2023-03-31 22:30:26 +08:00
|
|
|
}
|
2019-11-12 18:26:18 +08:00
|
|
|
}
|
2019-05-15 20:59:15 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
function inputField(container) {
|
|
|
|
return container.find('input, textarea');
|
|
|
|
}
|
2019-04-25 17:14:55 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
function initSmartAnnotation(container) {
|
|
|
|
if (container.data('smart-annotation')) {
|
2023-04-25 21:12:19 +08:00
|
|
|
SmartAnnotation.init(inputField(container), false);
|
2019-09-02 16:30:29 +08:00
|
|
|
}
|
2019-11-12 18:26:18 +08:00
|
|
|
}
|
2019-09-02 16:30:29 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
function initFields() {
|
|
|
|
$(editBlocks).not('.initialized').each(function() {
|
|
|
|
var container = $(this);
|
|
|
|
container.addClass('initialized');
|
|
|
|
initSmartAnnotation(container);
|
|
|
|
appendAfterLabel(container);
|
|
|
|
});
|
|
|
|
}
|
2019-04-25 17:14:55 +08:00
|
|
|
|
2020-01-06 23:07:23 +08:00
|
|
|
function updateSideBarNav(itemType, itemId, newData) {
|
|
|
|
let sidebar = $('#slide-panel');
|
|
|
|
let link = sidebar.find(`a[data-type="${itemType}"][data-id="${itemId}"]`);
|
|
|
|
link.prop('title', newData);
|
|
|
|
link.text(newData);
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
function updateField(container) {
|
|
|
|
var params;
|
|
|
|
var paramsGroup = container.data('params-group');
|
2020-01-06 23:07:23 +08:00
|
|
|
var itemId = container.data('item-id');
|
2019-11-12 18:26:18 +08:00
|
|
|
var fieldToUpdate = container.data('field-to-update');
|
2019-05-15 20:59:15 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
if (inputField(container).val() === container.attr('data-original-name')) {
|
2023-01-26 22:29:08 +08:00
|
|
|
$(`${editBlocks} .cancel-button`).trigger('click');
|
2019-11-12 18:26:18 +08:00
|
|
|
return false;
|
2019-04-25 17:14:55 +08:00
|
|
|
}
|
2019-11-12 18:26:18 +08:00
|
|
|
if (container.data('disabled')) return false;
|
|
|
|
container.data('disabled', true);
|
2019-04-25 17:14:55 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
if (paramsGroup) {
|
|
|
|
params = { [paramsGroup]: { [fieldToUpdate]: inputField(container).val() } };
|
|
|
|
} else {
|
|
|
|
params = { [fieldToUpdate]: inputField(container).val() };
|
|
|
|
}
|
2020-06-15 21:21:33 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
$.ajax({
|
|
|
|
url: container.data('path-to-update'),
|
|
|
|
type: 'PUT',
|
|
|
|
dataType: 'json',
|
|
|
|
data: params,
|
|
|
|
success: function(result) {
|
|
|
|
var viewData;
|
2022-12-08 17:16:37 +08:00
|
|
|
var parentContainer = container.parent();
|
2019-11-20 21:57:53 +08:00
|
|
|
if (container.data('response-field')) {
|
2019-11-12 18:26:18 +08:00
|
|
|
// If we want to modify preview element on backend
|
|
|
|
// we can use this data field and we will take string from response
|
2019-11-20 21:57:53 +08:00
|
|
|
viewData = result[container.data('response-field')];
|
2019-11-12 18:26:18 +08:00
|
|
|
} else {
|
|
|
|
// By default we just copy value from input string
|
2020-06-16 19:55:27 +08:00
|
|
|
viewData = $('<div>').text(inputField(container).val()).html();
|
2019-11-12 18:26:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
container.find('.view-mode')
|
|
|
|
.html(viewData)
|
|
|
|
.removeClass('hidden');
|
|
|
|
container
|
|
|
|
.attr('data-original-name', inputField(container).val())
|
|
|
|
.removeClass('error')
|
|
|
|
.attr('data-edit-mode', '0')
|
|
|
|
.data('disabled', false)
|
|
|
|
.trigger('inlineEditing:fieldUpdated');
|
|
|
|
inputField(container)
|
|
|
|
.attr('disabled', true)
|
|
|
|
.addClass('hidden')
|
|
|
|
.attr('value', inputField(container).val());
|
|
|
|
appendAfterLabel(container);
|
2020-01-06 23:07:23 +08:00
|
|
|
|
2022-12-08 17:16:37 +08:00
|
|
|
container.trigger('inlineEditing::updated', [inputField(container).val(), viewData]);
|
2020-06-17 20:05:23 +08:00
|
|
|
|
2020-01-06 23:07:23 +08:00
|
|
|
if (SIDEBAR_ITEM_TYPES.includes(paramsGroup)) {
|
|
|
|
updateSideBarNav(paramsGroup, itemId, viewData);
|
|
|
|
}
|
2022-12-08 17:16:37 +08:00
|
|
|
|
|
|
|
if (parentContainer.attr('data-original-title')) {
|
|
|
|
parentContainer.attr('data-original-title', inputField(container).val());
|
|
|
|
}
|
2019-11-12 18:26:18 +08:00
|
|
|
},
|
|
|
|
error: function(response) {
|
|
|
|
var error = response.responseJSON[fieldToUpdate];
|
2020-09-08 21:42:30 +08:00
|
|
|
if (response.status === 403) {
|
|
|
|
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
|
2023-04-14 21:10:35 +08:00
|
|
|
} else if (response.status === 422) {
|
|
|
|
HelperModule.flashAlertMsg(response.responseJSON.errors
|
|
|
|
? Object.values(response.responseJSON.errors).join(', ') : I18n.t('errors.general'), 'danger');
|
2020-09-08 21:42:30 +08:00
|
|
|
}
|
2019-11-12 18:26:18 +08:00
|
|
|
if (!error) error = response.responseJSON.errors[fieldToUpdate];
|
|
|
|
container.addClass('error');
|
2023-04-14 21:10:35 +08:00
|
|
|
if (error) container.find('.error-block').html(error.join(', '));
|
2023-09-19 19:13:52 +08:00
|
|
|
inputField(container).removeClass('!border-b-sn-science-blue');
|
|
|
|
inputField(container).addClass('!border-b-sn-delete-red');
|
2019-11-12 18:26:18 +08:00
|
|
|
inputField(container).focus();
|
|
|
|
container.data('disabled', false);
|
2022-12-08 17:16:37 +08:00
|
|
|
$('.tooltip').hide();
|
2019-03-06 16:17:34 +08:00
|
|
|
}
|
|
|
|
});
|
2019-11-12 18:26:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
function saveAllEditFields() {
|
|
|
|
$(editBlocks).each(function() {
|
|
|
|
var container = $(this);
|
|
|
|
if (!inputField(container).attr('disabled')) {
|
|
|
|
updateField(container);
|
2019-04-25 17:14:55 +08:00
|
|
|
}
|
|
|
|
});
|
2019-11-12 18:26:18 +08:00
|
|
|
}
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
$(document)
|
|
|
|
.off('click', editBlocks)
|
2023-01-05 19:43:19 +08:00
|
|
|
.off('keyup', `${editBlocks}`)
|
2019-11-12 18:26:18 +08:00
|
|
|
.off('click', `${editBlocks} .save-button`)
|
|
|
|
.off('click', `${editBlocks} .cancel-button`)
|
2019-11-20 21:57:53 +08:00
|
|
|
.off('blur', `${editBlocks} textarea, ${editBlocks} input`)
|
2023-01-05 19:43:19 +08:00
|
|
|
.on('keyup', `${editBlocks}`, function(e) {
|
2023-01-17 20:40:30 +08:00
|
|
|
var container = $(this);
|
2023-01-05 19:43:19 +08:00
|
|
|
if (e.keyCode === 27) {
|
|
|
|
$(`${editBlocks} .cancel-button`).click();
|
|
|
|
} // Esc
|
2023-01-17 20:40:30 +08:00
|
|
|
if (e.keyCode === 13 && !container.find('.view-mode').hasClass('hidden')) {
|
|
|
|
$(editBlocks).click();
|
|
|
|
}
|
2023-01-05 19:43:19 +08:00
|
|
|
})
|
2023-09-19 19:13:52 +08:00
|
|
|
.on('blur', `${editBlocks} textarea, ${editBlocks} input`, function(e) {
|
|
|
|
saveAllEditFields();
|
|
|
|
})
|
2019-11-12 18:26:18 +08:00
|
|
|
.on('click', editBlocks, function(e) {
|
|
|
|
// 'A' mean that, if we click on <a></a> element we will not go in edit mode
|
|
|
|
var container = $(this);
|
|
|
|
if (e.target.tagName === 'A') return true;
|
|
|
|
if (inputField(container).attr('disabled')) {
|
|
|
|
saveAllEditFields();
|
2023-01-05 19:43:19 +08:00
|
|
|
let input = inputField(container);
|
|
|
|
input.attr('disabled', false)
|
2019-11-12 18:26:18 +08:00
|
|
|
.removeClass('hidden')
|
|
|
|
.focus();
|
2023-01-05 19:43:19 +08:00
|
|
|
input[0].selectionStart = input[0].value.length;
|
|
|
|
input[0].selectionEnd = input[0].value.length;
|
2019-11-12 18:26:18 +08:00
|
|
|
container
|
|
|
|
.attr('data-edit-mode', '1');
|
|
|
|
container.find('.view-mode')
|
|
|
|
.addClass('hidden')
|
|
|
|
.closest('.inline_scroll_block')
|
|
|
|
.scrollTop(container.offsetTop);
|
2022-12-08 17:16:37 +08:00
|
|
|
$('.tooltip').hide();
|
2019-09-02 16:30:29 +08:00
|
|
|
}
|
2023-09-19 19:13:52 +08:00
|
|
|
inputField(container).removeClass('!border-b-sn-delete-red');
|
|
|
|
inputField(container).addClass('!border-b-sn-science-blue');
|
2019-11-12 18:26:18 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
return true;
|
2019-09-02 16:30:29 +08:00
|
|
|
})
|
2019-11-12 18:26:18 +08:00
|
|
|
.on('click', `${editBlocks} .save-button`, function(e) {
|
|
|
|
var container = $(this).closest(editBlocks);
|
|
|
|
updateField(container);
|
2019-04-25 17:14:55 +08:00
|
|
|
e.stopPropagation();
|
2019-11-12 18:26:18 +08:00
|
|
|
})
|
|
|
|
.on('click', `${editBlocks} .cancel-button`, function(e) {
|
|
|
|
var container = $(this).closest(editBlocks);
|
|
|
|
inputField(container)
|
|
|
|
.attr('disabled', true)
|
|
|
|
.addClass('hidden')
|
|
|
|
.val(container.data('original-name'));
|
|
|
|
container
|
|
|
|
.attr('data-edit-mode', '0')
|
|
|
|
.removeClass('error');
|
|
|
|
container.find('.view-mode')
|
|
|
|
.removeClass('hidden');
|
|
|
|
e.stopPropagation();
|
|
|
|
})
|
2023-02-15 01:24:50 +08:00
|
|
|
.on('keydown', `${editBlocks} input`, function(e) {
|
2019-11-12 18:26:18 +08:00
|
|
|
var container = $(this).closest(editBlocks);
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
updateField(container);
|
2023-02-15 01:24:50 +08:00
|
|
|
} else if (e.keyCode === 9) {
|
|
|
|
$(`${editBlocks} .cancel-button`).trigger('click');
|
2019-11-12 18:26:18 +08:00
|
|
|
}
|
2023-01-26 22:29:08 +08:00
|
|
|
e.stopPropagation();
|
2019-04-25 17:14:55 +08:00
|
|
|
});
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2019-11-12 18:26:18 +08:00
|
|
|
$(window).click((e) => {
|
|
|
|
if ($(e.target).closest('.atwho-view').length > 0) return false;
|
|
|
|
saveAllEditFields();
|
2019-04-25 17:14:55 +08:00
|
|
|
return true;
|
2019-03-06 16:17:34 +08:00
|
|
|
});
|
2019-11-12 18:26:18 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
init: () => {
|
|
|
|
initFields();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
|
|
|
$(document).on('turbolinks:load', function() {
|
|
|
|
inlineEditing.init();
|
|
|
|
});
|