| 1 |
/** |
| 2 |
* WPComment Validate |
| 3 |
* @Since version 24.2 |
| 4 |
* By: Najeeb Ahmad |
| 5 |
* Date: January 19, 2022 |
| 6 |
**/ |
| 7 |
|
| 8 |
/*global jQuery wpcomment_get_element_value wpcomment_input_vars*/ |
| 9 |
|
| 10 |
const WPComment_Validate = { |
| 11 |
|
| 12 |
field_meta: wpcomment_input_vars.field_meta.filter(f => f.required === "on" ), |
| 13 |
passed: true, |
| 14 |
$wpcomment_input: jQuery('.wpcomment-input.wpcomment-required'), |
| 15 |
$wpcomment_input_texts: jQuery('.wpcomment-input.wpcomment-required.text, .wpcomment-input.wpcomment-required.email, .wpcomment-input.wpcomment-required.number,.wpcomment-input.wpcomment-required.quantityoption'), |
| 16 |
|
| 17 |
init : async () => { |
| 18 |
|
| 19 |
jQuery(document).bind('wpcomment_uploaded_file_removed', async function(e) { |
| 20 |
WPComment_Validate.validate_data() |
| 21 |
.then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); |
| 22 |
}); |
| 23 |
|
| 24 |
jQuery(document).bind('wpcomment_field_shown', async function(e) { |
| 25 |
WPComment_Validate.validate_data() |
| 26 |
.then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); |
| 27 |
}); |
| 28 |
|
| 29 |
jQuery(document).bind('wpcomment_field_hidden', async function(e) { |
| 30 |
WPComment_Validate.validate_data() |
| 31 |
.then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); |
| 32 |
}); |
| 33 |
|
| 34 |
jQuery(document).on('change', WPComment_Validate.$wpcomment_input, async function(e){ |
| 35 |
WPComment_Validate.validate_data() |
| 36 |
.then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); |
| 37 |
}); |
| 38 |
|
| 39 |
// keyup events for texts input e.g: text,email,number |
| 40 |
WPComment_Validate.$wpcomment_input_texts.keyup(async function(e){ |
| 41 |
WPComment_Validate.validate_data() |
| 42 |
.then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); |
| 43 |
}); |
| 44 |
|
| 45 |
WPComment_Validate.validate_data() |
| 46 |
.then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); |
| 47 |
|
| 48 |
}, |
| 49 |
|
| 50 |
validate_data: () => { |
| 51 |
|
| 52 |
return new Promise( function(resolve, reject){ |
| 53 |
|
| 54 |
// console.log(WPComment_Validate.field_meta); |
| 55 |
const invalid_fields = WPComment_Validate.field_meta.filter(f => !WPComment_Validate.field_has_valid_data(f) && !WPComment_Validate.is_field_hidden(f.data_name)) |
| 56 |
const validate_result = invalid_fields.length > 0 ? false : true; |
| 57 |
|
| 58 |
return validate_result ? resolve() : reject(invalid_fields); |
| 59 |
}); |
| 60 |
}, |
| 61 |
|
| 62 |
is_field_hidden: (data_name) => { |
| 63 |
|
| 64 |
// console.log(data_name, jQuery(`.wpcomment-field-wrapper.${data_name}.wpcomment-c-hide`).length > 0); |
| 65 |
return jQuery(`.wpcomment-field-wrapper.${data_name}.wpcomment-c-hide`).length > 0; |
| 66 |
}, |
| 67 |
|
| 68 |
/** |
| 69 |
* When a variation is hidden. |
| 70 |
*/ |
| 71 |
disable_button: (invalid_fields) => { |
| 72 |
const $form = jQuery('form.comment-form'); |
| 73 |
$form |
| 74 |
.find( 'input[type="submit"]' ) |
| 75 |
.prop('disabled', true); |
| 76 |
// console.log('disabled', invalid_fields); |
| 77 |
WPComment_Validate.show_errors(invalid_fields); |
| 78 |
}, |
| 79 |
|
| 80 |
enable_button: () => { |
| 81 |
const $form = jQuery('form.comment-form'); |
| 82 |
$form |
| 83 |
.find( 'input[type="submit"]' ) |
| 84 |
.prop('disabled', false); |
| 85 |
|
| 86 |
//hide error |
| 87 |
jQuery('#wpcomment-error-container').html(''); |
| 88 |
}, |
| 89 |
|
| 90 |
show_errors: (invalid_fields) => { |
| 91 |
// console.log(invalid_fields); |
| 92 |
const $container = jQuery('#wpcomment-error-container').html(''); |
| 93 |
const $ul_container = jQuery('<ul/>').addClass('woocommerce-error').appendTo($container); |
| 94 |
invalid_fields.map(f => $ul_container.append( `<li>${WPComment_Validate.get_message(f)}</li>`) ); |
| 95 |
}, |
| 96 |
|
| 97 |
get_message(field_meta){ |
| 98 |
|
| 99 |
return field_meta.error_message !== "" ? `<b>${field_meta.title}</b> ${field_meta.error_message}` : `<b>${field_meta.title}</b> ${wpcomment_input_vars.validate_msg}`; |
| 100 |
}, |
| 101 |
|
| 102 |
field_has_valid_data(field) { |
| 103 |
|
| 104 |
// console.log(field); |
| 105 |
const data_name = field.data_name; |
| 106 |
const wpcomment_type = field.type === 'imageselect' ? 'imageselect' : WPComment_Validate.get_input_dom_type(data_name); |
| 107 |
let element_value = ''; |
| 108 |
|
| 109 |
switch (wpcomment_type) { |
| 110 |
case 'switcher': |
| 111 |
case 'radio': |
| 112 |
element_value = jQuery(`.wpcomment-input[data-data_name="${data_name}"]:checked`).length; |
| 113 |
return element_value !== 0; |
| 114 |
break; |
| 115 |
case 'palettes': |
| 116 |
case 'checkbox': |
| 117 |
element_value = jQuery('input[name="wpcomment[fields][' + data_name + '][]"]:checked').length; |
| 118 |
if( (field.min_checked && element_value < Number(field.min_checked) ) || |
| 119 |
(field.max_checked && element_value > Number(field.max_checked) ) |
| 120 |
){ |
| 121 |
// console.log('no ok'); |
| 122 |
return false; |
| 123 |
}else{ |
| 124 |
return element_value !== 0; |
| 125 |
} |
| 126 |
break; |
| 127 |
case 'image': |
| 128 |
element_value = jQuery(`.wpcomment-input[data-data_name="${data_name}"]:checked`).length; |
| 129 |
if( (field.min_checked && element_value < Number(field.min_checked) ) || |
| 130 |
(field.max_checked && element_value > Number(field.max_checked) ) |
| 131 |
){ |
| 132 |
// console.log('no ok'); |
| 133 |
return false; |
| 134 |
}else{ |
| 135 |
return element_value !== 0; |
| 136 |
} |
| 137 |
break; |
| 138 |
case 'imageselect': |
| 139 |
element_value = jQuery(`.wpcomment-input[data-data_name="${data_name}"]:checked`).length; |
| 140 |
// element_value = 0; |
| 141 |
return element_value !== 0; |
| 142 |
break; |
| 143 |
case 'fixedprice': |
| 144 |
var render_type = jQuery(`.wpcomment-input-${data_name}`).attr('data-input'); |
| 145 |
if( render_type == 'radio' ){ |
| 146 |
element_value = jQuery(`.wpcomment-input[data-data_name="${data_name}"]:checked`).length; |
| 147 |
}else{ |
| 148 |
element_value = jQuery(`.wpcomment-input[data-data_name="${data_name}"]`).val().length; |
| 149 |
} |
| 150 |
return element_value !== 0; |
| 151 |
break; |
| 152 |
|
| 153 |
default: |
| 154 |
element_value = jQuery(`.wpcomment-input[data-data_name="${data_name}"]`).val() != null ? jQuery(`.wpcomment-input[data-data_name="${data_name}"]`).val().length : 0; |
| 155 |
return element_value !== 0; |
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
}, |
| 160 |
|
| 161 |
|
| 162 |
text_events(e) { |
| 163 |
console.log(e.target) |
| 164 |
}, |
| 165 |
|
| 166 |
get_input_dom_type(data_name) { |
| 167 |
|
| 168 |
// const field_obj = jQuery(`input[name="wpcomment[fields][${data_name}]"], input[name="wpcomment[fields][${data_name}[]]"], select[name="wpcomment[fields][${data_name}]"]`); |
| 169 |
const field_obj = jQuery(`.wpcomment-input[data-data_name="${data_name}"]`); |
| 170 |
const wpcomment_type = field_obj.closest('.wpcomment-field-wrapper').data('type'); |
| 171 |
return wpcomment_type; |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
WPComment_Validate.init(); |