1(function($) { 2 3 $( document ).ready( function() { 4 5 var reset_fields = []; 6 7 var pewc_conditions = { 8 9 init: function() { 10 11 this.initial_check(); 12 $( '.pewc-condition-trigger input' ).on( 'change input keyup paste', this.trigger_condition_check ); 13 $( 'body' ).on( 'change', '.pewc-condition-trigger select', this.trigger_condition_check ); 14 $( '.pewc-calculation-trigger input' ).on( 'change input keyup paste', this.trigger_calculation ); 15 16 $( document ).on( 'ptuwc_opened_config_row', function ( event, instance, active_row ) { 17 pewc_conditions.initial_check(); 18 }); 19 20 if( pewc_vars.conditions_timer > 0 ) { 21 $( '.pewc-field-triggers-condition' ).on( 'pewc_update_select_box', this.trigger_field_condition_check ); 22 $( '.pewc-field-triggers-condition input' ).on( 'change input keyup paste', this.trigger_field_condition_check ); 23 $( '.pewc-field-triggers-condition select' ).on( 'update change', this.trigger_field_condition_check ); 24 $( '.pewc-field-triggers-condition .pewc-calculation-value' ).on( 'calculation_field_updated', this.trigger_field_condition_check ); 25 // $( 'body' ).on( 'pewc_update_select_box', this.trigger_field_condition_check ); 26 $( 'form.cart .qty' ).on( 'change input keyup paste', this.trigger_quantity_condition_check ); 27 $( 'body' ).on( 'pewc_reset_field_condition', this.trigger_field_reset_condition_check ); 28 29 // since 3.11.9 30 $( 'body' ).on( 'show_variation', this.trigger_attribute_condition_check ); 31 $( 'body' ).on( 'hide_variation', this.trigger_attribute_condition_check ); 32 33 // 3.20.1 34 $( 'body' ).on( 'pewc_reset_fields', this.reset_fields ); 35 36 if( typeof pewc_cost_triggers !== 'undefined' && pewc_cost_triggers.length > 0 ) { 37 var cost_interval = setInterval( 38 this.trigger_cost_condition_check, 39 pewc_vars.conditions_timer 40 ); 41 } 42 } 43 44 }, 45 46 initial_check: function() { 47 48 // Check the fields 49 if( pewc_vars.conditions_timer > 0 ) { 50 51 $( '.pewc-field-triggers-condition' ).each( function() { 52 53 var field = $( this ).closest( '.pewc-item' ); 54 var parent = pewc_conditions.get_field_parent( field ); 55 var field_value = pewc_conditions.get_field_value( $( field ).attr( 'data-field-id' ), $( field ).attr( 'data-field-type' ), parent ); 56 var triggers_for = JSON.parse( $( field ).attr( 'data-triggers-for' ) ); 57 58 // Iterate through each field that is conditional on the updated field 59 for( var g in triggers_for ) { 60 conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, parent ); 61 var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 62 pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 63 } 64 65 }); 66 67 } 68 69 // Check the groups 70 $( '.pewc-condition-trigger' ).each( function() { 71 var field = $( this ); 72 var groups = JSON.parse( $( field ).attr( 'data-trigger-groups' ) ); 73 for( var g in groups ) { 74 conditions_obtain = pewc_conditions.check_group_conditions( groups[g] ); 75 var action = $( '#pewc-group-' + groups[g] ).attr( 'data-condition-action' ); 76 pewc_conditions.assign_group_classes( conditions_obtain, action, groups[g] ); 77 } 78 }); 79 80 // 3.11.9, check all groups and fields using attributes on conditions 81 pewc_conditions.trigger_attribute_condition_check(); 82 83 }, 84 85 trigger_calculation: function() { 86 87 // Possibly add a delay here to ensure calculations are made 88 var calculations = $( this ).closest( '.pewc-item' ).attr( 'data-trigger-calculations' ); 89 if( calculations ) { 90 calculations = JSON.parse( calculations ); 91 for( var c in calculations ) { 92 $( '.pewc-field-' + calculations[c] ).find( '.pewc-calculation-value' ).trigger( 'change' ); 93 } 94 } 95 96 }, 97 98 trigger_condition_check: function() { 99 100 var field = $( this ).closest( '.pewc-item' ); 101 var groups = JSON.parse( $( field ).attr( 'data-trigger-groups' ) ); 102 pewc_conditions.trigger_group_conditions( groups ); 103 104 if( pewc_vars.reset_fields == 'yes' ) { 105 pewc_conditions.reset_fields(); 106 } 107 108 }, 109 110 trigger_group_conditions: function( groups ) { 111 for( var g in groups ) { 112 conditions_obtain = pewc_conditions.check_group_conditions( groups[g] ); 113 var action = $( '#pewc-group-' + groups[g] ).attr( 'data-condition-action' ); 114 pewc_conditions.assign_group_classes( conditions_obtain, action, groups[g] ); 115 } 116 // let's do this if we need to toggle other groups or fields that are dependent on a field inside a toggled group 117 $( 'body' ).trigger( 'pewc_reset_field_condition' ); 118 }, 119 120 get_field_parent: function( field ) { 121 122 var parent = $( field ).closest( '.product' ); 123 if( $( parent ).length < 1 ) { 124 parent = $( field ).closest( '.ptuwc-product-config-row' ); 125 } 126 127 return parent; 128 129 }, 130 131 get_field_group_id: function( field ) { 132 133 var group_id = $( field ).closest( '.pewc-group-wrap' ).attr( 'data-group-id' ); 134 return group_id; 135 136 }, 137 138 trigger_field_condition_check: function() { 139 140 var field = $( this ).closest( '.pewc-item' ); 141 var parent = pewc_conditions.get_field_parent( field ); 142 143 var field_value = pewc_conditions.get_field_value( $( field ).attr( 'data-field-id' ), $( field ).attr( 'data-field-type' ), parent ); 144 var triggers_for = JSON.parse( $( field ).attr( 'data-triggers-for' ) ); 145 146 // Iterate through each field that is conditional on the updated field
147 for( var g in triggers_for ) { 148 conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, parent ); 149 var group = $( '.pewc-field-' + triggers_for[g] ).closest( '.pewc-group-wrap' ); 150 var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 151 // if( $( group ).hasClass( 'pewc-group-hidden' ) ) { 152 // // Ensure that any fields in a hidden group trigger their conditions 153 // conditions_obtain = false; 154 // action = 'show'; 155 // } 156 pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 157 } 158 159 if( pewc_vars.reset_fields == 'yes' ) { 160 pewc_conditions.reset_fields(); 161 } 162 163 }, 164 165 // Iterate through fields that have had their values reset 166 // Ensures fields with dependent conditions will also get reset correctly 167 trigger_field_reset_condition_check: function() { 168 169 // Use a timer to allow complex pages to catch up 170 var reset_timer = setTimeout( 171 function() { 172 $( '.pewc-reset' ).each( function() { 173 $( this ).removeClass( 'pewc-reset' ); 174 var field = $( this ); 175 var parent = pewc_conditions.get_field_parent( field ); 176 var field_value = pewc_conditions.get_field_value( $( field ).attr( 'data-field-id' ), $( field ).attr( 'data-field-type' ), parent ); 177 var triggers_for = $( field ).attr( 'data-triggers-for' ); 178 if( triggers_for != undefined ) { 179 180 var triggers_for = JSON.parse( $( field ).attr( 'data-triggers-for' ) ); 181 // Iterate through each field that is conditional on the updated field 182 for( var g in triggers_for ) { 183 conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, parent ); 184 var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 185 pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 186 } 187 } 188 189 }); 190 }, 100 191 ); 192 193 }, 194 195 trigger_quantity_condition_check: function() { 196 197 if( typeof pewc_quantity_triggers === 'undefined' ) { 198 return; 199 } 200 201 var triggers_for = pewc_quantity_triggers; 202 // Iterate through each field that is conditional on the updated field 203 for( var g in triggers_for ) { 204 var parent = pewc_conditions.get_field_parent( $( '.pewc-field-'+triggers_for[g] ) ); 205 conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], $( 'form.cart .quantity input.qty' ).val(), parent ); 206 var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 207 pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 208 } 209 210 }, 211 212 trigger_cost_condition_check: function() { 213 214 var triggers_for = pewc_cost_triggers; 215 // Iterate through each field that is conditional on the updated field 216 // 3.21.7, added parent to the condition 217 for( var g in triggers_for ) { 218 var parent = pewc_conditions.get_field_parent( $( '.pewc-field-'+triggers_for[g] ) ); 219 conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], 0, parent ); 220 var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 221 pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 222 } 223 224 }, 225 226 check_group_conditions: function( group_id ) { 227 228 var conditions = JSON.parse( $( '#pewc-group-' + group_id ).attr( 'data-conditions' ) ); 229 var match = $( '#pewc-group-' + group_id ).attr( 'data-condition-match' ); 230 var is_visible = false; 231 if( match == 'all' ) { 232 is_visible = true; 233 } 234 for( var i in conditions ) { 235 var condition = conditions[i]; 236 if( ! condition.field_type ) { 237 condition.field_type = $( '.' + condition.field ).attr( 'data-field-type' ); 238 } 239 240 var field = $( '.pewc-field-' + $( '.' + condition.field ).attr( 'data-field-id' ) ); 241 var parent = pewc_conditions.get_field_parent( field ); 242 var value = pewc_conditions.get_field_value( $( '.' + condition.field ).attr( 'data-field-id' ), condition.field_type, parent ); 243 if ( condition.field.substring(0, 3) == 'pa_' ) { 244 value = $( '#'+condition.field ).val(); 245 } 246 var meets_condition = this.field_meets_condition( value, condition.rule, condition.value ); 247 if( meets_condition && match =='any' ) { 248 return true; 249 } else if( ! meets_condition && match =='all' ) { 250 return false;
251 } 252 } 253 254 return is_visible; 255 256 }, 257 258 check_field_conditions: function( field_id, field_value, parent ) { 259 260 var field = $( parent ).find( '.pewc-field-' + field_id ); 261 if( $( field ).length < 1 ) { 262 return false; 263 } 264 265 var conditions = JSON.parse( $( field ).attr( 'data-field-conditions' ) ); 266 var match = $( field ).attr( 'data-field-conditions-match' ); 267 var is_visible = false; 268 if( match == 'all' ) { 269 is_visible = true; 270 } 271 for( var i in conditions ) { 272 var condition = conditions[i]; 273 console.log( condition ); 274 if ( condition.field == 'cost' ) { 275 // 3.21.7 276 var field_value = $( '#pewc_total_calc_price' ).val(); 277 } else if ( condition.field == 'quantity' ) { 278 var field_value = $( 'form.cart .quantity input.qty' ).val(); 279 } else if ( condition.field.substring(0, 3) == 'pa_' ){ 280 var field_value = $( '#'+condition.field ).val(); 281 } else { 282 var field_value = this.get_field_value( $( '.' + condition.field ).attr( 'data-field-id' ), condition.field_type, parent ); 283 } 284 var meets_condition = this.field_meets_condition( field_value, condition.rule, condition.value ); 285 if( meets_condition && match == 'any' ) { 286 return true; 287 } else if( ! meets_condition && match =='all' ) { 288 return false; 289 } 290 } 291 292 return is_visible; 293 294 }, 295 296 // Get the value of the specified field 297 get_field_value: function( field_id, field_type, parent ) { 298 299 if( typeof field_id == 'undefined' ) { 300 return; 301 } 302 303 // var field_wrapper = $( '.' + field_id.replace( 'field', 'group' ) ); 304 var input_fields = ['text','number','advanced-preview']; 305 306 var field = $( parent ).find( '.pewc-field-' + field_id ); 307 308 // since 3.11.5 309 if ( field.hasClass( 'pewc-hidden-field') || field.closest( '.pewc-group-wrap' ).hasClass( 'pewc-group-hidden' ) ) { 310 if ( ! field.hasClass( 'pewc-reset-me' ) ) { 311 field.addClass( 'pewc-reset-me' ); // so that we can reset 312 } 313 return ''; // field is hidden so return a blank value 314 } 315 316 if( input_fields.includes( field_type ) ) { 317 return $( field ).find( 'input' ).val(); 318 } else if( field_type == 'select' || field_type == 'select-box' ) { 319 return $( field ).find( 'select' ).val(); 320 } else if( field_type == 'checkbox_group' ) { 321 var field_value = []; 322 $( field ).find( 'input:checked' ).each( function() { 323 field_value.push( $( this ).val() ); 324 }); 325 return field_value; 326 } else if( field_type == 'products' || field_type == 'product-categories' ) { 327 var field_value = []; 328 if ( field.hasClass( 'pewc-item-products-select' ) ) { 329 return $( field ).find( 'select' ).val(); 330 } 331 else { 332 $( field ).find( 'input:checked' ).each( function() { 333 field_value.push( Number( $( this ).val() ) ); 334 }); 335 } 336 return field_value; 337 } else if( field_type == 'image_swatch' ) { 338 if( $( field ).hasClass( 'pewc-item-image-swatch-checkbox' ) ) { 339 // Array 340 var field_value = []; 341 $( field ).find( 'input:checked' ).each( function() { 342 field_value.push( $( this ).val() ); 343 }); 344 return field_value; 345 } else { 346 return $( field ).find( 'input:radio:checked' ).val(); 347 } 348 } else if( field_type == 'checkbox' ) { 349 if( $( field ).find( 'input' ).prop( 'checked' ) ) { 350 return '__checked__'; 351 } 352 return false; 353 } else if( field_type == 'radio' ) { 354 return $( field ).find( 'input:radio:checked' ).val(); 355 } else if( field_type == 'quantity' ) { 356 return $( 'form.cart .quantity input.qty' ).val(); 357 } else if( field_type == 'cost' ) { 358 return $( '#pewc_total_calc_price' ).val(); 359 } else if( field_type == 'upload' ) { 360 return $( field ).find( '.pewc-number-uploads' ).val(); 361 } else if( field_type == 'calculation' ) { 362 return $( field ).find( '.pewc-calculation-value' ).val(); 363 } 364 365 }, 366 367 field_meets_condition: function( value, rule, required_value ) { 368 console.log( value, rule, required_value ); 369 if (value == undefined ) { 370 return false;
371 } else if( rule == 'is') { 372 return value == required_value; 373 } else if( rule == 'is-not' ) { 374 return value != required_value; 375 } else if( rule == 'contains' ) { 376 if ( typeof required_value === 'string' && required_value.indexOf( ',' ) !== -1 ) { 377 // 3.13.7, comma-separated product categories IDs 378 return this.csv_required_value_in_field_value( value, required_value ); 379 } else { 380 // sometimes value is an array of numbers (e.g. product IDs) so we have to parse required_value first 381 return value.includes( required_value ) || value.includes( parseFloat( required_value ) ); 382 } 383 } else if( rule == 'does-not-contain' ) { 384 if ( typeof required_value === 'string' && required_value.indexOf( ',' ) !== -1 ) { 385 // 3.13.7, comma-separated product categories IDs 386 return ! this.csv_required_value_in_field_value( value, required_value ); 387 } else { 388 return ! value.includes( required_value ) && ! value.includes( parseFloat( required_value ) ); 389 } 390 } else if ( rule == 'cost-equals' ) { 391 return parseFloat(value) == parseFloat(required_value); 392 } else if( rule == 'greater-than' || rule == 'cost-greater' ) { 393 return parseFloat(value) > parseFloat(required_value); 394 } else if( rule == 'greater-than-equals' ) { 395 return parseFloat(value) >= parseFloat(required_value); 396 } else if( rule == 'less-than' || rule == 'cost-less' ) { 397 return parseFloat(value) < parseFloat(required_value); 398 } else if( rule == 'less-than-equals' ) { 399 console.log( 'return', parseFloat(value), parseFloat(required_value), parseFloat(value) <= parseFloat(required_value) ); 400 return parseFloat(value) <= parseFloat(required_value); 401 } 402 403 }, 404 405 // 3.13.7 406 csv_required_value_in_field_value: function( field_value, required_value ) { 407 var required_values = required_value.split( ',' ); 408 for ( var i in required_values ) { 409 if ( ! field_value.includes( required_values[i] ) && ! field_value.includes( parseFloat( required_values[i] ) ) ) { 410 return false; 411 } 412 } 413 return true; 414 }, 415 416 assign_group_classes: function( conditions_obtain, action, group_id ) { 417 418 if( conditions_obtain ) { 419 if( action == 'show' ) { 420 $( '#pewc-group-' + group_id ).removeClass( 'pewc-group-hidden' ); 421 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-group-hidden' ); 422 $( '#pewc-group-' + group_id ).removeClass( 'pewc-reset-group' ); 423 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-reset-group' ); 424 } else { 425 $( '#pewc-group-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 426 $( '#pewc-tab-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 427 } 428 } else { 429 if( action == 'show' ) { 430 $( '#pewc-group-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 431 $( '#pewc-tab-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 432 433 // $( '#pewc-group-' + group_id ).find( '.pewc-field-triggers-condition' ).each( function() { 434 // Check each field in this group, in case of conditions on the fields 435 // $( this ).find( 'input' ).trigger( 'change' ); 436 // pewc_conditions.trigger_field_condition_check_by_id( $( this ).attr( 'data-field-id' ) ); 437 // }); 438 } else { 439 $( '#pewc-group-' + group_id ).removeClass( 'pewc-group-hidden' ); 440 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-group-hidden' ); 441 $( '#pewc-group-' + group_id ).removeClass( 'pewc-reset-group' ); 442 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-reset-group' ); 443 } 444 } 445 446 // moved here since 3.11.5. Let's always trigger this because sometimes another field is dependent on a just-hidden field 447 pewc_conditions.trigger_fields_within_hidden_groups( group_id ); 448 // also moved here since 3.11.5 because the replace main image function for image swatch depends on this 449 $( 'body' ).trigger( 'pewc_group_visibility_updated', [ group_id, action ] ); 450 $( 'body' ).trigger('pewc_force_update_total_js'); // added in 3.11.9 451 452 // Iterate through each field in the group to check for layered swatches 453 $( '#pewc-group-' + group_id ).find( '.pewc-item' ).each( function( layer_index, element ) { 454 pewc_conditions.hide_layered_images( $( this ), $( this ).attr( 'data-field-id' ) ); 455 }); 456 457 }, 458 459 trigger_fields_within_hidden_groups: function( group_id ) { 460 461 $( '#pewc-group-' + group_id ).find( '.pewc-field-triggers-condition' ).each( function() { 462 // Check each field in this group, in case of conditions on the fields 463 var field = $( '.pewc-field-' + $( this ).attr( 'data-field-id' ) ); 464 var parent = pewc_conditions.get_field_parent( field ); 465 var field_value = pewc_conditions.get_field_value( $( field ).attr( 'data-field-id' ), $( field ).attr( 'data-field-type' ), parent ); 466 var triggers_for = JSON.parse( $( field ).attr( 'data-triggers-for' ) ); 467 468 // Iterate through each field that is conditional on the updated field 469 for( var g in triggers_for ) { 470 conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, parent ); 471 var group = $( '.pewc-field-' + triggers_for[g] ).closest( '.pewc-group-wrap' ); 472 var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 473 // if( $( group ).hasClass( 'pewc-group-hidden' ) ) { 474 // // Ensure that any fields in a hidden group trigger their conditions 475 // conditions_obtain = false;
476 // action = 'show'; 477 // } 478 pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 479 } 480 481 }); 482 483 if( pewc_vars.reset_fields == 'yes' ) { 484 // pewc_conditions.reset_fields(); 485 } 486 487 }, 488 489 assign_field_classes: function( conditions_obtain, action, field_id, parent ) { 490 491 var field = $( parent ).find( '.pewc-field-' + field_id ); 492 //$( 'body' ).trigger( 'pewc_field_visibility_updated', [ field.attr('data-id'), action ] ); // commented out on 3.21.7 493 494 if( conditions_obtain ) { 495 if( action == 'show' ) { 496 $( field ).removeClass( 'pewc-hidden-field' ); 497 $( parent ).removeClass( 'pewc-hidden-field-' + $( '.pewc-field-' + field_id ).attr( 'data-field-id' ) ); 498 } else { 499 if( ! $( '.pewc-field-' + field_id ).hasClass( 'pewc-hidden-field' ) ) { 500 $( field ).addClass( 'pewc-hidden-field pewc-reset-me' ); 501 $( parent ).addClass( 'pewc-hidden-field-' + $( '.pewc-field-' + field_id ).attr( 'data-field-id' ) ); 502 } 503 } 504 } else { 505 if( action == 'show' ) { 506 if( $( field).hasClass( 'pewc-item-advanced-preview' ) ) { 507 $( parent ).addClass( 'pewc-hidden-field-' + $( '.pewc-field-' + field_id ).attr( 'data-field-id' ) ); 508 } 509 // 3.24.8, changed hasClass selector from pewc-hidden-field to pewc-reset-me, because sometimes a field already has pewc-hidden-field and so pewc-reset-me is not added 510 if( ! $( field).hasClass( 'pewc-reset-me' ) ) { 511 $( field ).removeClass( 'pewc-hidden-field' ); // avoid adding this twice? 512 $( field ).addClass( 'pewc-hidden-field pewc-reset-me' ); 513 } 514 } else { 515 $( field).removeClass( 'pewc-hidden-field' ); 516 $( parent ).removeClass( 'pewc-hidden-field-' + $( '.pewc-field-' + field_id ).attr( 'data-field-id' ) ); 517 } 518 } 519 520 // Hide layered images 521 if( $( field ).hasClass( 'pewc-layered-image' ) ) { 522 pewc_conditions.hide_layered_images( field, field_id ); 523 } 524 525 // 3.21.7, moved here so that field visibility has already been updated before other plugins hook into this 526 $( 'body' ).trigger( 'pewc_field_visibility_updated', [ field.attr('data-id'), action ] ); 527 528 }, 529 530 hide_layered_images: function( field, field_id ) { 531 532 var group_id = pewc_conditions.get_field_group_id( field ); 533 var is_field_hidden = $( field ).hasClass( 'pewc-hidden-field' ); 534 var is_group_hidden = $( '.pewc-group-wrap-' + group_id ).hasClass( 'pewc-group-hidden' ); 535 536 if( is_field_hidden || is_group_hidden ) { 537 // Hide the layer too 538 $( '.pewc-layer-' + field_id ).fadeOut( 150 ); 539 } else { 540 $( '.pewc-layer-' + field_id ).fadeIn( 150 ); 541 } 542 543 }, 544 545 reset_fields: function() { 546 547 if( $( '.pewc-reset-me' ).length < 1 && $( '.pewc-reset-group' ).length < 1 ) { 548 return; 549 } 550 551 $( '.pewc-reset-me' ).each( function() { 552 553 var field = $( this ); 554 pewc_conditions.reset_field_value( field ); 555 $( field ).removeClass( 'pewc-reset-me' ).addClass( 'pewc-reset' ); 556 557 }); 558 559 $( '.pewc-reset-group' ).each( function() { 560 561 $( this ).find( '.pewc-item' ).each( function() { 562 563 var field = $( this ); 564 pewc_conditions.reset_field_value( field ); 565 566 }); 567 568 }); 569 570 }, 571 572 // improvement: maybe don't apply default_values here, which can cause issues in the backend if field is hidden, and another field is dependent on the hidden field 573 reset_field_value: function( field ) { 574 575 // Iterate through all fields with pewc-reset-me class 576 var inputs = ['date', 'name_price', 'number', 'text', 'textarea', 'advanced-preview']; 577 var checks = ['checkbox', 'checkbox_group', 'radio']; 578 var field_type = $( field ).attr( 'data-field-type' ); 579 var default_value = $( field ).attr( 'data-default-value' ); 580 $( field ).attr( 'data-field-value', default_value ); 581 582 if( inputs.includes( field_type ) ) { 583 // 3.21.1, the trigger is needed by Text Preview, but could cause an infinite loop, so we check if the default value has already been added 584 if ( ( default_value || default_value == '' ) && $( field ).find( '.pewc-form-field' ).val() != default_value ) { 585 $( field ).find( '.pewc-form-field' ).val( default_value ).trigger( 'change' ); 586 } 587 } else if( field_type == 'image_swatch' ) { 588 // 3.17.2 version, removes the swatch from the main image if swatch field is hidden 589 $( field ).find( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).each(function(){ 590 if ( $(this).hasClass( 'checked' ) ) { 591 $(this).removeClass( 'checked' ); // needed for swatch fields where "allow multiple" is enabled 592 $(this).trigger( 'click' ); // this triggers the update_add_on_image() function in pewc.js 593 } 594 if ( $(this).find( 'input' ).val() == default_value ) { 595 // this field has a default value, add checked class back 596 $(this).addClass( 'checked' ); 597 $(this).trigger( 'click' ); // 3.24.8, added to trigger swatches with conditions and layers 598 } else { 599 $(this).find( 'input' ).prop( 'checked', false ); 600 } 601 }); 602 // 3.24.6, maybe remove the layer if we're resetting anyway? So that when the field is shown again, the layer doesn't contain the last selection 603 if ( default_value === '' && $( field ).hasClass( 'pewc-layered-image' ) ) { 604 $( '.pewc-layer-' + $( field ).attr( 'data-field-id' ) ).remove(); 605 } 606 } else if( field_type == 'products' || field_type == 'product-categories' ) { 607 $( field ).find( 'input' ).prop( 'checked', false ); 608 $( field ).find( '.pewc-form-field' ).val( 0 ); 609 $( field ).find( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).removeClass( 'checked' ); 610 // 3.13.0, put back default value 611 if ( default_value ) { 612 // convert new value to a valid ID string first (e.g. no spaces, lower case) 613 var default_value2 = default_value.toLowerCase().replaceAll(' ', '_'); 614 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).prop( 'checked', true ); 615 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).closest( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).addClass( 'checked' ); 616 } 617 } else if( checks.includes( field_type ) ) { 618 $( field ).find( 'input' ).prop( 'checked', false ); 619 // 3.23.1 620 if ( $( field ).hasClass( 'pewc-text-swatch' ) ) { 621 $( field ).find( '.active-swatch' ).removeClass( 'active-swatch' ); 622 } 623 default_value = $( field ).hasClass( 'pewc-hidden-field' ) ? '' : $( field ).attr( 'data-default-value' ); // 3.21.7, if checkbox is hidden, nullify the value, so that conditions based on this does not match in the backend 624 if ( default_value ) { 625 if ( field_type === 'checkbox' ) { 626 $( field ).find( 'input' ).prop( 'checked', true ); // 3.17.2 627 // 3.23.1 628 if ( $( field ).hasClass( 'pewc-text-swatch' ) ) { 629 $( field ).find( 'input' ).closest( '.pewc-checkbox-form-label' ).addClass( 'active-swatch' ); 630 } 631 } else { 632 // convert new value to a valid ID string first (e.g. no spaces, lower case) 633 var default_value2 = default_value.toLowerCase().replaceAll(' ', '_'); 634 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).prop( 'checked', true ); 635 // 3.23.1 636 if ( field_type === 'radio' && $( field ).hasClass( 'pewc-text-swatch' ) ) { 637 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).closest( '.pewc-radio-form-label' ).addClass( 'active-swatch' ); // 3.23.1 638 } 639 } 640 } 641 } else if( field_type == 'select' || field_type == 'select-box' ) { 642 if( default_value ) { 643 $( field ).find( '.pewc-form-field' ).val( default_value ); 644 } else { 645 //$( field ).find( '.pewc-form-field' ).prop( 'selectedIndex', 0 ); // commented out in 3.21.7, so that conditions based on hidden select-box fields don't get matched in the backend 646 $( field ).find( '.pewc-form-field' ).val( '' ); 647 } 648 } else if( field_type == 'calculation' ) {
649 $( field ).attr( 'data-price', 0 ).attr( 'data-field-price', 0 ); 650 var action = $( field ).find( '.pewc-action' ).val(); 651 if( pewc_vars.conditions_timer > 0 ) { 652 if( action == 'price' ) { 653 $( '#pewc_calc_set_price' ).val( 0 ); 654 $( field ).find( '.pewc-calculation-value' ).val( 0 ).trigger( 'change' ); 655 } else { 656 $( field ).find( '.pewc-calculation-value' ).val( 0 ); 657 } 658 } else { 659 // This is an older method with some performance issues 660 $( field ).find( '.pewc-calculation-value' ).val( 0 ).trigger( 'change' ); 661 if( action == 'price' ) { 662 $( '#pewc_calc_set_price' ).val( 0 ); 663 } 664 } 665 } else if ( field_type == 'color-picker' ) { 666 // 3.17.2 667 if ( default_value ) { 668 $( field ).find( '.pewc-color-picker-field' ).val( default_value ).trigger( 'change' ); 669 } else { 670 $( field ).find( '.pewc-color-picker-field' ).val( '' ).trigger( 'change' ); 671 } 672 } 673 674 // Does this trigger a group? 675 if( $( field ).attr( 'data-trigger-groups' ) ) { 676 var groups = JSON.parse( $( field ).attr( 'data-trigger-groups' ) ); 677 pewc_conditions.trigger_group_conditions( groups ); 678 } 679 680 if ( $( field ).attr( 'data-field-value') != '' && ! $( field ).hasClass( 'pewc-active-field') ) { 681 // this is added so that the summary panel can detect the field 682 $( field ).addClass( 'pewc-active-field' ); 683 } 684 // 3.12.2 685 if ( $( field ).attr( 'data-field-value') == '' && $( field ).attr( 'data-field-price' ) != 0 ) { 686 // maybe also reset price 687 $( field ).attr( 'data-field-price', 0 ); 688 } 689 // we force update_total_js so that the summary panel is also updated 690 $( 'body' ).trigger('pewc_force_update_total_js'); 691 $( 'body' ).trigger( 'pewc_reset_field_condition' ); 692 693 }, 694 695 // since 3.11.9, not sure if this is necessary if the group already has the class? 696 group_has_attribute_conditions: function( group ) { 697 698 if ( group.attr( 'data-condition-action') != '' && group.attr( 'data-conditions-match' ) != '' && group.attr( 'data-conditions' ) != '' ) { 699 var data_conditions = JSON.parse( group.attr( 'data-conditions' ) ); 700 if ( data_conditions.length > 0 ) { 701 var has_attribute_condition = false; 702 for ( var i in data_conditions ) { 703 if ( data_conditions[i].field.substring( 0, 3 ) == 'pa_' ) { 704 has_attribute_condition = true; 705 break; 706 } 707 } 708 return has_attribute_condition; 709 } 710 } 711 return false; 712 713 }, 714 715 // since 3.11.9 716 trigger_groups_with_attribute_conditions: function( event, variation, purchasable ) { 717 718 $( '.pewc-group-wrap.pewc-has-attribute-condition' ).each( function() { 719 var group = $( this ); 720 721 // check if this group has conditions 722 //if ( pewc_conditions.group_has_attribute_conditions( group ) ) { 723 // this group is dependent on attributes, check now 724 var group_id = parseFloat( group.attr( 'id' ).replace( 'pewc-group-', '' ) ); 725 conditions_obtain = pewc_conditions.check_group_conditions( group_id ); 726 var action = $( '#pewc-group-' + group_id ).attr( 'data-condition-action' ); 727 pewc_conditions.assign_group_classes( conditions_obtain, action, group_id ); 728 //} 729 }); 730 731 }, 732 733 // since 3.11.9 734 trigger_fields_with_attribute_conditions: function( event, variation, purchasable ) { 735 736 $( '.pewc-item.pewc-field-has-attribute-condition' ).each( function() { 737 738 var field = $( this ); 739 var field_id = field.attr( 'data-field-id' ); 740 var parent = pewc_conditions.get_field_parent( field ); 741 742 conditions_obtain = pewc_conditions.check_field_conditions( field_id, '', parent ); 743 var action = $( '.pewc-field-' + field_id ).attr( 'data-field-conditions-action' ); 744 745 pewc_conditions.assign_field_classes( conditions_obtain, action, field_id, parent ); 746 747 if( pewc_vars.reset_fields == 'yes' ) { 748 pewc_conditions.reset_fields(); 749 } 750 751 }); 752 753 }, 754 755 // since 3.11.9 756 trigger_attribute_condition_check: function( event, variation, purchasable ) { 757 758 pewc_conditions.trigger_groups_with_attribute_conditions( event, variation, purchasable ); 759 pewc_conditions.trigger_fields_with_attribute_conditions( event, variation, purchasable ); 760 761 } 762 } 763 764 pewc_conditions.init(); 765 766 }); 767 768})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.