Files
NodeBB/public/src/modules/notifications.js

98 lines
2.8 KiB
JavaScript
Raw Normal View History

define(function() {
var Notifications = {};
Notifications.prepareDOM = function() {
var notifContainer = $('.notifications'),
notifTrigger = notifContainer.children('a'),
notifList = $('#notif-list'),
notifIcon = $('.notifications > a > i');
notifTrigger.on('click', function(e) {
e.preventDefault();
2014-01-30 12:55:55 -05:00
if (!notifContainer.hasClass('open')) {
2014-01-16 20:29:11 -05:00
socket.emit('notifications.get', null, function(err, data) {
2014-01-27 17:15:17 -05:00
2014-01-30 12:55:55 -05:00
var numRead = data.read.length,
numUnread = data.unread.length,
x;
2014-01-27 17:15:17 -05:00
2014-01-30 12:55:55 -05:00
notifList.html('');
2014-01-16 20:29:11 -05:00
if (!err && (data.read.length + data.unread.length) > 0) {
for (x = 0; x < numUnread; x++) {
2014-01-30 12:55:55 -05:00
notifList.append($('<li class="' + data.unread[x].readClass + '"><a href="' + data.unread[x].path + '"><span class="pull-right">' + utils.relativeTime(data.unread[x].datetime, true) + '</span>' + data.unread[x].text + '</a></li>'));
}
2014-01-30 12:55:55 -05:00
for (x = 0; x < numRead; x++) {
2014-01-30 12:55:55 -05:00
notifList.append($('<li class="' + data.read[x].readClass + '"><a href="' + data.read[x].path + '"><span class="pull-right">' + utils.relativeTime(data.read[x].datetime, true) + '</span>' + data.read[x].text + '</a></li>'));
}
2014-01-30 12:55:55 -05:00
} else {
2014-02-10 12:41:29 -05:00
translator.translate('<li class="no-notifs"><a>[[notifications:no_notifs]]</a></li>', function(translated) {
notifList.append($(translated));
});
}
2014-02-10 12:41:29 -05:00
translator.translate('<li class="pagelink"><a href="' + RELATIVE_PATH + '/notifications">[[notifications:see_all]]</a></li>', function(translated) {
notifList.append($(translated));
});
2014-01-27 15:41:08 -05:00
updateNotifCount(data.unread.length);
2014-01-16 20:14:09 -05:00
socket.emit('modules.notifications.mark_all_read', null, function(err) {
if (!err) {
2014-01-27 15:41:08 -05:00
updateNotifCount(0);
2014-01-16 20:14:09 -05:00
}
});
});
}
});
var updateNotifCount = function(count) {
if (count > 0) {
2014-01-27 01:58:16 -05:00
notifIcon.removeClass('fa-bell-o').addClass('fa-bell');
} else {
2014-01-27 01:58:16 -05:00
notifIcon.removeClass('fa-bell').addClass('fa-bell-o');
}
2014-01-27 15:41:08 -05:00
notifIcon.toggleClass('unread-count', count > 0);
notifIcon.attr('data-content', count > 20 ? '20+' : count);
Tinycon.setBubble(count);
localStorage.setItem('notifications:count', count);
};
socket.emit('notifications.getCount', function(err, count) {
if (!err) {
updateNotifCount(count);
} else {
updateNotifCount(0);
}
});
socket.on('event:new_notification', function() {
2014-01-27 01:58:16 -05:00
app.alert({
alert_id: 'new_notif',
title: 'New notification',
message: 'You have unread notifications.',
type: 'warning',
timeout: 2000
});
app.refreshTitle();
if (ajaxify.currentPage === 'notifications') {
ajaxify.refresh();
}
2014-01-27 15:41:08 -05:00
var savedCount = parseInt(localStorage.getItem('notifications:count'), 10) || 0;
updateNotifCount(savedCount + 1);
});
socket.on('event:notifications.updateCount', function(count) {
updateNotifCount(count);
});
};
return Notifications;
});