311 lines
10 KiB
JavaScript
311 lines
10 KiB
JavaScript
var timer;
|
|
var siteRoot = location.protocol + '//' + location.host;
|
|
var dtLastActivity;
|
|
var saveButton;
|
|
var deleteButton;
|
|
var editButton;
|
|
var canEnabledDelBtn;
|
|
var errorPlaceholder;
|
|
var checking;
|
|
var updating;
|
|
var dataIsEdited;
|
|
|
|
var dataEditedText = "Sorry, this {entity} is being edited by {username}. Please, try to <a href='" + document.location.href + "'>refresh</a> the page a little bit later.";
|
|
var timedInactivityText = "Sorry, you have exceeded the period of inactivity and we cannot save your changes now. Please reload the page to try edit this object again.";
|
|
var UserFinishedEditing = "{entity} is now available for editing. <a href='"+document.location.href+"'>Refresh</a> the page to load latest changes and start editing";
|
|
var serverErrorText = "Server is not responding";
|
|
var dataEditedPopover = "{entity} is being edited by {username}. Please, try again later.";
|
|
var serverError404Text = "It seems like the connection to the server was lost and server considered you left the edit page.";
|
|
var serverError401Text = "You are not authorized to edit the {entity}. Please, <a href='" + siteRoot + "/Account/Login'>sign in</a> and try to edit the {entity} again";
|
|
|
|
|
|
// call it when click on the link before redirect to locked page
|
|
function CheckLock(buttonId, tableId, fieldId) {
|
|
var request = {
|
|
'tableId': tableId,
|
|
'fieldId': fieldId
|
|
};
|
|
var result = false;
|
|
|
|
$.ajax({
|
|
type: "post",
|
|
url: siteRoot + "/ContentLockerApi/IsLock",
|
|
data: request,
|
|
async: false,
|
|
error: function (response) {
|
|
ShowPopover(buttonId, serverErrorText);
|
|
result = false;
|
|
},
|
|
success: function (response) {
|
|
if (response.Status == true) {
|
|
ShowPopover(buttonId, dataEditedPopover.replace(/\{username\}/g, response.LockedBy).replace(/\{entity\}/g, response.EntityTitle));
|
|
result = false;
|
|
}
|
|
else {
|
|
result = true;
|
|
}
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
function CheckingLocks(tableId, fieldId, externalReferencesCount, editButtonId, deleteButtonId, erorPlaceholderId) {
|
|
checking = true;
|
|
deleteButton = deleteButtonId;
|
|
editButton = editButtonId;
|
|
errorPlaceholder = erorPlaceholderId;
|
|
dataIsEdited = false;
|
|
if (externalReferencesCount > 0) {
|
|
canEnabledDelBtn = false;
|
|
}
|
|
else {
|
|
canEnabledDelBtn = true;
|
|
}
|
|
|
|
var request = {
|
|
'tableId': tableId,
|
|
'fieldId': fieldId
|
|
};
|
|
IsLock(request);
|
|
}
|
|
// call it on the edit page to lock the object
|
|
function StartEdit(tableId, fieldId, deleteButtonId, saveButtonId, erorPlaceholderId) {
|
|
|
|
updating = true;
|
|
errorPlaceholder = erorPlaceholderId;
|
|
deleteButton = deleteButtonId;
|
|
saveButton = saveButtonId;
|
|
|
|
var request = {
|
|
'tableId': tableId,
|
|
'fieldId': fieldId
|
|
};
|
|
$('#danger').hide();
|
|
AddLock(request);
|
|
$(window).on('unload', function () {
|
|
RemoveLock(request.tableId, request.fieldId);
|
|
});
|
|
|
|
$("#lnkLogout").click(function () {
|
|
RemoveLock(request.tableId, request.fieldId);
|
|
});
|
|
}
|
|
// call it on the details (always) and edit page (if content is locked by someone else)
|
|
function IsLock(request) {
|
|
if(!checking)
|
|
return;
|
|
var canGoOn = true;
|
|
$.ajax({
|
|
type: "post",
|
|
url: siteRoot + "/ContentLockerApi/IsLock",
|
|
data: request,
|
|
error: function(response) {
|
|
$(editButton).addClass("disabled");
|
|
LockTabElements();
|
|
$(deleteButton).addClass("disabled");
|
|
RemoveErrorMessage();
|
|
if (response.status != null) {
|
|
if (response.status == 401)
|
|
ShowErrorMessage(serverError401Text.replace(/\{entity\}/g, response.EntityTitle), true);
|
|
else
|
|
ShowErrorMessage(serverErrorText, true);
|
|
} else
|
|
ShowErrorMessage(serverErrorText, true);
|
|
},
|
|
success: function(response) {
|
|
if (response == null || response.Status == null) {
|
|
$(editButton).addClass("disabled");
|
|
LockTabElements();
|
|
$(deleteButton).addClass("disabled");
|
|
RemoveErrorMessage();
|
|
ShowErrorMessage(serverErrorText, true);
|
|
} else {
|
|
if (response.Status == true) {
|
|
$(editButton).addClass("disabled");
|
|
LockTabElements();
|
|
$(deleteButton).addClass("disabled");
|
|
ShowErrorMessage(dataEditedText.replace(/\{username\}/g, response.LockedBy).replace(/\{entity\}/g, response.EntityTitle), false);
|
|
dataIsEdited = true;
|
|
} else {
|
|
if (dataIsEdited) {
|
|
RemoveErrorMessage();
|
|
canGoOn = false;
|
|
ShowErrorMessage(UserFinishedEditing.replace(/\{entity\}/g, response.EntityTitle, false), true);
|
|
}
|
|
dataIsEdited = false;
|
|
//$(editButton).removeClass("disabled");
|
|
///UnlockTabElements();
|
|
//if (canEnabledDelBtn) {
|
|
// $(deleteButton).removeClass("disabled");
|
|
//}
|
|
}
|
|
if (canGoOn) {
|
|
setTimeout(IsLock, _isLockCheckIntervalMs, request);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
// call it to send lock ajax request to the server
|
|
function AddLock(request) {
|
|
$.ajax({
|
|
type: "post",
|
|
url: siteRoot + "/ContentLockerApi/AddLock",
|
|
data: request,
|
|
error: function(response) {
|
|
$(saveButton).addClass("disabled");
|
|
LockTabElements();
|
|
$(deleteButton).addClass("disabled");
|
|
ShowErrorMessage(serverErrorText, true);
|
|
},
|
|
success: function (response) {
|
|
if (response == null || response.Status == null) {
|
|
$(saveButton).addClass("disabled");
|
|
LockTabElements();
|
|
$(deleteButton).addClass("disabled");
|
|
ShowErrorMessage(serverErrorText, true);
|
|
} else {
|
|
if (response.Status == true) {
|
|
dtLastActivity = new Date().getTime();
|
|
setTimeout(UpdateLock, _isLockCheckIntervalMs, request);
|
|
} else {
|
|
$(saveButton).addClass("disabled");
|
|
LockTabElements();
|
|
$(deleteButton).addClass("disabled");
|
|
ShowErrorMessage(dataEditedText.replace(/\{username\}/g, response.LockedBy).replace(/\{entity\}/g, response.EntityTitle), false);
|
|
CheckingLocks(request.tableId, request.fieldId, 0, saveButton, deleteButton, errorPlaceholder);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
// automatically called every X seconds to extend locked period
|
|
function UpdateLock(request) {
|
|
if (!updating)
|
|
return;
|
|
$.ajax({
|
|
type: "post",
|
|
url: siteRoot + "/ContentLockerApi/UpdateLock",
|
|
data: request,
|
|
error: function(returnval) {
|
|
$(saveButton).addClass("disabled");
|
|
LockTabElements();
|
|
$(deleteButton).addClass("disabled");
|
|
ShowErrorMessage(serverErrorText, true);
|
|
},
|
|
success: function(returnval) {
|
|
if (returnval == true) {
|
|
if (new Date().getTime() < dtLastActivity + _periodOfInactivity*1000) {
|
|
setTimeout(UpdateLock, _isLockCheckIntervalMs, request);
|
|
} else {
|
|
$(saveButton).addClass("disabled");
|
|
LockTabElements();
|
|
$(deleteButton).addClass("disabled");
|
|
ShowErrorMessage(timedInactivityText, true);
|
|
RemoveLock(request.tableId, request.fieldId);
|
|
}
|
|
} else {
|
|
$(saveButton).addClass("disabled");
|
|
LockTabElements();
|
|
$(deleteButton).addClass("disabled");
|
|
ShowErrorMessage(serverErrorText, true);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
// call it if user leave the edit page or no activity for a long time
|
|
function RemoveLock(tableId, fieldId) {
|
|
var request = {
|
|
'tableId': tableId,
|
|
'fieldId': fieldId
|
|
};
|
|
|
|
$.ajax({
|
|
type: "post",
|
|
async: false,
|
|
url: siteRoot + "/ContentLockerApi/RemoveLock",
|
|
data: request
|
|
});
|
|
}
|
|
|
|
|
|
|
|
// reset timer of inactivity
|
|
function ResetInactivePeriod() {
|
|
dtLastActivity = new Date().getTime();
|
|
}
|
|
|
|
function ShowErrorMessage(errorText, enabledReloadLink) {
|
|
if (document.getElementById(errorPlaceholder).childElementCount == 0) {
|
|
var alertMsg = document.createElement('div');
|
|
alertMsg.className = "alert alert-danger";
|
|
|
|
var erormsg = document.createElement('span');
|
|
$(erormsg).html(errorText);
|
|
|
|
alertMsg.appendChild(erormsg);
|
|
|
|
if (enabledReloadLink) {
|
|
var linkPlaceholder = document.createElement('span');
|
|
$(linkPlaceholder).css('float','right');
|
|
|
|
var link = document.createElement('a');
|
|
link.href = document.location.href;
|
|
$(link).text("Reload");
|
|
|
|
linkPlaceholder.appendChild(link);
|
|
alertMsg.appendChild(linkPlaceholder);
|
|
}
|
|
document.getElementById(errorPlaceholder).appendChild(alertMsg);
|
|
}
|
|
}
|
|
|
|
function RemoveErrorMessage() {
|
|
var erorRootElement = document.getElementById(errorPlaceholder)
|
|
|
|
if (erorRootElement.childElementCount > 0)
|
|
|
|
while (erorRootElement.hasChildNodes()) {
|
|
erorRootElement.removeChild(erorRootElement.lastChild);
|
|
}
|
|
}
|
|
|
|
function ShowPopover(buttonId, message) {
|
|
$('#' + buttonId).popover({
|
|
html: 'true',
|
|
trigger: 'manual',
|
|
title: '<strong>Not allowed</strong>' +
|
|
'<button type="button" class="close" onclick="$(this).parent().parent().prev(\'.popover-warning\').popover(\'destroy\');">×</button>',
|
|
content: message
|
|
});
|
|
$('#' + buttonId).popover('show');
|
|
}
|
|
|
|
function LockTabElements() {
|
|
var lockableElements = document.getElementsByClassName('lockable');
|
|
for (var i = 0; i < lockableElements.length; ++i) {
|
|
var item = lockableElements[i];
|
|
$(item).addClass("disabled");
|
|
}
|
|
}
|
|
|
|
function UnlockTabElements() {
|
|
var lockableElements = document.getElementsByClassName('lockable');
|
|
for (var i = 0; i < lockableElements.length; ++i) {
|
|
var item = lockableElements[i];
|
|
$(item).removeClass("disabled");
|
|
}
|
|
}
|
|
|
|
function StopChecking ()
|
|
{
|
|
checking = false;
|
|
}
|
|
|
|
function StopEdit()
|
|
{
|
|
updating = false;
|
|
}
|
|
|
|
|
|
|