From 4224233eca7cbe22d8cdef487054d7ccacff818c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 7 Sep 2016 10:08:44 +0530 Subject: [PATCH] Make the item list code re-useable --- src/pyj/book_list/item_list.pyj | 72 ++++++++++++++++----------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/src/pyj/book_list/item_list.pyj b/src/pyj/book_list/item_list.pyj index 0a0fb19fad..6ec25ed08d 100644 --- a/src/pyj/book_list/item_list.pyj +++ b/src/pyj/book_list/item_list.pyj @@ -2,7 +2,7 @@ # License: GPL v3 Copyright: 2015, Kovid Goyal from __python__ import hash_literals -from dom import build_rule, svgicon, add_extra_css +from dom import build_rule, svgicon, add_extra_css, clear from elementmaker import E from book_list.theme import get_font_size, get_color @@ -21,6 +21,39 @@ add_extra_css(def(): return style ) +def build_list(container, items, subtitle): + c = container + clear(c) + c.classList.add(CLASS_NAME) + if subtitle: + c.appendChild(E.p(subtitle, style="font-style:italic; padding: 1em 1ex; border-bottom: solid 1px currentColor")) + ul = E.ul() + c.appendChild(ul) + has_icons = False + for item in items: + if item.icon_name: + has_icons = True + break + + for item in items: + ic = '' + if has_icons: + if item.icon_name: + ic = svgicon(item.icon_name) + ic = E.span(ic, '\xa0') + ul.appendChild(E.li(E.a(href='javascript:void(0)', + E.div(ic, item.title, class_='item-title') + ))) + a = ul.lastChild.firstChild + if item.subtitle: + a.appendChild(E.div(item.subtitle, class_='item-subtitle', style='padding-top:1ex')) + a.addEventListener('click', def(event): event.preventDefault();) + if item.action: + ul.lastChild.addEventListener('click', item.action) + +def create_item(title, action=None, subtitle=None, icon_name=None): + return {'title':title, 'action':action, 'subtitle':subtitle, 'icon_name':icon_name} + class ItemsView: def __init__(self, interface_data, book_list_container): @@ -44,42 +77,7 @@ class ItemsView: def is_visible(self, val): self.container.style.display = 'block' if val else 'none' - def clear(self): - c = self.container - while c.lastChild is not c.firstChild: - c.removeChild(c.lastChild) - return c - def init(self, data): items = getattr(data, 'items', data) subtitle = getattr(data, 'subtitle', None) - c = self.clear() - if subtitle: - c.appendChild(E.p(subtitle, style="font-style:italic; padding: 1em 1ex; border-bottom: solid 1px currentColor")) - ul = E.ul() - c.appendChild(ul) - has_icons = False - for item in items: - if item.icon_name: - has_icons = True - break - - for item in items: - ic = '' - if has_icons: - if item.icon_name: - ic = svgicon(item.icon_name) - ic = E.span(ic, '\xa0') - ul.appendChild(E.li(E.a(href='javascript:void(0)', - E.div(ic, item.title, class_='item-title') - ))) - a = ul.lastChild.firstChild - if item.subtitle: - a.appendChild(E.div(item.subtitle, class_='item-subtitle', style='padding-top:1ex')) - a.addEventListener('click', def(event): event.preventDefault();) - if item.action: - ul.lastChild.addEventListener('click', item.action) - - -def create_item(title, action=None, subtitle=None, icon_name=None): - return {'title':title, 'action':action, 'subtitle':subtitle, 'icon_name':icon_name} + build_list(self.container, items, subtitle)