topic lists: Refactor handling of parent element.

The widget that gets built in topic_list.build_widget()
now knows how to add itself to its parent element and expose
an interface to retrieve the parent.
This commit is contained in:
Steve Howell 2016-11-10 11:05:14 -08:00 committed by Tim Abbott
parent 3b7430ba82
commit d8b5558699
2 changed files with 18 additions and 15 deletions

View File

@ -30,13 +30,15 @@ global.compile_template('topic_list_item');
return 1;
};
var widget = topic_list.build_widget(stream, active_topic, max_topics);
var parent_elem = $('<div>');
var widget = topic_list.build_widget(parent_elem, stream, active_topic, max_topics);
var topic_html = widget.get_dom();
assert.equal(widget.get_parent(), parent_elem);
var topic = $(topic_html).find('a').text().trim();
assert.equal(topic, 'coding');
var output_html = '<div>' + topic_html.html() + '</div>';
global.write_test_output("test_topic_list_build_widget", topic_html.html());
global.write_test_output("test_topic_list_build_widget", parent_elem.html());
}());

View File

@ -33,7 +33,7 @@ exports.set_count = function (stream_name, topic, count) {
}
};
exports.build_widget = function (stream, active_topic, max_topics) {
exports.build_widget = function (parent_elem, stream, active_topic, max_topics) {
var self = {};
self.topic_items = new Dict({fold_case: true});
@ -86,6 +86,10 @@ exports.build_widget = function (stream, active_topic, max_topics) {
return ul;
}
self.get_parent = function () {
return parent_elem;
};
self.is_for_stream = function (stream_name) {
return stream === stream_name;
};
@ -124,6 +128,13 @@ exports.build_widget = function (stream, active_topic, max_topics) {
self.dom = build_list(stream, active_topic, max_topics);
parent_elem.append(self.dom);
if (active_topic) {
self.activate_topic(active_topic);
}
return self;
};
@ -131,18 +142,8 @@ exports.rebuild = function (stream_li, stream) {
var max_topics = 5;
var active_topic = narrow.topic();
exports.remove_expanded_topics();
var widget = exports.build_widget(stream, active_topic, max_topics);
stream_li.append(widget.get_dom());
if (active_topic) {
widget.activate_topic(active_topic);
}
active_widget = widget; // set our global
active_widget = exports.build_widget(stream_li, stream, active_topic, max_topics);
};
// For zooming, we only do topic-list stuff here...let stream_list