Skip to content

Commit dbe8e74

Browse files
committed
remove jquery dependency
1 parent 618885e commit dbe8e74

1 file changed

Lines changed: 80 additions & 57 deletions

File tree

src/cs_dynamicpages/browser/static/edit_dynamicpagerow.js

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,48 @@
33
* Loads configuration from control panel and applies it to the form
44
*/
55

6-
(function ($) {
6+
(function() {
77
"use strict";
88

9+
// Store row type configurations globally
10+
let rowTypeConfigs = {};
11+
let rowTypeSelect = null;
12+
913
function initialize() {
1014
// 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')) {
1517
return;
1618
}
1719

1820
// 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',
2424
headers: {
25-
Accept: "application/json",
25+
'Accept': 'application/json',
2626
},
27+
credentials: 'same-origin'
2728
})
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+
});
3643
}
3744

38-
// Store row type configurations globally
39-
let rowTypeConfigs = {};
40-
4145
function processRowTypeFields(rowTypeFields) {
4246
// Store all row type configurations
43-
rowTypeFields.forEach(function (rowTypeConfig) {
47+
rowTypeFields.forEach(rowTypeConfig => {
4448
rowTypeConfigs[rowTypeConfig.row_type] = {
4549
fields: rowTypeConfig.each_row_type_fields || [],
4650
};
@@ -51,43 +55,62 @@
5155
}
5256

5357
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);
8268

8369
// Initial setup
8470
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+
});
8585

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
90108
}
91109

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

Comments
 (0)