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 for( var g in triggers_for ) { 217 conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g] ); 218 var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 219 pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g] ); 220 } 221 222 }, 223 224 check_group_conditions: function( group_id ) { 225 226 var conditions = JSON.parse( $( '#pewc-group-' + group_id ).attr( 'data-conditions' ) ); 227 var match = $( '#pewc-group-' + group_id ).attr( 'data-condition-match' ); 228 var is_visible = false; 229 if( match == 'all' ) { 230 is_visible = true; 231 } 232 for( var i in conditions ) { 233 var condition = conditions[i]; 234 if( ! condition.field_type ) { 235 condition.field_type = $( '.' + condition.field ).attr( 'data-field-type' ); 236 } 237 238 var field = $( '.pewc-field-' + $( '.' + condition.field ).attr( 'data-field-id' ) ); 239 var parent = pewc_conditions.get_field_parent( field ); 240 var value = pewc_conditions.get_field_value( $( '.' + condition.field ).attr( 'data-field-id' ), condition.field_type, parent ); 241 if ( condition.field.substring(0, 3) == 'pa_' ) { 242 value = $( '#'+condition.field ).val(); 243 } 244 var meets_condition = this.field_meets_condition( value, condition.rule, condition.value ); 245 if( meets_condition && match =='any' ) { 246 return true; 247 } else if( ! meets_condition && match =='all' ) { 248 return false;
249 } 250 } 251 252 return is_visible; 253 254 }, 255 256 check_field_conditions: function( field_id, field_value, parent ) { 257 258 var field = $( parent ).find( '.pewc-field-' + field_id ); 259 if( $( field ).length < 1 ) { 260 return false; 261 } 262 263 var conditions = JSON.parse( $( field ).attr( 'data-field-conditions' ) ); 264 var match = $( field ).attr( 'data-field-conditions-match' ); 265 var is_visible = false; 266 if( match == 'all' ) { 267 is_visible = true; 268 } 269 for( var i in conditions ) { 270 var condition = conditions[i]; 271 if ( condition.field == 'quantity' ) { 272 var field_value = $( 'form.cart .quantity input.qty' ).val(); 273 } else if ( condition.field.substring(0, 3) == 'pa_' ){ 274 var field_value = $( '#'+condition.field ).val(); 275 } else { 276 var field_value = this.get_field_value( $( '.' + condition.field ).attr( 'data-field-id' ), condition.field_type, parent ); 277 } 278 var meets_condition = this.field_meets_condition( field_value, condition.rule, condition.value ); 279 if( meets_condition && match == 'any' ) { 280 return true; 281 } else if( ! meets_condition && match =='all' ) { 282 return false; 283 } 284 } 285 286 return is_visible; 287 288 }, 289 290 // Get the value of the specified field 291 get_field_value: function( field_id, field_type, parent ) { 292 293 if( typeof field_id == 'undefined' ) { 294 return; 295 } 296 297 // var field_wrapper = $( '.' + field_id.replace( 'field', 'group' ) ); 298 var input_fields = ['text','number','advanced-preview']; 299 300 var field = $( parent ).find( '.pewc-field-' + field_id ); 301 302 // since 3.11.5 303 if ( field.hasClass( 'pewc-hidden-field') || field.closest( '.pewc-group-wrap' ).hasClass( 'pewc-group-hidden' ) ) { 304 if ( ! field.hasClass( 'pewc-reset-me' ) ) { 305 field.addClass( 'pewc-reset-me' ); // so that we can reset 306 } 307 return ''; // field is hidden so return a blank value 308 } 309 310 if( input_fields.includes( field_type ) ) { 311 return $( field ).find( 'input' ).val(); 312 } else if( field_type == 'select' || field_type == 'select-box' ) { 313 return $( field ).find( 'select' ).val(); 314 } else if( field_type == 'checkbox_group' ) { 315 var field_value = []; 316 $( field ).find( 'input:checked' ).each( function() { 317 field_value.push( $( this ).val() ); 318 }); 319 return field_value; 320 } else if( field_type == 'products' || field_type == 'product-categories' ) { 321 var field_value = []; 322 if ( field.hasClass( 'pewc-item-products-select' ) ) { 323 return $( field ).find( 'select' ).val(); 324 } 325 else { 326 $( field ).find( 'input:checked' ).each( function() { 327 field_value.push( Number( $( this ).val() ) ); 328 }); 329 } 330 return field_value; 331 } else if( field_type == 'image_swatch' ) { 332 if( $( field ).hasClass( 'pewc-item-image-swatch-checkbox' ) ) { 333 // Array 334 var field_value = []; 335 $( field ).find( 'input:checked' ).each( function() { 336 field_value.push( $( this ).val() ); 337 }); 338 return field_value; 339 } else { 340 return $( field ).find( 'input:radio:checked' ).val(); 341 } 342 } else if( field_type == 'checkbox' ) { 343 if( $( field ).find( 'input' ).prop( 'checked' ) ) { 344 return '__checked__'; 345 } 346 return false; 347 } else if( field_type == 'radio' ) { 348 return $( field ).find( 'input:radio:checked' ).val(); 349 } else if( field_type == 'quantity' ) { 350 return $( 'form.cart .quantity input.qty' ).val(); 351 } else if( field_type == 'cost' ) { 352 return $( '#pewc_total_calc_price' ).val(); 353 } else if( field_type == 'upload' ) { 354 return $( field ).find( '.pewc-number-uploads' ).val(); 355 } else if( field_type == 'calculation' ) { 356 return $( field ).find( '.pewc-calculation-value' ).val(); 357 } 358 359 }, 360 361 field_meets_condition: function( value, rule, required_value ) { 362 363 if (value == undefined ) { 364 return false; 365 } else if( rule == 'is') { 366 return value == required_value; 367 } else if( rule == 'is-not' ) { 368 return value != required_value; 369 } else if( rule == 'contains' ) { 370 if ( typeof required_value === 'string' && required_value.indexOf( ',' ) !== -1 ) { 371 // 3.13.7, comma-separated product categories IDs 372 return this.csv_required_value_in_field_value( value, required_value ); 373 } else { 374 // sometimes value is an array of numbers (e.g. product IDs) so we have to parse required_value first 375 return value.includes( required_value ) || value.includes( parseFloat( required_value ) ); 376 } 377 } else if( rule == 'does-not-contain' ) { 378 if ( typeof required_value === 'string' && required_value.indexOf( ',' ) !== -1 ) { 379 // 3.13.7, comma-separated product categories IDs 380 return ! this.csv_required_value_in_field_value( value, required_value ); 381 } else { 382 return ! value.includes( required_value ) && ! value.includes( parseFloat( required_value ) ); 383 } 384 } else if ( rule == 'cost-equals' ) { 385 return parseFloat(value) == parseFloat(required_value); 386 } else if( rule == 'greater-than' || rule == 'cost-greater' ) { 387 return parseFloat(value) > parseFloat(required_value); 388 } else if( rule == 'greater-than-equals' ) { 389 return parseFloat(value) >= parseFloat(required_value); 390 } else if( rule == 'less-than' || rule == 'cost-less' ) { 391 return parseFloat(value) < parseFloat(required_value); 392 } else if( rule == 'less-than-equals' ) { 393 return parseFloat(value) <= parseFloat(required_value); 394 } 395 396 }, 397 398 // 3.13.7 399 csv_required_value_in_field_value: function( field_value, required_value ) { 400 var required_values = required_value.split( ',' ); 401 for ( var i in required_values ) { 402 if ( ! field_value.includes( required_values[i] ) && ! field_value.includes( parseFloat( required_values[i] ) ) ) { 403 return false;
404 } 405 } 406 return true; 407 }, 408 409 assign_group_classes: function( conditions_obtain, action, group_id ) { 410 411 if( conditions_obtain ) { 412 if( action == 'show' ) { 413 $( '#pewc-group-' + group_id ).removeClass( 'pewc-group-hidden' ); 414 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-group-hidden' ); 415 $( '#pewc-group-' + group_id ).removeClass( 'pewc-reset-group' ); 416 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-reset-group' ); 417 } else { 418 $( '#pewc-group-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 419 $( '#pewc-tab-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 420 } 421 } else { 422 if( action == 'show' ) { 423 $( '#pewc-group-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 424 $( '#pewc-tab-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 425 426 // $( '#pewc-group-' + group_id ).find( '.pewc-field-triggers-condition' ).each( function() { 427 // Check each field in this group, in case of conditions on the fields 428 // $( this ).find( 'input' ).trigger( 'change' ); 429 // pewc_conditions.trigger_field_condition_check_by_id( $( this ).attr( 'data-field-id' ) ); 430 // }); 431 } else { 432 $( '#pewc-group-' + group_id ).removeClass( 'pewc-group-hidden' ); 433 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-group-hidden' ); 434 $( '#pewc-group-' + group_id ).removeClass( 'pewc-reset-group' ); 435 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-reset-group' ); 436 } 437 } 438 439 // moved here since 3.11.5. Let's always trigger this because sometimes another field is dependent on a just-hidden field 440 pewc_conditions.trigger_fields_within_hidden_groups( group_id ); 441 // also moved here since 3.11.5 because the replace main image function for image swatch depends on this 442 $( 'body' ).trigger( 'pewc_group_visibility_updated', [ group_id, action ] ); 443 $( 'body' ).trigger('pewc_force_update_total_js'); // added in 3.11.9 444 445 // Iterate through each field in the group to check for layered swatches 446 $( '#pewc-group-' + group_id ).find( '.pewc-item' ).each( function( layer_index, element ) { 447 pewc_conditions.hide_layered_images( $( this ), $( this ).attr( 'data-field-id' ) ); 448 }); 449 450 }, 451 452 trigger_fields_within_hidden_groups: function( group_id ) { 453 454 $( '#pewc-group-' + group_id ).find( '.pewc-field-triggers-condition' ).each( function() { 455 // Check each field in this group, in case of conditions on the fields 456 var field = $( '.pewc-field-' + $( this ).attr( 'data-field-id' ) ); 457 var parent = pewc_conditions.get_field_parent( field ); 458 var field_value = pewc_conditions.get_field_value( $( field ).attr( 'data-field-id' ), $( field ).attr( 'data-field-type' ), parent ); 459 var triggers_for = JSON.parse( $( field ).attr( 'data-triggers-for' ) ); 460 461 // Iterate through each field that is conditional on the updated field 462 for( var g in triggers_for ) { 463 conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, parent ); 464 var group = $( '.pewc-field-' + triggers_for[g] ).closest( '.pewc-group-wrap' ); 465 var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 466 // if( $( group ).hasClass( 'pewc-group-hidden' ) ) { 467 // // Ensure that any fields in a hidden group trigger their conditions 468 // conditions_obtain = false; 469 // action = 'show'; 470 // } 471 pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 472 } 473 474 }); 475 476 if( pewc_vars.reset_fields == 'yes' ) { 477 // pewc_conditions.reset_fields(); 478 } 479 480 }, 481 482 assign_field_classes: function( conditions_obtain, action, field_id, parent ) { 483 484 var field = $( parent ).find( '.pewc-field-' + field_id ); 485 $( 'body' ).trigger( 'pewc_field_visibility_updated', [ field.attr('data-id'), action ] ); 486 487 if( conditions_obtain ) { 488 if( action == 'show' ) { 489 $( field ).removeClass( 'pewc-hidden-field' ); 490 $( parent ).removeClass( 'pewc-hidden-field-' + $( '.pewc-field-' + field_id ).attr( 'data-field-id' ) ); 491 } else { 492 if( ! $( '.pewc-field-' + field_id ).hasClass( 'pewc-hidden-field' ) ) { 493 $( field ).addClass( 'pewc-hidden-field pewc-reset-me' ); 494 $( parent ).addClass( 'pewc-hidden-field-' + $( '.pewc-field-' + field_id ).attr( 'data-field-id' ) ); 495 } 496 } 497 } else { 498 if( action == 'show' ) { 499 if( $( field).hasClass( 'pewc-item-advanced-preview' ) ) { 500 $( parent ).addClass( 'pewc-hidden-field-' + $( '.pewc-field-' + field_id ).attr( 'data-field-id' ) ); 501 } 502 if( ! $( field).hasClass( 'pewc-hidden-field' ) ) { 503 $( field ).addClass( 'pewc-hidden-field pewc-reset-me' ); 504 } 505 } else { 506 $( field).removeClass( 'pewc-hidden-field' ); 507 $( parent ).removeClass( 'pewc-hidden-field-' + $( '.pewc-field-' + field_id ).attr( 'data-field-id' ) ); 508 } 509 } 510 511 // Hide layered images 512 if( $( field ).hasClass( 'pewc-layered-image' ) ) { 513 pewc_conditions.hide_layered_images( field, field_id ); 514 } 515 516 }, 517 518 hide_layered_images: function( field, field_id ) { 519 520 var group_id = pewc_conditions.get_field_group_id( field ); 521 var is_field_hidden = $( field ).hasClass( 'pewc-hidden-field' ); 522 var is_group_hidden = $( '.pewc-group-wrap-' + group_id ).hasClass( 'pewc-group-hidden' ); 523 524 if( is_field_hidden || is_group_hidden ) { 525 // Hide the layer too 526 $( '.pewc-layer-' + field_id ).fadeOut( 150 ); 527 } else { 528 $( '.pewc-layer-' + field_id ).fadeIn( 150 ); 529 } 530 531 }, 532 533 reset_fields: function() { 534 535 if( $( '.pewc-reset-me' ).length < 1 && $( '.pewc-reset-group' ).length < 1 ) { 536 return; 537 } 538 539 $( '.pewc-reset-me' ).each( function() { 540 541 var field = $( this ); 542 pewc_conditions.reset_field_value( field ); 543 $( field ).removeClass( 'pewc-reset-me' ).addClass( 'pewc-reset' ); 544 545 }); 546 547 $( '.pewc-reset-group' ).each( function() { 548 549 $( this ).find( '.pewc-item' ).each( function() { 550 551 var field = $( this ); 552 pewc_conditions.reset_field_value( field ); 553 554 }); 555 556 }); 557 558 }, 559 560 reset_field_value: function( field ) { 561 562 // Iterate through all fields with pewc-reset-me class 563 var inputs = ['date', 'name_price', 'number', 'text', 'textarea', 'advanced-preview']; 564 var checks = ['checkbox', 'checkbox_group', 'radio']; 565 var field_type = $( field ).attr( 'data-field-type' ); 566 var default_value = $( field ).attr( 'data-default-value' ); 567 $( field ).attr( 'data-field-value', default_value ); 568
569 if( inputs.includes( field_type ) ) { 570 // 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 571 if ( ( default_value || default_value == '' ) && $( field ).find( '.pewc-form-field' ).val() != default_value ) { 572 $( field ).find( '.pewc-form-field' ).val( default_value ).trigger( 'change' ); 573 } 574 } else if( field_type == 'image_swatch' ) { 575 // 3.17.2 version, removes the swatch from the main image if swatch field is hidden 576 $( field ).find( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).each(function(){ 577 if ( $(this).hasClass( 'checked' ) ) { 578 $(this).removeClass( 'checked' ); // needed for swatch fields where "allow multiple" is enabled 579 $(this).trigger( 'click' ); // this triggers the update_add_on_image() function in pewc.js 580 } 581 if ( $(this).find( 'input' ).val() == default_value ) { 582 // this field has a default value, add checked class back 583 $(this).addClass( 'checked' ); 584 } else { 585 $(this).find( 'input' ).prop( 'checked', false ); 586 } 587 }); 588 } else if( field_type == 'products' ) { 589 $( field ).find( 'input' ).prop( 'checked', false ); 590 $( field ).find( '.pewc-form-field' ).val( '' ); 591 $( field ).find( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).removeClass( 'checked' ); 592 // 3.13.0, put back default value 593 if ( default_value ) { 594 // convert new value to a valid ID string first (e.g. no spaces, lower case) 595 var default_value2 = default_value.toLowerCase().replaceAll(' ', '_'); 596 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).prop( 'checked', true ); 597 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).closest( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).addClass( 'checked' ); 598 } 599 } else if( checks.includes( field_type ) ) { 600 $( field ).find( 'input' ).prop( 'checked', false ); 601 if ( default_value ) { 602 if ( field_type === 'checkbox' ) { 603 $( field ).find( 'input' ).prop( 'checked', true ); // 3.17.2 604 } else { 605 // convert new value to a valid ID string first (e.g. no spaces, lower case) 606 var default_value2 = default_value.toLowerCase().replaceAll(' ', '_'); 607 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).prop( 'checked', true ); 608 } 609 } 610 } else if( field_type == 'select' ) { 611 if( default_value ) { 612 $( field ).find( '.pewc-form-field' ).val( default_value ); 613 } else { 614 $( field ).find( '.pewc-form-field' ).prop( 'selectedIndex', 0 ); 615 } 616 } else if( field_type == 'calculation' ) { 617 $( field ).attr( 'data-price', 0 ).attr( 'data-field-price', 0 ); 618 var action = $( field ).find( '.pewc-action' ).val(); 619 if( pewc_vars.conditions_timer > 0 ) { 620 if( action == 'price' ) { 621 $( '#pewc_calc_set_price' ).val( 0 ); 622 $( field ).find( '.pewc-calculation-value' ).val( 0 ).trigger( 'change' ); 623 } else { 624 $( field ).find( '.pewc-calculation-value' ).val( 0 ); 625 } 626 } else { 627 // This is an older method with some performance issues 628 $( field ).find( '.pewc-calculation-value' ).val( 0 ).trigger( 'change' ); 629 if( action == 'price' ) { 630 $( '#pewc_calc_set_price' ).val( 0 ); 631 } 632 } 633 } else if ( field_type == 'color-picker' ) { 634 // 3.17.2 635 if ( default_value ) { 636 $( field ).find( '.pewc-color-picker-field' ).val( default_value ).trigger( 'change' ); 637 } else { 638 $( field ).find( '.pewc-color-picker-field' ).val( '' ).trigger( 'change' ); 639 } 640 } 641 642 // Does this trigger a group? 643 if( $( field ).attr( 'data-trigger-groups' ) ) { 644 var groups = JSON.parse( $( field ).attr( 'data-trigger-groups' ) ); 645 pewc_conditions.trigger_group_conditions( groups ); 646 } 647 648 if ( $( field ).attr( 'data-field-value') != '' && ! $( field ).hasClass( 'pewc-active-field') ) { 649 // this is added so that the summary panel can detect the field 650 $( field ).addClass( 'pewc-active-field' ); 651 } 652 // 3.12.2 653 if ( $( field ).attr( 'data-field-value') == '' && $( field ).attr( 'data-field-price' ) != 0 ) { 654 // maybe also reset price 655 $( field ).attr( 'data-field-price', 0 ); 656 } 657 // we force update_total_js so that the summary panel is also updated 658 $( 'body' ).trigger('pewc_force_update_total_js'); 659 $( 'body' ).trigger( 'pewc_reset_field_condition' ); 660 661 }, 662 663 // since 3.11.9, not sure if this is necessary if the group already has the class? 664 group_has_attribute_conditions: function( group ) { 665 666 if ( group.attr( 'data-condition-action') != '' && group.attr( 'data-conditions-match' ) != '' && group.attr( 'data-conditions' ) != '' ) { 667 var data_conditions = JSON.parse( group.attr( 'data-conditions' ) ); 668 if ( data_conditions.length > 0 ) { 669 var has_attribute_condition = false;
670 for ( var i in data_conditions ) { 671 if ( data_conditions[i].field.substring( 0, 3 ) == 'pa_' ) { 672 has_attribute_condition = true; 673 break; 674 } 675 } 676 return has_attribute_condition; 677 } 678 } 679 return false; 680 681 }, 682 683 // since 3.11.9 684 trigger_groups_with_attribute_conditions: function( event, variation, purchasable ) { 685 686 $( '.pewc-group-wrap.pewc-has-attribute-condition' ).each( function() { 687 var group = $( this ); 688 689 // check if this group has conditions 690 //if ( pewc_conditions.group_has_attribute_conditions( group ) ) { 691 // this group is dependent on attributes, check now 692 var group_id = parseFloat( group.attr( 'id' ).replace( 'pewc-group-', '' ) ); 693 conditions_obtain = pewc_conditions.check_group_conditions( group_id ); 694 var action = $( '#pewc-group-' + group_id ).attr( 'data-condition-action' ); 695 pewc_conditions.assign_group_classes( conditions_obtain, action, group_id ); 696 //} 697 }); 698 699 }, 700 701 // since 3.11.9 702 trigger_fields_with_attribute_conditions: function( event, variation, purchasable ) { 703 704 $( '.pewc-item.pewc-field-has-attribute-condition' ).each( function() { 705 706 var field = $( this ); 707 var field_id = field.attr( 'data-field-id' ); 708 var parent = pewc_conditions.get_field_parent( field ); 709 710 conditions_obtain = pewc_conditions.check_field_conditions( field_id, '', parent ); 711 var action = $( '.pewc-field-' + field_id ).attr( 'data-field-conditions-action' ); 712 713 pewc_conditions.assign_field_classes( conditions_obtain, action, field_id, parent ); 714 715 if( pewc_vars.reset_fields == 'yes' ) { 716 pewc_conditions.reset_fields(); 717 } 718 719 }); 720 721 }, 722 723 // since 3.11.9 724 trigger_attribute_condition_check: function( event, variation, purchasable ) { 725 726 pewc_conditions.trigger_groups_with_attribute_conditions( event, variation, purchasable ); 727 pewc_conditions.trigger_fields_with_attribute_conditions( event, variation, purchasable ); 728 729 } 730 } 731 732 pewc_conditions.init(); 733 734 }); 735 736})(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.