Implement label counts for stream messages.

(imported from commit 45d3d5ba8c2be3ebed8385fb714044ac9419f3c2)
This commit is contained in:
Luke Faraone 2013-02-12 14:41:01 -05:00
parent 86fbdfe183
commit a9e602c1bb
3 changed files with 82 additions and 3 deletions

View File

@ -889,6 +889,10 @@ function sort_narrow_list() {
});
}
exports.get_filter_li = function(type, name) {
return $("#" + type + "_filters li[data-name='" + encodeURIComponent(name) + "']");
};
exports.add_narrow_filter = function(name, type, uri) {
var list_item;
@ -904,7 +908,7 @@ exports.add_narrow_filter = function(name, type, uri) {
return false;
}
if ($("#" + type + "_filters li[data-name='" + encodeURIComponent(name) + "']").length) {
if (exports.get_filter_li(type, name).length) {
// already exists
return false;
}
@ -913,7 +917,8 @@ exports.add_narrow_filter = function(name, type, uri) {
list_item = $('<li>').attr('data-name', encodeURIComponent(name))
.html($('<a>').attr('href', uri)
.addClass('subscription_name')
.text(name));
.text(name)
.append('<span class="count">(<span class="value"></span>)</span>'));
if (type === "stream" && subs.have(name).invite_only) {
list_item.append("<i class='icon-lock'/>");
}
@ -921,8 +926,29 @@ exports.add_narrow_filter = function(name, type, uri) {
sort_narrow_list();
};
exports.get_count = function (type, name) {
return exports.get_filter_li(type, name).find('.count .value').text();
};
exports.set_count = function (type, name, count) {
var count_span = exports.get_filter_li(type, name).find('.count');
var value_span = count_span.find('.value');
if (count === 0) {
return exports.clear_count(type, name);
}
count_span.show();
value_span.text(count);
};
exports.clear_count = function (type, name) {
exports.get_filter_li(type, name).find('.count').hide()
.find('.value').text('');
};
exports.remove_narrow_filter = function (name, type) {
$("#" + type + "_filters li[data-name='" + encodeURIComponent(name) + "']").remove();
exports.get_filter_li(type, name).remove();
};
exports.set_presence_list = function(users, presence_info) {

View File

@ -166,6 +166,52 @@ $(function () {
keepTracking: true});
});
function message_range(start, end) {
// Returns messages from the global message_dict in the specified range, inclusive
var result = [];
var i;
for (i = start; i <= end; i++) {
if (message_dict[i] !== undefined) {
result.push(message_dict[i]);
}
}
return result;
}
var unread_filters = {'stream': {}, 'private': {}};
function process_unread_counts(messages, decrement) {
var existing, hashkey;
$.each(messages, function (index, message) {
if (message.id <= furthest_read && decrement !== true) {
return;
}
if (message.sender_email === email) {
return;
}
if (message.type === 'stream') {
hashkey = message.display_recipient;
} else {
hashkey = message.display_reply_to;
}
existing = unread_filters[message.type][hashkey];
if (existing === undefined) {
unread_filters[message.type][hashkey] = {};
}
if (decrement) {
delete unread_filters[message.type][hashkey][message.id];
} else {
unread_filters[message.type][hashkey][message.id] = true;
}
});
$.each(unread_filters.stream, function(index, obj) {
ui.set_count("stream", index, Object.keys(obj).length);
});
}
function update_selected_message(message, opts) {
var cls = 'selected_message';
$('.' + cls).removeClass(cls);
@ -175,6 +221,7 @@ function update_selected_message(message, opts) {
// Narrowing is a temporary view on top of the home view and
// doesn't affect your pointer in the home view.
// Similarly, lurk mode does not affect your pointer.
process_unread_counts(message_range(furthest_read + 1, new_selected_id), true);
if (! narrow.active() && lurk_stream === undefined) {
persistent_message_id = new_selected_id;
if (new_selected_id > furthest_read)
@ -521,6 +568,7 @@ function add_messages(messages, add_to_home) {
&& !narrow.active()) {
prepended = true;
}
process_unread_counts(messages);
}
if (narrow.active()) {

View File

@ -128,6 +128,11 @@ li.active-filter {
font-weight: bold;
}
ul.filters .count {
margin-left: 0.5em;
display: none;
}
.phone_new_message_button {
margin-top: 5px;
float: left;