You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/public/src/modules/notifications.js

109 lines
2.6 KiB
JavaScript

'use strict';
/* globals define, socket, translator, utils, config, app, ajaxify, Tinycon*/
define('notifications', ['sounds'], function(sound) {
var Notifications = {};
Notifications.prepareDOM = function() {
var notifContainer = $('.notifications'),
notifTrigger = notifContainer.children('a'),
notifList = $('#notif-list'),
10 years ago
notifIcon = $('.notification-icon');
notifTrigger.on('click', function(e) {
e.preventDefault();
10 years ago
if (notifContainer.hasClass('open')) {
return;
}
10 years ago
socket.emit('notifications.get', null, function(err, data) {
if (err) {
return app.alertError(err.message);
}
10 years ago
10 years ago
var notifs = data.unread.concat(data.read);
translator.toggleTimeagoShorthand();
for(var i=0; i<notifs.length; ++i) {
notifs[i].timeago = $.timeago(new Date(parseInt(notifs[i].datetime, 10)));
}
translator.toggleTimeagoShorthand();
10 years ago
10 years ago
templates.parse('partials/notifications_list', {notifications: notifs}, function(html) {
11 years ago
notifList.translateHtml(html);
10 years ago
});
10 years ago
});
10 years ago
});
10 years ago
notifList.on('click', '[data-nid]', function() {
10 years ago
var nid = $(this).attr('data-nid');
10 years ago
socket.emit('notifications.markRead', nid, function(err) {
if (err) {
app.alertError(err.message);
}
});
});
10 years ago
notifList.on('click', '.mark-all-read', function() {
socket.emit('notifications.markAllRead', function(err) {
if (err) {
app.alertError(err.message);
}
updateNotifCount(0);
});
});
11 years ago
function updateNotifCount(count) {
if (count > 0) {
notifIcon.removeClass('fa-bell-o').addClass('fa-bell');
} else {
notifIcon.removeClass('fa-bell').addClass('fa-bell-o');
}
11 years ago
notifIcon.toggleClass('unread-count', count > 0);
notifIcon.attr('data-content', count > 20 ? '20+' : count);
Tinycon.setBubble(count);
};
function increaseNotifCount() {
var count = parseInt(notifIcon.attr('data-content'), 10) + 1;
updateNotifCount(count);
}
socket.emit('notifications.getCount', function(err, count) {
if (!err) {
updateNotifCount(count);
} else {
updateNotifCount(0);
}
});
socket.on('event:new_notification', function(notifData) {
app.alert({
alert_id: 'new_notif',
title: '[[notifications:new_notification]]',
message: '[[notifications:you_have_unread_notifications]]',
type: 'warning',
timeout: 2000
});
app.refreshTitle();
if (ajaxify.currentPage === 'notifications') {
ajaxify.refresh();
}
increaseNotifCount();
sound.play('notification');
});
socket.on('event:notifications.updateCount', function(count) {
updateNotifCount(count);
});
};
return Notifications;
});