popovers: Refactor functions using get_action_menu_menu_items.

actions_menu_handle_keyboard now only gets the action menu items
and passes them to the newly added popover_items_handle_keyboard.
popover_items_handle_keyboard takes the key and menu items as its
parameters. The function can be used when handling keyboard input
like user profile popover. Similar refactor has been carried out in
focus_first_action_popover_item. This refactor is a part of adding
the missing support of keyboard navigation to user profile popover.
This commit is contained in:
Shubham Padia
2018-06-12 14:50:39 +05:30
committed by showell
parent 91086e80c0
commit 68d58b92cc

View File

@@ -400,10 +400,7 @@ function get_action_menu_menu_items() {
return $('li:not(.divider):visible a', popover_data.$tip);
}
function focus_first_action_popover_item() {
// For now I recommend only calling this when the user opens the menu with a hotkey.
// Our popup menus act kind of funny when you mix keyboard and mouse.
var items = get_action_menu_menu_items();
function focus_first_popover_item(items) {
if (!items) {
return;
}
@@ -411,6 +408,33 @@ function focus_first_action_popover_item() {
items.eq(0).expectOne().focus();
}
function popover_items_handle_keyboard(key, items) {
if (!items) {
return;
}
var index = items.index(items.filter(':focus'));
if (key === "enter" && index >= 0 && index < items.length) {
return items[index].click();
}
if (index === -1) {
index = 0;
} else if ((key === 'down_arrow' || key === 'vim_down') && index < items.length - 1) {
index += 1;
} else if ((key === 'up_arrow' || key === 'vim_up') && index > 0) {
index -= 1;
}
items.eq(index).focus();
}
function focus_first_action_popover_item() {
// For now I recommend only calling this when the user opens the menu with a hotkey.
// Our popup menus act kind of funny when you mix keyboard and mouse.
var items = get_action_menu_menu_items();
focus_first_popover_item(items);
}
exports.open_message_menu = function (message) {
if (message.locally_echoed) {
// Don't open the popup for locally echoed messages for now.
@@ -429,23 +453,7 @@ exports.open_message_menu = function (message) {
exports.actions_menu_handle_keyboard = function (key) {
var items = get_action_menu_menu_items();
if (!items) {
return;
}
var index = items.index(items.filter(':focus'));
if (key === "enter" && index >= 0 && index < items.length) {
return items[index].click();
}
if (index === -1) {
index = 0;
} else if ((key === 'down_arrow' || key === 'vim_down') && index < items.length - 1) {
index += 1;
} else if ((key === 'up_arrow' || key === 'vim_up') && index > 0) {
index -= 1;
}
items.eq(index).focus();
popover_items_handle_keyboard(key, items);
};
exports.actions_popped = function () {