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

138 lines
3.5 KiB
JavaScript

'use strict';
/* globals define, socket, utils, config, app, ajaxify, templates, Tinycon*/
10 years ago
define('notifications', ['sounds', 'translator', 'components'], function(sound, translator, components) {
var Notifications = {};
Notifications.prepareDOM = function() {
var notifContainer = $('.notifications'),
notifTrigger = notifContainer.children('a'),
notifList = $('#notif-list'),
notifIcon = components.get('notifications/icon');
notifTrigger.on('click', function(e) {
e.preventDefault();
10 years ago
if (notifContainer.hasClass('open')) {
return;
}
Notifications.loadNotifications(notifList);
10 years ago
});
10 years ago
notifList.on('click', '[data-nid]', function() {
10 years ago
var unread = $(this).hasClass('unread');
if (!unread) {
return;
}
socket.emit('notifications.markRead', $(this).attr('data-nid'), function(err) {
10 years ago
if (err) {
10 years ago
return app.alertError(err.message);
10 years ago
}
10 years ago
incrementNotifCount(-1);
10 years ago
});
});
10 years ago
notifContainer.on('click', '.mark-all-read', function() {
10 years ago
socket.emit('notifications.markAllRead', function(err) {
if (err) {
app.alertError(err.message);
}
10 years ago
Notifications.updateNotifCount(0);
10 years ago
});
});
notifList.on('click', '.mark-read', function(e) {
10 years ago
var liEl = $(this).parent(),
unread = liEl.hasClass('unread');
e.preventDefault();
e.stopPropagation();
10 years ago
socket.emit('notifications.mark' + (unread ? 'Read' : 'Unread'), liEl.attr('data-nid'), function(err) {
if (err) {
app.alertError(err.message);
}
liEl.toggleClass('unread');
10 years ago
incrementNotifCount(unread ? -1 : 1);
});
});
10 years ago
function incrementNotifCount(delta) {
var count = parseInt(notifIcon.attr('data-content'), 10) + delta;
10 years ago
Notifications.updateNotifCount(count);
}
socket.emit('notifications.getCount', function(err, count) {
if (!err) {
10 years ago
Notifications.updateNotifCount(count);
} else {
10 years ago
Notifications.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();
}
10 years ago
incrementNotifCount(1);
sound.play('notification');
});
socket.on('event:notifications.updateCount', function(count) {
10 years ago
Notifications.updateNotifCount(count);
});
};
Notifications.loadNotifications = function(notifList) {
socket.emit('notifications.get', null, function(err, data) {
if (err) {
return app.alertError(err.message);
}
var notifs = data.unread.concat(data.read).sort(function(a, b) {
return parseInt(a.datetime, 10) > parseInt(b.datetime, 10) ? -1 : 1;
});
translator.toggleTimeagoShorthand();
for(var i=0; i<notifs.length; ++i) {
notifs[i].timeago = $.timeago(new Date(parseInt(notifs[i].datetime, 10)));
}
translator.toggleTimeagoShorthand();
templates.parse('partials/notifications_list', {notifications: notifs}, function(html) {
notifList.translateHtml(html);
});
});
};
10 years ago
Notifications.updateNotifCount = function(count) {
var notifIcon = components.get('notifications/icon');
10 years ago
if (count > 0) {
notifIcon.removeClass('fa-bell-o').addClass('fa-bell');
} else {
notifIcon.removeClass('fa-bell').addClass('fa-bell-o');
}
notifIcon.toggleClass('unread-count', count > 0);
notifIcon.attr('data-content', count > 20 ? '20+' : count);
Tinycon.setBubble(count);
};
return Notifications;
});