|
1 |
| -/* global django */ |
2 |
| -django.jQuery(($) => { |
3 |
| - const tabbed = $(".tabbed") |
4 |
| - if (tabbed.length >= 1) { |
5 |
| - let anchor = tabbed.eq(0) |
6 |
| - /* Break out of the .inline-related containment, avoids ugly h3's */ |
7 |
| - if (anchor.parents(".inline-related").length) { |
8 |
| - anchor = anchor.parents(".inline-related") |
| 1 | +/* Tabbed fieldsets without jQuery dependency */ |
| 2 | +;(() => { |
| 3 | + function initializeTabbedFieldsets() { |
| 4 | + const tabbedElements = document.querySelectorAll(".tabbed") |
| 5 | + if (tabbedElements.length === 0) return |
| 6 | + |
| 7 | + let anchor = tabbedElements[0] |
| 8 | + // Break out of the .inline-related containment, avoids ugly h3's |
| 9 | + const inlineRelated = anchor.closest(".inline-related") |
| 10 | + if (inlineRelated) { |
| 11 | + anchor = inlineRelated |
9 | 12 | }
|
10 |
| - anchor.before( |
11 |
| - '<div id="tabbed" class="clearfix">' + |
12 |
| - '<div class="tabs clearfix"></div>' + |
13 |
| - '<div class="modules tabbed-modules"></div>' + |
14 |
| - "</div>", |
15 |
| - ) |
16 | 13 |
|
17 |
| - const $tabs = $("#tabbed > .tabs") |
18 |
| - const $modules = $("#tabbed > .modules") |
| 14 | + // Create the tabbed container |
| 15 | + const tabbedContainer = document.createElement("div") |
| 16 | + tabbedContainer.id = "tabbed" |
| 17 | + tabbedContainer.className = "clearfix" |
| 18 | + tabbedContainer.innerHTML = |
| 19 | + '<div class="tabs clearfix"></div>' + |
| 20 | + '<div class="modules tabbed-modules"></div>' |
| 21 | + |
| 22 | + anchor.parentNode.insertBefore(tabbedContainer, anchor) |
| 23 | + |
| 24 | + const tabsContainer = document |
| 25 | + .getElementById("tabbed") |
| 26 | + .querySelector(".tabs") |
| 27 | + const modulesContainer = document |
| 28 | + .getElementById("tabbed") |
| 29 | + .querySelector(".modules") |
19 | 30 | let errorIndex = -1
|
20 | 31 | let uncollapseIndex = -1
|
21 | 32 |
|
22 |
| - tabbed.each(function createTabs(index) { |
23 |
| - const $old = $(this) |
24 |
| - const $title = $old.children("h2") |
| 33 | + // Process each tabbed element |
| 34 | + tabbedElements.forEach((element, index) => { |
| 35 | + const title = element.querySelector("h2") |
25 | 36 |
|
26 |
| - if ($old.find(".errorlist").length) { |
27 |
| - $title.addClass("has-error") |
| 37 | + if (element.querySelector(".errorlist")) { |
| 38 | + title.classList.add("has-error") |
28 | 39 | errorIndex = errorIndex < 0 ? index : errorIndex
|
29 | 40 | }
|
30 |
| - if ($old.is(".uncollapse")) { |
| 41 | + |
| 42 | + if (element.classList.contains("uncollapse")) { |
31 | 43 | uncollapseIndex = uncollapseIndex < 0 ? index : uncollapseIndex
|
32 | 44 | }
|
33 | 45 |
|
34 |
| - $title.attr("data-index", index) |
35 |
| - $title.addClass("tab") |
36 |
| - $tabs.append($title) |
37 |
| - |
38 |
| - $old.addClass("content-editor-invisible") |
| 46 | + title.setAttribute("data-index", index) |
| 47 | + title.classList.add("tab") |
| 48 | + tabsContainer.appendChild(title) |
39 | 49 |
|
40 |
| - $modules.append($old) |
| 50 | + element.classList.add("content-editor-invisible") |
| 51 | + modulesContainer.appendChild(element) |
41 | 52 | })
|
42 | 53 |
|
43 |
| - $tabs.on("click", "[data-index]", function () { |
44 |
| - const $tab = $(this) |
45 |
| - if ($tab.hasClass("active")) { |
46 |
| - $tab.removeClass("active") |
47 |
| - $modules.children().addClass("content-editor-invisible") |
| 54 | + // Add click handler for tabs |
| 55 | + tabsContainer.addEventListener("click", (event) => { |
| 56 | + const target = event.target.closest("[data-index]") |
| 57 | + if (!target) return |
| 58 | + |
| 59 | + const index = Number.parseInt(target.getAttribute("data-index"), 10) |
| 60 | + const isActive = target.classList.contains("active") |
| 61 | + |
| 62 | + if (isActive) { |
| 63 | + target.classList.remove("active") |
| 64 | + modulesContainer.querySelectorAll(".tabbed").forEach((module) => { |
| 65 | + module.classList.add("content-editor-invisible") |
| 66 | + }) |
48 | 67 | } else {
|
49 |
| - $tabs.find(".active").removeClass("active") |
50 |
| - $tab.addClass("active") |
51 |
| - $modules |
52 |
| - .children() |
53 |
| - .addClass("content-editor-invisible") |
54 |
| - .eq($tab.data("index")) |
55 |
| - .removeClass("content-editor-invisible") |
| 68 | + // Remove active from all tabs |
| 69 | + tabsContainer.querySelectorAll(".active").forEach((tab) => { |
| 70 | + tab.classList.remove("active") |
| 71 | + }) |
| 72 | + target.classList.add("active") |
| 73 | + |
| 74 | + // Hide all modules and show the selected one |
| 75 | + const modules = modulesContainer.querySelectorAll(".tabbed") |
| 76 | + modules.forEach((module, moduleIndex) => { |
| 77 | + if (moduleIndex === index) { |
| 78 | + module.classList.remove("content-editor-invisible") |
| 79 | + } else { |
| 80 | + module.classList.add("content-editor-invisible") |
| 81 | + } |
| 82 | + }) |
56 | 83 | }
|
57 | 84 | })
|
58 | 85 |
|
| 86 | + // Auto-open tab with errors or marked for uncollapse |
59 | 87 | if (errorIndex >= 0 || uncollapseIndex >= 0) {
|
60 | 88 | const index = errorIndex >= 0 ? errorIndex : uncollapseIndex
|
61 |
| - $tabs.find(`[data-index=${index}]`).click() |
| 89 | + const targetTab = tabsContainer.querySelector(`[data-index="${index}"]`) |
| 90 | + if (targetTab) { |
| 91 | + targetTab.click() |
| 92 | + } |
62 | 93 | }
|
63 | 94 | }
|
64 |
| -}) |
| 95 | + |
| 96 | + // Initialize when DOM is ready |
| 97 | + if (document.readyState === "loading") { |
| 98 | + document.addEventListener("DOMContentLoaded", initializeTabbedFieldsets) |
| 99 | + } else { |
| 100 | + initializeTabbedFieldsets() |
| 101 | + } |
| 102 | +})() |
0 commit comments