From d8b5558699b47231eba2991fd4b0b7546a341f6d Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 10 Nov 2016 11:05:14 -0800 Subject: [PATCH] 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. --- frontend_tests/node_tests/topic_list.js | 8 +++++--- static/js/topic_list.js | 25 +++++++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/frontend_tests/node_tests/topic_list.js b/frontend_tests/node_tests/topic_list.js index cfea9ac49a..243f323aa0 100644 --- a/frontend_tests/node_tests/topic_list.js +++ b/frontend_tests/node_tests/topic_list.js @@ -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 = $('
'); + 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 = '
' + topic_html.html() + '
'; - global.write_test_output("test_topic_list_build_widget", topic_html.html()); + global.write_test_output("test_topic_list_build_widget", parent_elem.html()); }()); diff --git a/static/js/topic_list.js b/static/js/topic_list.js index 72f2db19c1..b7021d35fd 100644 --- a/static/js/topic_list.js +++ b/static/js/topic_list.js @@ -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