function getTemplateFromArray(itemsArray) {
var template = user_notification_template;
for (var search in itemsArray) {
var replace = itemsArray[search];
if (typeof replace == 'function') {
continue;
}
if (search == 'icon') {
replace = '';
} else if (search == 'image' && !empty(replace) && !isValidURL(replace)) {
replace = webSiteRootURL + replace;
} else if (search == 'element_class' && !empty(itemsArray.id)) {
replace += " UserNotificationsJS_" + itemsArray.id;
} else if (search == 'created' && typeof _serverSystemTimezone !== 'undefined') {
m = moment.tz(itemsArray.created, _serverSystemTimezone).local();
replace = m.fromNow();
} else if (search == 'href' && !empty(replace) && !isValidURL(replace)) {
replace = webSiteRootURL + replace;
}
template = template.replace(new RegExp('{' + search + '}', 'g'), replace);
}
template = cleanUpTemplate(template);
return template;
}
function addTemplateFromArray(itemsArray) {
if (typeof itemsArray === 'function') {
return false;
}
//console.log('addTemplateFromArray', itemsArray);
if (!empty(itemsArray.element_id) && $('#' + (itemsArray.element_id)).length) {
return false;
}
var template = getTemplateFromArray(itemsArray);
var priority = 6;
if (!isNaN(itemsArray.priority)) {
priority = itemsArray.priority;
}
if (empty(priority)) {
priority = 6;
}
var selector = '#topMenuUserNotifications ul .list-group .priority' + priority;
console.log('addTemplateFromArray prepend', selector);
try {
$(selector).prepend(template);
updateUserNotificationCount();
} catch (e) {
console.log('addTemplateFromArray prepend error', selector, e);
}
return true;
}
function cleanUpTemplate(template) {
for (var index in requiredUserNotificationTemplateFields) {
var search = requiredUserNotificationTemplateFields[index];
if (typeof search !== 'string') {
continue;
}
//console.log('cleanUpTemplate', search);
template = template.replace(new RegExp('\{' + search + '\}', 'g'), '');
}
template = template.replace(//g, "");
return template;
}
function userNotification(itemsArray, toast, customTitle) {
console.log('UserNotification::userNotification', itemsArray, toast, customTitle);
addTemplateFromArray(itemsArray);
var title = itemsArray.title;
if (!empty(customTitle)) {
title = customTitle;
}
if (empty(title)) {
title = itemsArray.msg;
}
if (!empty(toast) && !empty(title)) {
switch (itemsArray.type) {
case 'success':
avideoToastSuccess(title);
break;
case 'warning':
avideoToastWarning(title);
break;
case 'danger':
avideoToastError(title);
break;
case 'info':
avideoToastInfo(title);
break;
default:
avideoToast(title);
break;
}
}
}
function socketUserNotificationCallback(json) {
console.log('socketUserNotificationCallback 1', json);
var toast = false;
var customTitle = false;
if (!empty(json.toast)) {
toast = json.toast;
}
if (!empty(json.customTitle)) {
customTitle = json.customTitle;
}
console.log('socketUserNotificationCallback 2', toast, customTitle);
userNotification(json, toast, customTitle);
}
var _updateUserNotificationCountTimeout;
function updateUserNotificationCount() {
clearTimeout(_updateUserNotificationCountTimeout);
_updateUserNotificationCountTimeout = setTimeout(function () {
var valueNow = parseInt($('#topMenuUserNotifications a > span.badge-notify').text());
var total = $('#topMenuUserNotifications > ul .list-group a').length;
console.log('updateUserNotificationCount', total);
if (total <= 0) {
$('#topMenuUserNotifications').addClass('hasNothingToShow');
$('#topMenuUserNotifications').removeClass('hasSomethingToShow');
$('#topMenuUserNotifications > a > span.badge').removeClass('badge-notify');
$('#topMenuUserNotifications > a > span.badge').text(0);
} else if (total != valueNow) {
$('#topMenuUserNotifications').removeClass('hasNothingToShow');
$('#topMenuUserNotifications').addClass('hasSomethingToShow');
$('#topMenuUserNotifications > a > span.badge').addClass('badge-notify');
animateChilds('#topMenuUserNotifications .dropdown-menu .list-group .priority', 'animate__bounceInRight', 0.05);
$('#topMenuUserNotifications > a > span.badge').hide();
setTimeout(function () {
var selector = '#topMenuUserNotifications > a > span.badge';
countToOrRevesrse(selector, total);
$(selector).show();
}, 1);
createFilterButtons();
checkIfCanDeleteNotifications();
}
}, 500);
}
async function getUserNotification() {
var url = webSiteRootURL + 'plugin/UserNotifications/getNotifications.json.php';
$.ajax({
url: url,
success: function (response) {
modal.hidePleaseWait();
if (response.error) {
avideoToastError(response.msg);
} else {
for (var item in response.notifications) {
var itemsArray = response.notifications[item];
if (typeof itemsArray === 'function') {
continue;
}
addTemplateFromArray(itemsArray);
}
updateUserNotificationCount();
}
}
});
}
function deleteUserNotification(id, t) {
//modal.showPleaseWait();
$(t).parent().removeClass('animate__bounceInRight');
$(t).parent().addClass('animate__flipOutX');
var url = webSiteRootURL + 'plugin/UserNotifications/View/User_notifications/delete.json.php';
$.ajax({
url: url,
data: {id: id},
type: 'post',
success: function (response) {
//modal.hidePleaseWait();
if (response.error) {
avideoAlertError(response.msg);
} else {
//avideoToastSuccess(response.msg);
setTimeout(function () {
$(t).parent().remove();
updateUserNotificationCount();
}, 500);
}
}
});
}
function getCountNotificationIcons() {
var selector = '#topMenuUserNotifications > ul .list-group .icon i';
var iconsCountList = [];
$(selector).each(function (index) {
var className = $(this).attr('class');
var classNameType = $(this).parent().attr('class');
classNameType = classNameType.replace("icon bg-", "");
var id = $(this).closest('div.userNotifications').attr('id');
var listIndex = className + classNameType;
//console.log('getCountNotificationIcons class', listIndex);
if (empty(iconsCountList[listIndex])) {
iconsCountList[listIndex] = [];
}
iconsCountList[listIndex].push([id, classNameType, className]);
});
//console.log('getCountNotificationIcons finish', iconsCountList);
return iconsCountList;
}
function createFilterButtons() {
var icons = getCountNotificationIcons();
var buttons = '