1619
|
1 from browser import document |
|
2 |
|
3 def init_collapsible_cards(parent_elt=None) -> None: |
|
4 """Initialize cards which can be collapsed.""" |
|
5 parent = parent_elt or document |
|
6 cards = parent.select('.collapsible-card') |
|
7 |
|
8 for card in cards: |
|
9 header = card.select_one('.collapsible-header') |
|
10 content = card.select_one('.collapsible-content') |
|
11 |
|
12 content.style.maxHeight = 'none' |
|
13 natural_height = content.scrollHeight |
|
14 content.dataset.natural_height = natural_height |
|
15 |
|
16 header.bind('click', lambda ev: _toggle_card(ev)) |
|
17 |
|
18 def _toggle_card(event): |
|
19 """Collapse/expand the card.""" |
|
20 header = event.currentTarget |
|
21 card = header.closest('.collapsible-card') |
|
22 content = card.select_one('.collapsible-content') |
|
23 |
|
24 if content.style.maxHeight == '0px': |
|
25 # Expand |
|
26 content.style.maxHeight = f"{content.dataset.natural_height}px" |
|
27 header.classList.remove('collapsed') |
|
28 else: |
|
29 # Collapse |
|
30 content.style.maxHeight = '0px' |
|
31 header.classList.add('collapsed') |