// Radio conditioned fields $(document).on('ready formily_loaded', function(){ var showConditionedField = function(field){ var cField = field.parent(0).find('input, textarea').not(field); if(field.data('formilyConditioned')) { field.get(0).removeAttribute('name'); cField.attr('name', field.data('c2Name')); } if(cField.attr('type') == 'file') { if(cField.attr('multiple')) { cField.parents('.formily-input:first').show(0); } else { cField.parent().show(0); cField.show(0); } } else { cField.show(0); } }; var hideConditionedField = function(field){ var cField = field.parent(0).find('input, textarea').not(field); if(field.data('formilyConditioned')) { field.attr('name', field.data('cName')); cField.get(0).removeAttribute('name'); } if(cField.attr('multiple')) { cField.parents('.formily-input:first').hide(0); field.parent().find('.formily-files-container').html(''); } else { cField.hide(0); field.parent().find('.formily-files-container').html(''); } if(cField.attr('type') == 'file') { // Remove all files var input_id = field.attr('data-c2-name').match(/\[(\d+)\](\[\])?$/)[1]; var form = field.parents('form'); var data = { user_id: form.find('input[name=user_id]').val(), authenticity_token: form.find('input[name=authenticity_token]').val(), form_id: form.find('input[name=form_id]').val(), resource_id: form.find('input[name=resource_id]').val(), resource_type: form.find('input[name=resource_type]').val(), input_id: input_id }; $.ajax({ url: '/formily/file/delete_by_input_id', data: data, type: 'DELETE' }); } }; // Initialize radio groups $('fieldset.formily-radio-group').each(function(_, elem){ var self = $(elem); var radio = self.find('input[type=radio][checked]'); if(radio.length > 0) { self.data('prevRadio', radio); } else { self.data('prevRadio', 0); } }); $(document).on('change', 'input[type=radio][data-formily-conditioned]', function(e){ var self = $(this); var fieldset = self.parents('fieldset'); if(fieldset.data('prevRadio') == 0){ fieldset.data('prevRadio', self); } var prevRadio = fieldset.data('prevRadio'); if(prevRadio.not(self).length == 0) { // First init showConditionedField(self); self.prop('checked', true); self.attr('checked', 'checked'); } else { if(prevRadio.data('formilyConditioned')) { // Hide conditioned field var i = prevRadio.parent(0).find('input, textarea').not(prevRadio); if(i.attr('type') == 'file') { if (confirm('Delete all files ?')) { if(self.data('formilyConditioned')) { showConditionedField(self); } hideConditionedField(prevRadio); prevRadio.prop('checked', false); prevRadio.get(0).removeAttribute('checked'); self.prop('checked', true); self.attr('checked', 'checked'); fieldset.data('prevRadio', self); } else { if(self.data('formilyConditioned')) { hideConditionedField(self); } prevRadio.prop('checked', true); prevRadio.attr('checked', 'checked'); self.prop('checked', false); self.get(0).removeAttribute('checked'); fieldset.data('prevRadio', prevRadio); } } else { hideConditionedField(prevRadio); showConditionedField(self); self.prop('checked', true); self.attr('checked', 'checked'); prevRadio.prop('checked', false); prevRadio.get(0).removeAttribute('checked'); fieldset.data('prevRadio', self); } } else { showConditionedField(self); self.prop('checked', true); self.attr('checked', 'checked'); fieldset.data('prevRadio', self); } } e.preventDefault(); }); });