Fix forward not working when changing searches

This commit is contained in:
Kovid Goyal 2015-12-20 16:47:22 +05:30
parent 396219e539
commit 6a0c039d93
3 changed files with 23 additions and 7 deletions

View File

@ -1,6 +1,7 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
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)

View File

@ -1,7 +1,7 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
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':

View File

@ -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