PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.5.1
Tableberg – Simple Gutenberg Table Block v0.5.1
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / includes / assets / js / frontend.js

frontend.js in Tableberg – Simple Gutenberg Table Block 0.5.1, at includes/assets/js/frontend.js

473 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (() => {
2 "use strict";
3 document.addEventListener("DOMContentLoaded", () => {
4 const tables = document.querySelectorAll("[data-tableberg-responsive]");
5 if (tables.length) {
6 window.addEventListener("resize", () => {
7 tables.forEach(resizeTable);
8 });
9 tables.forEach(resizeTable);
10 }
11 });
12
13 /**
14 *
15 * @param {HTMLTableElement} table
16 */
17 function resizeTable(table) {
18 const opts = table.dataset;
19
20 if (
21 opts.tablebergMobileWidth &&
22 window.innerWidth <= opts.tablebergMobileWidth
23 ) {
24 if (opts.tablebergMobileMode === "stack") {
25 const renderMode =
26 "stack-" +
27 opts.tablebergMobileDirection +
28 "-" +
29 opts.tablebergMobileCount;
30 if (opts.tablebergMobileDirection === "row") {
31 toRowStack(
32 table,
33 opts.tablebergMobileHeader,
34 opts.tablebergMobileCount,
35 renderMode
36 );
37 } else {
38 toColStack(
39 table,
40 opts.tablebergMobileHeader,
41 opts.tablebergMobileCount,
42 renderMode
43 );
44 }
45 } else if (opts.tablebergMobileMode === "scroll") {
46 toScrollTable(table);
47 } else {
48 reviveTable(table);
49 }
50 } else if (
51 opts.tablebergTabletWidth &&
52 window.innerWidth <= opts.tablebergTabletWidth
53 ) {
54 if (opts.tablebergTabletMode === "stack") {
55 const renderMode =
56 "stack-" +
57 opts.tablebergTabletDirection +
58 "-" +
59 opts.tablebergTabletCount;
60 if (opts.tablebergTabletDirection === "row") {
61 toRowStack(
62 table,
63 opts.tablebergTabletHeader,
64 opts.tablebergTabletCount,
65 renderMode
66 );
67 } else {
68 toColStack(
69 table,
70 opts.tablebergTabletHeader,
71 opts.tablebergTabletCount,
72 renderMode
73 );
74 }
75 } else if (opts.tablebergTabletMode === "scroll") {
76 toScrollTable(table);
77 } else {
78 reviveTable(table);
79 }
80 } else {
81 reviveTable(table);
82 }
83 }
84
85 /**
86 *
87 * @param {HTMLTableElement} table
88 * @param {boolean} header
89 * @param {number} count
90 * @param {string} tag
91 */
92
93 function toRowStack(table, header, count, tag) {
94 if (table.dataset.tablebergLast === tag) {
95 return;
96 }
97 reviveTable(table);
98 setTableClassName(table, "tableberg-rowstack-table");
99 table.setAttribute("data-tableberg-last", tag);
100 const colGroup = table.querySelector("colgroup");
101 if (colGroup) {
102 colGroup.style.display = "none";
103 }
104
105 const cells = table.querySelectorAll("th,td");
106
107 const tbody = table.querySelector("tbody") || table;
108 tbody.innerHTML = "";
109
110 const masterRowMap = new Map();
111
112 const headerArr = [];
113 let colCount = Math.max(count || 1, 1);
114
115 const cols = parseInt(table.dataset.tablebergCols);
116
117 if (table.dataset.tablebergHeader) {
118 for (const cell of cells) {
119 if (cell.dataset.tablebergRow > 0) {
120 break;
121 }
122 markRowCell(cell, "header");
123 }
124 }
125
126 if (header) {
127 colCount++;
128 for (const cell of cells) {
129 if (cell.dataset.tablebergRow > 0) {
130 break;
131 }
132 headerArr.push(cell);
133 }
134 }
135
136 let rowCount = 0;
137 for (const cell of cells) {
138 const subRow = parseInt(cell.dataset.tablebergCol);
139 const masterRow = masterRowMap.get(subRow);
140
141 let row = parseInt(cell.dataset.tablebergRow);
142 if (table.dataset.tablebergHeader && row > 0) {
143 row++;
144 }
145
146 if (
147 table.dataset.tablebergFooter &&
148 ((table.dataset.tablebergHeader &&
149 row == table.dataset.tablebergRows) ||
150 (!table.dataset.tablebergHeader &&
151 row + 1 == table.dataset.tablebergRows))
152 ) {
153 markRowCell(cell, "footer");
154 } else if (row > 0) {
155 if (row % 2) {
156 markRowCell(cell, "even-row");
157 } else {
158 markRowCell(cell, "odd-row");
159 }
160 }
161
162 if (!masterRow) {
163 const rowEl = document.createElement("tr");
164 masterRowMap.set(subRow, {
165 lastRow: rowCount,
166 count: 1,
167 lastRowEL: rowEl,
168 });
169 rowEl.appendChild(cell);
170 tbody.appendChild(rowEl);
171 rowCount++;
172 } else if (masterRow.count == colCount) {
173 const rowEl = document.createElement("tr");
174 tbody.appendChild(rowEl);
175
176 let thisRowColCount = 1;
177 if (header) {
178 const headerCell = headerArr[subRow].cloneNode(true);
179 headerCell.setAttribute("data-tableberg-tmp", "1");
180 rowEl.appendChild(headerCell);
181 thisRowColCount++;
182 }
183 masterRowMap.set(subRow, {
184 lastRow: rowCount,
185 count: thisRowColCount,
186 lastRowEL: rowEl,
187 });
188
189 rowEl.appendChild(cell);
190
191 if (rowCount % cols === 0) {
192 rowEl.style.borderTop = "3px solid gray";
193 }
194 rowCount++;
195 } else {
196 masterRow.count++;
197 masterRow.lastRowEL.appendChild(cell);
198 }
199 }
200 }
201
202 /**
203 *
204 * @param {HTMLTableElement} table
205 * @param {boolean} header
206 * @param {number} count
207 * @param {string} tag
208 */
209
210 function toColStack(table, header, count, tag) {
211 const oldMode = table.dataset.tablebergLast;
212 if (oldMode === tag) {
213 return;
214 }
215 if (!header) {
216 return;
217 }
218 count = parseInt(count);
219 reviveTable(table);
220
221 table.setAttribute("data-tableberg-last", tag);
222
223 setTableClassName(table, "tableberg-colstack-table");
224
225 const cells = Array.from(table.querySelectorAll("th,td"));
226
227 if (oldMode && oldMode.match("stack-row")) {
228 cells.sort((a, b) => {
229 const aRow = parseInt(a.dataset.tablebergRow);
230 const bRow = parseInt(b.dataset.tablebergRow);
231 const diff1 = aRow - bRow;
232 if (diff1 !== 0) {
233 return diff1;
234 }
235 const aCol = parseInt(a.dataset.tablebergCol);
236 const bCol = parseInt(b.dataset.tablebergCol);
237 return aCol - bCol;
238 });
239 }
240
241 const tbody = table.querySelector("tbody") || table;
242 tbody.innerHTML = "";
243
244 const headerArr = [];
245 let stackRowCount = Math.max(count || 1, 1);
246
247 let rowIdxStart = 0;
248 let rowCount = -1,
249 lastRow = -1,
250 stackTrack = 0,
251 lastRowEl;
252
253 if (header) {
254 stackRowCount++;
255 rowCount++;
256 lastRowEl = document.createElement("tr");
257 markRow(lastRowEl, "header");
258 tbody.appendChild(lastRowEl);
259 stackTrack++;
260
261 for (; rowIdxStart < cells.length; rowIdxStart++) {
262 const cell = cells[rowIdxStart];
263 if (cell.dataset.tablebergRow > 0) {
264 break;
265 }
266 lastRowEl.appendChild(cell);
267 headerArr.push(cell);
268 }
269 }
270
271 const footer = table.dataset.tablebergFooter;
272 const footerArr = [];
273
274 if (footer) {
275 const fRow = parseInt(table.dataset.tablebergRows) - 1;
276 let i = cells.length - 1;
277 for (; i > -1; i--) {
278 if (cells[i].dataset.tablebergRow < fRow) {
279 break;
280 }
281 footerArr.unshift(cells[i]);
282 }
283 cells.splice(i + 1);
284 }
285
286 for (let idx = rowIdxStart; idx < cells.length; idx++) {
287 const cell = cells[idx];
288
289 if (lastRow != cell.dataset.tablebergRow) {
290 lastRow = cell.dataset.tablebergRow;
291
292 let row = parseInt(lastRow);
293
294 if (header) {
295 row++;
296 if (stackTrack == stackRowCount) {
297 rowCount++;
298
299 lastRowEl = document.createElement("tr");
300 markRow(lastRowEl, "header");
301 lastRowEl.setAttribute("data-tableberg-tmp", "1");
302 tbody.appendChild(lastRowEl);
303
304 stackTrack = 1;
305
306 for (const cell of headerArr) {
307 lastRowEl.appendChild(cell.cloneNode(true));
308 }
309 }
310 }
311 rowCount++;
312 lastRowEl = document.createElement("tr");
313
314 if (row % 2) {
315 markRow(lastRowEl, "even-row");
316 } else {
317 markRow(lastRowEl, "odd-row");
318 }
319 tbody.appendChild(lastRowEl);
320 stackTrack++;
321 }
322
323 lastRowEl.appendChild(cell);
324 }
325
326 if (footerArr.length > 0) {
327 lastRowEl = document.createElement("tr");
328 markRow(lastRowEl, "footer");
329 tbody.appendChild(lastRowEl);
330
331 footerArr.forEach((cell) => lastRowEl.appendChild(cell));
332 }
333 }
334
335 /**
336 *
337 * @param {HTMLTableElement} table
338 * @returns
339 */
340
341 function reviveTable(table) {
342 const oldMode = table.dataset.tablebergLast;
343 if (!oldMode) {
344 return;
345 }
346 table.removeAttribute("data-tableberg-last");
347 table.parentElement.classList.remove("tableberg-scroll-x");
348
349 table
350 .querySelectorAll("[data-tableberg-tmp]")
351 .forEach((el) => el.remove());
352
353 if (!oldMode || !oldMode.match("stack")) {
354 return;
355 }
356
357 setTableClassName(table);
358
359 const colGroup = table.querySelector("colgroup");
360 if (colGroup) {
361 colGroup.removeAttribute("style");
362 }
363
364 const cells = Array.from(table.querySelectorAll("th,td"));
365 cells.sort((a, b) => {
366 const aRow = parseInt(a.dataset.tablebergRow);
367 const bRow = parseInt(b.dataset.tablebergRow);
368 const diff1 = aRow - bRow;
369 if (diff1 !== 0) {
370 return diff1;
371 }
372 const aCol = parseInt(a.dataset.tablebergCol);
373 const bCol = parseInt(b.dataset.tablebergCol);
374 return aCol - bCol;
375 });
376
377 const tbody = table.querySelector("tbody") || table;
378 tbody.innerHTML = "";
379
380 let lastRow = -1,
381 lastRowEl;
382
383 for (const cell of cells) {
384 if (lastRow != cell.dataset.tablebergRow) {
385 lastRow = cell.dataset.tablebergRow;
386 lastRowEl = document.createElement("tr");
387
388 let row = parseInt(lastRow);
389 if (table.dataset.tablebergHeader) {
390 if (row === 0) {
391 markRow(lastRowEl, "header");
392 } else {
393 row++;
394 }
395 }
396
397 if (
398 table.dataset.tablebergFooter &&
399 row == table.dataset.tablebergRows
400 ) {
401 markRow(lastRowEl, "footer");
402 } else if (row > 0) {
403 if (row % 2) {
404 markRow(lastRowEl, "even-row");
405 } else {
406 markRow(lastRowEl, "odd-row");
407 }
408 }
409
410 tbody.appendChild(lastRowEl);
411 }
412
413 lastRowEl.appendChild(cell);
414 }
415 }
416
417 function toScrollTable(table) {
418 const opts = table.dataset;
419 if (opts.tablebergLast === "scroll") {
420 return;
421 }
422 if (opts.tablebergLast) {
423 reviveTable(table);
424 }
425 table.setAttribute("data-tableberg-last", "scroll");
426 table.parentElement.classList.add("tableberg-scroll-x");
427 }
428
429 /**
430 *
431 * @param {HTMLTableCellElement} cell
432 * @param {"even-row" | "odd-row" | "header" | "footer"} oddEven
433 */
434
435 function markRowCell(cell, oddEven) {
436 [
437 "tableberg-even-row-cell",
438 "tableberg-header-cell",
439 "tableberg-footer-cell",
440 "tableberg-odd-row-cell",
441 ].forEach((className) => cell.classList.remove(className));
442 cell.classList.add(`tableberg-${oddEven}-cell`);
443 }
444
445 /**
446 *
447 * @param {HTMLTableRowElement} row
448 * @param {"even-row" | "odd-row" | "header" | "footer"} oddEven
449 */
450
451 function markRow(row, oddEven) {
452 [
453 "tableberg-even-row",
454 "tableberg-header",
455 "tableberg-footer",
456 "tableberg-odd-row",
457 ].forEach((className) => row.classList.remove(className));
458 row.classList.add(`tableberg-${oddEven}`);
459 }
460
461 /**
462 *
463 * @param {HTMLTableElement} table
464 * @param {string} className
465 */
466 function setTableClassName(table, className) {
467 ["tableberg-rowstack-table", "tableberg-rowstack-table"].forEach(
468 (className) => table.classList.remove(className)
469 );
470 className && table.classList.add(className);
471 }
472 })();
473