| 1 |
( function ( $ ) { |
| 2 |
/** |
| 3 |
* Checkout Custom Field Validations |
| 4 |
* This will collect all the present fields in the woocommerce form and adds an class if the field |
| 5 |
* is blank |
| 6 |
*/ |
| 7 |
const wcf_custom_field_validation = function () { |
| 8 |
const custom_field_add_class = function ( |
| 9 |
field_value, |
| 10 |
field_row, |
| 11 |
field_wrap, |
| 12 |
field_type |
| 13 |
) { |
| 14 |
if ( |
| 15 |
field_value === '' || |
| 16 |
( 'select' === field_type && field_value === ' ' ) |
| 17 |
) { |
| 18 |
if ( field_row.hasClass( 'validate-required' ) ) { |
| 19 |
field_wrap.addClass( 'field-required' ); |
| 20 |
} |
| 21 |
} else { |
| 22 |
field_wrap.removeClass( 'field-required' ); |
| 23 |
field_row.find( '.wcf-field-required-error' ).remove(); |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
const numberFieldValidation = function ( |
| 28 |
field_value, |
| 29 |
field_row, |
| 30 |
field_wrap, |
| 31 |
minValue, |
| 32 |
maxValue |
| 33 |
) { |
| 34 |
if ( |
| 35 |
field_value === '' || |
| 36 |
field_value < minValue || |
| 37 |
field_value > maxValue |
| 38 |
) { |
| 39 |
field_wrap.addClass( 'field-required' ); |
| 40 |
field_wrap.after( |
| 41 |
'<span class="wcf-field-required-error">' + |
| 42 |
cartflows.field_validation_msgs.number_field + |
| 43 |
minValue + |
| 44 |
' & ' + |
| 45 |
maxValue + |
| 46 |
'</span>' |
| 47 |
); |
| 48 |
} else { |
| 49 |
field_wrap.removeClass( 'field-required' ); |
| 50 |
field_row.find( '.wcf-field-required-error' ).remove(); |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
const fields_wrapper = $( |
| 55 |
'form.woocommerce-checkout #customer_details' |
| 56 |
), |
| 57 |
$all_fields = fields_wrapper.find( 'input, textarea' ), |
| 58 |
$selects = fields_wrapper.find( 'select' ); |
| 59 |
|
| 60 |
$all_fields.on( 'blur', function () { |
| 61 |
const $this = $( this ), |
| 62 |
field_type = $this.attr( 'type' ), |
| 63 |
field_row = $this.closest( 'p.form-row' ), |
| 64 |
field_value = $this.val(); |
| 65 |
|
| 66 |
custom_field_add_class( field_value, field_row, $this, field_type ); |
| 67 |
if ( 'number' === field_type ) { |
| 68 |
const minValue = $this.attr( 'min' ); |
| 69 |
const maxValue = $this.attr( 'max' ); |
| 70 |
numberFieldValidation( |
| 71 |
Number( field_value ), |
| 72 |
field_row, |
| 73 |
$this, |
| 74 |
Number( minValue ), |
| 75 |
Number( maxValue ) |
| 76 |
); |
| 77 |
} |
| 78 |
} ); |
| 79 |
|
| 80 |
$selects.on( 'blur', function () { |
| 81 |
const $this = $( this ), |
| 82 |
field_row = $this.closest( 'p.form-row' ), |
| 83 |
field_type = 'select', |
| 84 |
field_wrap = field_row.find( '.select2-container--default' ), |
| 85 |
field_value = field_row.find( 'select' ).val(); |
| 86 |
|
| 87 |
custom_field_add_class( |
| 88 |
field_value, |
| 89 |
field_row, |
| 90 |
field_wrap, |
| 91 |
field_type |
| 92 |
); |
| 93 |
} ); |
| 94 |
}; |
| 95 |
|
| 96 |
const wcf_check_is_local_storage = function () { |
| 97 |
const test = 'test'; |
| 98 |
try { |
| 99 |
localStorage.setItem( test, test ); |
| 100 |
localStorage.removeItem( test ); |
| 101 |
return true; |
| 102 |
} catch ( e ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
const wcf_persistent_data = function () { |
| 108 |
if ( 'yes' !== cartflows.allow_persistence ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if ( false === wcf_check_is_local_storage() ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
const checkout_cust_form = |
| 117 |
'form.woocommerce-checkout #customer_details'; |
| 118 |
|
| 119 |
const wcf_form_data = { |
| 120 |
set() { |
| 121 |
const checkout_data = []; |
| 122 |
const checkout_form = $( |
| 123 |
'form.woocommerce-checkout #customer_details' |
| 124 |
); |
| 125 |
|
| 126 |
localStorage.removeItem( 'cartflows_checkout_form' ); |
| 127 |
|
| 128 |
checkout_form |
| 129 |
.find( |
| 130 |
'input[type=text], select, input[type=email], input[type=tel]' |
| 131 |
) |
| 132 |
.each( function () { |
| 133 |
checkout_data.push( { |
| 134 |
name: this.name, |
| 135 |
value: this.value, |
| 136 |
} ); |
| 137 |
} ); |
| 138 |
|
| 139 |
// Fix the dutch address order. |
| 140 |
fixDutchAddressOrder(); |
| 141 |
|
| 142 |
cartflows_checkout_form = JSON.stringify( checkout_data ); |
| 143 |
localStorage.setItem( |
| 144 |
'cartflows_checkout_form', |
| 145 |
cartflows_checkout_form |
| 146 |
); |
| 147 |
}, |
| 148 |
get() { |
| 149 |
if ( |
| 150 |
localStorage.getItem( 'cartflows_checkout_form' ) !== null |
| 151 |
) { |
| 152 |
checkout_data = JSON.parse( |
| 153 |
localStorage.getItem( 'cartflows_checkout_form' ) |
| 154 |
); |
| 155 |
|
| 156 |
for ( let i = 0; i < checkout_data.length; i++ ) { |
| 157 |
if ( |
| 158 |
$( |
| 159 |
'form.woocommerce-checkout [name=' + |
| 160 |
checkout_data[ i ].name + |
| 161 |
']' |
| 162 |
).hasClass( 'select2-hidden-accessible' ) |
| 163 |
) { |
| 164 |
$( |
| 165 |
'form.woocommerce-checkout [name=' + |
| 166 |
checkout_data[ i ].name + |
| 167 |
']' |
| 168 |
).selectWoo( 'val', [ checkout_data[ i ].value ] ); |
| 169 |
} else { |
| 170 |
$( |
| 171 |
'form.woocommerce-checkout [name=' + |
| 172 |
checkout_data[ i ].name + |
| 173 |
']' |
| 174 |
).val( checkout_data[ i ].value ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
}, |
| 179 |
}; |
| 180 |
|
| 181 |
const fixDutchAddressOrder = function () { |
| 182 |
const billing_country = $( '#billing_country' ).val(); |
| 183 |
const billing_address = $( '#billing_address_1' ).val(); |
| 184 |
|
| 185 |
if ( billing_country === 'NL' && billing_address ) { |
| 186 |
const match_billing = |
| 187 |
billing_address.match( /^(\d+\S*)\s+(.+)$/ ); |
| 188 |
if ( match_billing ) { |
| 189 |
$( '#billing_address_1' ).val( |
| 190 |
match_billing[ 2 ] + ' ' + match_billing[ 1 ] |
| 191 |
); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
const shipping_country = $( '#shipping_country' ).val(); |
| 196 |
const shipping_address = $( '#shipping_address_1' ).val(); |
| 197 |
|
| 198 |
if ( shipping_country === 'NL' && shipping_address ) { |
| 199 |
const match_shipping = |
| 200 |
shipping_address.match( /^(\d+\S*)\s+(.+)$/ ); |
| 201 |
if ( match_shipping ) { |
| 202 |
$( '#shipping_address_1' ).val( |
| 203 |
match_shipping[ 2 ] + ' ' + match_shipping[ 1 ] |
| 204 |
); |
| 205 |
} |
| 206 |
} |
| 207 |
}; |
| 208 |
|
| 209 |
wcf_form_data.get(); |
| 210 |
|
| 211 |
$( |
| 212 |
checkout_cust_form + ' input, ' + checkout_cust_form + ' select' |
| 213 |
).on( 'change', function () { |
| 214 |
wcf_form_data.set(); |
| 215 |
} ); |
| 216 |
}; |
| 217 |
|
| 218 |
/** |
| 219 |
* Floating Labels Animation |
| 220 |
*/ |
| 221 |
const wcf_floating_labels = function () { |
| 222 |
if ( $( '.wcf-field-floating-labels' ).length < 1 ) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
const wcf_anim_field_label = function () { |
| 227 |
const wrapper = $( '.wcf-field-floating-labels' ); |
| 228 |
const $inputs = wrapper.find( 'input' ); |
| 229 |
const $select_input = wrapper.find( '.select2' ); |
| 230 |
const $textarea = wrapper.find( 'textarea' ); |
| 231 |
|
| 232 |
//Add focus class on clicked on input types |
| 233 |
$inputs.on( 'focus', function () { |
| 234 |
const $this = $( this ), |
| 235 |
field_row = $this.closest( '.form-row' ); |
| 236 |
has_class = field_row.hasClass( 'wcf-anim-label' ); |
| 237 |
field_value = $this.val(); |
| 238 |
|
| 239 |
if ( field_value === '' ) { |
| 240 |
field_row.addClass( 'wcf-anim-label' ); |
| 241 |
} |
| 242 |
} ); |
| 243 |
|
| 244 |
//Remove focus class on clicked outside/other input types |
| 245 |
$inputs.on( 'focusout', function () { |
| 246 |
const $this = $( this ), |
| 247 |
field_row = $this.closest( '.form-row' ); |
| 248 |
has_class = field_row.hasClass( 'wcf-anim-label' ); |
| 249 |
field_value = $this.val(); |
| 250 |
|
| 251 |
if ( field_value === '' ) { |
| 252 |
field_row.removeClass( 'wcf-anim-label' ); |
| 253 |
} else { |
| 254 |
field_row.addClass( 'wcf-anim-label' ); |
| 255 |
} |
| 256 |
} ); |
| 257 |
|
| 258 |
//Add focus class on clicked on Select |
| 259 |
$select_input.on( 'click', function () { |
| 260 |
const $this = $( this ), |
| 261 |
field_row = $this.closest( '.form-row' ); |
| 262 |
has_class = field_row.hasClass( 'wcf-anim-label' ); |
| 263 |
field_value = $this |
| 264 |
.find( '.select2-selection__rendered' ) |
| 265 |
.text(); |
| 266 |
|
| 267 |
if ( field_value === '' ) { |
| 268 |
field_row.addClass( 'wcf-anim-label' ); |
| 269 |
} |
| 270 |
} ); |
| 271 |
|
| 272 |
//Remove focus class on clicked outside/another Select or fields |
| 273 |
$select_input.on( 'focusout', function () { |
| 274 |
const $this = $( this ), |
| 275 |
field_row = $this.closest( '.form-row' ); |
| 276 |
has_class = field_row.hasClass( 'wcf-anim-label' ); |
| 277 |
field_value = $this |
| 278 |
.find( '.select2-selection__rendered' ) |
| 279 |
.text(); |
| 280 |
|
| 281 |
if ( field_value === '' ) { |
| 282 |
field_row.removeClass( 'wcf-anim-label' ); |
| 283 |
} else { |
| 284 |
field_row.addClass( 'wcf-anim-label' ); |
| 285 |
} |
| 286 |
} ); |
| 287 |
|
| 288 |
//Add focus class on clicked on textarea |
| 289 |
$textarea.on( 'click', function () { |
| 290 |
const $this = $( this ), |
| 291 |
field_row = $this.closest( '.form-row' ); |
| 292 |
has_class = field_row.hasClass( 'wcf-anim-label' ); |
| 293 |
field_value = $this.val(); |
| 294 |
|
| 295 |
if ( field_value === '' ) { |
| 296 |
field_row.addClass( 'wcf-anim-label' ); |
| 297 |
} |
| 298 |
} ); |
| 299 |
|
| 300 |
//Remove focus class on clicked outside/another textarea or fields |
| 301 |
$textarea.on( 'focusout', function () { |
| 302 |
const $this = $( this ), |
| 303 |
field_row = $this.closest( '.form-row' ); |
| 304 |
has_class = field_row.hasClass( 'wcf-anim-label' ); |
| 305 |
field_value = $this.val(); |
| 306 |
|
| 307 |
if ( field_value === '' ) { |
| 308 |
field_row.removeClass( 'wcf-anim-label' ); |
| 309 |
} else { |
| 310 |
field_row.addClass( 'wcf-anim-label' ); |
| 311 |
} |
| 312 |
} ); |
| 313 |
}; |
| 314 |
|
| 315 |
const wcf_anim_field_label_event = function () { |
| 316 |
const wrapper = $( '.wcf-field-floating-labels' ); |
| 317 |
|
| 318 |
//Add focus class automatically if value is present in input |
| 319 |
const $all_inputs = wrapper.find( 'input' ); |
| 320 |
|
| 321 |
$( $all_inputs ).each( function () { |
| 322 |
const $this = $( this ), |
| 323 |
field_type = $this.attr( 'type' ), |
| 324 |
form_row = $this.closest( '.form-row' ), |
| 325 |
input_elem_value = $this.val(); |
| 326 |
|
| 327 |
$this.attr( 'placeholder', '' ); |
| 328 |
|
| 329 |
_add_anim_class( input_elem_value, field_type, form_row ); |
| 330 |
} ); |
| 331 |
|
| 332 |
//Add focus class automatically if value is present in selects |
| 333 |
const $all_selects = wrapper.find( 'select' ); |
| 334 |
|
| 335 |
$( $all_selects ).each( function () { |
| 336 |
const $this = $( this ), |
| 337 |
form_row = $this.closest( '.form-row' ), |
| 338 |
field_type = 'select', |
| 339 |
input_elem_value = $this.val(); |
| 340 |
|
| 341 |
_add_anim_class( input_elem_value, field_type, form_row ); |
| 342 |
} ); |
| 343 |
|
| 344 |
// Common function to add wcf-anim-label |
| 345 |
function _add_anim_class( input_elem_value, field_type, form_row ) { |
| 346 |
if ( |
| 347 |
input_elem_value !== '' || |
| 348 |
( input_elem_value !== ' ' && 'select' === field_type ) |
| 349 |
) { |
| 350 |
form_row.addClass( 'wcf-anim-label' ); |
| 351 |
} |
| 352 |
|
| 353 |
if ( field_type === 'checkbox' ) { |
| 354 |
form_row.removeClass( 'wcf-anim-label' ); |
| 355 |
} |
| 356 |
|
| 357 |
if ( field_type === 'hidden' ) { |
| 358 |
form_row.removeClass( 'wcf-anim-label' ); |
| 359 |
form_row.addClass( 'wcf-anim-label-fix' ); |
| 360 |
} |
| 361 |
} |
| 362 |
}; |
| 363 |
|
| 364 |
wcf_anim_field_label(); |
| 365 |
wcf_anim_field_label_event(); |
| 366 |
}; |
| 367 |
|
| 368 |
$( function () { |
| 369 |
wcf_persistent_data(); |
| 370 |
|
| 371 |
wcf_custom_field_validation(); |
| 372 |
|
| 373 |
wcf_floating_labels(); |
| 374 |
} ); |
| 375 |
} )( jQuery ); |
| 376 |
|