|
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 | + // Store row type configurations globally |
| 10 | + let rowTypeConfigs = {}; |
| 11 | + let rowTypeSelect = null; |
| 12 | + |
9 | 13 | function initialize() { |
10 | 14 | // Only run on DynamicPageRow edit form |
11 | | - if ( |
12 | | - !$("body.template-edit").length || |
13 | | - !$("body.portaltype-dynamicpagerow").length |
14 | | - ) { |
| 15 | + if (!document.body.classList.contains('template-edit') || |
| 16 | + !document.body.classList.contains('portaltype-dynamicpagerow')) { |
15 | 17 | return; |
16 | 18 | } |
17 | 19 |
|
18 | 20 | // Get configuration from control panel |
19 | | - let base_url = $("body").data("portal-url"); |
20 | | - $.ajax({ |
21 | | - url: `${base_url}/@registry/cs_dynamicpages.dynamica_pages_control_panel.row_type_fields`, |
22 | | - type: "GET", |
23 | | - dataType: "json", |
| 21 | + const baseUrl = document.body.dataset.portalUrl || ''; |
| 22 | + fetch(`${baseUrl}/@registry/cs_dynamicpages.dynamica_pages_control_panel.row_type_fields`, { |
| 23 | + method: 'GET', |
24 | 24 | headers: { |
25 | | - Accept: "application/json", |
| 25 | + 'Accept': 'application/json', |
26 | 26 | }, |
| 27 | + credentials: 'same-origin' |
27 | 28 | }) |
28 | | - .done(function (data) { |
29 | | - if (data.length > 0) { |
30 | | - processRowTypeFields(data); |
31 | | - } |
32 | | - }) |
33 | | - .fail(function (error) { |
34 | | - console.error("Error loading row type fields:", error); |
35 | | - }); |
| 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); |
| 38 | + } |
| 39 | + }) |
| 40 | + .catch(error => { |
| 41 | + console.error('Error loading row type fields:', error); |
| 42 | + }); |
36 | 43 | } |
37 | 44 |
|
38 | | - // Store row type configurations globally |
39 | | - let rowTypeConfigs = {}; |
40 | | - |
41 | 45 | function processRowTypeFields(rowTypeFields) { |
42 | 46 | // Store all row type configurations |
43 | | - rowTypeFields.forEach(function (rowTypeConfig) { |
| 47 | + rowTypeFields.forEach(rowTypeConfig => { |
44 | 48 | rowTypeConfigs[rowTypeConfig.row_type] = { |
45 | 49 | fields: rowTypeConfig.each_row_type_fields || [], |
46 | 50 | }; |
|
51 | 55 | } |
52 | 56 |
|
53 | 57 | function updateFieldVisibility() { |
54 | | - const rowTypeSelect = $('select[name$=".row_type:list"]'); |
55 | | - if (!rowTypeSelect.length) return; |
56 | | - |
57 | | - function toggleFields() { |
58 | | - const selectedRowType = rowTypeSelect.val(); |
59 | | - if (!selectedRowType) return; |
60 | | - |
61 | | - const config = rowTypeConfigs[selectedRowType]; |
62 | | - |
63 | | - // Always show the row type select |
64 | | - rowTypeSelect.closest(".field").show(); |
65 | | - |
66 | | - if (!config || !config.fields || config.fields.length === 0) { |
67 | | - // If no configuration for this row type, show all fields |
68 | | - $(".field").show(); |
69 | | - } else { |
70 | | - // Otherwise, show only the configured fields |
71 | | - // First hide all fields except the row type select |
72 | | - $(".field").not(rowTypeSelect.closest(".field")).hide(); |
73 | | - |
74 | | - // Show the fields for the selected row type |
75 | | - config.fields.forEach(function (fieldName) { |
76 | | - $(`[data-fieldname$="form.widgets.${fieldName}"]`) |
77 | | - .closest(".field") |
78 | | - .show(); |
79 | | - }); |
80 | | - } |
81 | | - } |
| 58 | + rowTypeSelect = document.querySelector('select[name$=".row_type:list"]'); |
| 59 | + if (!rowTypeSelect) return; |
| 60 | + |
| 61 | + // Remove previous event listener if it exists |
| 62 | + const newSelect = rowTypeSelect.cloneNode(true); |
| 63 | + rowTypeSelect.parentNode.replaceChild(newSelect, rowTypeSelect); |
| 64 | + rowTypeSelect = newSelect; |
| 65 | + |
| 66 | + // Add new event listener |
| 67 | + rowTypeSelect.addEventListener('change', toggleFields); |
82 | 68 |
|
83 | 69 | // Initial setup |
84 | 70 | toggleFields(); |
| 71 | + } |
| 72 | + |
| 73 | + function toggleFields() { |
| 74 | + const selectedRowType = rowTypeSelect.value; |
| 75 | + if (!selectedRowType) return; |
| 76 | + |
| 77 | + const config = rowTypeConfigs[selectedRowType]; |
| 78 | + const allFields = document.querySelectorAll('.field'); |
| 79 | + const rowTypeField = rowTypeSelect.closest('.field'); |
| 80 | + |
| 81 | + // Show all fields first |
| 82 | + allFields.forEach(field => { |
| 83 | + field.style.display = ''; |
| 84 | + }); |
85 | 85 |
|
86 | | - // Update on change |
87 | | - rowTypeSelect |
88 | | - .off("change.rowTypeChange") |
89 | | - .on("change.rowTypeChange", toggleFields); |
| 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 | + }); |
| 93 | + |
| 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 = ''; |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + // If no config, all fields remain visible |
90 | 108 | } |
91 | 109 |
|
92 | | - $(document).ready(initialize); |
93 | | -})(jQuery); |
| 110 | + // Initialize when DOM is fully loaded |
| 111 | + if (document.readyState === 'loading') { |
| 112 | + document.addEventListener('DOMContentLoaded', initialize); |
| 113 | + } else { |
| 114 | + initialize(); |
| 115 | + } |
| 116 | +})(); |
0 commit comments