2014-04-07 17:09:55 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/* globals define, app, socket */
|
|
|
|
|
|
2014-01-26 14:56:16 -05:00
|
|
|
define(['forum/recent'], function(recent) {
|
2013-11-26 23:55:55 -05:00
|
|
|
var Unread = {},
|
|
|
|
|
loadingMoreTopics = false;
|
2013-10-03 15:04:25 -04:00
|
|
|
|
|
|
|
|
Unread.init = function() {
|
2013-11-27 12:39:07 -05:00
|
|
|
app.enterRoom('recent_posts');
|
2013-10-03 15:04:25 -04:00
|
|
|
|
|
|
|
|
$('#new-topics-alert').on('click', function() {
|
2013-10-25 11:05:06 -04:00
|
|
|
$(this).addClass('hide');
|
2013-08-10 16:14:50 -04:00
|
|
|
});
|
2013-08-19 10:58:02 -04:00
|
|
|
|
2014-01-26 14:56:16 -05:00
|
|
|
recent.watchForNewPosts();
|
2013-10-03 15:04:25 -04:00
|
|
|
|
2014-04-07 17:09:55 -04:00
|
|
|
$('#markSelectedRead').on('click', function() {
|
|
|
|
|
function getSelectedTids() {
|
2014-03-03 15:26:15 -05:00
|
|
|
var tids = [];
|
2014-04-07 17:09:55 -04:00
|
|
|
$('#topics-container .category-item.selected').each(function() {
|
2014-03-03 15:26:15 -05:00
|
|
|
tids.push($(this).attr('data-tid'));
|
|
|
|
|
});
|
|
|
|
|
return tids;
|
|
|
|
|
}
|
2014-04-07 17:09:55 -04:00
|
|
|
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!');
|
|
|
|
|
}
|
2014-03-03 15:26:15 -05:00
|
|
|
|
2014-04-07 17:09:55 -04:00
|
|
|
doneRemovingTids(tids);
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-03-03 15:26:15 -05:00
|
|
|
|
2014-04-07 17:09:55 -04:00
|
|
|
$('#markAllRead').on('click', function() {
|
|
|
|
|
socket.emit('topics.markAllRead', function(err) {
|
2014-01-16 19:58:57 -05:00
|
|
|
if(err) {
|
|
|
|
|
return app.alertError('There was an error marking topics read!');
|
2013-10-03 15:04:25 -04:00
|
|
|
}
|
2014-01-16 19:58:57 -05:00
|
|
|
|
2014-04-07 17:09:55 -04:00
|
|
|
app.alertSuccess('[[unread:topics_marked_as_read.success]]');
|
|
|
|
|
|
2014-01-16 19:58:57 -05:00
|
|
|
$('#topics-container').empty();
|
|
|
|
|
$('#category-no-topics').removeClass('hidden');
|
2014-04-07 17:09:55 -04:00
|
|
|
$('.markread').addClass('hidden');
|
|
|
|
|
|
2014-01-16 19:58:57 -05:00
|
|
|
$('#numUnreadBadge')
|
|
|
|
|
.removeClass('badge-important')
|
|
|
|
|
.addClass('badge-inverse')
|
|
|
|
|
.html('0');
|
2013-10-03 15:04:25 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2014-04-07 17:09:55 -04:00
|
|
|
$('.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);
|
|
|
|
|
});
|
|
|
|
|
|
2014-01-26 14:56:16 -05:00
|
|
|
if ($("body").height() <= $(window).height() && $('#topics-container').children().length >= 20) {
|
|
|
|
|
$('#load-more-btn').show();
|
|
|
|
|
}
|
2013-10-03 15:04:25 -04:00
|
|
|
|
2014-01-26 14:56:16 -05:00
|
|
|
$('#load-more-btn').on('click', function() {
|
|
|
|
|
loadMoreTopics();
|
|
|
|
|
});
|
2013-10-03 15:04:25 -04:00
|
|
|
|
2014-01-26 14:56:16 -05:00
|
|
|
app.enableInfiniteLoading(function() {
|
|
|
|
|
if(!loadingMoreTopics) {
|
|
|
|
|
loadMoreTopics();
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-08-10 16:14:50 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
function loadMoreTopics() {
|
2014-02-18 23:06:18 -05:00
|
|
|
if(!$('#topics-container').length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-02-22 00:49:32 -05:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
loadingMoreTopics = true;
|
2014-01-16 15:10:37 -05:00
|
|
|
socket.emit('topics.loadMoreUnreadTopics', {
|
2014-01-26 17:17:34 -05:00
|
|
|
after: $('#topics-container').attr('data-nextstart')
|
2014-01-16 20:53:32 -05:00
|
|
|
}, function(err, data) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
if (data.topics && data.topics.length) {
|
2014-01-26 14:56:16 -05:00
|
|
|
recent.onTopicsLoaded('unread', data.topics);
|
2014-01-26 17:17:34 -05:00
|
|
|
$('#topics-container').attr('data-nextstart', data.nextStart);
|
2013-10-03 15:04:25 -04:00
|
|
|
} else {
|
|
|
|
|
$('#load-more-btn').hide();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadingMoreTopics = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-08-10 16:14:50 -04:00
|
|
|
|
2014-04-07 17:09:55 -04:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
return Unread;
|
|
|
|
|
});
|