Debian Codegraph @

src/pyj/book_list/item_list.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import hash_literals
4
5 from elementmaker import E
6
7 from book_list.theme import get_font_size, get_color
8 from dom import build_rule, svgicon, add_extra_css, clear
9
10 CLASS_NAME = 'generic-items-list'
11
12 add_extra_css(def ():
13 sel = '.' + CLASS_NAME + ' '
14 style = ''
15 style += build_rule(sel, padding='0.75rem 1rem')
16 style += build_rule(sel + ' > p',
17 margin='0 0 0.75rem 0',
18 padding='0.6rem 0.75rem',
19 border='var(--calibre-border-style-subtle)',
20 border_radius='12px',
21 background_color='rgba(0,0,0,0.02)',
22 font_style='normal',
23 opacity='0.8'
24 )
25 style += build_rule(sel + ' ul', list_style='none', margin='0', padding='0', display='flex', flex_direction='column', gap='0.5rem')
26 style += build_rule(sel + 'li',
27 padding='0',
28 border='var(--calibre-border-style-subtle)',
29 border_radius='14px',
30 background_color=get_color('window-background'),
31 box_shadow='0 1px 2px rgba(0,0,0,0.06)',
32 cursor='pointer',
33 overflow='hidden'
34 )
35 style += build_rule(sel + 'li:hover',
36 background_color='var(--calibre-color-primary-light)',
37 box_shadow='0 8px 20px rgba(0,0,0,0.10)',
38 transform='translateY(-1px)'
39 )
40 style += build_rule(sel + 'li:active', transform='translateY(0px)')
41 style += build_rule(sel + ' .item-title-body .item-title',
42 font_size=get_font_size('item-list-title'), font_weight='700')
43 style += build_rule(sel + ' .item-subtitle',
44 font_size=get_font_size('item-list-subtitle'), font_style='normal', opacity='0.78')
45 sel += ' li > div:nth-child(1)'
46 style += build_rule(sel,
47 padding='0.85rem 0.95rem',
48 position='relative',
49 display='flex',
50 align_items='flex-start',
51 flex_wrap='wrap',
52 gap='0.5rem',
53 box_sizing='border-box',
54 )
55 style += build_rule(sel + ' .item-title-body',
56 display='flex',
57 align_items='center',
58 gap='0.5rem',
59 min_width='0',
60 flex='1 1 auto',
61 )
62 style += build_rule(sel + ' .item-title-body > span:not(.item-title)', opacity='0.8')
63 style += r'''
64 @media (max-width: 420px) {
65 .generic-items-list { padding: 0.6rem 0.75rem; }
66 .generic-items-list ul { gap: 0.45rem; }
67 .generic-items-list li > div:nth-child(1) { padding: 0.75rem 0.75rem; }
68 }
69 '''
70 return style
71 )
72
73
74 def side_action(action, ev):
75 ev.stopPropagation(), ev.preventDefault()
76 if action:
77 li = ev.currentTarget.closest('li')
78 action(li)
79
80
81 def add_classes(elem, class_names):
82 for cls in (class_names or '').toString().split(/\s+/):
83 if cls:
84 elem.classList.add(cls)
85
86
87 def build_list(container, items, subtitle):
88 c = container
89 clear(c)
90 c.classList.add(CLASS_NAME)
91 if subtitle:
92 c.appendChild(E.p(subtitle, style='font-style:normal; font-weight:700; padding: 1em 1ex; border-bottom: solid 1px currentColor'))
93 ul = E.ul()
94 c.appendChild(ul)
95 has_icons = False
96 for item in items:
97 if item.icon:
98 has_icons = True
99 break
100
101 for item in items:
102 title_body = E.div(class_='item-title-body')
103 if has_icons:
104 ic = ''
105 if item.icon:
106 ic = svgicon(item.icon)
107 title_body.appendChild(E.span(ic, '\xa0'))
108 tit = item.title
109 if jstype(tit) is 'string':
110 title_body.appendChild(E.span((tit or '') + '', class_='item-title'))
111 else:
112 title_body.appendChild(tit)
113 row = E.div(class_='item-first-row')
114 add_classes(row, item.row_class)
115 row.appendChild(title_body)
116 nm = item.nav_list_meta
117 if nm and (nm.rating or nm.count):
118 stack = E.div(class_='item-subtitle item-subtitle-nav')
119 if nm.rating:
120 stack.appendChild(E.div(nm.rating, class_='item-subtitle-rating'))
121 if nm.count:
122 stack.appendChild(E.div(nm.count, class_='item-subtitle-count'))
123 row.appendChild(stack)
124 elif item.subtitle:
125 row.appendChild(E.div(item.subtitle, class_='item-subtitle', style='padding-top:1ex'))
126 li = E.li(row)
127 add_classes(li, item.li_class)
128 ul.appendChild(li)
129 if item.action:
130 li.addEventListener('click', item.action)
131 if item.data:
132 li.dataset.userData = item.data
133 if item.side_actions?.length:
134 s = li.style
135 s.display = 'flex'
136 s.alignItems = 'center'
137 li.firstChild.style.flexGrow = '10'
138 li.appendChild(E.div(
139 style='display: flex; align-items: center; margin-left: 0.5rem'
140 ))
141 for x in item.side_actions:
142 li.lastChild.appendChild(E.div(
143 class_='simple-link',
144 style='padding: 0.5rem',
145 title=x.tooltip or '',
146 onclick=side_action.bind(None, x.action),
147 svgicon(x.icon)
148 ))
149
150
151 def create_side_action(icon, action=None, tooltip=None):
152 return {'icon': icon, 'action': action, 'tooltip': tooltip}
153
154
155 def create_item(title, action=None, subtitle=None, icon=None, data=None, side_actions=None, nav_list_meta=None, li_class=None, row_class=None):
156 return {
157 'title': title, 'action': action, 'subtitle': subtitle, 'icon': icon, 'data': data,
158 'side_actions': side_actions or [], 'nav_list_meta': nav_list_meta,
159 'li_class': li_class, 'row_class': row_class,
160 }
161
162
163 def create_item_list(container, items, subtitle=None):
164 build_list(container, items, subtitle)