2019-04-25 17:14:55 +08:00
|
|
|
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "initInlineEditing" }]*/
|
2019-05-15 20:59:15 +08:00
|
|
|
/* global SmartAnnotation */
|
2019-03-06 16:17:34 +08:00
|
|
|
function initInlineEditing(title) {
|
2019-04-25 17:14:55 +08:00
|
|
|
var editBlocks = $('.' + title + '-editable-field');
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2019-04-25 17:14:55 +08:00
|
|
|
$.each(editBlocks, function(i, element) {
|
|
|
|
var editBlock = element;
|
|
|
|
var $editBlock = $(editBlock);
|
|
|
|
var $inputString = $editBlock.find('input');
|
|
|
|
var inputString;
|
|
|
|
if (editBlock.dataset.editMode !== undefined) return true;
|
|
|
|
editBlock.dataset.editMode = 0;
|
|
|
|
$editBlock.addClass('inline-edit-active');
|
|
|
|
if ($inputString.length === 0) {
|
|
|
|
$inputString = $editBlock.find('textarea');
|
|
|
|
}
|
2019-05-15 20:59:15 +08:00
|
|
|
|
2019-05-14 22:25:46 +08:00
|
|
|
inputString = $inputString[0];
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2019-05-15 20:59:15 +08:00
|
|
|
if (editBlock.dataset.smartAnnotation === 'true') {
|
|
|
|
SmartAnnotation.init($inputString);
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:25:46 +08:00
|
|
|
function saveAllEditFields() {
|
|
|
|
$('.inline-edit-active').find('.save-button').click();
|
2019-03-06 16:17:34 +08:00
|
|
|
}
|
2019-04-25 17:14:55 +08:00
|
|
|
|
2019-09-02 16:30:29 +08:00
|
|
|
function appendAfterLabel() {
|
|
|
|
if (editBlock.dataset.labelAfter) {
|
|
|
|
$(editBlock.dataset.labelAfter).appendTo($editBlock.find('.view-mode'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appendAfterLabel();
|
|
|
|
|
2019-04-25 17:14:55 +08:00
|
|
|
function updateField() {
|
|
|
|
var params = {};
|
|
|
|
|
|
|
|
if (inputString.value === editBlock.dataset.originalName) {
|
|
|
|
inputString.disabled = true;
|
|
|
|
editBlock.dataset.editMode = 0;
|
2019-09-02 16:30:29 +08:00
|
|
|
editBlock.dataset.error = false;
|
2019-05-15 20:59:15 +08:00
|
|
|
$inputString.addClass('hidden');
|
|
|
|
$editBlock.find('.view-mode').removeClass('hidden');
|
2019-04-25 17:14:55 +08:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-04 20:40:21 +08:00
|
|
|
if (editBlock.disable) return false;
|
|
|
|
editBlock.disable = true;
|
2019-04-25 17:14:55 +08:00
|
|
|
params[editBlock.dataset.paramsGroup] = {};
|
|
|
|
params[editBlock.dataset.paramsGroup][editBlock.dataset.fieldToUpdate] = inputString.value;
|
|
|
|
$.ajax({
|
|
|
|
url: editBlock.dataset.pathToUpdate,
|
|
|
|
type: 'PUT',
|
|
|
|
dataType: 'json',
|
|
|
|
data: params,
|
2019-05-15 20:59:15 +08:00
|
|
|
success: function(result) {
|
|
|
|
var viewData;
|
|
|
|
if (editBlock.dataset.responseField) {
|
|
|
|
// If we want to modify preview element on backend
|
|
|
|
// we can use this data field and we will take string from response
|
|
|
|
viewData = result[editBlock.dataset.responseField];
|
|
|
|
} else {
|
|
|
|
// By default we just copy value from input string
|
|
|
|
viewData = inputString.value;
|
|
|
|
}
|
2019-04-25 17:14:55 +08:00
|
|
|
editBlock.dataset.originalName = inputString.value;
|
|
|
|
editBlock.dataset.error = false;
|
2019-05-15 20:59:15 +08:00
|
|
|
$inputString.addClass('hidden');
|
2019-05-21 21:19:44 +08:00
|
|
|
$editBlock.find('.view-mode').html(viewData).removeClass('hidden');
|
2019-09-02 16:30:29 +08:00
|
|
|
appendAfterLabel();
|
2019-05-15 20:59:15 +08:00
|
|
|
|
2019-04-25 17:14:55 +08:00
|
|
|
inputString.disabled = true;
|
|
|
|
editBlock.dataset.editMode = 0;
|
2019-06-04 20:40:21 +08:00
|
|
|
editBlock.disable = false;
|
2019-04-25 17:14:55 +08:00
|
|
|
},
|
|
|
|
error: function(response) {
|
|
|
|
var errors = response.responseJSON;
|
|
|
|
editBlock.dataset.error = true;
|
|
|
|
if (response.responseJSON.errors === undefined) {
|
|
|
|
errors = errors[editBlock.dataset.fieldToUpdate][0];
|
|
|
|
} else {
|
|
|
|
errors = errors.errors[editBlock.dataset.fieldToUpdate][0];
|
|
|
|
}
|
|
|
|
$editBlock.find('.error-block')[0].innerHTML = errors;
|
|
|
|
$inputString.focus();
|
2019-06-04 20:40:21 +08:00
|
|
|
editBlock.disable = false;
|
2019-03-06 16:17:34 +08:00
|
|
|
}
|
2019-04-25 17:14:55 +08:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-15 20:59:15 +08:00
|
|
|
$editBlock.click((e) => {
|
|
|
|
// 'A' mean that, if we click on <a></a> element we will not go in edit mode
|
|
|
|
if (e.target.tagName === 'A') return true;
|
2019-04-25 17:14:55 +08:00
|
|
|
if (inputString.disabled) {
|
2019-05-14 22:25:46 +08:00
|
|
|
saveAllEditFields();
|
2019-04-25 17:14:55 +08:00
|
|
|
editBlock.dataset.editMode = 1;
|
2019-06-14 22:20:47 +08:00
|
|
|
$editBlock.closest('.inline_scroll_block').scrollTop(editBlock.offsetTop);
|
2019-04-25 17:14:55 +08:00
|
|
|
inputString.disabled = false;
|
2019-05-15 20:59:15 +08:00
|
|
|
$inputString.removeClass('hidden');
|
|
|
|
$editBlock.find('.view-mode').addClass('hidden');
|
2019-04-25 17:14:55 +08:00
|
|
|
$inputString.focus();
|
2019-03-06 16:17:34 +08:00
|
|
|
}
|
2019-04-25 17:14:55 +08:00
|
|
|
e.stopPropagation();
|
2019-05-15 20:59:15 +08:00
|
|
|
return true;
|
2019-03-06 16:17:34 +08:00
|
|
|
});
|
|
|
|
|
2019-05-15 20:59:15 +08:00
|
|
|
$(window).click((e) => {
|
|
|
|
if ($(e.target).closest('.atwho-view').length > 0) return false;
|
2019-04-25 17:14:55 +08:00
|
|
|
if (inputString.disabled === false) {
|
|
|
|
updateField();
|
|
|
|
}
|
|
|
|
editBlock.dataset.editMode = 0;
|
2019-05-15 20:59:15 +08:00
|
|
|
return true;
|
2019-04-25 17:14:55 +08:00
|
|
|
});
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2019-04-25 17:14:55 +08:00
|
|
|
$($editBlock.find('.save-button')).click(e => {
|
2019-03-06 16:17:34 +08:00
|
|
|
updateField();
|
2019-04-25 17:14:55 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2019-09-02 16:30:29 +08:00
|
|
|
$inputString.keyup((e) => {
|
|
|
|
if(e.keyCode === 13 && $inputString.is('input')) {
|
|
|
|
updateField();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-04-25 17:14:55 +08:00
|
|
|
$($editBlock.find('.cancel-button')).click(e => {
|
|
|
|
inputString.disabled = true;
|
|
|
|
editBlock.dataset.editMode = 0;
|
|
|
|
editBlock.dataset.error = false;
|
|
|
|
inputString.value = editBlock.dataset.originalName;
|
2019-05-15 20:59:15 +08:00
|
|
|
$inputString.addClass('hidden');
|
|
|
|
$editBlock.find('.view-mode').removeClass('hidden');
|
2019-04-25 17:14:55 +08:00
|
|
|
$inputString.keydown();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2019-03-06 16:17:34 +08:00
|
|
|
|
2019-04-25 17:14:55 +08:00
|
|
|
return true;
|
2019-03-06 16:17:34 +08:00
|
|
|
});
|
|
|
|
}
|