scinote-web/app/assets/javascripts/shared/inline_editing.js

223 lines
7.4 KiB
JavaScript
Raw Normal View History

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 */
2023-09-19 19:13:52 +08:00
let inlineEditing = (function() {
const SIDEBAR_ITEM_TYPES = ['project', 'experiment', 'my_module', 'repository'];
2023-09-19 19:13:52 +08:00
const editBlocks = '.inline-init-handler';
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'));
}
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() }));
}
2019-11-12 18:26:18 +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')) {
SmartAnnotation.init(inputField(container), false);
}
2019-11-12 18:26:18 +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
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');
var itemId = container.data('item-id');
2019-11-12 18:26:18 +08:00
var fieldToUpdate = container.data('field-to-update');
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() };
}
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;
var parentContainer = container.parent();
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
viewData = result[container.data('response-field')];
2019-11-12 18:26:18 +08:00
} else {
// By default we just copy value from input string
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);
container.trigger('inlineEditing::updated', [inputField(container).val(), viewData]);
2020-06-17 20:05:23 +08:00
if (SIDEBAR_ITEM_TYPES.includes(paramsGroup)) {
updateSideBarNav(paramsGroup, itemId, viewData);
}
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');
} 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');
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);
$('.tooltip').hide();
}
});
2019-11-12 18:26:18 +08:00
return true;
}
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-11-12 18:26:18 +08:00
$(document)
.off('click', editBlocks)
.off('keyup', `${editBlocks}`)
2019-11-12 18:26:18 +08:00
.off('click', `${editBlocks} .save-button`)
.off('click', `${editBlocks} .cancel-button`)
.off('blur', `${editBlocks} textarea, ${editBlocks} input`)
.on('keyup', `${editBlocks}`, function(e) {
var container = $(this);
if (e.keyCode === 27) {
$(`${editBlocks} .cancel-button`).click();
} // Esc
if (e.keyCode === 13 && !container.find('.view-mode').hasClass('hidden')) {
$(editBlocks).click();
}
})
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();
let input = inputField(container);
input.attr('disabled', false)
2019-11-12 18:26:18 +08:00
.removeClass('hidden')
.focus();
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);
$('.tooltip').hide();
}
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-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();
})
.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);
} 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-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-11-12 18:26:18 +08:00
return {
init: () => {
initFields();
}
};
}());
$(document).on('turbolinks:load', function() {
inlineEditing.init();
});