buddy list: Extract activity.narrow_for_user.

This change makes a common code path for these two operations:
        * clicking on a user
        * hitting enter when a user is highlighted

The newer codepath, for the enter key, had some differences that
were just confusing.  For example, there's no need to open the
compose box, since that's already handled by the narrowing code.

For possibly dubious reasons, I let each handler still call
popovers.hide_all() on its own, since it makes the code a bit
more consistent with existing code patterns.
This commit is contained in:
Steve Howell
2018-04-19 21:14:58 +00:00
committed by Tim Abbott
parent 54389f7b41
commit 76d83af62b
3 changed files with 26 additions and 24 deletions

View File

@@ -477,20 +477,27 @@ exports.toggle_filter_displayed = function () {
}
};
exports.narrow_for_user = function (opts) {
var user_id = buddy_list.get_key_from_li({li: opts.li});
var email = people.get_person_from_user_id(user_id).email;
var narrow_opts = {
select_first_unread: true,
trigger: 'sidebar',
};
narrow.by('pm-with', email, narrow_opts);
exports.clear_and_hide_search();
};
function keydown_enter_key() {
// Is there at least one user?
if ($('#user_presences li.user_sidebar_entry.narrow-filter').length > 0) {
// There must be a 'highlighted_user' user
var select_user = $('#user_presences li.user_sidebar_entry.narrow-filter.highlighted_user')
.expectOne().attr('data-user-id');
var email = people.get_person_from_user_id(select_user).email;
narrow.by('pm-with', email, {select_first_unread: true, trigger: 'user sidebar'});
compose_actions.start('private', { trigger: 'sidebar enter key',
private_message_recipient: email});
// Clear the user filter
exports.clear_and_hide_search();
var li = $('#user_presences li.user_sidebar_entry.narrow-filter.highlighted_user');
exports.narrow_for_user({li: li});
popovers.hide_all();
}
}

View File

@@ -13,6 +13,11 @@ var buddy_list = (function () {
return $("li.user_sidebar_entry[data-user-id='" + user_id + "']");
};
self.get_key_from_li = function (opts) {
var user_id = opts.li.expectOne().attr('data-user-id');
return user_id;
};
self.insert_or_move = function (opts) {
var user_id = opts.key;
var info = opts.item;

View File

@@ -335,22 +335,12 @@ $(function () {
});
$('#user_presences').expectOne().on('click', '.selectable_sidebar_block', function (e) {
var user_id = $(e.target).parents('li').attr('data-user-id');
var email = people.get_person_from_user_id(user_id).email;
activity.clear_and_hide_search();
narrow.by('pm-with', email, {select_first_unread: true, trigger: 'sidebar'});
// The preventDefault is necessary so that clicking the
// link doesn't jump us to the top of the page.
var li = $(e.target).parents('li');
activity.narrow_for_user({li: li});
e.preventDefault();
// The stopPropagation is necessary so that we don't
// see the following sequence of events:
// 1. This click "opens" the composebox
// 2. This event propagates to the body, which says "oh, hey, the
// composebox is open and you clicked out of it, you must want to
// stop composing!"
e.stopPropagation();
// Since we're stopping propagation we have to manually close any
// open popovers.
popovers.hide_all();
});