v1.18.x
Barış Soner Uşaklı 9 years ago
parent 13623a620b
commit 3e46ef8c35

@ -1,10 +1,12 @@
'use strict'; 'use strict';
/* globals define, socket, utils, config, app, ajaxify, templates, Tinycon*/ /* globals define, socket, app, ajaxify, templates, Tinycon*/
define('notifications', ['sounds', 'translator', 'components'], function(sound, translator, components) { define('notifications', ['sounds', 'translator', 'components'], function(sound, translator, components) {
var Notifications = {}; var Notifications = {};
var unreadNotifs = {};
Notifications.prepareDOM = function() { Notifications.prepareDOM = function() {
var notifContainer = components.get('notifications'), var notifContainer = components.get('notifications'),
notifTrigger = notifContainer.children('a'), notifTrigger = notifContainer.children('a'),
@ -29,34 +31,40 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound,
notifList.on('click', '[data-nid]', function() { notifList.on('click', '[data-nid]', function() {
var unread = $(this).hasClass('unread'); var unread = $(this).hasClass('unread');
var nid = $(this).attr('data-nid');
if (!unread) { if (!unread) {
return; return;
} }
socket.emit('notifications.markRead', $(this).attr('data-nid'), function(err) { socket.emit('notifications.markRead', nid, function(err) {
if (err) { if (err) {
return app.alertError(err.message); return app.alertError(err.message);
} }
incrementNotifCount(-1); incrementNotifCount(-1);
if (unreadNotifs[nid]) {
delete unreadNotifs[nid];
}
}); });
}); });
notifContainer.on('click', '.mark-all-read', Notifications.markAllRead); notifContainer.on('click', '.mark-all-read', Notifications.markAllRead);
notifList.on('click', '.mark-read', function(e) { notifList.on('click', '.mark-read', function() {
var liEl = $(this).parent(), var liEl = $(this).parent();
unread = liEl.hasClass('unread'); var unread = liEl.hasClass('unread');
var nid = liEl.attr('data-nid');
e.preventDefault();
e.stopPropagation();
socket.emit('notifications.mark' + (unread ? 'Read' : 'Unread'), liEl.attr('data-nid'), function(err) { socket.emit('notifications.mark' + (unread ? 'Read' : 'Unread'), nid, function(err) {
if (err) { if (err) {
return app.alertError(err.message); return app.alertError(err.message);
} }
liEl.toggleClass('unread'); liEl.toggleClass('unread');
incrementNotifCount(unread ? -1 : 1); incrementNotifCount(unread ? -1 : 1);
if (unread && unreadNotifs[nid]) {
delete unreadNotifs[nid];
}
}); });
return false;
}); });
function incrementNotifCount(delta) { function incrementNotifCount(delta) {
@ -96,10 +104,13 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound,
if (ajaxify.currentPage === 'notifications') { if (ajaxify.currentPage === 'notifications') {
ajaxify.refresh(); ajaxify.refresh();
} }
if (!unreadNotifs[notifData.nid]) {
incrementNotifCount(1);
incrementNotifCount(1); sound.play('notification');
unreadNotifs[notifData.nid] = true;
sound.play('notification'); }
}); });
socket.on('event:notifications.updateCount', function(count) { socket.on('event:notifications.updateCount', function(count) {
@ -158,6 +169,7 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound,
app.alertError(err.message); app.alertError(err.message);
} }
Notifications.updateNotifCount(0); Notifications.updateNotifCount(0);
unreadNotifs = {};
}); });
}; };

Loading…
Cancel
Save