From 536236d9b1e111e085e9f2ceb74b566112fb401c Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 19 Apr 2018 12:17:22 +0000 Subject: [PATCH] buddy list: Extract buddy_list.js. --- .eslintrc.json | 1 + frontend_tests/node_tests/activity.js | 1 + static/js/activity.js | 35 +++++++------------ static/js/buddy_list.js | 50 +++++++++++++++++++++++++++ tools/test-js-with-node | 1 + zproject/settings.py | 1 + 6 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 static/js/buddy_list.js diff --git a/.eslintrc.json b/.eslintrc.json index 8509b8a15b..fb0f5a2930 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -143,6 +143,7 @@ "tab_bar": false, "emoji": false, "presence": false, + "buddy_list": false, "activity": false, "invite": false, "colorspace": false, diff --git a/frontend_tests/node_tests/activity.js b/frontend_tests/node_tests/activity.js index 02784295ba..c83882c395 100644 --- a/frontend_tests/node_tests/activity.js +++ b/frontend_tests/node_tests/activity.js @@ -36,6 +36,7 @@ zrequire('narrow'); zrequire('util'); zrequire('presence'); zrequire('people'); +zrequire('buddy_list'); zrequire('activity'); zrequire('stream_list'); diff --git a/static/js/activity.js b/static/js/activity.js index e9879e9c9f..cafe4f357d 100644 --- a/static/js/activity.js +++ b/static/js/activity.js @@ -84,7 +84,9 @@ function update_group_count_in_dom(count_span, value_span, count) { } function get_pm_list_item(user_id) { - return $("li.user_sidebar_entry[data-user-id='" + user_id + "']"); + return buddy_list.find_li({ + key: user_id, + }); } function get_group_list_item(user_ids_string) { @@ -305,27 +307,12 @@ exports.insert_user_into_list = function (user_id) { } var info = info_for(user_id); - $('#user_presences').find('[data-user-id="' + user_id + '"]').remove(); - var html = templates.render('user_presence_row', info); + buddy_list.insert_or_move({ + key: user_id, + item: info, + compare_function: compare_function, + }); - var items = $('#user_presences li').toArray(); - - function insert() { - var i = 0; - - for (i = 0; i < items.length; i += 1) { - var li = $(items[i]); - var list_user_id = li.attr('data-user-id'); - if (compare_function(user_id, list_user_id) < 0) { - li.before(html); - return; - } - } - - $('#user_presences').append(html); - } - - insert(); exports.update_scrollbar.users(); var elt = get_pm_list_item(user_id); @@ -344,8 +331,10 @@ exports.build_user_sidebar = function () { // function. return typeof person !== "undefined"; }); - var html = templates.render('user_presence_rows', {users: user_info}); - $('#user_presences').html(html); + + buddy_list.populate({ + items: user_info, + }); // Update user fading, if necessary. compose_fade.update_faded_users(); diff --git a/static/js/buddy_list.js b/static/js/buddy_list.js new file mode 100644 index 0000000000..9782429dd8 --- /dev/null +++ b/static/js/buddy_list.js @@ -0,0 +1,50 @@ +var buddy_list = (function () { + var self = {}; + + self.populate = function (opts) { + var user_info = opts.items; + + var html = templates.render('user_presence_rows', {users: user_info}); + $('#user_presences').html(html); + }; + + self.find_li = function (opts) { + var user_id = opts.key; + return $("li.user_sidebar_entry[data-user-id='" + user_id + "']"); + }; + + self.insert_or_move = function (opts) { + var user_id = opts.key; + var info = opts.item; + var compare_function = opts.compare_function; + + $('#user_presences').find('[data-user-id="' + user_id + '"]').remove(); + var html = templates.render('user_presence_row', info); + + var items = $('#user_presences li').toArray(); + + function insert() { + var i = 0; + + for (i = 0; i < items.length; i += 1) { + var li = $(items[i]); + var list_user_id = li.attr('data-user-id'); + if (compare_function(user_id, list_user_id) < 0) { + li.before(html); + return; + } + } + + $('#user_presences').append(html); + } + + insert(); + }; + + return self; +}()); + +if (typeof module !== 'undefined') { + module.exports = buddy_list; +} + diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 8306d70e8c..ddd36acb7e 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -27,6 +27,7 @@ enforce_fully_covered = { 'static/js/activity.js', 'static/js/alert_words.js', 'static/js/bot_data.js', + 'static/js/buddy_list.js', 'static/js/channel.js', 'static/js/colorspace.js', 'static/js/common.js', diff --git a/zproject/settings.py b/zproject/settings.py index c1ef4442b0..619cd5bfd7 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -1127,6 +1127,7 @@ JS_SPECS = { 'js/server_events_dispatch.js', 'js/zulip.js', 'js/presence.js', + 'js/buddy_list.js', 'js/activity.js', 'js/user_events.js', 'js/colorspace.js',