From e5cf44d398029ec5425d802d2e140d4078f98922 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 7 Apr 2014 17:09:55 -0400 Subject: [PATCH] closes #1353 mark all read will mark everything read, can also select topics and mark them read, can mark specific categories read too --- public/language/en_GB/unread.json | 7 ++- public/src/forum/unread.js | 100 +++++++++++++++++++++++++++--- src/socket.io/topics.js | 43 ++++++++++++- src/topics/unread.js | 7 ++- 4 files changed, 142 insertions(+), 15 deletions(-) diff --git a/public/language/en_GB/unread.json b/public/language/en_GB/unread.json index 92abe978e9..8bd7a20711 100644 --- a/public/language/en_GB/unread.json +++ b/public/language/en_GB/unread.json @@ -1,6 +1,9 @@ { "title": "Unread", "no_unread_topics": "There are no unread topics.", - "mark_all_read": "Mark all as Read", - "load_more": "Load More" + "load_more": "Load More", + "mark_as_read": "Mark as Read", + "selected": "Selected", + "all": "All", + "topics_marked_as_read.success": "Topics marked as read!" } \ No newline at end of file diff --git a/public/src/forum/unread.js b/public/src/forum/unread.js index ac9b275a68..3cac9fd7e5 100644 --- a/public/src/forum/unread.js +++ b/public/src/forum/unread.js @@ -1,3 +1,7 @@ +'use strict'; + +/* globals define, app, socket */ + define(['forum/recent'], function(recent) { var Unread = {}, loadingMoreTopics = false; @@ -11,26 +15,39 @@ define(['forum/recent'], function(recent) { recent.watchForNewPosts(); - $('#mark-allread-btn').on('click', function() { - function getUnreadTids() { + $('#markSelectedRead').on('click', function() { + function getSelectedTids() { var tids = []; - $('#topics-container .category-item[data-tid]').each(function() { + $('#topics-container .category-item.selected').each(function() { tids.push($(this).attr('data-tid')); }); return tids; } + var tids = getSelectedTids(); + if(!tids.length) { + return; + } + socket.emit('topics.markTidsRead', tids, function(err) { + if(err) { + return app.alertError('There was an error marking topics read!'); + } - var btn = $(this); + doneRemovingTids(tids); + }); + }); - socket.emit('topics.markAllRead', getUnreadTids(), function(err) { + $('#markAllRead').on('click', function() { + socket.emit('topics.markAllRead', function(err) { if(err) { return app.alertError('There was an error marking topics read!'); } - btn.remove(); + app.alertSuccess('[[unread:topics_marked_as_read.success]]'); + $('#topics-container').empty(); $('#category-no-topics').removeClass('hidden'); - app.alertSuccess('All topics marked as read!'); + $('.markread').addClass('hidden'); + $('#numUnreadBadge') .removeClass('badge-important') .addClass('badge-inverse') @@ -38,6 +55,37 @@ define(['forum/recent'], function(recent) { }); }); + $('.markread').on('click', '.category', function() { + function getCategoryTids(cid) { + var tids = []; + $('#topics-container .category-item[data-cid="' + cid + '"]').each(function() { + tids.push($(this).attr('data-tid')); + }); + return tids; + } + var cid = $(this).attr('data-cid'); + var tids = getCategoryTids(cid); + + socket.emit('topics.markCategoryTopicsRead', cid, function(err) { + if(err) { + return app.alertError('There was an error marking topics read!'); + } + + doneRemovingTids(tids); + }); + }); + + socket.emit('categories.get', onCategoriesLoaded); + + $('#topics-container').on('click', '.select', function() { + var select = $(this); + var isChecked = !select.hasClass('fa-square-o'); + + select.toggleClass('fa-check-square-o', !isChecked); + select.toggleClass('fa-square-o', isChecked); + select.parents('.category-item').toggleClass('selected', !isChecked); + }); + if ($("body").height() <= $(window).height() && $('#topics-container').children().length >= 20) { $('#load-more-btn').show(); } @@ -77,5 +125,43 @@ define(['forum/recent'], function(recent) { } }; + function doneRemovingTids(tids) { + removeTids(tids); + + app.alertSuccess('[[unread:topics_marked_as_read.success]]'); + + if (!$('#topics-container').children().length) { + $('#category-no-topics').removeClass('hidden'); + $('.markread').addClass('hidden'); + } + } + + function removeTids(tids) { + for(var i=0; i ' + category.name + ''); + + $('.markread .dropdown-menu').append(link); + } + return Unread; }); \ No newline at end of file diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index c774c655d5..fa911543f3 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -97,18 +97,18 @@ SocketTopics.markAsRead = function(socket, data) { }); }; -SocketTopics.markAllRead = function(socket, tids, callback) { +SocketTopics.markTidsRead = function(socket, tids, callback) { if (!Array.isArray(tids)) { return callback(new Error('invalid-data')); } - topics.markAllRead(socket.uid, tids, function(err) { + topics.markTidsRead(socket.uid, tids, function(err) { if(err) { return callback(err); } - index.server.sockets.in('uid_' + socket.uid).emit('event:unread.updateCount', null, 0); + topics.pushUnreadCount(socket.uid); for (var i=0; i