Make the top bar API id free

This commit is contained in:
Kovid Goyal 2017-02-07 23:43:05 +05:30
parent ff12d0be17
commit 4875060c50
3 changed files with 18 additions and 20 deletions

View File

@ -22,8 +22,8 @@ def show_recent():
def init(container_id): def init(container_id):
create_top_bar(container_id, run_animation=True)
container = document.getElementById(container_id) container = document.getElementById(container_id)
create_top_bar(container, run_animation=True)
interface_data = get_interface_data() interface_data = get_interface_data()
# Recent books # Recent books

View File

@ -26,8 +26,7 @@ add_extra_css(def():
return style return style
) )
def create_markup(container_id): def create_markup(container):
container = document.getElementById(container_id)
for i in range(2): for i in range(2):
bar = E.div( bar = E.div(
class_=CLASS_NAME, class_=CLASS_NAME,
@ -43,12 +42,11 @@ def create_markup(container_id):
) )
container.appendChild(bar) container.appendChild(bar)
def get_bars(container_id): def get_bars(container):
container = document.getElementById(container_id)
return container.getElementsByClassName(CLASS_NAME) return container.getElementsByClassName(CLASS_NAME)
def set_left_data(container_id, title='calibre', icon='heart', action=None, tooltip='', run_animation=False, title_action=None, title_tooltip=None): def set_left_data(container, title='calibre', icon='heart', action=None, tooltip='', run_animation=False, title_action=None, title_tooltip=None):
bars = get_bars(container_id) bars = get_bars(container)
if icon is 'heart': if icon is 'heart':
if not tooltip: if not tooltip:
tooltip = _('Donate to support calibre development') tooltip = _('Donate to support calibre development')
@ -77,19 +75,19 @@ def set_left_data(container_id, title='calibre', icon='heart', action=None, tool
a = a.nextSibling a = a.nextSibling
a.addEventListener('click', def(event): event.preventDefault(), title_action();) a.addEventListener('click', def(event): event.preventDefault(), title_action();)
def set_title(container_id, text): def set_title(container, text):
bars = get_bars(container_id) bars = get_bars(container)
for bar in bars: for bar in bars:
bar.firstChild.firstChild.nextSibling.textContent = text bar.firstChild.firstChild.nextSibling.textContent = text
def create_top_bar(container_id, **kw): def create_top_bar(container, **kw):
create_markup(container_id) create_markup(container)
set_left_data(container_id, **kw) set_left_data(container, **kw)
def add_button(container_id, icon, action=None, tooltip=''): def add_button(container, icon, action=None, tooltip=''):
if not icon: if not icon:
raise ValueError('An icon must be specified') raise ValueError('An icon must be specified')
bars = get_bars(container_id) bars = get_bars(container)
for i, bar in enumerate(bars): for i, bar in enumerate(bars):
right = bar.firstChild.nextSibling right = bar.firstChild.nextSibling
right.appendChild(E.a( right.appendChild(E.a(
@ -103,8 +101,8 @@ def add_button(container_id, icon, action=None, tooltip=''):
event.preventDefault(), action() event.preventDefault(), action()
) )
def set_button_visibility(container_id, icon, visible): def set_button_visibility(container, icon, visible):
for bar in get_bars(container_id): for bar in get_bars(container):
right = bar.firstChild.nextSibling right = bar.firstChild.nextSibling
elem = right.querySelector(f'[data-button-icon="{icon}"]') elem = right.querySelector(f'[data-button-icon="{icon}"]')
if elem: if elem:

View File

@ -157,7 +157,7 @@ def create_books_list(container):
container.appendChild(E.div()), container.appendChild(E.div()) container.appendChild(E.div()), container.appendChild(E.div())
apply_view_mode(get_session_data().get('view_mode')) apply_view_mode(get_session_data().get('view_mode'))
create_more_button(container.lastChild) create_more_button(container.lastChild)
add_button(container.parentNode.id, icon='sort-amount-desc', action=show_panel.bind(None, 'book_list^sort'), tooltip=_('Sort Books')) add_button(container.parentNode, icon='sort-amount-desc', action=show_panel.bind(None, 'book_list^sort'), tooltip=_('Sort Books'))
def check_for_books_loaded(): def check_for_books_loaded():
@ -189,7 +189,7 @@ def init(container_id):
container = document.getElementById(container_id) container = document.getElementById(container_id)
lid = container.dataset.library_id = url_books_query().library_id lid = container.dataset.library_id = url_books_query().library_id
title = interface_data.library_map[lid] title = interface_data.library_map[lid]
create_top_bar(container_id, title=title, action=back, icon='home') create_top_bar(container, title=title, action=back, icon='home')
container.appendChild(E.div()) container.appendChild(E.div())
container.lastChild.appendChild(E.div(_('Loading books from the {} calibre library, please wait...').format(title), style='margin: 1ex 1em')) container.lastChild.appendChild(E.div(_('Loading books from the {} calibre library, please wait...').format(title), style='margin: 1ex 1em'))
conditional_timeout(container_id, 5, check_for_books_loaded) conditional_timeout(container_id, 5, check_for_books_loaded)
@ -215,7 +215,8 @@ def create_sort_panel(container_id):
if not library_data.sortable_fields: if not library_data.sortable_fields:
show_panel('book_list', replace=True) show_panel('book_list', replace=True)
return return
create_top_bar(container_id, title=_('Sort books by…'), action=back, icon='close') container = document.getElementById(container_id)
create_top_bar(container, title=_('Sort books by…'), action=back, icon='close')
items = [] items = []
csf, csf_order = current_sorted_field() csf, csf_order = current_sorted_field()
new_sort_order = 'desc' if csf_order is 'asc' else 'asc' new_sort_order = 'desc' if csf_order is 'asc' else 'asc'
@ -230,7 +231,6 @@ def create_sort_panel(container_id):
else: else:
action = change_sort.bind(None, field, None) action = change_sort.bind(None, field, None)
items.push(create_item(name, subtitle=subtitle, icon=icon_name, action=action)) items.push(create_item(name, subtitle=subtitle, icon=icon_name, action=action))
container = document.getElementById(container_id)
container.appendChild(E.div()) container.appendChild(E.div())
create_item_list(container.lastChild, items, _('Change how the list of books is sorted')) create_item_list(container.lastChild, items, _('Change how the list of books is sorted'))
# }}} # }}}