|
| 1 | +/** |
| 2 | + * JavaScript for DynamicPageRow edit form |
| 3 | + * Loads configuration from control panel and applies it to the form |
| 4 | + */ |
| 5 | + |
| 6 | +(function ($) { |
| 7 | + "use strict"; |
| 8 | + |
| 9 | + function initialize() { |
| 10 | + // Only run on DynamicPageRow edit form |
| 11 | + if ( |
| 12 | + !$("body.template-edit").length || |
| 13 | + !$("body.portaltype-dynamicpagerow").length |
| 14 | + ) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + // 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", |
| 24 | + headers: { |
| 25 | + Accept: "application/json", |
| 26 | + }, |
| 27 | + }) |
| 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 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + // Store row type configurations globally |
| 39 | + let rowTypeConfigs = {}; |
| 40 | + |
| 41 | + function processRowTypeFields(rowTypeFields) { |
| 42 | + // Store all row type configurations |
| 43 | + rowTypeFields.forEach(function (rowTypeConfig) { |
| 44 | + rowTypeConfigs[rowTypeConfig.row_type] = { |
| 45 | + fields: rowTypeConfig.each_row_type_fields || [], |
| 46 | + }; |
| 47 | + }); |
| 48 | + |
| 49 | + // Initial setup |
| 50 | + updateFieldVisibility(); |
| 51 | + } |
| 52 | + |
| 53 | + 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 | + } |
| 82 | + |
| 83 | + // Initial setup |
| 84 | + toggleFields(); |
| 85 | + |
| 86 | + // Update on change |
| 87 | + rowTypeSelect |
| 88 | + .off("change.rowTypeChange") |
| 89 | + .on("change.rowTypeChange", toggleFields); |
| 90 | + } |
| 91 | + |
| 92 | + $(document).ready(initialize); |
| 93 | +})(jQuery); |
0 commit comments