From ff67af8312779345b0060b85dd8f01b5e089778b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 18 Aug 2020 13:03:13 +0530 Subject: [PATCH] Content server: Fix Esc ky not working in several views. Fixes #1849958 [[Enhancement] Go back by clicking Esc on server](https://bugs.launchpad.net/calibre/+bug/1849958) --- src/pyj/book_list/add.pyj | 3 ++- src/pyj/book_list/convert_book.pyj | 4 +++- src/pyj/book_list/local_books.pyj | 6 ++++-- src/pyj/book_list/views.pyj | 5 +++-- src/pyj/widgets.pyj | 10 ++++++++++ 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/pyj/book_list/add.pyj b/src/pyj/book_list/add.pyj index 8b10658884..55ec035e0c 100644 --- a/src/pyj/book_list/add.pyj +++ b/src/pyj/book_list/add.pyj @@ -16,7 +16,7 @@ from file_uploads import ( ) from session import get_interface_data from utils import safe_set_inner_html -from widgets import create_button +from widgets import create_button, enable_escape_key state = { 'in_progress': False, @@ -175,6 +175,7 @@ def add_books_panel(container_id): state.in_progress = True state.container_id = container_id state.fake_send = False + enable_escape_key(container, on_close) return upload_files_widget(container, files_chosen) diff --git a/src/pyj/book_list/convert_book.pyj b/src/pyj/book_list/convert_book.pyj index fb77bbd212..de79dc1b89 100644 --- a/src/pyj/book_list/convert_book.pyj +++ b/src/pyj/book_list/convert_book.pyj @@ -15,7 +15,7 @@ from book_list.ui import set_panel_handler from dom import add_extra_css, build_rule, clear from modals import error_dialog from utils import conditional_timeout, human_readable, parse_url_params -from widgets import create_button +from widgets import create_button, enable_escape_key CLASS_NAME = 'convert-book-panel' current_state = 'initializing' @@ -243,6 +243,7 @@ def create_configuring_markup(): conversion_data.configuring_group_title = group_title current_state = 'configure-group' apply_state_to_markup() + document.getElementById(overall_container_id).focus() def is_group_configurable(name): if entry_points[name]: @@ -475,6 +476,7 @@ def init(container_id): conditional_timeout(container_id, 5, check_for_data_loaded) q = parse_url_params() fetch_conversion_data(q.book_id) + enable_escape_key(container, on_close.bind(None, container_id)) set_panel_handler('convert_book', init) diff --git a/src/pyj/book_list/local_books.pyj b/src/pyj/book_list/local_books.pyj index 9d4ac49f4e..a2cd1a6009 100644 --- a/src/pyj/book_list/local_books.pyj +++ b/src/pyj/book_list/local_books.pyj @@ -13,7 +13,7 @@ from book_list.views import DEFAULT_MODE, get_view_mode, setup_view_mode from dom import clear, ensure_id from modals import create_custom_dialog, error_dialog from utils import conditional_timeout, safe_set_inner_html -from widgets import create_button +from widgets import create_button, enable_escape_key CLASS_NAME = 'local-books-list' @@ -221,7 +221,8 @@ def show_recent(): def init(container_id): container = document.getElementById(container_id) - create_top_bar(container, title=_('Downloaded books'), action=home, icon='home') + on_close = home + create_top_bar(container, title=_('Downloaded books'), action=on_close, icon='home') add_button(container, 'trash', confirm_delete_all, _('Delete all downloaded books')) # book list recent = E.div(class_=CLASS_NAME) @@ -232,6 +233,7 @@ def init(container_id): _('Loading downloaded books from local storage, please wait...') )) conditional_timeout(recent_container_id, 5, show_recent) + enable_escape_key(container, on_close) diff --git a/src/pyj/book_list/views.pyj b/src/pyj/book_list/views.pyj index 7a98709199..f0f52b24cd 100644 --- a/src/pyj/book_list/views.pyj +++ b/src/pyj/book_list/views.pyj @@ -40,7 +40,7 @@ from dom import add_extra_css, build_rule, clear, ensure_id, set_css from modals import error_dialog from session import get_interface_data from utils import conditional_timeout, parse_url_params, safe_set_inner_html -from widgets import create_button, create_spinner +from widgets import create_button, create_spinner, enable_escape_key CLASS_NAME = 'book-list-container' ITEM_CLASS_NAME = 'book-list-item' @@ -377,6 +377,7 @@ def create_sort_panel(container_id): items.push(create_item(name, subtitle=subtitle, icon=icon_name, action=action)) container.appendChild(E.div()) create_item_list(container.lastChild, items, _('Change how the list of books is sorted')) + enable_escape_key(container, close_action) # }}} # Searching {{{ @@ -468,7 +469,7 @@ def create_more_actions_panel(container_id): )) container.appendChild(E.div()) create_item_list(container.lastChild, items) - + enable_escape_key(container, back) # }}} # Adding books {{{ diff --git a/src/pyj/widgets.pyj b/src/pyj/widgets.pyj index 2c08d5638a..42c3e13439 100644 --- a/src/pyj/widgets.pyj +++ b/src/pyj/widgets.pyj @@ -216,3 +216,13 @@ add_extra_css(def(): ans += Breadcrumbs.STYLE_RULES + '\n' return ans ) + + +def enable_escape_key(container, action): + container.setAttribute('tabindex', '0') + container.addEventListener('keydown', def(ev): + if ev.key is 'Escape': + ev.stopPropagation(), ev.preventDefault() + action() + , {'passive': False}) + container.focus()