Debian Codegraph @

src/pyj/book_list/ui.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import hash_literals, bound_methods
4
5 from elementmaker import E
6
7 from book_list.constants import book_list_container_id
8 from book_list.globals import get_current_query
9 from book_list.library_data import current_library_id, current_virtual_library
10 from book_list.router import push_state
11 from dom import ensure_id, clear
12 from utils import encode_query_with_path
13
14
15 panel_handlers = {}
16 default_panel_handler = None
17
18
19 def set_panel_handler(panel, handler):
20 panel_handlers[panel] = handler
21
22
23 def set_default_panel_handler(handler):
24 nonlocal default_panel_handler
25 default_panel_handler = handler
26
27
28 def develop_panel(container_id):
29 # To use, go to URL:
30 # http://localhost:8080/#panel=develop-widgets&widget_module=<module name>
31 # Implement the develop(container) method in that module.
32 container = document.getElementById(container_id)
33 q = get_current_query()
34 m = q.widget_module
35 if m:
36 m = get_module(m)
37 if m?.develop:
38 m.develop(container)
39 else:
40 container.textContent = 'The module {} either does not exist or has no develop method.'.format(q.widget_module)
41
42 set_panel_handler('develop-widgets', develop_panel)
43
44
45 def currently_showing_panel():
46 c = document.getElementById(book_list_container_id)
47 return c.dataset.panel
48
49
50 def add_library_info(query):
51 if not query.library_id:
52 query.library_id = current_library_id()
53 if not query.vl:
54 if query.vl is None:
55 v'delete query.vl'
56 else:
57 vlid = current_virtual_library()
58 if vlid:
59 query.vl = vlid
60
61
62 def prepare_query(query, panel):
63 q = {k: query[k] for k in (query or {}) if k is not 'panel'}
64 if panel is not 'home':
65 q.panel = panel
66 add_library_info(q)
67 return q
68
69
70 def query_as_href(query, panel):
71 q = prepare_query(query, panel or 'book_list')
72 return encode_query_with_path(q)
73
74
75 def show_panel(panel, query=None, replace=False):
76 push_state(prepare_query(query, panel), replace=replace)
77
78
79 def apply_url_state(state):
80 panel = state.panel or 'home'
81 c = document.getElementById(book_list_container_id)
82 clear(c)
83 c.appendChild(E.div())
84 c.dataset.panel = panel
85 handler = panel_handlers[panel] or default_panel_handler
86 handler(ensure_id(c.firstChild, 'panel'))
87
88
89 apply_url_state.back_from_current = def back_from_current(current_query):
90 q = current_query
91 if q.panel:
92 if '^' in q.panel:
93 q = {k: q[k] for k in q}
94 q.panel = q.panel.rpartition('^')[0]
95 elif q.panel is 'book_list':
96 q = {}
97 else:
98 q = {'panel': 'book_list'}
99 add_library_info(q)
100 return q