From 6a0c039d93a256fae93e08814e7904223077e5d9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 20 Dec 2015 16:47:22 +0530 Subject: [PATCH] Fix forward not working when changing searches --- src/pyj/book_list/boss.pyj | 17 ++++++++++++++++- src/pyj/book_list/views.pyj | 10 +++++----- src/pyj/modals.pyj | 3 ++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/pyj/book_list/boss.pyj b/src/pyj/book_list/boss.pyj index b61d150fdb..54f14a5d10 100644 --- a/src/pyj/book_list/boss.pyj +++ b/src/pyj/book_list/boss.pyj @@ -1,6 +1,7 @@ # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2015, Kovid Goyal +from ajax import encode_query from dom import set_css from elementmaker import E from modals import error_dialog, create_modal_container @@ -21,6 +22,7 @@ class Boss: self.interface_data = interface_data self.current_library_id = interface_data['default_library'] self.current_library_name = interface_data['library_map'][self.current_library_id] + self.current_mode = 'book_list' self.update_window_title() div = E.div(id='book-list-container') document.body.appendChild(div) @@ -51,7 +53,7 @@ class Boss: if mode == 'book_list': search = data.search or '' if search != self.ui.books_view.interface_data.search_result.query: - self.ui.books_view.change_search(search) + self.ui.books_view.change_search(search, push_state=False) def change_books(self, data): data.search_result.sort = str.split(data.search_result.sort, ',')[:2].join(',') @@ -63,3 +65,16 @@ class Boss: self.interface_data.metadata = data.metadata self.interface_data.search_result = data.search_result self.ui.refresh_books_view() + + def push_state(self): + query = {} + if self.current_mode != 'book_list': + query.current_mode = self.current_mode + idata = self.interface_data + if idata.library_id != idata.default_library: + query.library_id = idata.library_id + sq = idata.search_result.query + if sq: + query.search = sq + query = encode_query(query) or '?' + window.history.pushState(None, '', query) diff --git a/src/pyj/book_list/views.pyj b/src/pyj/book_list/views.pyj index 0d8abe640c..ccfb577479 100644 --- a/src/pyj/book_list/views.pyj +++ b/src/pyj/book_list/views.pyj @@ -1,7 +1,7 @@ # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2015, Kovid Goyal -from ajax import ajax_send, encode_query +from ajax import ajax_send from dom import set_css, build_rule, clear from elementmaker import E from gettext import gettext as _ @@ -232,13 +232,13 @@ class BooksView: elif end_type != 'abort': error_dialog(_('Could not change sort order'), xhr.error_html) - def change_search(self, query): + def change_search(self, query, push_state=True): self.abort_get_more_books() query = query or '' sd = get_session_data() data = {'search':query, 'sort':sd.get('sort'), 'library_id':self.interface_data.library_id} ajax_progress_dialog('interface-data/get-books', self.search_change_completed.bind(self), _( - 'Fetching data from server, please wait') + '…', query=data) + 'Fetching data from server, please wait') + '…', query=data, extra_data_for_callback={'push_state':push_state}) def search_change_completed(self, end_type, xhr, ev): if end_type == 'load': @@ -248,9 +248,9 @@ class BooksView: boss.change_books(data) except Exception as err: return error_dialog(_('Could not change search query'), err + '', details=err.stack) - query = {'search':self.interface_data.search_result.query, 'library_id':self.interface_data.library_id} - window.history.pushState(None, '', encode_query(query)) boss.ui.close_all_panels() + if xhr.extra_data_for_callback.push_state: + boss.push_state() self.update_fetching_status() window.scrollTo(0, 0) elif end_type != 'abort': diff --git a/src/pyj/modals.pyj b/src/pyj/modals.pyj index 0fc1404a4e..b23eaa8da1 100644 --- a/src/pyj/modals.pyj +++ b/src/pyj/modals.pyj @@ -215,7 +215,7 @@ def progress_dialog(msg, on_close=None): # Call close() once the task being performed is finished return create_progress_dialog(msg, on_close) -def ajax_progress_dialog(path, on_complete, msg, **kw): +def ajax_progress_dialog(path, on_complete, msg, extra_data_for_callback=None, **kw): pd = None def on_complete_callback(event_type, xhr, ev): nonlocal pd @@ -225,6 +225,7 @@ def ajax_progress_dialog(path, on_complete, msg, **kw): def on_progress_callback(loaded, total, xhr): pd.update_progress(loaded, total) xhr = ajax(path, on_complete_callback, on_progress=on_progress_callback, **kw) + xhr.extra_data_for_callback = extra_data_for_callback pd = progress_dialog(msg, xhr.abort.bind(xhr)) xhr.send() return xhr, pd