2013-05-06 02:54:15 +02:00
|
|
|
var stream_list = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-05-16 23:40:07 +02:00
|
|
|
var last_private_message_count = 0;
|
2013-05-07 19:07:05 +02:00
|
|
|
var previous_sort_order;
|
2013-05-16 23:40:07 +02:00
|
|
|
|
2013-05-06 02:54:15 +02:00
|
|
|
exports.sort_narrow_list = function () {
|
2013-05-06 23:36:22 +02:00
|
|
|
var streams = subs.subscribed_streams();
|
|
|
|
if (streams.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sort_recent = (streams.length > 40);
|
|
|
|
|
|
|
|
streams.sort(function(a, b) {
|
2013-05-06 02:54:15 +02:00
|
|
|
if (sort_recent) {
|
2013-05-06 23:36:22 +02:00
|
|
|
if (recent_subjects[b] !== undefined &&
|
|
|
|
recent_subjects[a] === undefined) {
|
2013-05-06 02:54:15 +02:00
|
|
|
return 1;
|
2013-05-06 23:36:22 +02:00
|
|
|
} else if (recent_subjects[b] === undefined &&
|
|
|
|
recent_subjects[a] !== undefined) {
|
2013-05-06 02:54:15 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-05-06 23:36:22 +02:00
|
|
|
return util.strcmp(a, b);
|
2013-05-06 02:54:15 +02:00
|
|
|
});
|
|
|
|
|
2013-05-07 19:07:05 +02:00
|
|
|
if (previous_sort_order !== undefined
|
|
|
|
&& util.array_compare(previous_sort_order, streams)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
previous_sort_order = streams;
|
|
|
|
|
2013-05-06 23:36:22 +02:00
|
|
|
var parent = $('#stream_filters');
|
2013-05-06 02:54:15 +02:00
|
|
|
parent.empty();
|
|
|
|
|
2013-05-06 23:36:22 +02:00
|
|
|
var elems = [];
|
|
|
|
$.each(streams, function(i, stream) {
|
|
|
|
// TODO: we should export the sub objects better
|
|
|
|
var li = $(subs.have(stream).sidebar_li);
|
2013-05-06 02:54:15 +02:00
|
|
|
if (sort_recent) {
|
2013-05-06 23:36:22 +02:00
|
|
|
if (recent_subjects[stream] === undefined) {
|
|
|
|
li.addClass('inactive_stream');
|
2013-05-06 02:54:15 +02:00
|
|
|
} else {
|
2013-05-06 23:36:22 +02:00
|
|
|
li.removeClass('inactive_stream');
|
2013-05-06 02:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-06 23:36:22 +02:00
|
|
|
elems.push(li.get(0));
|
2013-05-06 02:54:15 +02:00
|
|
|
});
|
2013-05-06 23:36:22 +02:00
|
|
|
$(elems).appendTo(parent);
|
2013-05-06 02:54:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function iterate_to_find(selector, data_name, context) {
|
|
|
|
var retval = $();
|
|
|
|
$(selector, context).each(function (idx, elem) {
|
|
|
|
var jelem = $(elem);
|
|
|
|
if (jelem.attr('data-name') === data_name) {
|
|
|
|
retval = jelem;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-05-06 22:18:01 +02:00
|
|
|
function get_filter_li(type, name) {
|
2013-05-06 02:54:15 +02:00
|
|
|
if (type === 'stream') {
|
|
|
|
return $("#stream_sidebar_" + subs.stream_id(name));
|
|
|
|
}
|
|
|
|
return iterate_to_find("#" + type + "_filters > li", name);
|
2013-05-06 22:18:01 +02:00
|
|
|
}
|
2013-05-06 02:54:15 +02:00
|
|
|
|
2013-05-06 22:18:01 +02:00
|
|
|
function get_subject_filter_li(stream, subject) {
|
|
|
|
var stream_li = get_filter_li('stream', stream);
|
2013-05-06 02:54:15 +02:00
|
|
|
return iterate_to_find(".expanded_subjects li", subject, stream_li);
|
2013-05-06 22:18:01 +02:00
|
|
|
}
|
2013-05-06 02:54:15 +02:00
|
|
|
|
2013-05-09 21:12:53 +02:00
|
|
|
// Adds the sidebar stream name that, when clicked,
|
|
|
|
// narrows to that stream
|
2013-05-06 22:18:01 +02:00
|
|
|
function add_narrow_filter(name, type) {
|
2013-05-09 21:12:53 +02:00
|
|
|
var uri = narrow.by_stream_uri(name);
|
2013-05-06 02:54:15 +02:00
|
|
|
var list_item;
|
|
|
|
|
2013-05-06 22:18:01 +02:00
|
|
|
if (get_filter_li(type, name).length) {
|
2013-05-06 02:54:15 +02:00
|
|
|
// already exists
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For some reason, even though the span is inline-block, if it is empty it
|
|
|
|
// takes up no space and you don't see the background color. Thus, add a
|
|
|
|
// to get the inline-block behavior we want.
|
|
|
|
var swatch = $('<span/>').attr('id', "stream_sidebar_swatch_" + subs.stream_id(name))
|
|
|
|
.addClass('streamlist_swatch')
|
|
|
|
.css('background-color', subs.get_color(name)).html(" ");
|
2013-05-14 21:02:10 +02:00
|
|
|
list_item = $('<li>').attr('data-name', name)
|
|
|
|
.addClass("narrow-filter")
|
|
|
|
.html(swatch);
|
2013-05-06 02:54:15 +02:00
|
|
|
if (type === 'stream') {
|
|
|
|
list_item.attr('id', "stream_sidebar_" + subs.stream_id(name));
|
2013-05-15 23:36:50 +02:00
|
|
|
if (subs.have(name).in_home_view === false) {
|
|
|
|
list_item.addClass("out_of_home_view");
|
|
|
|
}
|
2013-05-06 02:54:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
list_item.append($('<a>').attr('href', uri)
|
|
|
|
.addClass('subscription_name')
|
|
|
|
.text(name)
|
2013-05-14 21:02:10 +02:00
|
|
|
.append('<span class="count">(<span class="value"></span>)</span>'))
|
|
|
|
.append('<span class="arrow pull-right">▽</span>');
|
2013-05-06 02:54:15 +02:00
|
|
|
if (type === "stream" && subs.have(name).invite_only) {
|
|
|
|
list_item.append("<i class='icon-lock'/>");
|
|
|
|
}
|
|
|
|
$("#" + type + "_filters").append(list_item);
|
2013-05-06 23:36:22 +02:00
|
|
|
return list_item;
|
2013-05-06 22:18:01 +02:00
|
|
|
}
|
2013-05-06 02:54:15 +02:00
|
|
|
|
|
|
|
exports.get_count = function (type, name) {
|
2013-05-06 22:18:01 +02:00
|
|
|
return get_filter_li(type, name).find('.count .value').text();
|
2013-05-06 02:54:15 +02:00
|
|
|
};
|
|
|
|
|
2013-05-16 22:38:09 +02:00
|
|
|
function update_count_in_dom(count_span, value_span, count) {
|
2013-05-07 20:02:24 +02:00
|
|
|
if (count === 0) {
|
2013-05-16 22:38:09 +02:00
|
|
|
count_span.hide();
|
|
|
|
value_span.text('');
|
|
|
|
return;
|
2013-05-07 20:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
count_span.show();
|
|
|
|
value_span.text(count);
|
|
|
|
}
|
|
|
|
|
2013-05-06 02:54:15 +02:00
|
|
|
exports.set_count = function (type, name, count) {
|
2013-05-06 22:18:01 +02:00
|
|
|
var count_span = get_filter_li(type, name).find('.count');
|
2013-05-06 02:54:15 +02:00
|
|
|
var value_span = count_span.find('.value');
|
2013-05-16 22:38:09 +02:00
|
|
|
update_count_in_dom(count_span, value_span, count);
|
2013-05-07 20:02:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.set_subject_count = function (stream, subject, count) {
|
|
|
|
var count_span = get_subject_filter_li(stream, subject).find('.subject_count');
|
|
|
|
var value_span = count_span.find('.value');
|
|
|
|
|
|
|
|
if (count_span.length === 0 || value_span.length === 0) {
|
|
|
|
return;
|
2013-05-06 02:54:15 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 22:38:09 +02:00
|
|
|
update_count_in_dom(count_span, value_span, count);
|
2013-05-06 02:54:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.remove_narrow_filter = function (name, type) {
|
2013-05-06 22:18:01 +02:00
|
|
|
get_filter_li(type, name).remove();
|
2013-05-06 02:54:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.remove_all_narrow_filters = function () {
|
|
|
|
$("#stream_filters").children().remove();
|
|
|
|
};
|
|
|
|
|
|
|
|
function rebuild_recent_subjects(stream, subject) {
|
|
|
|
$('.expanded_subjects').remove();
|
2013-05-06 22:18:01 +02:00
|
|
|
var stream_li = get_filter_li('stream', stream);
|
2013-05-06 02:54:15 +02:00
|
|
|
var subjects = recent_subjects[stream] || [];
|
2013-05-07 20:02:24 +02:00
|
|
|
$.each(subjects, function (idx, subject_obj) {
|
2013-05-17 21:32:26 +02:00
|
|
|
var num_unread = unread.num_unread_for_subject(stream, subject_obj.subject);
|
|
|
|
subject_obj.unread = num_unread;
|
|
|
|
subject_obj.has_unread = num_unread !== 0;
|
2013-05-07 20:02:24 +02:00
|
|
|
});
|
2013-05-06 02:54:15 +02:00
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2013-05-06 02:54:15 +02:00
|
|
|
stream_li.append(templates.render('sidebar_subject_list',
|
|
|
|
{subjects: subjects,
|
|
|
|
stream: stream}));
|
|
|
|
if (subject !== undefined) {
|
2013-05-06 22:18:01 +02:00
|
|
|
get_subject_filter_li(stream, subject).addClass('active-subject-filter');
|
2013-05-06 02:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.update_streams_sidebar = function () {
|
|
|
|
exports.sort_narrow_list();
|
|
|
|
|
|
|
|
if (! narrow.active()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var op_stream = narrow.filter().operands('stream');
|
|
|
|
var op_subject = narrow.filter().operands('subject');
|
|
|
|
var subject;
|
|
|
|
if (op_stream.length !== 0) {
|
|
|
|
if (op_subject.length !== 0) {
|
|
|
|
subject = op_subject[0];
|
|
|
|
}
|
|
|
|
if (subs.have(op_stream[0])) {
|
|
|
|
rebuild_recent_subjects(op_stream[0], subject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-16 23:40:07 +02:00
|
|
|
function do_new_private_messages_animation() {
|
|
|
|
var li = get_filter_li("global", "private-message");
|
|
|
|
li.addClass("new_private_messages");
|
|
|
|
function mid_animation() {
|
|
|
|
li.removeClass("new_private_messages");
|
|
|
|
li.addClass("new_private_messages_fadeout");
|
|
|
|
}
|
|
|
|
function end_animation() {
|
|
|
|
li.removeClass("new_private_messages_fadeout");
|
|
|
|
}
|
|
|
|
setTimeout(mid_animation, 3000);
|
|
|
|
setTimeout(end_animation, 6000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function animate_private_message_changes(new_private_message_count) {
|
|
|
|
if (new_private_message_count > last_private_message_count) {
|
|
|
|
do_new_private_messages_animation();
|
|
|
|
}
|
|
|
|
last_private_message_count = new_private_message_count;
|
|
|
|
}
|
|
|
|
|
2013-05-14 03:58:07 +02:00
|
|
|
exports.update_dom_with_unread_counts = function (counts) {
|
|
|
|
// counts is just a data object that gets calculated elsewhere
|
|
|
|
// Our job is to update some DOM elements.
|
|
|
|
|
|
|
|
// counts.stream_count maps streams to counts
|
|
|
|
$.each(counts.stream_count, function(stream, count) {
|
|
|
|
exports.set_count("stream", stream, count);
|
|
|
|
});
|
|
|
|
|
|
|
|
// counts.subject_count maps streams to hashes of subjects to counts
|
|
|
|
$.each(counts.subject_count, function(stream, subject_hash) {
|
|
|
|
$.each(subject_hash, function(subject, count) {
|
|
|
|
exports.set_subject_count(stream, subject, count);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// integer counts
|
|
|
|
exports.set_count("global", "private-message", counts.private_message_count);
|
|
|
|
exports.set_count("global", "home", counts.home_unread_messages);
|
2013-05-16 23:40:07 +02:00
|
|
|
|
|
|
|
// For now increases in private messages get special treatment in terms of
|
|
|
|
// animating the left pane. It is unlikely that we will generalize this,
|
|
|
|
// since Starred messages are user-initiated and Home messages would be too
|
|
|
|
// spammy.
|
|
|
|
animate_private_message_changes(counts.private_message_count);
|
2013-05-14 03:58:07 +02:00
|
|
|
};
|
|
|
|
|
2013-05-06 02:54:15 +02:00
|
|
|
$(function () {
|
|
|
|
$(document).on('narrow_activated.zephyr', function (event) {
|
|
|
|
$("ul.filters li").removeClass('active-filter active-subject-filter');
|
|
|
|
|
|
|
|
// TODO: handle confused filters like "in:all stream:foo"
|
|
|
|
var op_in = event.filter.operands('in');
|
|
|
|
if (op_in.length !== 0) {
|
|
|
|
if (['all', 'home'].indexOf(op_in[0]) !== -1) {
|
|
|
|
$("#global_filters li[data-name='" + op_in[0] + "']").addClass('active-filter');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var op_is = event.filter.operands('is');
|
|
|
|
if (op_is.length !== 0) {
|
|
|
|
if (['private-message', 'starred'].indexOf(op_is[0]) !== -1) {
|
|
|
|
$("#global_filters li[data-name='" + op_is[0] + "']").addClass('active-filter');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var op_stream = event.filter.operands('stream');
|
|
|
|
if (op_stream.length !== 0 && subs.have(op_stream[0])) {
|
2013-05-06 22:18:01 +02:00
|
|
|
var stream_li = get_filter_li('stream', op_stream[0]);
|
2013-05-06 02:54:15 +02:00
|
|
|
var op_subject = event.filter.operands('subject');
|
|
|
|
var subject;
|
|
|
|
if (op_subject.length !== 0) {
|
|
|
|
subject = op_subject[0];
|
|
|
|
} else {
|
|
|
|
stream_li.addClass('active-filter');
|
|
|
|
}
|
|
|
|
rebuild_recent_subjects(op_stream[0], subject);
|
|
|
|
}
|
2013-05-16 18:16:50 +02:00
|
|
|
process_visible_unread_messages();
|
2013-05-06 02:54:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('narrow_deactivated.zephyr', function (event) {
|
|
|
|
$("ul.filters li").removeClass('active-filter active-subject-filter');
|
|
|
|
$("ul.expanded_subjects").remove();
|
|
|
|
$("#global_filters li[data-name='home']").addClass('active-filter');
|
|
|
|
});
|
2013-05-06 02:54:15 +02:00
|
|
|
|
|
|
|
$(document).on('sub_obj_created.zephyr', function (event) {
|
|
|
|
if (event.sub.subscribed) {
|
|
|
|
var stream_name = event.sub.name;
|
2013-05-06 23:36:22 +02:00
|
|
|
var li = add_narrow_filter(stream_name, "stream");
|
|
|
|
if (li) {
|
|
|
|
event.sub.sidebar_li = li;
|
|
|
|
}
|
2013-05-06 02:54:15 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('subscription_add_done.zephyr', function (event) {
|
|
|
|
var stream_name = event.sub.name;
|
2013-05-06 23:36:22 +02:00
|
|
|
var li = add_narrow_filter(stream_name, "stream");
|
|
|
|
if (li) {
|
|
|
|
event.sub.sidebar_li = li;
|
|
|
|
}
|
2013-05-06 02:54:15 +02:00
|
|
|
exports.sort_narrow_list();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('subscription_remove_done.zephyr', function (event) {
|
|
|
|
var stream_name = event.sub.name;
|
|
|
|
exports.remove_narrow_filter(stream_name, 'stream');
|
2013-05-13 17:23:07 +02:00
|
|
|
// We need to make sure we resort if the removed sub gets added again
|
|
|
|
previous_sort_order = undefined;
|
2013-05-06 02:54:15 +02:00
|
|
|
});
|
2013-05-06 02:54:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|