Changes for page Helpers

Last modified by Benjamin Fischer on 2025/06/17 11:57

From version 1.2
edited by Benjamin Fischer
on 2025/05/15 13:32
Change comment: There is no comment for this version
To version 2.10
edited by Benjamin Fischer
on 2025/05/27 12:48
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,0 +1,1 @@
1 +Contains JSX helpers for customized LiveTable views.
XWiki.JavaScriptExtension[0]
Code
... ... @@ -1,15 +1,17 @@
1 -define("xwiki-custom-livetable", [], function () {
2 - const func = (tabName, rowFunc, colTT = {}) => {
3 - let tagCols;
1 +((() => {
2 + const livetableRowHook = {};
3 + const tagCols = {};
4 4  
5 - document.observe('xwiki:livetable:loading', () => {
6 - const tab = document.getElementById(tabName);
7 - if (!tab) return;
5 + document.observe('xwiki:livetable:loading', () => {
6 + for (const tab of document.querySelectorAll(".xwiki-livetable")) {
7 + const tableId = tab.id;
8 + if (!tableId) continue;
8 8  
9 9   // coulmn data sources
10 10   const conf = JSON.parse(tab.dataset.settings);
11 - tagCols = (conf.columnDescriptors.tags ?? { aux: [] }).aux ?? [["Tags", true]];
12 - tagCols.forEach(([name], i) => {
12 +
13 + tagCols[tableId] = (conf.columnDescriptors.tags ?? { aux: [] }).aux ?? [["Tags", true]];
14 + tagCols[tableId].forEach(([name], i) => {
13 13   const cn = `tags-${i}`;
14 14   conf.columns.splice(conf.columns.indexOf("tags"), 1, cn);
15 15   conf.columnDescriptors[cn] = {
... ... @@ -22,15 +22,38 @@
22 22   tab.dataset.settings = JSON.stringify(conf);
23 23  
24 24   // column headers
27 + const tagIdxs = [];
25 25   tab.querySelectorAll(".xwiki-livetable-display-header .tagsCol").forEach(
26 - (v, i) => v.textContent = tagCols[i][0]
29 + (v, i) => {
30 + v.textContent = tagCols[tableId][i][0];
31 + tagIdxs.push(v.cellIndex);
32 + }
27 27   );
28 28  
35 + // filter fileds for tags
36 + const tagFilterHelp = document.querySelector(".xwiki-livetable-tagcloud-container > h2")?.textContent ?? "Click on tag(s) to filter.";
37 + const filterCells = tab.querySelectorAll(".xwiki-livetable-display-header-filter");
38 + for (let pos; (pos = tagIdxs.length - 1) >= 0;) {
39 + while (tagIdxs[pos] === tagIdxs[pos - 1] + 1)
40 + filterCells[tagIdxs[pos--]].remove();
41 + const e = filterCells[tagIdxs[pos]];
42 + e.colSpan = span = tagIdxs.length - pos;
43 + e.textContent = tagFilterHelp;
44 + e.style.verticalAlign = "middle";
45 + e.style.textAlign = "center";
46 + tagIdxs.splice(pos);
47 + }
48 +
49 + // filter fields
50 + tab.querySelectorAll(".xwiki-livetable-display-header-filter input[type=text]").forEach(
51 + v => v.placeholder = "Filter ..."
52 + );
53 +
29 29   // column tooltips
30 30   tab.querySelectorAll(".xwiki-livetable-display-header-text").forEach(
31 31   el => {
32 32   const t = el.textContent.trim();
33 - const c = conf.columnDescriptors[t].tooltip ?? colTT[t];
58 + const c = conf.columnDescriptors[t]?.tooltip;
34 34   if (c) {
35 35   el.title = t;
36 36   el.dataset.content = c;
... ... @@ -41,7 +41,7 @@
41 41   )
42 42  
43 43   // handle clicks for tags
44 - document.getElementById(`${tabName}-display`).addEventListener("click", ev => {
69 + tab.getElementsByClassName("xwiki-livetable-display-body")[0].addEventListener("click", ev => {
45 45   if (ev.button) return; // only left click
46 46   if (ev.target.nodeName !== "SPAN") return;
47 47   if (!ev.target.classList.contains("ltTag")) return;
... ... @@ -54,26 +54,123 @@
54 54   lt.tags = Object.keys(st);
55 55   lt.clearCache();
56 56   lt.showRows(1, lt.limit);
57 - })
58 - });
82 + });
59 59  
60 - document.observe(`xwiki:livetable:${tabName}:receivedEntries`, ({ memo: { data } }) => {
61 - for (const row of data.rows) {
62 - if (rowFunc) rowFunc(row)
84 + }
85 + });
63 63  
64 - // tags
65 - const tags = new Set(row.tags_value.slice(1, -1).split(", "));
66 - tagCols.forEach(([name, ...want], i) =>
67 - row[`tags-${i}`] = (
68 - want[0] === true
69 - ? Array.from(tags)
70 - : want.filter(tag => tags.delete(tag))
71 - ).sort().map(tag => `<span class="ltTag" style="cursor:pointer;">${tag}</span>`).join(", ")
72 - );
73 - }
74 - });
87 + document.observe("xwiki:livetable:receivedEntries", ({ memo: { data, tableId } }) => {
88 + const rf = livetableRowHook[tableId];
89 + for (const row of data.rows) {
90 + if (rf) rf(row);
91 +
92 + // tags
93 + const tags = new Set(row.tags_value.slice(1, -1).split(", "));
94 + tagCols[tableId]?.forEach(([name, ...want], i) =>
95 + row[`tags-${i}`] = (
96 + want[0] === true
97 + ? Array.from(tags)
98 + : want.filter(tag => tags.delete(tag))
99 + ).sort().map(tag => `<span class="ltTag" style="cursor:pointer;white-space:nowrap;">${tag}</span>`).join(", ")
100 + );
101 + }
102 + });
103 +
104 + livetableRowHook.jobs = row => {
105 + const a = new Element("a", { href: row.link });
106 + a.innerHTML = `<b class="wikiexternallink">${row.doc_title}</b>`;
107 + row.doc_title = a.outerHTML;
108 +
109 + row.doc_date = new Date(row.doc_date)?.toLocaleDateString() ?? row.doc_date.split(" ")[0];
110 + row.valid = new Date(row.valid_value)?.toLocaleDateString() ?? row.valid;
75 75   };
76 76  
77 - func.func = func;
78 - return func;
79 -});
113 + livetableRowHook.materials = row => {
114 + const ol = new DOMParser().parseFromString(row.doc_location, "text/xml").documentElement;
115 + const li = ol.children;
116 +
117 + switch (li[0].textContent) {
118 + case "MoccaCalendar":
119 + li[1].remove(); // the sub calendar
120 + case "Material collection":
121 + li[0].remove(); // the "app"
122 + }
123 + for (let i = 1; i < li.length - 1; i++) // intermediate
124 + li[i].textContent = li[i].textContent; // remove link
125 +
126 + const a = li[li.length - 1].children[0];
127 + const b = a.ownerDocument.createElement("b");
128 + a.replaceWith(b);
129 + b.appendChild(a);
130 + a.textContent = row.doc_title;
131 +
132 + const u = new URL(row.link_value);
133 + let icon;
134 + if (u.hostname.includes("github")) icon = "github";
135 + if (u.hostname.includes("github.io")) icon = "book";
136 + if (u.hostname.includes("gitlab")) icon = "gitlab";
137 + if (u.hostname.includes("google")) icon = "google";
138 + if (u.hostname.includes("colab")) icon = "code";
139 + if (u.hostname.includes("youtube")) icon = "youtube-play";
140 + if (u.pathname.endsWith(".pdf")) icon = "file-pdf-o";
141 + if (u.pathname.endsWith(".pptx")) icon = "file-powerpoint-o";
142 + if (u.pathname.endsWith(".ipynb")) icon = "code";
143 + if (icon) {
144 + const s = a.ownerDocument.createElement("span");
145 + s.appendChild(a.ownerDocument.createElement("wbr"));
146 + s.setAttribute("class", `fa fa-${icon}`);
147 + s.setAttribute("style", "padding-right: .3em;");
148 + a.insertBefore(s, a.firstChild);
149 + }
150 + a.setAttribute("class", "wikiexternallink");
151 + a.setAttribute("href", u.href);
152 +
153 + row.doc_location = ol.outerHTML;
154 + row.date = new Date(row.date_value)?.toLocaleDateString(undefined, {year: "numeric", month: "numeric"}) ?? row.date;
155 + };
156 +
157 + const coopDetailsLabel = "Cooperation Details";
158 + livetableRowHook.research_groups = row => {
159 + const a = new Element("a", { href: row.URL });
160 + a.innerHTML = row.affiliation_value.split("\n").map((a, i) => i ? a : `<b class="wikiexternallink">${a}</b>`).join("<br/>");
161 + a.title = row.doc_title;
162 + if (row.content !== "-") {
163 + let c = row.content;
164 + if (row.coopDetails !== "-")
165 + c = `${c}<br/><b>${coopDetailsLabel}:</b> ${row.coopDetails}`;
166 + a.dataset.content = c;
167 + a.dataset.toggle = "popover";
168 + }
169 + row.affiliation = a.outerHTML;
170 +
171 + row.coop = row.coop.replace(/\b \.\.\. \b/g, " ...<br/>");
172 + if (row.coopDetails !== "-") {
173 + const d = new Element("div");
174 + d.innerHTML = row.coop;
175 + d.title = coopDetailsLabel;
176 + d.dataset.content = row.coopDetails;
177 + d.dataset.toggle = "popover";
178 + row.coop = d.outerHTML;
179 + }
180 +
181 + row.doc_date = new Date(row.doc_date)?.toLocaleDateString(undefined, {year: "numeric", month: "numeric"}) ?? row.doc_date.split(" ")[0].split("/").slice(0, 2).join("/");
182 + };
183 +
184 + require(['jquery', 'bootstrap'], function($) {
185 + document.styleSheets[0].insertRule(`#mainContentArea .popover {max-width: 50%;}`);
186 +
187 + const ttApply = ({ memo: { tableId } }) => {
188 + if (tableId)
189 + $(`#${tableId} [data-toggle="popover"]`).popover({
190 + html: true,
191 + container: `#mainContentArea`,
192 + trigger: "hover",
193 + });
194 + };
195 +
196 + document.observe(`xwiki:livetable:displayComplete`, ttApply);
197 + for (const tab of document.querySelectorAll(".xwiki-livetable"))
198 + ttApply({ memo: { tableId: tab.id }});
199 + });
200 +})());
201 +