Last modified by wiki admin on 2026/08/26 01:04

From version 1.1
edited by wiki admin
on 2026/08/24 21:04
Change comment: There is no comment for this version
To version 8.1
edited by wiki admin
on 2026/08/24 22:49
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,1 +1,1 @@
1 -Article accordion for the customer-facing design. Edit the object, not this text.
1 +Article accordion. Edit the object, not this text.
XWiki.JavaScriptExtension[0]
Code
... ... @@ -1,0 +1,212 @@
1 +/* ===========================================================================
2 + Alder Support Center - article accordion
3 + Goes in an XWiki.JavaScriptExtension object on AlderTheme.Scripts (alder wiki).
4 +
5 + Turns each top-level H2 inside #xwikicontent into a collapsible section, so a
6 + long omnibus article becomes a short list of section titles. No article needs
7 + editing: the sections come from headings the content already has.
8 +
9 + Deliberate behaviours, all reversible by disabling this object:
10 + * Sections open independently. Opening one does not close another.
11 + * The first section starts open, so the page never looks empty.
12 + * Fewer than two H2s: nothing happens, the article renders normally.
13 + * Edit/preview modes are skipped, or authors would be editing generated
14 + markup.
15 + * A link to a heading inside a collapsed section opens that section and
16 + scrolls to it, including on first load and on later hash changes.
17 + =========================================================================== */
18 +(function () {
19 + 'use strict';
20 +
21 + var MIN_SECTIONS = 2;
22 +
23 + function ready(fn) {
24 + if (document.readyState === 'loading') {
25 + document.addEventListener('DOMContentLoaded', fn);
26 + } else {
27 + fn();
28 + }
29 + }
30 +
31 + function chevron() {
32 + var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
33 + svg.setAttribute('class', 'alder-chev');
34 + svg.setAttribute('viewBox', '0 0 24 24');
35 + var path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
36 + path.setAttribute('d', 'M6 9l6 6 6-6');
37 + svg.appendChild(path);
38 + return svg;
39 + }
40 +
41 + function isWikiHome() {
42 + // The employees home page has several H2s (departments, article status,
43 + // publishing workflow). Collapsing a landing page into rows hides the
44 + // thing people came for, so skip it. Matches both URL schemes this
45 + // deployment serves: /bin/view/Main/ and /wiki/<name>/view/Main/.
46 + var path = (window.location.pathname || '').replace(/\/+$/, '');
47 + return /\/view\/Main(\/WebHome)?$/.test(path);
48 + }
49 +
50 + function build() {
51 + var body = document.body;
52 + if (!body || body.className.indexOf('viewbody') === -1) {
53 + return; // edit, preview, or anything that is not a rendered page
54 + }
55 + if (isWikiHome()) { return; }
56 + var content = document.getElementById('xwikicontent');
57 + if (!content) { return; }
58 +
59 + // XWiki renders each section as its own <div class="f-accordion-panel">,
60 + // with the section heading as the first child and everything else after it:
61 + //
62 + // <div class="f-accordion-panel" data-controller="accordion">
63 + // <h2>Arm/Disarm</h2> <- section heading
64 + // <h1>Arm</h1><p>...</p> <- content, including Helpjuice's inverted
65 + // </div> h1 "subsections"
66 + //
67 + // So the section container already exists and we only need to make it
68 + // collapsible. Two earlier versions of this script tried to rebuild sections
69 + // by grouping sibling headings -- first from content.children (headings are
70 + // nested, so it found none), then by grouping headings that share a parent
71 + // (each heading has its OWN panel, so every group had one item and the
72 + // script bailed silently). Both looked fine on a hand-written preview page.
73 + var made = [];
74 + var panels = content.querySelectorAll('.f-accordion-panel');
75 +
76 + var eligible = [];
77 + for (var p = 0; p < panels.length; p++) {
78 + var first = panels[p].firstElementChild;
79 + if (first && /^H[1-4]$/.test(first.tagName)) {
80 + eligible.push({ panel: panels[p], heading: first });
81 + }
82 + }
83 +
84 + if (eligible.length >= MIN_SECTIONS) {
85 + eligible.forEach(function (item, index) {
86 + var panel = item.panel;
87 + var heading = item.heading;
88 +
89 + var details = document.createElement('details');
90 + details.className = 'alder-sec';
91 + if (index === 0) { details.open = true; }
92 + if (heading.id) { details.id = heading.id; heading.removeAttribute('id'); }
93 +
94 + var summary = document.createElement('summary');
95 + var title = document.createElement('span');
96 + title.className = 'alder-sec-title';
97 + title.textContent = (heading.textContent || '').trim();
98 + summary.appendChild(title);
99 + summary.appendChild(chevron());
100 + details.appendChild(summary);
101 +
102 + // The heading is now the summary, so it must not also appear inside.
103 + panel.removeChild(heading);
104 +
105 + var wrap = document.createElement('div');
106 + wrap.className = 'alder-sec-body';
107 + while (panel.firstChild) { wrap.appendChild(panel.firstChild); }
108 + details.appendChild(wrap);
109 +
110 + // Keep the panel as the outer element: it carries XWiki's own
111 + // data-controller, and removing it could break behaviour we did not
112 + // write and cannot see.
113 + panel.appendChild(details);
114 + made.push(details);
115 + });
116 + } else {
117 + // Fallback for content without those panels -- hand-written wiki markup,
118 + // and any XWiki version that renders sections differently.
119 + var found = content.querySelectorAll('h2');
120 + if (found.length < MIN_SECTIONS) { return; }
121 +
122 + var byParent = [];
123 + for (var f = 0; f < found.length; f++) {
124 + var parent = found[f].parentNode;
125 + var slot = null;
126 + for (var b = 0; b < byParent.length; b++) {
127 + if (byParent[b].parent === parent) { slot = byParent[b]; break; }
128 + }
129 + if (!slot) { slot = { parent: parent, items: [] }; byParent.push(slot); }
130 + slot.items.push(found[f]);
131 + }
132 + byParent.sort(function (a, b) { return b.items.length - a.items.length; });
133 +
134 + var container = byParent[0].parent;
135 + var headings = byParent[0].items;
136 + if (headings.length < MIN_SECTIONS) { return; }
137 +
138 + headings.forEach(function (h2, index) {
139 + var details = document.createElement('details');
140 + details.className = 'alder-sec';
141 + if (index === 0) { details.open = true; }
142 +
143 + var summary = document.createElement('summary');
144 + var title = document.createElement('span');
145 + title.className = 'alder-sec-title';
146 + title.textContent = (h2.textContent || '').trim();
147 + summary.appendChild(title);
148 + summary.appendChild(chevron());
149 + details.appendChild(summary);
150 +
151 + var wrap = document.createElement('div');
152 + wrap.className = 'alder-sec-body';
153 + details.appendChild(wrap);
154 +
155 + if (h2.id) { details.id = h2.id; h2.removeAttribute('id'); }
156 + container.insertBefore(details, h2);
157 +
158 + var node = h2;
159 + while (node && !(node !== h2 && node.tagName === 'H2')) {
160 + var next = node.nextSibling;
161 + wrap.appendChild(node);
162 + node = next;
163 + if (node && node.nodeType === 1 && node.tagName === 'H2') { break; }
164 + }
165 + made.push(details);
166 + });
167 + }
168 +
169 + if (!made.length) { return; }
170 +
171 + var tools = document.createElement('div');
172 + tools.className = 'alder-tools';
173 + tools.appendChild(button('Expand all', true, made));
174 + tools.appendChild(button('Collapse all', false, made));
175 + made[0].parentNode.insertBefore(tools, made[0]);
176 +
177 + openFromHash();
178 + window.addEventListener('hashchange', openFromHash);
179 + }
180 +
181 + function button(label, open, sections) {
182 + var b = document.createElement('button');
183 + b.type = 'button';
184 + b.textContent = label;
185 + b.addEventListener('click', function () {
186 + sections.forEach(function (d) { d.open = open; });
187 + });
188 + return b;
189 + }
190 +
191 + function openFromHash() {
192 + var hash = window.location.hash;
193 + if (!hash || hash.length < 2) { return; }
194 + var id = hash.slice(1);
195 + var target = null;
196 + try {
197 + target = document.getElementById(id);
198 + } catch (e) {
199 + return;
200 + }
201 + if (!target) { return; }
202 + // Open the target itself if it is a section, plus any collapsed ancestor.
203 + var node = target;
204 + while (node && node !== document.body) {
205 + if (node.tagName === 'DETAILS') { node.open = true; }
206 + node = node.parentNode;
207 + }
208 + if (target.scrollIntoView) { target.scrollIntoView(); }
209 + }
210 +
211 + ready(build);
212 +})();
Name
... ... @@ -1,0 +1,1 @@
1 +Article accordion
Parse content
... ... @@ -1,0 +1,1 @@
1 +No
Use this extension
... ... @@ -1,0 +1,1 @@
1 +always