# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2018, Kovid Goyal from __python__ import bound_methods, hash_literals from elementmaker import E from gettext import gettext as _, ngettext from book_list.globals import get_session_data from dom import unique_id from session import get_interface_data from utils import fmt_sidx CONTAINER = unique_id('reader-hf-prefs') def create_item(region, label): def sep(): return E.option('\xa0', disabled=True) def opt(label, name, selected): return E.option(label, id=name, value=name, selected=bool(selected)) return E.tr( E.td(label + ':', style='padding: 1ex 1rem'), E.td(E.select( data_region=region, opt(_('Empty'), 'empty', True), sep(), opt(_('Book title'), 'title'), opt(_('Authors'), 'authors'), opt(_('Series'), 'series'), sep(), opt(_('Top level section'), 'top-section'), opt(_('Current section'), 'section'), sep(), opt(_('Progress'), 'progress'), opt(_('Time to read book'), 'time-book'), opt(_('Time to read chapter'), 'time-chapter'), opt(_('Time to read chapter and book'), 'time-chapter-book'), opt(_('Clock'), 'clock'), )) ) def create_items(which): ans = E.table( data_which=which, create_item('left', _('Left')), create_item('middle', _('Middle')), create_item('right', _('Right')) ) return ans def apply_setting(table, val): for region in 'left middle right'.split(' '): sel = table.querySelector(f'select[data-region={region}]') for opt in sel.selectedOptions: opt.selected = False x = val[region] or 'empty' opt = sel.namedItem(x) if opt: opt.selected = True else: sel.selectedIndex = 0 def get_setting(table): ans = {} for region in 'left middle right'.split(' '): sel = table.querySelector(f'select[data-region={region}]') if sel.selectedIndex > -1: ans[region] = sel.options[sel.selectedIndex].value return ans def create_head_foot_panel(container): container.appendChild(E.div(id=CONTAINER)) container = container.lastChild container.appendChild(E.h4(_('Header'), style="margin: 1rem")) container.appendChild(create_items('header')) container.appendChild(E.hr()) container.appendChild(E.h4(_('Footer'), style="margin: 1rem; margin-top: 0")) container.appendChild(create_items('footer')) sd = get_session_data() for which in 'header footer'.split(' '): table = container.querySelector(f'table[data-which={which}]') apply_setting(table, sd.get(which) or {}) def commit_head_foot(onchange, container): sd = get_session_data() changed = False for which in 'header footer'.split(' '): prev = sd.get(which) or {} table = container.querySelector(f'table[data-which={which}]') current = get_setting(table) for region in 'left middle right'.split(' '): if prev[region] is not current[region]: changed = True sd.set(which, get_setting(table)) if changed: onchange() if window.Intl?.DateTimeFormat: time_formatter = window.Intl.DateTimeFormat(undefined, {'hour':'numeric', 'minute':'numeric'}) else: time_formatter = {'format': def(date): return '{}:{}'.format(date.getHours(), date.getMinutes()) } def format_time_left(seconds): hours, minutes = divmod(int(seconds / 60), 60) if hours <= 0: if minutes < 1: return _('almost done') minutes = minutes return ngettext('{} min', '{} mins', minutes).format(minutes) if not minutes: return ngettext('{} hour', '{} hours', hours).format(hours) return _('{} h {} mins').format(hours, minutes) def render_head_foot(div, which, region, progress_frac, metadata, current_toc_node, current_toc_toplevel_node, book_time, chapter_time): template = get_session_data().get(which) or {} field = template[region] or 'empty' interface_data = get_interface_data() text = '' has_clock = False if field is 'progress': percent = min(100, max(Math.round(progress_frac * 100), 0)) text = percent + '%' elif field is 'title': text = metadata.title or _('Untitled') elif field is 'authors': text = ' & '.join(metadata.authors or v'[]') elif field is 'series': if metadata.series: ival = fmt_sidx(ival, use_roman=interface_data.use_roman_numerals_for_series_number) text = _('{0} of {1}').format(ival, metadata.series) elif field is 'clock': text = time_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 '' elif field.startswith('time-'): if book_time is None or chapter_time is None: text = _('Calculating...') else: if field is 'time-book': text = format_time_left(book_time) elif field is 'time-chapter': text = format_time_left(chapter_time) else: text = '{} ({})'.format(format_time_left(chapter_time), format_time_left(book_time)) if text is not div.textContent: div.textContent = text div.style.display = 'block' if text else 'none' return text, has_clock