Finish up header/footer template implementation

Also fix highlighted toc nodes not including parent nodes
This commit is contained in:
Kovid Goyal 2018-01-04 09:09:14 +05:30
parent ff7350cfb0
commit c173072c6a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 51 additions and 12 deletions

View File

@ -14,7 +14,6 @@ New features for the in-browser viewer
- Allow loading fonts from the computer running calibre and using them
for reading.
- Templates for headers/footers including current chapter name
New features for the server generally
---------------------------------------

View File

@ -239,6 +239,7 @@ class IframeBoss:
self.send_message('content_loaded', progress_frac=self.get_progress_frac())
self.last_cfi = None
window.setTimeout(self.update_cfi, 0)
window.setTimeout(self.update_toc_position, 0)
def calculate_progress_frac(self, current_name, spine_index):

View File

@ -110,7 +110,7 @@ else:
}
def render_head_foot(div, which, region, progress_frac, metadata):
def render_head_foot(div, which, region, progress_frac, metadata, current_toc_node, current_toc_toplevel_node):
template = get_session_data().get(which) or {}
field = template[region] or 'empty'
interface_data = get_interface_data()
@ -130,6 +130,12 @@ def render_head_foot(div, which, region, progress_frac, metadata):
elif field is 'clock':
text = date_formatter.format(Date())
has_clock = True
elif field is 'section':
text = current_toc_node.title if current_toc_node else ''
elif field is 'top-section':
text = current_toc_toplevel_node.title if current_toc_toplevel_node else ''
if not text:
text = current_toc_node.title if current_toc_node else ''
if text is not div.textContent:
div.textContent = text
div.style.display = 'block' if text else 'none'

View File

@ -61,6 +61,33 @@ def get_border_nodes(toc, id_map):
return before, after
def get_current_toc_nodes():
toc = current_book().manifest.toc
parent_map, id_map = get_toc_maps(toc)
data = update_visible_toc_nodes.data
ans = {}
if data.has_visible:
ans = data.visible_anchors
else:
if data.before:
ans[data.before] = True
else:
before = get_border_nodes(toc, id_map)[0]
if before:
ans[before.id] = True
ans = Object.keys(ans)
if ans.length:
pid = node_id = ans[0]
p = parent_map[pid]
while p and p.title:
pid = p.id
p = parent_map[pid]
p = id_map[pid]
return id_map[node_id], p if p and p.title else None
return None, None
def get_highlighted_toc_nodes(toc, parent_map, id_map):
data = update_visible_toc_nodes.data
ans = {}
@ -74,10 +101,10 @@ def get_highlighted_toc_nodes(toc, parent_map, id_map):
if before:
ans[before.id] = True
for node_id in Object.keys(ans):
pid = parent_map[node_id]
while pid:
ans[pid] = True
pid = parent_map[pid]
p = parent_map[node_id]
while p and p.title:
ans[p.id] = True
p = parent_map[p.id]
return ans
def get_toc_maps(toc):

View File

@ -22,7 +22,7 @@ from read_book.prefs.font_size import change_font_size_by
from read_book.prefs.head_foot import render_head_foot
from read_book.resources import load_resources
from read_book.search import SearchOverlay, find_in_spine
from read_book.toc import update_visible_toc_nodes
from read_book.toc import update_visible_toc_nodes, get_current_toc_nodes
from read_book.touch import set_left_margin_handler, set_right_margin_handler
from session import get_device_uuid, get_interface_data
from utils import html_escape, is_ios, parse_url_params, username_key
@ -117,6 +117,7 @@ class View:
self.ui = ui
self.loaded_resources = {}
self.current_progress_frac = 0
self.current_toc_node = self.current_toc_toplevel_node = None
self.clock_timer_id = 0
sd = get_session_data()
left_margin = E.div(svgicon('caret-left'), style='width:{}px;'.format(sd.get('margin_left', 20)), class_='book-side-margin', id='book-left-margin', onclick=self.left_margin_clicked)
@ -501,15 +502,18 @@ class View:
nonlocal has_clock
if sd.get(sz_attr, 20) > 5:
mi = self.book.metadata
texta, hca = render_head_foot(div.firstChild, name, 'left', self.current_progress_frac, mi)
textb, hcb = render_head_foot(div.firstChild.nextSibling, name, 'middle', self.current_progress_frac, mi)
textc, hcc = render_head_foot(div.lastChild, name, 'right', self.current_progress_frac, mi)
texta, hca = render_head_foot(div.firstChild, name, 'left', self.current_progress_frac, mi, self.current_toc_node, self.current_toc_toplevel_node)
textb, hcb = render_head_foot(div.firstChild.nextSibling, name, 'middle', self.current_progress_frac, mi, self.current_toc_node, self.current_toc_toplevel_node)
textc, hcc = render_head_foot(div.lastChild, name, 'right', self.current_progress_frac, mi, self.current_toc_node, self.current_toc_toplevel_node)
has_clock = hca or hcb or hcc
if textc and not textb and not texta:
# There is only a right, we want it to be right aligned,
# not left-aligned
# Want right-aligned
div.firstChild.style.display = 'block'
div.firstChild.nextSibling.style.display = 'block'
elif not texta and not textc and textb:
# Want centered
div.firstChild.style.display = 'block'
div.lastChild.style.display = 'block'
else:
for c in div.childNodes:
c.style.display = 'none'
@ -531,6 +535,8 @@ class View:
def on_update_toc_position(self, data):
update_visible_toc_nodes(data.visible_anchors)
self.current_toc_node, self.current_toc_toplevel_node = get_current_toc_nodes()
self.update_header_footer()
def show_spine_item(self, resource_data):
self.pending_load = resource_data