|
3 | 3 | * Loads configuration from control panel and applies it to the form |
4 | 4 | */ |
5 | 5 |
|
6 | | -(function() { |
| 6 | +(function () { |
7 | 7 | "use strict"; |
8 | 8 |
|
9 | 9 | // Store row type configurations globally |
10 | 10 | let rowTypeConfigs = {}; |
11 | 11 | let rowTypeSelect = null; |
12 | 12 |
|
13 | | - function initialize() { |
14 | | - // Only run on DynamicPageRow edit form |
15 | | - if (!document.body.classList.contains('template-edit') || |
16 | | - !document.body.classList.contains('portaltype-dynamicpagerow')) { |
| 13 | + function initialize(context = document) { |
| 14 | + // Check if we're in an edit form or an add form |
| 15 | + const isEditForm = |
| 16 | + context === document && |
| 17 | + document.body.classList.contains("template-edit") && |
| 18 | + document.body.classList.contains("portaltype-dynamicpagerow"); |
| 19 | + |
| 20 | + // Check if we're in an add form or a dynamically added form |
| 21 | + const isAddForm = (context === document ? document : context).querySelector( |
| 22 | + "form.view-name-add-DynamicPageRow" |
| 23 | + ); |
| 24 | + |
| 25 | + if (!isEditForm && !isAddForm) { |
17 | 26 | return; |
18 | 27 | } |
19 | 28 |
|
20 | 29 | // Get configuration from control panel |
21 | | - const baseUrl = document.body.dataset.portalUrl || ''; |
22 | | - fetch(`${baseUrl}/@registry/cs_dynamicpages.dynamica_pages_control_panel.row_type_fields`, { |
23 | | - method: 'GET', |
24 | | - headers: { |
25 | | - 'Accept': 'application/json', |
26 | | - }, |
27 | | - credentials: 'same-origin' |
28 | | - }) |
29 | | - .then(response => { |
30 | | - if (!response.ok) { |
31 | | - throw new Error('Network response was not ok'); |
32 | | - } |
33 | | - return response.json(); |
34 | | - }) |
35 | | - .then(data => { |
36 | | - if (data && data.length > 0) { |
37 | | - processRowTypeFields(data); |
| 30 | + const baseUrl = document.body.dataset.portalUrl || ""; |
| 31 | + fetch( |
| 32 | + `${baseUrl}/@registry/cs_dynamicpages.dynamica_pages_control_panel.row_type_fields`, |
| 33 | + { |
| 34 | + method: "GET", |
| 35 | + headers: { |
| 36 | + Accept: "application/json", |
| 37 | + }, |
| 38 | + credentials: "same-origin", |
38 | 39 | } |
39 | | - }) |
40 | | - .catch(error => { |
41 | | - console.error('Error loading row type fields:', error); |
42 | | - }); |
| 40 | + ) |
| 41 | + .then((response) => { |
| 42 | + if (!response.ok) { |
| 43 | + throw new Error("Network response was not ok"); |
| 44 | + } |
| 45 | + return response.json(); |
| 46 | + }) |
| 47 | + .then((data) => { |
| 48 | + if (data && data.length > 0) { |
| 49 | + processRowTypeFields(data); |
| 50 | + } |
| 51 | + }) |
| 52 | + .catch((error) => { |
| 53 | + console.error("Error loading row type fields:", error); |
| 54 | + }); |
43 | 55 | } |
44 | 56 |
|
45 | 57 | function processRowTypeFields(rowTypeFields) { |
46 | 58 | // Store all row type configurations |
47 | | - rowTypeFields.forEach(rowTypeConfig => { |
| 59 | + rowTypeFields.forEach((rowTypeConfig) => { |
48 | 60 | rowTypeConfigs[rowTypeConfig.row_type] = { |
49 | 61 | fields: rowTypeConfig.each_row_type_fields || [], |
50 | 62 | }; |
|
64 | 76 | rowTypeSelect = newSelect; |
65 | 77 |
|
66 | 78 | // Add new event listener |
67 | | - rowTypeSelect.addEventListener('change', toggleFields); |
| 79 | + rowTypeSelect.addEventListener("change", toggleFields); |
68 | 80 |
|
69 | | - // Initial setup |
| 81 | + // Initial setup - force update for the default value |
70 | 82 | toggleFields(); |
71 | 83 | } |
72 | 84 |
|
|
75 | 87 | if (!selectedRowType) return; |
76 | 88 |
|
77 | 89 | const config = rowTypeConfigs[selectedRowType]; |
78 | | - const allFields = document.querySelectorAll('.field'); |
79 | | - const rowTypeField = rowTypeSelect.closest('.field'); |
| 90 | + const allFields = document.querySelectorAll(".field"); |
| 91 | + const rowTypeField = rowTypeSelect.closest(".field"); |
80 | 92 |
|
81 | 93 | // Show all fields first |
82 | | - allFields.forEach(field => { |
83 | | - field.style.display = ''; |
| 94 | + allFields.forEach((field) => { |
| 95 | + field.style.display = ""; |
84 | 96 | }); |
85 | 97 |
|
86 | | - if (config && config.fields && config.fields.length > 0) { |
87 | | - // Hide all fields except row type select |
88 | | - allFields.forEach(field => { |
89 | | - if (field !== rowTypeField) { |
90 | | - field.style.display = 'none'; |
91 | | - } |
92 | | - }); |
| 98 | + // Always hide all fields except row type select by default |
| 99 | + allFields.forEach((field) => { |
| 100 | + if (field !== rowTypeField) { |
| 101 | + field.style.display = "none"; |
| 102 | + } |
| 103 | + }); |
93 | 104 |
|
94 | | - // Show only the configured fields |
95 | | - config.fields.forEach(fieldName => { |
96 | | - const fieldElement = document.querySelector( |
97 | | - `[data-fieldname$="form.widgets.${fieldName}"]` |
98 | | - ); |
99 | | - if (fieldElement) { |
100 | | - const fieldContainer = fieldElement.closest('.field'); |
101 | | - if (fieldContainer) { |
102 | | - fieldContainer.style.display = ''; |
| 105 | + // If we have a config for this row type, show its fields |
| 106 | + if (config && config.fields && config.fields.length > 0) { |
| 107 | + config.fields.forEach((fieldName) => { |
| 108 | + // Try different field name patterns to match the actual field in the form |
| 109 | + const fieldSelectors = [ |
| 110 | + `[data-fieldname$="form.widgets.${fieldName}"]`, |
| 111 | + `[data-fieldname$="form.widgets.I${fieldName}"]`, |
| 112 | + `[data-fieldname*="${fieldName}"]`, |
| 113 | + `#formfield-form-widgets-${fieldName}`, |
| 114 | + `#formfield-form-widgets-I${fieldName}`, |
| 115 | + ]; |
| 116 | + |
| 117 | + for (const selector of fieldSelectors) { |
| 118 | + const fieldElement = document.querySelector(selector); |
| 119 | + if (fieldElement) { |
| 120 | + const fieldContainer = fieldElement.closest(".field"); |
| 121 | + if (fieldContainer) { |
| 122 | + fieldContainer.style.display = ""; |
| 123 | + break; // Found and showed the field, no need to check other selectors |
| 124 | + } |
103 | 125 | } |
104 | 126 | } |
105 | 127 | }); |
| 128 | + } else if (selectedRowType === "horizontal-row-type") { |
| 129 | + // If no config but we're on the default type, show all fields |
| 130 | + allFields.forEach((field) => { |
| 131 | + field.style.display = ""; |
| 132 | + }); |
106 | 133 | } |
107 | | - // If no config, all fields remain visible |
108 | 134 | } |
109 | 135 |
|
110 | 136 | // Initialize when DOM is fully loaded |
111 | | - if (document.readyState === 'loading') { |
112 | | - document.addEventListener('DOMContentLoaded', initialize); |
113 | | - } else { |
| 137 | + function start() { |
114 | 138 | initialize(); |
| 139 | + |
| 140 | + // Handle dynamically loaded content (for add forms) |
| 141 | + document.body.addEventListener("DOMNodeInserted", function (e) { |
| 142 | + const form = e.target.closest ? e.target.closest("form") : null; |
| 143 | + if ( |
| 144 | + form && |
| 145 | + (form.classList.contains("view-name-add-DynamicPageRow") || |
| 146 | + form.id === "form") |
| 147 | + ) { |
| 148 | + initialize(form); |
| 149 | + } |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + if (document.readyState === "loading") { |
| 154 | + document.addEventListener("DOMContentLoaded", start); |
| 155 | + } else { |
| 156 | + start(); |
115 | 157 | } |
116 | 158 | })(); |
0 commit comments