Move get_topic_filter_li to topic_list.js.

This also brought along:
    iterate_to_find (copied, see large comment explaining why)
    activate_topic (extracted from a one-liner)
    set_count (formerly stream_list.set_subject_count)

For get_topic_filter_li, we now pass in stream_li instead of
stream to decouple parent/child responsibilities between the
components.

Also, I made some s/subject/topic/ fixes.
This commit is contained in:
Steve Howell
2016-10-26 16:36:20 -07:00
committed by Tim Abbott
parent 4f38cfdc7f
commit 9b6b743a1a
2 changed files with 41 additions and 20 deletions

View File

@@ -226,11 +226,6 @@ function reset_to_unnarrowed(narrowed_within_same_stream) {
remove_expanded_private_messages();
}
function get_subject_filter_li(stream, subject) {
var stream_li = get_filter_li('stream', stream);
return iterate_to_find(".expanded_subjects li.expanded_subject", subject, stream_li);
}
function get_private_message_filter_li(conversation) {
var pm_li = get_filter_li('global', 'private');
return iterate_to_find(".expanded_private_messages li.expanded_private_message",
@@ -353,19 +348,6 @@ function set_count_toggle_button(elem, count) {
}
}
exports.set_subject_count = function (stream, subject, count) {
var subject_li = get_subject_filter_li(stream, subject);
var count_span = subject_li.find('.subject_count');
var value_span = count_span.find('.value');
if (count_span.length === 0 || value_span.length === 0) {
return;
}
topic_list.update_count_in_dom(count_span, value_span, count);
};
exports.set_pm_conversation_count = function (conversation, count) {
var pm_li = get_private_message_filter_li(conversation);
var count_span = pm_li.find('.private_message_count');
@@ -470,7 +452,7 @@ function rebuild_recent_topics(stream, active_topic) {
stream_li.append(topic_dom);
if (active_topic) {
get_subject_filter_li(stream, active_topic).addClass('active-sub-filter');
topic_list.activate_topic(stream_li, active_topic);
}
}
@@ -574,8 +556,9 @@ exports.update_dom_with_unread_counts = function (counts) {
// counts.subject_count maps streams to hashes of subjects to counts
counts.subject_count.each(function (subject_hash, stream) {
var stream_li = get_filter_li('stream', stream);
subject_hash.each(function (count, subject) {
exports.set_subject_count(stream, subject, count);
topic_list.set_count(stream_li, subject, count);
});
});

View File

@@ -2,6 +2,31 @@ var topic_list = (function () {
var exports = {};
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) : $();
}
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');
};
exports.update_count_in_dom = function (count_span, value_span, count) {
if (count === 0) {
count_span.hide();
@@ -13,6 +38,19 @@ exports.update_count_in_dom = function (count_span, value_span, count) {
}
};
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);
};
return exports;
}());
if (typeof module !== 'undefined') {