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.

86 lines
2.2 KiB
JavaScript

8 years ago
'use strict';
define('forum/footer', ['notifications', 'chat', 'components', 'translator'], function (Notifications, Chat, components, translator) {
Notifications.prepareDOM();
Chat.prepareDOM();
translator.prepareDOM();
8 years ago
function updateUnreadTopicCount(url, count) {
8 years ago
if (!utils.isNumber(count)) {
return;
}
$('a[href="' + config.relative_path + url + '"] i')
.toggleClass('unread-count', count > 0)
.attr('data-content', count > 99 ? '99+' : count);
}
function updateUnreadChatCount(count) {
components.get('chat/icon')
.toggleClass('unread-count', count > 0)
9 years ago
.attr('data-content', count > 99 ? '99+' : count);
}
function initUnreadTopics() {
var unreadTopics = {};
function onNewPost(data) {
if (data && data.posts && data.posts.length) {
var post = data.posts[0];
10 years ago
if (parseInt(post.uid, 10) !== parseInt(app.user.uid, 10) && !unreadTopics[post.topic.tid]) {
increaseUnreadCount();
markTopicsUnread(post.topic.tid);
unreadTopics[post.topic.tid] = true;
}
}
}
function increaseUnreadCount() {
10 years ago
var count = parseInt($('#unread-count i').attr('data-content'), 10) + 1;
8 years ago
updateUnreadTopicCount('/unread', count);
}
function markTopicsUnread(tid) {
$('[data-tid="' + tid + '"]').addClass('unread');
}
$(window).on('action:ajaxify.end', function (ev, data) {
if (data.url) {
var tid = data.url.match(/^topic\/(\d+)/);
if (tid && tid[1]) {
delete unreadTopics[tid[1]];
}
}
});
socket.on('event:new_post', onNewPost);
}
if (app.user.uid) {
socket.emit('user.getUnreadCounts', function (err, data) {
if (err) {
return app.alert(err.message);
}
8 years ago
updateUnreadCounters(data);
updateUnreadChatCount(data.unreadChatCount);
Notifications.updateNotifCount(data.unreadNotificationCount);
});
}
8 years ago
function updateUnreadCounters(data) {
updateUnreadTopicCount('/unread', data.unreadTopicCount);
updateUnreadTopicCount('/unread/new', data.unreadNewTopicCount);
updateUnreadTopicCount('/unread/watched', data.unreadWatchedTopicCount);
}
socket.on('event:unread.updateCount', updateUnreadCounters);
socket.on('event:unread.updateChatCount', updateUnreadChatCount);
initUnreadTopics();
});