Skip to content

Node.js based forum software built for the modern web

refactor: move header unread code to separate module

Barış Soner Uşaklı40230725c352
diff --git a/public/src/app.js b/public/src/app.js
index 92bdccc1bc..87c8d3ddfc 100644
--- a/public/src/app.js
+++ b/public/src/app.js
@@ -85,11 +85,10 @@ app.flags = {};
'translator',
'messages',
'search',
- 'forum/unread',
'forum/header',
'hooks',
'timeago/jquery.timeago',
- ], function (taskbar, helpers, pagination, translator, messages, search, unread, header, hooks) {
+ ], function (taskbar, helpers, pagination, translator, messages, search, header, hooks) {
header.prepareDOM();
translator.prepareDOM();
taskbar.init();
@@ -97,9 +96,6 @@ app.flags = {};
pagination.init();
search.init();
- if (app.user.uid > 0) {
- unread.initUnreadTopics();
- }
function finishLoad() {
hooks.fire('action:app.load');
messages.show();
diff --git a/public/src/client/header.js b/public/src/client/header.js
index f7537b7021..31de3bc475 100644
--- a/public/src/client/header.js
+++ b/public/src/client/header.js
@@ -1,9 +1,17 @@
'use strict';
-define('forum/header', ['forum/header/notifications', 'forum/header/chat', 'alerts'], function (notifications, chat, alerts) {
+define('forum/header', [
+ 'forum/header/unread',
+ 'forum/header/notifications',
+ 'forum/header/chat',
+ 'alerts',
+], function (unread, notifications, chat, alerts) {
const module = {};
module.prepareDOM = function () {
+ if (app.user.uid > 0) {
+ unread.initUnreadTopics();
+ }
notifications.prepareDOM();
chat.prepareDOM();
handleStatusChange();
diff --git a/public/src/client/header/unread.js b/public/src/client/header/unread.js
new file mode 100644
index 0000000000..40e8e143cf
--- /dev/null
+++ b/public/src/client/header/unread.js
@@ -0,0 +1,96 @@
+'use strict';
+
+define('forum/header/unread', function () {
+ const unread = {};
+ const watchStates = {
+ ignoring: 1,
+ notwatching: 2,
+ watching: 3,
+ };
+
+ unread.initUnreadTopics = function () {
+ const unreadTopics = app.user.unreadData;
+
+ function onNewPost(data) {
+ if (data && data.posts && data.posts.length && unreadTopics) {
+ const post = data.posts[0];
+ if (parseInt(post.uid, 10) === parseInt(app.user.uid, 10) ||
+ (!post.topic.isFollowing && post.categoryWatchState !== watchStates.watching)
+ ) {
+ return;
+ }
+
+ const tid = post.topic.tid;
+ if (!unreadTopics[''][tid] || !unreadTopics.new[tid] ||
+ !unreadTopics.watched[tid] || !unreadTopics.unreplied[tid]) {
+ markTopicsUnread(tid);
+ }
+
+ if (!unreadTopics[''][tid]) {
+ increaseUnreadCount('');
+ unreadTopics[''][tid] = true;
+ }
+ const isNewTopic = post.isMain && parseInt(post.uid, 10) !== parseInt(app.user.uid, 10);
+ if (isNewTopic && !unreadTopics.new[tid]) {
+ increaseUnreadCount('new');
+ unreadTopics.new[tid] = true;
+ }
+ const isUnreplied = parseInt(post.topic.postcount, 10) <= 1;
+ if (isUnreplied && !unreadTopics.unreplied[tid]) {
+ increaseUnreadCount('unreplied');
+ unreadTopics.unreplied[tid] = true;
+ }
+
+ if (post.topic.isFollowing && !unreadTopics.watched[tid]) {
+ increaseUnreadCount('watched');
+ unreadTopics.watched[tid] = true;
+ }
+ }
+ }
+
+ function increaseUnreadCount(filter) {
+ const unreadUrl = '/unread' + (filter ? '?filter=' + filter : '');
+ const newCount = 1 + parseInt($('a[href="' + config.relative_path + unreadUrl + '"].navigation-link i').attr('data-content'), 10);
+ updateUnreadTopicCount(unreadUrl, newCount);
+ }
+
+ function markTopicsUnread(tid) {
+ $('[data-tid="' + tid + '"]').addClass('unread');
+ }
+
+ $(window).on('action:ajaxify.end', function () {
+ if (ajaxify.data.template.topic) {
+ ['', 'new', 'watched', 'unreplied'].forEach(function (filter) {
+ delete unreadTopics[filter][ajaxify.data.tid];
+ });
+ }
+ });
+ socket.removeListener('event:new_post', onNewPost);
+ socket.on('event:new_post', onNewPost);
+
+ socket.removeListener('event:unread.updateCount', updateUnreadCounters);
+ socket.on('event:unread.updateCount', updateUnreadCounters);
+ };
+
+ function updateUnreadCounters(data) {
+ updateUnreadTopicCount('/unread', data.unreadTopicCount);
+ updateUnreadTopicCount('/unread?filter=new', data.unreadNewTopicCount);
+ updateUnreadTopicCount('/unread?filter=watched', data.unreadWatchedTopicCount);
+ updateUnreadTopicCount('/unread?filter=unreplied', data.unreadUnrepliedTopicCount);
+ }
+
+ function updateUnreadTopicCount(url, count) {
+ if (!utils.isNumber(count)) {
+ return;
+ }
+
+ $('a[href="' + config.relative_path + url + '"].navigation-link i')
+ .toggleClass('unread-count', count > 0)
+ .attr('data-content', count > 99 ? '99+' : count);
+
+ $('#mobile-menu [data-unread-url="' + url + '"]').attr('data-content', count > 99 ? '99+' : count);
+ }
+ unread.updateUnreadTopicCount = updateUnreadTopicCount;
+
+ return unread;
+});
diff --git a/public/src/client/unread.js b/public/src/client/unread.js
index 9ae0bf7a09..e331636915 100644
--- a/public/src/client/unread.js
+++ b/public/src/client/unread.js
@@ -2,16 +2,10 @@
define('forum/unread', [
- 'topicSelect', 'components', 'topicList', 'categorySelector', 'alerts',
-], function (topicSelect, components, topicList, categorySelector, alerts) {
+ 'forum/header/unread', 'topicSelect', 'components', 'topicList', 'categorySelector', 'alerts',
+], function (headerUnread, topicSelect, components, topicList, categorySelector, alerts) {
const Unread = {};
- const watchStates = {
- ignoring: 1,
- notwatching: 2,
- watching: 3,
- };
-
Unread.init = function () {
app.enterRoom('unread_topics');
@@ -19,7 +13,7 @@ define('forum/unread', [
topicList.init('unread');
- updateUnreadTopicCount('/' + ajaxify.data.selectedFilter.url, ajaxify.data.topicCount);
+ headerUnread.updateUnreadTopicCount('/' + ajaxify.data.selectedFilter.url, ajaxify.data.topicCount);
};
function handleMarkRead() {
@@ -114,88 +108,5 @@ define('forum/unread', [
}
}
- function updateUnreadTopicCount(url, count) {
- if (!utils.isNumber(count)) {
- return;
- }
-
- $('a[href="' + config.relative_path + url + '"].navigation-link i')
- .toggleClass('unread-count', count > 0)
- .attr('data-content', count > 99 ? '99+' : count);
-
- $('#mobile-menu [data-unread-url="' + url + '"]').attr('data-content', count > 99 ? '99+' : count);
- }
-
- Unread.initUnreadTopics = function () {
- const unreadTopics = app.user.unreadData;
-
- function onNewPost(data) {
- if (data && data.posts && data.posts.length && unreadTopics) {
- const post = data.posts[0];
- if (parseInt(post.uid, 10) === parseInt(app.user.uid, 10) ||
- (!post.topic.isFollowing && post.categoryWatchState !== watchStates.watching)
- ) {
- return;
- }
-
- const tid = post.topic.tid;
- if (!unreadTopics[''][tid] || !unreadTopics.new[tid] ||
- !unreadTopics.watched[tid] || !unreadTopics.unreplied[tid]) {
- markTopicsUnread(tid);
- }
-
- if (!unreadTopics[''][tid]) {
- increaseUnreadCount('');
- unreadTopics[''][tid] = true;
- }
- const isNewTopic = post.isMain && parseInt(post.uid, 10) !== parseInt(app.user.uid, 10);
- if (isNewTopic && !unreadTopics.new[tid]) {
- increaseUnreadCount('new');
- unreadTopics.new[tid] = true;
- }
- const isUnreplied = parseInt(post.topic.postcount, 10) <= 1;
- if (isUnreplied && !unreadTopics.unreplied[tid]) {
- increaseUnreadCount('unreplied');
- unreadTopics.unreplied[tid] = true;
- }
-
- if (post.topic.isFollowing && !unreadTopics.watched[tid]) {
- increaseUnreadCount('watched');
- unreadTopics.watched[tid] = true;
- }
- }
- }
-
- function increaseUnreadCount(filter) {
- const unreadUrl = '/unread' + (filter ? '?filter=' + filter : '');
- const newCount = 1 + parseInt($('a[href="' + config.relative_path + unreadUrl + '"].navigation-link i').attr('data-content'), 10);
- updateUnreadTopicCount(unreadUrl, newCount);
- }
-
- function markTopicsUnread(tid) {
- $('[data-tid="' + tid + '"]').addClass('unread');
- }
-
- $(window).on('action:ajaxify.end', function () {
- if (ajaxify.data.template.topic) {
- ['', 'new', 'watched', 'unreplied'].forEach(function (filter) {
- delete unreadTopics[filter][ajaxify.data.tid];
- });
- }
- });
- socket.removeListener('event:new_post', onNewPost);
- socket.on('event:new_post', onNewPost);
-
- socket.removeListener('event:unread.updateCount', updateUnreadCounters);
- socket.on('event:unread.updateCount', updateUnreadCounters);
- };
-
- function updateUnreadCounters(data) {
- updateUnreadTopicCount('/unread', data.unreadTopicCount);
- updateUnreadTopicCount('/unread?filter=new', data.unreadNewTopicCount);
- updateUnreadTopicCount('/unread?filter=watched', data.unreadWatchedTopicCount);
- updateUnreadTopicCount('/unread?filter=unreplied', data.unreadUnrepliedTopicCount);
- }
-
return Unread;
});