Make the item list code re-useable

This commit is contained in:
Kovid Goyal 2016-09-07 10:08:44 +05:30
parent 3172744e22
commit 4224233eca

View File

@ -2,7 +2,7 @@
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
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)