| | |
| | | form.dataset.currentType = newType; |
| | | |
| | | // Use PopulateForm to properly populate all fields including repeaters |
| | | if (window.jvbPopulateForm) { |
| | | const populator = new window.jvbPopulateForm(); |
| | | const preservedFields = []; |
| | | if (window.jvbPopulate) { |
| | | this.populate = window.jvbPopulate.populate; |
| | | |
| | | // Populate each field that exists in both schemas |
| | | Object.keys(currentData).forEach(fieldName => { |
| | | const fieldWrapper = form.querySelector(`[data-field="${fieldName}"]`); |
| | | if (fieldWrapper) { |
| | | const fieldType = this.getFieldType(fieldWrapper); |
| | | const fieldValue = currentData[fieldName]; |
| | | |
| | | // Use PopulateForm's methods for complex fields |
| | | if (fieldType === 'repeater' && Array.isArray(fieldValue)) { |
| | | populator.populateRepeaterField(fieldWrapper, fieldName, fieldValue); |
| | | preservedFields.push(fieldName); |
| | | } else if (fieldValue !== null && fieldValue !== undefined && fieldValue !== '') { |
| | | // Simple field - populate directly |
| | | const field = fieldWrapper.querySelector(`[name="${fieldName}"]`) || |
| | | fieldWrapper.querySelector(`[name^="${fieldName}"]`); |
| | | if (field) { |
| | | this.populateSimpleField(field, fieldValue); |
| | | preservedFields.push(fieldName); |
| | | } |
| | | } |
| | | this.populate(fieldWrapper, fieldName, fieldValue); |
| | | } |
| | | }); |
| | | |
| | | // Announce changes |
| | | if (preservedFields.length > 0) { |
| | | const message = `Schema type changed to ${newType}. Preserved ${preservedFields.length} field value(s).`; |
| | | console.log(message); |
| | | |
| | | if (this.a11y && typeof this.a11y.announce === 'function') { |
| | | this.a11y.announce(message); |
| | | } |
| | | } else { |
| | | const message = `Schema type changed to ${newType}.`; |
| | | if (this.a11y && typeof this.a11y.announce === 'function') { |
| | | this.a11y.announce(message); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Collect form data into structured object |
| | |
| | | } |
| | | |
| | | /** |
| | | * Populate a simple field with value |
| | | */ |
| | | populateSimpleField(field, value) { |
| | | if (field.type === 'checkbox') { |
| | | field.checked = value === '1' || value === 'true' || value === true; |
| | | } else if (field.tagName === 'SELECT') { |
| | | setTimeout(() => { |
| | | field.value = value; |
| | | }, 10); |
| | | } else { |
| | | field.value = value; |
| | | } |
| | | |
| | | // Visual feedback |
| | | field.classList.add('value-preserved'); |
| | | setTimeout(() => field.classList.remove('value-preserved'), 2000); |
| | | } |
| | | |
| | | /** |
| | | * Add CSS for preserved field indication |
| | | */ |
| | | addPreservedFieldStyles() { |