1# vim:fileencoding=utf-8
2# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
3from __python__ import hash_literals
4
5from dom import build_rule, svgicon, add_extra_css, clear
6from elementmaker import E
7
8from book_list.theme import get_font_size, get_color
9
10CLASS_NAME = 'generic-items-list'
11
12add_extra_css(def():
13    sel = '.' + CLASS_NAME + ' '
14    style = ''
15    style += build_rule(sel + 'li', padding='0', border_bottom='solid 1px ' + get_color('window-foreground'), border_top='solid 1px ' + get_color('window-background'), cursor='pointer', list_style='none')
16    style += build_rule(sel + '.item-title', font_size=get_font_size('item-list-title'))
17    style += build_rule(sel + ' .item-subtitle', font_size=get_font_size('item-list-subtitle'), font_style='italic')
18    sel += ' li > div:nth-child(1)'
19    style += build_rule(sel, padding='1rem')
20    style += build_rule(sel + ':hover', color=get_color('list-hover-foreground'), background_color=get_color('list-hover-background'), border_top_color=get_color('list-hover-foreground'))
21    style += build_rule(sel + ':active', transform='scale(1, 1.5)')
22    return style
23)
24
25
26def side_action(action, ev):
27    ev.stopPropagation(), ev.preventDefault()
28    if action:
29        li = ev.currentTarget.closest('li')
30        action(li)
31
32
33def build_list(container, items, subtitle):
34    c = container
35    clear(c)
36    c.classList.add(CLASS_NAME)
37    if subtitle:
38        c.appendChild(E.p(subtitle, style="font-style:italic; padding: 1em 1ex; border-bottom: solid 1px currentColor"))
39    ul = E.ul()
40    c.appendChild(ul)
41    has_icons = False
42    for item in items:
43        if item.icon:
44            has_icons = True
45            break
46
47    for item in items:
48        ic = ''
49        if has_icons:
50            if item.icon:
51                ic = svgicon(item.icon)
52            ic = E.span(ic, '\xa0')
53        li = E.li(E.div(ic, item.title, class_='item-title'))
54        ul.appendChild(li)
55        if item.subtitle:
56            li.firstChild.appendChild(E.div(item.subtitle, class_='item-subtitle', style='padding-top:1ex'))
57        if item.action:
58            li.addEventListener('click', item.action)
59        if item.data:
60            li.dataset.userData = item.data
61        if item.side_actions?.length:
62            s = li.style
63            s.display = 'flex'
64            s.alignItems = 'center'
65            li.firstChild.style.flexGrow = '10'
66            li.appendChild(E.div(
67                style='display: flex; align-items: center; margin-left: 0.5rem'
68            ))
69            for x in item.side_actions:
70                li.lastChild.appendChild(E.div(
71                    class_='simple-link',
72                    style='padding: 0.5rem',
73                    title=x.tooltip or '',
74                    onclick=side_action.bind(None, x.action),
75                    svgicon(x.icon)
76                ))
77
78
79def create_side_action(icon, action=None, tooltip=None):
80    return {'icon':icon, 'action': action, 'tooltip': tooltip}
81
82
83def create_item(title, action=None, subtitle=None, icon=None, data=None, side_actions=None):
84    return {'title':title, 'action':action, 'subtitle':subtitle, 'icon':icon, 'data': data, 'side_actions': side_actions or v'[]'}
85
86
87def create_item_list(container, items, subtitle=None):
88    build_list(container, items, subtitle)
89