diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2026-03-22 16:13:59 +0100 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2026-03-22 16:13:59 +0100 |
| commit | 1620fc15d1d21989fb38d9f3596cdf03f8535ff5 (patch) | |
| tree | fe16b79cba4b1105ccbc9c4ab16b4b606dd10950 /resources/assets/javascripts | |
| parent | dda96cbbdf9237d90297ed1559f0a4d27713b2ec (diff) | |
implement logic that may collapse list, fixes #6380biest-6380
Diffstat (limited to 'resources/assets/javascripts')
| -rw-r--r-- | resources/assets/javascripts/bootstrap/lists.js | 57 | ||||
| -rw-r--r-- | resources/assets/javascripts/entry-base.js | 1 |
2 files changed, 58 insertions, 0 deletions
diff --git a/resources/assets/javascripts/bootstrap/lists.js b/resources/assets/javascripts/bootstrap/lists.js new file mode 100644 index 0000000..ac7b85c --- /dev/null +++ b/resources/assets/javascripts/bootstrap/lists.js @@ -0,0 +1,57 @@ +import {$gettext} from "@/assets/javascripts/lib/gettext"; + +(() => { + let uniqueId = 0; + + STUDIP.ready(() => { + document.querySelectorAll('.collapsible-list').forEach(list => { + if (!list.matches('ol,ul')) { + console.error('Not applicable for anything else than ol/ul lists'); + } + + const data = list.dataset; + + if (data.collapsibleListInitialized !== undefined) { + return; + } + + data.collapsibleListInitialized = true; + + const label = data.label ?? $gettext('Einträge'); + const max = Number.parseInt(data.collapseAfter ?? 5, 10); + const items = Array.from(list.children); + const hiddenItems = items.slice(max); + const hiddenCount = hiddenItems.length; + + if (hiddenCount === 0) { + return; + } + + const id = list.getAttribute('id') ?? `collapsable-list-${uniqueId++}`; + list.setAttribute('id', id); + + const button = Object.assign(document.createElement('button'), { + className: 'as-link', + textContent: '', + type: 'button', + }); + button.setAttribute('aria-controls', id); + + const update = (collapsed) => { + button.textContent = collapsed + ? $gettext('%{count} weitere %{label} anzeigen', {count: hiddenCount, label}) + : $gettext('weniger %{label} anzeigen', {label}); + button.setAttribute('aria-expanded', collapsed ? 'false' : 'true'); + hiddenItems.forEach(li => li.toggleAttribute('hidden', collapsed)); + }; + + update(true); + + button.addEventListener('click', () => { + update(button.getAttribute('aria-expanded') === 'true'); + }); + + list.after(button); + }); + }); +})(); diff --git a/resources/assets/javascripts/entry-base.js b/resources/assets/javascripts/entry-base.js index ad114f1..b4f807c 100644 --- a/resources/assets/javascripts/entry-base.js +++ b/resources/assets/javascripts/entry-base.js @@ -15,6 +15,7 @@ import "./jquery-bundle.js" import "./init.js" import "./bootstrap/responsive.js" import "./bootstrap/vue.js" +import "./bootstrap/lists.js" import "./studip-ui.js" import "./bootstrap/fullscreen.js" |
