Store the parsed form of the current query for convenient global access

This commit is contained in:
Kovid Goyal 2016-02-12 14:05:24 +05:30
parent 40d4224363
commit 942d18810c
3 changed files with 16 additions and 5 deletions

View File

@ -9,7 +9,7 @@ from gettext import gettext as _
from widgets import get_widget_css from widgets import get_widget_css
from utils import parse_url_params from utils import parse_url_params
from book_list.globals import get_session_data, set_boss from book_list.globals import get_session_data, set_boss, set_current_query
from book_list.theme import get_color from book_list.theme import get_color
from book_list.ui import UI from book_list.ui import UI
@ -30,8 +30,9 @@ class Boss:
self.ui = UI(interface_data, div) self.ui = UI(interface_data, div)
window.onerror = self.onerror.bind(self) window.onerror = self.onerror.bind(self)
self.history_count = 0 self.history_count = 0
self.ui.apply_state() # Render the book list
data = parse_url_params() data = parse_url_params()
set_current_query(data)
self.ui.apply_state() # Render the book list
if not data.mode or data.mode == 'book_list': if not data.mode or data.mode == 'book_list':
if data.panel != self.ui.current_panel: if data.panel != self.ui.current_panel:
self.ui.show_panel(data.panel, push_state=False) self.ui.show_panel(data.panel, push_state=False)
@ -60,6 +61,7 @@ class Boss:
def onpopstate(self, ev): def onpopstate(self, ev):
data = parse_url_params() data = parse_url_params()
set_current_query(data)
mode = data.mode or 'book_list' mode = data.mode or 'book_list'
self.history_count -= 1 self.history_count -= 1
if mode == 'book_list': if mode == 'book_list':
@ -94,6 +96,7 @@ class Boss:
if sq: if sq:
query.search = sq query.search = sq
query = encode_query(query) or '?' query = encode_query(query) or '?'
set_current_query(query)
if replace: if replace:
window.history.replaceState(None, '', query) window.history.replaceState(None, '', query)
else: else:

View File

@ -3,6 +3,7 @@
boss = None boss = None
session_data = None session_data = None
current_query = {}
def get_boss(): def get_boss():
return boss return boss
@ -19,3 +20,10 @@ def set_session_data(sd):
def get_session_data(): def get_session_data():
return session_data return session_data
def get_current_query():
return current_query
def set_current_query(val):
nonlocal current_query
current_query = val

View File

@ -126,17 +126,17 @@ class UI:
self.show_panel(self.ROOT_PANEL) self.show_panel(self.ROOT_PANEL)
def replace_panel(self, panel_name, force=False): def replace_panel(self, panel_name, force=False):
get_boss().push_state(replace=True)
if force or panel_name != self.current_panel: if force or panel_name != self.current_panel:
self.current_panel = panel_name or self.ROOT_PANEL self.current_panel = panel_name or self.ROOT_PANEL
self.apply_state() self.apply_state()
get_boss().push_state(replace=True)
def show_panel(self, panel_name, push_state=True, force=False): def show_panel(self, panel_name, push_state=True, force=False):
if push_state:
get_boss().push_state()
if force or panel_name != self.current_panel: if force or panel_name != self.current_panel:
self.current_panel = panel_name or self.ROOT_PANEL self.current_panel = panel_name or self.ROOT_PANEL
self.apply_state() self.apply_state()
if push_state:
get_boss().push_state()
def refresh_books_view(self): def refresh_books_view(self):
self.books_view.refresh() self.books_view.refresh()