2016-10-27 01:02:41 +02:00
|
|
|
var topic_list = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2016-10-27 01:36:20 +02:00
|
|
|
function iterate_to_find(selector, name_to_find, context) {
|
|
|
|
// This code is duplicated with stream_list.js, but we should
|
|
|
|
// not try to de-dup this; instead, we should try to make it sane
|
|
|
|
// for topics and avoid O(N) iteration.
|
|
|
|
//
|
|
|
|
// We could start by using canonical lowercase values for the
|
|
|
|
// data-name attributes (and eventually use topic ids when the
|
|
|
|
// back end allows). Either that, or we should have a data
|
|
|
|
// structure that links topic names to list items, so that we
|
|
|
|
// don't have to search the DOM at all.
|
|
|
|
var lowercase_name = name_to_find.toLowerCase();
|
|
|
|
var found = _.find($(selector, context), function (elem) {
|
|
|
|
return $(elem).attr('data-name').toLowerCase() === lowercase_name;
|
|
|
|
});
|
|
|
|
return found ? $(found) : $();
|
|
|
|
}
|
|
|
|
|
2016-10-27 01:53:47 +02:00
|
|
|
exports.remove_expanded_topics = function () {
|
|
|
|
popovers.hide_topic_sidebar_popover();
|
|
|
|
$("ul.expanded_subjects").remove();
|
|
|
|
};
|
|
|
|
|
2016-10-27 01:36:20 +02:00
|
|
|
function get_topic_filter_li(stream_li, topic) {
|
|
|
|
return iterate_to_find(".expanded_subjects li.expanded_subject", topic, stream_li);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.activate_topic = function (stream_li, active_topic) {
|
|
|
|
get_topic_filter_li(stream_li, active_topic).addClass('active-sub-filter');
|
|
|
|
};
|
|
|
|
|
2016-10-27 01:02:41 +02:00
|
|
|
exports.update_count_in_dom = function (count_span, value_span, count) {
|
|
|
|
if (count === 0) {
|
|
|
|
count_span.hide();
|
|
|
|
value_span.text('');
|
|
|
|
} else {
|
|
|
|
count_span.removeClass("zero_count");
|
|
|
|
count_span.show();
|
|
|
|
value_span.text(count);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-27 01:36:20 +02:00
|
|
|
exports.set_count = function (stream_li, topic, count) {
|
|
|
|
var topic_li = get_topic_filter_li(stream_li, topic);
|
|
|
|
var count_span = topic_li.find('.subject_count');
|
|
|
|
var value_span = count_span.find('.value');
|
|
|
|
|
|
|
|
if (count_span.length === 0 || value_span.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.update_count_in_dom(count_span, value_span, count);
|
|
|
|
};
|
|
|
|
|
2016-10-27 02:02:40 +02:00
|
|
|
exports.build_list = function (stream, active_topic, max_topics) {
|
2016-10-28 19:01:43 +02:00
|
|
|
var topics = stream_data.get_recent_topics(stream) || [];
|
2016-10-27 02:02:40 +02:00
|
|
|
|
|
|
|
if (active_topic) {
|
|
|
|
active_topic = active_topic.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
var hiding_topics = false;
|
|
|
|
|
2016-10-29 18:11:01 +02:00
|
|
|
var ul = $('<ul class="expanded_subjects">');
|
|
|
|
ul.attr('data-stream', stream);
|
|
|
|
|
2016-10-28 19:01:43 +02:00
|
|
|
_.each(topics, function (subject_obj, idx) {
|
2016-10-27 02:02:40 +02:00
|
|
|
var topic_name = subject_obj.subject;
|
|
|
|
var num_unread = unread.num_unread_for_subject(stream, subject_obj.canon_subject);
|
|
|
|
|
2016-10-28 19:01:43 +02:00
|
|
|
// Show the most recent topics, as well as any with unread messages
|
2016-10-29 18:11:01 +02:00
|
|
|
var always_visible = (idx < max_topics) || (num_unread > 0) ||
|
|
|
|
(active_topic === topic_name);
|
2016-10-27 02:02:40 +02:00
|
|
|
|
|
|
|
if (!always_visible) {
|
|
|
|
hiding_topics = true;
|
|
|
|
}
|
|
|
|
|
2016-10-29 18:11:01 +02:00
|
|
|
var topic_info = {
|
2016-10-27 02:02:40 +02:00
|
|
|
topic_name: topic_name,
|
|
|
|
unread: num_unread,
|
|
|
|
is_zero: num_unread === 0,
|
|
|
|
is_muted: muting.is_topic_muted(stream, topic_name),
|
|
|
|
zoom_out_hide: !always_visible,
|
|
|
|
url: narrow.by_stream_subject_uri(stream, topic_name)
|
|
|
|
};
|
2016-10-29 18:11:01 +02:00
|
|
|
var li = templates.render('topic_list_item', topic_info);
|
|
|
|
ul.append(li);
|
2016-10-27 02:02:40 +02:00
|
|
|
});
|
|
|
|
|
2016-10-29 18:11:01 +02:00
|
|
|
if (hiding_topics) {
|
|
|
|
var show_more = $('<li class="show-more-topics">');
|
|
|
|
show_more.attr('data-stream', stream);
|
|
|
|
var link = $('<a href="#">');
|
|
|
|
link.html(i18n.t('more topics'));
|
|
|
|
show_more.html(link);
|
|
|
|
ul.append(show_more);
|
|
|
|
}
|
2016-10-27 02:02:40 +02:00
|
|
|
|
2016-10-29 18:11:01 +02:00
|
|
|
return ul;
|
2016-10-27 02:02:40 +02:00
|
|
|
};
|
|
|
|
|
2016-10-27 02:33:24 +02:00
|
|
|
exports.rebuild = function (stream_li, stream, active_topic) {
|
|
|
|
var max_topics = 5;
|
|
|
|
|
|
|
|
exports.remove_expanded_topics();
|
|
|
|
|
|
|
|
var topic_dom = exports.build_list(stream, active_topic, max_topics);
|
|
|
|
stream_li.append(topic_dom);
|
|
|
|
|
|
|
|
if (active_topic) {
|
|
|
|
exports.activate_topic(stream_li, active_topic);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-27 03:47:05 +02:00
|
|
|
exports.set_click_handlers = function (callbacks) {
|
|
|
|
$('#stream_filters').on('click', '.show-more-topics', function (e) {
|
|
|
|
callbacks.zoom_in();
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.show-all-streams').on('click', function (e) {
|
|
|
|
callbacks.zoom_out();
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#stream_filters').on('click', '.subject_box', function (e) {
|
|
|
|
if (e.metaKey || e.ctrlKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In a more componentized world, we would delegate some
|
|
|
|
// of this stuff back up to our parents.
|
|
|
|
if (ui.home_tab_obscured()) {
|
|
|
|
ui.change_tab_to('#home');
|
|
|
|
}
|
|
|
|
|
|
|
|
var stream = $(e.target).parents('ul').attr('data-stream');
|
|
|
|
var topic = $(e.target).parents('li').attr('data-name');
|
|
|
|
|
|
|
|
narrow.activate([{operator: 'stream', operand: stream},
|
|
|
|
{operator: 'topic', operand: topic}],
|
|
|
|
{select_first_unread: true, trigger: 'sidebar'});
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-27 01:36:20 +02:00
|
|
|
|
2016-10-27 01:02:41 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = topic_list;
|
|
|
|
}
|