mark all read will mark everything read, can also select topics and mark
them read, can mark specific categories read too
v1.18.x
barisusakli 11 years ago
parent e383524334
commit e5cf44d398

@ -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!"
}

@ -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<tids.length; ++i) {
$('#topics-container .category-item[data-tid="' + tids[i] + '"]').remove();
}
}
function onCategoriesLoaded(err, data) {
createCategoryLinks(data.categories);
console.log(data);
}
function createCategoryLinks(categories) {
categories = categories.filter(function(category) {
return !category.disabled;
});
for(var i=0; i<categories.length; ++i) {
createCategoryLink(categories[i]);
}
}
function createCategoryLink(category) {
var link = $('<li role="presentation" class="category" data-cid="' + category.cid + '"><a role="menuitem" href="#"><i class="fa fa-fw ' + category.icon + '"></i> ' + category.name + '</a></li>');
$('.markread .dropdown-menu').append(link);
}
return Unread;
});

@ -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<tids.length; ++i) {
topics.markTopicNotificationsRead(tids[i], socket.uid);
@ -118,6 +118,43 @@ SocketTopics.markAllRead = function(socket, tids, callback) {
});
};
SocketTopics.markAllRead = function(socket, data, callback) {
topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) {
if (err) {
return callback(err);
}
SocketTopics.markTidsRead(socket, tids, callback);
});
};
SocketTopics.markCategoryTopicsRead = function(socket, cid, callback) {
topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) {
if (err) {
return callback(err);
}
var keys = tids.map(function(tid) {
return 'topic:' + tid;
});
db.getObjectsFields(keys, ['tid', 'cid'], function(err, topicData) {
if (err) {
return callback(err);
}
tids = topicData.filter(function(topic) {
return parseInt(topic.cid, 10) === parseInt(cid, 10);
}).map(function(topic) {
return topic.tid;
});
SocketTopics.markTidsRead(socket, tids, callback);
});
});
};
SocketTopics.markAsUnreadForAll = function(socket, tid, callback) {
topics.markAsUnreadForAll(tid, function(err) {
if(err) {

@ -71,7 +71,8 @@ module.exports = function(Topics) {
var unreadTopics = {
no_topics_message: '',
show_markallread_button: 'hidden',
show_markread_button: 'hidden',
showSelect: true,
nextStart : 0,
topics: []
};
@ -94,7 +95,7 @@ module.exports = function(Topics) {
unreadTopics.topics = topicData;
unreadTopics.nextStart = parseInt(rank, 10) + 1;
unreadTopics.no_topics_message = (!topicData || topicData.length === 0) ? '' : 'hidden';
unreadTopics.show_markallread_button = topicData.length === 0 ? 'hidden' : '';
unreadTopics.show_markread_button = topicData.length === 0 ? 'hidden' : '';
callback(null, unreadTopics);
});
@ -152,7 +153,7 @@ module.exports = function(Topics) {
});
};
Topics.markAllRead = function(uid, tids, callback) {
Topics.markTidsRead = function(uid, tids, callback) {
if(!tids || !tids.length) {
return callback();
}

Loading…
Cancel
Save