| 1 |
jQuery(document).ready(function($){ |
| 2 |
wdk_dependfields_submit_form('form.form_listing'); |
| 3 |
wdk_dependfields_submit_form_query('form.form_listing'); |
| 4 |
}); |
| 5 |
|
| 6 |
const wdk_dependfields_submit_form = ($selector = null, parent_field_selector = '.wdk-field-edit') => { |
| 7 |
var $ = jQuery; |
| 8 |
var el_category,el_form, class_hide, hide_fields; |
| 9 |
class_hide = 'wdk-depend-hidden'; |
| 10 |
el_form = jQuery($selector); |
| 11 |
el_category = el_form.find('*[name="category_id"]'); |
| 12 |
|
| 13 |
el_category.on('input', function(){ |
| 14 |
hide_fields(); |
| 15 |
}); |
| 16 |
|
| 17 |
hide_fields = () => { |
| 18 |
var value = el_category.find('option:selected').attr('value'); |
| 19 |
el_form.find('.'+class_hide).removeClass(class_hide) |
| 20 |
if(el_category.find('option:selected').length && typeof script_dependfields_submitform.hidden_fields[value] != 'undefined') { |
| 21 |
el_form.find('*[name *="field_"]').each(function(){ |
| 22 |
var field_id = $(this).attr('name').substr(6); |
| 23 |
if(script_dependfields_submitform.hidden_fields[value].indexOf(','+field_id+',') != -1) { |
| 24 |
$(this).closest(parent_field_selector).addClass(class_hide); |
| 25 |
} |
| 26 |
}); |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
/* init hide fields */ |
| 31 |
hide_fields(); |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
/* depend fields, remove required and data from hidden based on depend fields */ |
| 36 |
const wdk_dependfields_submit_form_query = ($selector = null, parent_field_selector = '.wdk-field-edit') => { |
| 37 |
var $ = jQuery; |
| 38 |
var el_form, class_hide, hide_fields; |
| 39 |
class_hide = 'wdk-depend-hidden'; |
| 40 |
el_form = jQuery($selector); |
| 41 |
|
| 42 |
el_form.on('submit', function(e){ |
| 43 |
el_form.find('.'+class_hide).find('input:not([type="checkbox"]):not([name="element_id"]):not([type="radio"]):not([type="hidden"]),textarea,select').val('').removeAttr('required'); |
| 44 |
el_form.find('.'+class_hide).find('select').val(jQuery(this).find('option:first').val()).removeAttr('required'); |
| 45 |
el_form.find('.'+class_hide).find('input[type="checkbox"]').prop('checked', false).removeAttr('required'); |
| 46 |
|
| 47 |
}) |
| 48 |
} |