openvk/Web/static/js/notifications.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-10-14 22:21:10 +03:00
Function.noop = () => {};
var _n_counter = 0;
var counter = 0;
2022-04-16 18:56:42 +03:00
window.addEventListener("focus", () => {
document.title = document.title.replace(/^\([0-9]+\) /, ""); // remove notification counter xD
});
function NewNotification(title, body, avatar = null, callback = () => {}, time = 5000, count = true) {
2021-10-14 22:21:10 +03:00
if(avatar != null) {
avatar = '<avatar>' +
'<img src="' + avatar + '">' +
'</avatar>';
} else {
avatar = '';
}
_n_counter += 1;
let id = _n_counter;
let notification = u(
`<div class="notification_ballon notification_ballon_wrap" id="n${id}">
<notification_title>
${title}
<a class="close">X</a>
</notification_title>
<wrap>
${avatar}
<content>
${body}
</content>
</wrap>
</div>
`);
u(".notifications_global_wrap").append(notification);
function getPrototype() {
return u("#n"+id);
}
function __closeNotification() {
2022-04-16 18:56:42 +03:00
if(document.visibilityState != "visible")
return setTimeout(() => {__closeNotification()}, time); // delay notif deletion
2021-10-14 22:21:10 +03:00
getPrototype().addClass('disappears');
2022-04-16 18:56:42 +03:00
return setTimeout(() => {getPrototype().remove()}, 500);
2021-10-14 22:21:10 +03:00
}
if(count == true) {
counter++;
2022-04-16 18:56:42 +03:00
document.title = `(${counter}) ${document.title}`;
}
2022-04-16 18:56:42 +03:00
setTimeout(() => {__closeNotification()}, time);
2021-10-14 22:21:10 +03:00
notification.children('notification_title').children('a.close').on('click', function(e) {
__closeNotification();
});
notification.on('click', function(e) {
if (!notification.hasClass('disappears')) {
Reflect.apply(callback, {
closeNotification: () => __closeNotification(),
$notification: () => getPrototype()
}, [e]);
__closeNotification();
}
});
}