IW4M-Admin/WebfrontCore/wwwroot/js/action.js

184 lines
5.5 KiB
JavaScript
Raw Normal View History

function hideLoader() {
2022-04-19 19:43:58 -04:00
$('#mainLoadingBar').fadeOut();
$('.modal-loading-bar').fadeOut();
}
function showLoader() {
2022-04-19 19:43:58 -04:00
$('#mainLoadingBar').fadeIn();
$('.modal-loading-bar').fadeIn();
}
function errorLoader() {
2022-04-19 19:43:58 -04:00
$('#mainLoadingBar').addClass('bg-danger').delay(2000).fadeOut();
}
function staleLoader() {
2022-04-19 19:43:58 -04:00
$('#mainLoadingBar').addClass('bg-grey');
}
2022-04-19 19:43:58 -04:00
function getUrlParameter(sParam) {
let sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
2022-04-19 19:43:58 -04:00
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
}
return false;
}
function clearQueryString() {
const uri = window.location.href.toString();
if (uri.indexOf("?") > 0) {
const cleanUri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, cleanUri);
}
}
const entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'/': '&#x2F;',
'`': '&#x60;',
'=': '&#x3D;'
};
function escapeHtml (string) {
return String(string).replace(/[&<>"'`=\/]/g, function (s) {
return entityMap[s];
});
2022-04-19 19:43:58 -04:00
}
function buildToastUri(message, duration) {
let uri = '&';
if (window.location.href.toString().indexOf('?') <= 0) {
uri = '?';
}
uri += `toastMessage=${escape(message)}${duration ? `&duration=${duration}` : ''}`;
return uri;
}
2022-04-19 19:43:58 -04:00
$(document).ready(function () {
let toastMessage = getUrlParameter('toastMessage');
2022-04-22 17:56:29 -04:00
const duration = parseInt(getUrlParameter('duration'));
2022-04-19 19:43:58 -04:00
if (toastMessage) {
toastMessage = unescape(toastMessage);
}
if (toastMessage) {
clearQueryString();
halfmoon.initStickyAlert({
content: toastMessage,
title: 'Success',
alertType: 'alert-success',
2022-04-22 17:56:29 -04:00
fillType: 'filled',
timeShown: duration
2022-04-19 19:43:58 -04:00
});
}
hideLoader();
$(document).off('click', '.profile-action');
2022-04-22 17:56:29 -04:00
$(document).on('click', '.profile-action', function (e) {
e.preventDefault();
const actionType = $(this).data('action');
const actionId = $(this).data('action-id');
2022-04-22 17:56:29 -04:00
const responseDuration = $(this).data('response-duration') || 5000;
2022-04-19 19:43:58 -04:00
const actionIdKey = actionId === undefined ? '' : `?id=${actionId}`;
showLoader();
$.get(`/Action/${actionType}Form/${actionIdKey}`)
.done(function (response) {
2022-04-22 17:56:29 -04:00
$('#actionModal .modal-message').fadeOut('fast')
$('#actionModal').attr('data-response-duration', responseDuration);
2022-04-19 19:43:58 -04:00
$('#actionModalContent').html(response);
hideLoader();
2022-04-22 17:56:29 -04:00
halfmoon.toggleModal('actionModal');
})
.fail(function (jqxhr, textStatus, error) {
2022-04-19 19:43:58 -04:00
halfmoon.initStickyAlert({
content: jqxhr.responseText,
title: 'Error',
alertType: 'alert-danger',
fillType: 'filled'
});
});
});
/*
* handle action submit
*/
$(document).on('submit', '.action-form', function (e) {
e.preventDefault();
$(this).append($('#target_id input'));
2022-04-19 19:43:58 -04:00
const modal = $('#actionModal');
const shouldRefresh = modal.data('should-refresh', modal.find('.refreshable').length !== 0);
const data = $(this).serialize();
2022-04-22 17:56:29 -04:00
const duration = modal.data('response-duration');
showLoader();
2022-04-19 19:43:58 -04:00
$.get($(this).attr('action') + '/?' + data)
.done(function (response) {
hideLoader();
// success without content
if (response.length === 0) {
location.reload();
2022-04-19 19:43:58 -04:00
} else {
let message = response;
try {
message = response.map(r => escapeHtml(r.response));
}
catch{}
if (shouldRefresh) {
window.location = `${window.location.href.replace('#', '')}${buildToastUri(message, duration)}`;
2022-04-19 19:43:58 -04:00
}
else {
modal.modal();
halfmoon.initStickyAlert({
content: escapeHtml(message),
title: 'Executed',
alertType: 'alert-primary',
fillType: 'filled'
});
}
}
})
2022-04-19 19:43:58 -04:00
.fail(function (jqxhr) {
hideLoader();
2022-04-19 19:43:58 -04:00
let message = jqxhr.responseText;
try {
const jsonMessage = $.parseJSON(message);
if (jsonMessage) {
message = jsonMessage.map(r => escapeHtml(r.response));
}
}
2022-04-19 19:43:58 -04:00
catch{}
if (message instanceof Array)
{
message = message.join("<br/>");
}
2022-04-19 19:43:58 -04:00
halfmoon.initStickyAlert({
content: message,
2022-04-19 19:43:58 -04:00
title: 'Error',
alertType: 'alert-danger',
fillType: 'filled'
});
});
});
2022-04-19 19:43:58 -04:00
});