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 // 3.26.5, added pewc_trigger_initial_check, used in pewc-repeatable.js 17 $( document ).on( 'ptuwc_opened_config_row, pewc_trigger_initial_check', function ( event, instance, active_row ) { 18 pewc_conditions.initial_check(); 19 }); 20 21 // 3.26.5 22 $( document ).on( 'pewc_attach_condition_events', function( event ){ 23 pewc_conditions.attach_events(); 24 }); 25 26 // 3.26.11 27 $( 'body' ).on( 'pewc_field_visibility_updated', function( e, field_id, action ){ 28 pewc_conditions.put_back_default( field_id ); 29 }); 30 31 if( pewc_vars.conditions_timer > 0 ) { 32 33 // 3.26.5 34 this.attach_events(); 35 36 $( 'form.cart .qty' ).on( 'change input keyup paste', this.trigger_quantity_condition_check ); 37 $( 'body' ).on( 'pewc_reset_field_condition', this.trigger_field_reset_condition_check ); 38 39 // since 3.11.9 40 $( 'body' ).on( 'show_variation', this.trigger_attribute_condition_check ); 41 $( 'body' ).on( 'hide_variation', this.trigger_attribute_condition_check ); 42 43 // 3.20.1 44 $( 'body' ).on( 'pewc_reset_fields', this.reset_fields ); 45 46 if( typeof pewc_cost_triggers !== 'undefined' && pewc_cost_triggers.length > 0 ) { 47 var cost_interval = setInterval( 48 this.trigger_cost_condition_check, 49 pewc_vars.conditions_timer 50 ); 51 } 52 } 53 54 }, 55 56 // 3.26.5, created a separate function so that we can re-attach these to repeatable groups 57 attach_events: function() { 58 $( '.pewc-field-triggers-condition' ).on( 'pewc_update_select_box', this.trigger_field_condition_check ); 59 $( '.pewc-field-triggers-condition input' ).on( 'change input keyup paste', this.trigger_field_condition_check ); 60 $( '.pewc-field-triggers-condition select' ).on( 'update change', this.trigger_field_condition_check ); 61 $( '.pewc-field-triggers-condition .pewc-calculation-value' ).on( 'calculation_field_updated', this.trigger_field_condition_check ); 62 }, 63 64 initial_check: function() { 65 66 // Check the fields 67 if( pewc_vars.conditions_timer > 0 ) { 68 69 $( '.pewc-field-triggers-condition' ).each( function() { 70 71 var field = $( this ).closest( '.pewc-item' ); 72 var parent = pewc_conditions.get_field_parent( field ); 73 var field_value = pewc_conditions.get_field_value( $( field ).attr( 'data-field-id' ), $( field ).attr( 'data-field-type' ), parent ); 74 var triggers_for = JSON.parse( $( field ).attr( 'data-triggers-for' ) ); 75 76 // 3.26.5 77 pewc_conditions.check_triggered_fields( $( field ), field_value, triggers_for, parent ); 78 79 // Iterate through each field that is conditional on the updated field 80 //for( var g in triggers_for ) { 81 // conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, parent ); 82 // var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 83 // pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 84 //} 85 86 }); 87 88 } 89 90 // Check the groups 91 $( '.pewc-condition-trigger' ).each( function() { 92 var field = $( this ); 93 var groups = JSON.parse( $( field ).attr( 'data-trigger-groups' ) ); 94 for( var g in groups ) { 95 conditions_obtain = pewc_conditions.check_group_conditions( groups[g] ); 96 var action = $( '#pewc-group-' + groups[g] ).attr( 'data-condition-action' ); 97 pewc_conditions.assign_group_classes( conditions_obtain, action, groups[g] ); 98 } 99 }); 100 101 // 3.11.9, check all groups and fields using attributes on conditions 102 pewc_conditions.trigger_attribute_condition_check(); 103 104 }, 105 106 trigger_calculation: function() { 107 108 // Possibly add a delay here to ensure calculations are made 109 var calculations = $( this ).closest( '.pewc-item' ).attr( 'data-trigger-calculations' ); 110 if( calculations ) { 111 calculations = JSON.parse( calculations ); 112 for( var c in calculations ) { 113 $( '.pewc-field-' + calculations[c] ).find( '.pewc-calculation-value' ).trigger( 'change' ); 114 } 115 } 116 117 }, 118 119 trigger_condition_check: function() { 120 121 var field = $( this ).closest( '.pewc-item' ); 122 var groups = JSON.parse( $( field ).attr( 'data-trigger-groups' ) ); 123 pewc_conditions.trigger_group_conditions( groups ); 124 125 if( pewc_vars.reset_fields == 'yes' ) { 126 pewc_conditions.reset_fields(); 127 } 128 129 }, 130 131 trigger_group_conditions: function( groups ) { 132 for( var g in groups ) { 133 conditions_obtain = pewc_conditions.check_group_conditions( groups[g] ); 134 var action = $( '#pewc-group-' + groups[g] ).attr( 'data-condition-action' ); 135 pewc_conditions.assign_group_classes( conditions_obtain, action, groups[g] ); 136 } 137 // let's do this if we need to toggle other groups or fields that are dependent on a field inside a toggled group 138 $( 'body' ).trigger( 'pewc_reset_field_condition' ); 139 }, 140 141 get_field_parent: function( field ) { 142 143 var parent = $( field ).closest( '.product' ); 144 if( $( parent ).length < 1 ) { 145 parent = $( field ).closest( '.ptuwc-product-config-row' ); 146 } 147 148 return parent; 149 150 }, 151 152 get_field_group_id: function( field ) { 153 154 var group_id = $( field ).closest( '.pewc-group-wrap' ).attr( 'data-group-id' ); 155 return group_id; 156 157 }, 158 159 trigger_field_condition_check: function() { 160 161 var field = $( this ).closest( '.pewc-item' ); 162 var parent = pewc_conditions.get_field_parent( field ); 163 164 var field_value = pewc_conditions.get_field_value( $( field ).attr( 'data-field-id' ), $( field ).attr( 'data-field-type' ), parent ); 165 var triggers_for = JSON.parse( $( field ).attr( 'data-triggers-for' ) ); 166 167 // 3.26.5 168 pewc_conditions.check_triggered_fields( $( field ), field_value, triggers_for, parent ); 169 170 // Iterate through each field that is conditional on the updated field 171 //for( var g in triggers_for ) { 172 // conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, parent ); 173 // var group = $( '.pewc-field-' + triggers_for[g] ).closest( '.pewc-group-wrap' ); 174 // var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 175 // pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 176 //} 177 178 if( pewc_vars.reset_fields == 'yes' ) { 179 pewc_conditions.reset_fields(); 180 } 181 182 }, 183 184 // Iterate through fields that have had their values reset 185 // Ensures fields with dependent conditions will also get reset correctly 186 trigger_field_reset_condition_check: function() { 187 188 // Use a timer to allow complex pages to catch up 189 var reset_timer = setTimeout( 190 function() { 191 $( '.pewc-reset' ).each( function() { 192 $( this ).removeClass( 'pewc-reset' ); 193 var field = $( this ); 194 var parent = pewc_conditions.get_field_parent( field ); 195 if ( $( field ).hasClass( 'pewc-repeatable-field' ) ) { 196 // 3.26.5, use the group wrapper as the parent because it's possible we have duplicate fields 197 parent = $( field ).closest( '.pewc-group-wrap' ); 198 } 199 var field_value = pewc_conditions.get_field_value( $( field ).attr( 'data-field-id' ), $( field ).attr( 'data-field-type' ), parent ); 200 var triggers_for = $( field ).attr( 'data-triggers-for' ); 201 if( triggers_for != undefined ) { 202 203 var triggers_for = JSON.parse( $( field ).attr( 'data-triggers-for' ) ); 204 205 // 3.26.5 206 pewc_conditions.check_triggered_fields( $( field ), field_value, triggers_for, parent ); 207 208 // Iterate through each field that is conditional on the updated field 209 //for( var g in triggers_for ) { 210 // conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, parent ); 211 // var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 212 // pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 213 //} 214 } 215 216 }); 217 }, 100 218 ); 219 220 }, 221 222 trigger_quantity_condition_check: function() { 223 224 if( typeof pewc_quantity_triggers === 'undefined' ) { 225 return; 226 } 227 228 var triggers_for = pewc_quantity_triggers; 229 230 // 3.26.5 231 pewc_conditions.check_triggered_fields( 'quantity', $( 'form.cart .quantity input.qty' ).val(), triggers_for ); 232 233 // Iterate through each field that is conditional on the updated field 234 //for( var g in triggers_for ) { 235 // var parent = pewc_conditions.get_field_parent( $( '.pewc-field-'+triggers_for[g] ) ); 236 // conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], $( 'form.cart .quantity input.qty' ).val(), parent ); 237 // var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 238 // pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 239 //} 240 241 }, 242 243 trigger_cost_condition_check: function() { 244 245 var triggers_for = pewc_cost_triggers; 246 247 // 3.26.5 248 pewc_conditions.check_triggered_fields( 'cost', 0, triggers_for ); 249 250 // Iterate through each field that is conditional on the updated field 251 // 3.21.7, added parent to the condition 252 //for( var g in triggers_for ) { 253 // var parent = pewc_conditions.get_field_parent( $( '.pewc-field-'+triggers_for[g] ) ); 254 // conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], 0, parent ); 255 // var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 256 // pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 257 //} 258 259 }, 260 261 check_group_conditions: function( group_id ) { 262 263 var conditions = JSON.parse( $( '#pewc-group-' + group_id ).attr( 'data-conditions' ) ); 264 var match = $( '#pewc-group-' + group_id ).attr( 'data-condition-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_type ) { 272 condition.field_type = $( '.' + condition.field ).attr( 'data-field-type' ); 273 } 274 275 var field = $( '.pewc-field-' + $( '.' + condition.field ).attr( 'data-field-id' ) ); 276 var parent = pewc_conditions.get_field_parent( field ); 277 var value = pewc_conditions.get_field_value( $( '.' + condition.field ).attr( 'data-field-id' ), condition.field_type, parent ); 278 if ( condition.field.substring(0, 3) == 'pa_' ) { 279 value = $( '#'+condition.field ).val(); 280 } 281 var meets_condition = this.field_meets_condition( value, condition.rule, condition.value ); 282 if( meets_condition && match =='any' ) { 283 return true; 284 } else if( ! meets_condition && match =='all' ) { 285 return false; 286 } 287 } 288 289 return is_visible; 290 291 }, 292 293 check_field_conditions: function( field_id, field_value, parent ) { 294 295 var field = $( parent ).find( '.pewc-field-' + field_id ); 296 if( $( field ).length < 1 ) { 297 return false; 298 } 299 300 var conditions = JSON.parse( $( field ).attr( 'data-field-conditions' ) ); 301 var match = $( field ).attr( 'data-field-conditions-match' ); 302 var is_visible = false; 303 if( match == 'all' ) { 304 is_visible = true; 305 } 306 var loop_parent = parent; // 3.26.5 307 308 for( var i in conditions ) { 309 var condition = conditions[i]; 310 if ( condition.field == 'cost' ) { 311 // 3.21.7 312 var field_value = $( '#pewc_total_calc_price' ).val(); 313 } else if ( condition.field == 'quantity' ) { 314 var field_value = $( 'form.cart .quantity input.qty' ).val(); 315 } else if ( condition.field.substring(0, 3) == 'pa_' ){ 316 var attribute_field = $( '#' + condition.field ); 317 var field_value = $( attribute_field ).val(); 318 if ( field_value != undefined && field_value.indexOf( ',' ) !== -1 && $( attribute_field ).is( 'input' ) ) { 319 // 3.26.11, this could be a hidden input field with a list of attributes, split into an array? 320 field_value = field_value.split( ',' ); 321 } 322 } else { 323 if ( $( parent ).find( '.pewc-field-' + $( '.' + condition.field ).attr( 'data-field-id' ) ).length < 1 ) { 324 // this field is not a sibling in this repeatable field? change parent back 325 loop_parent = pewc_conditions.get_field_parent( $( field ) ); 326 } else { 327 loop_parent = parent; 328 } 329 var field_value = this.get_field_value( $( '.' + condition.field ).attr( 'data-field-id' ), condition.field_type, loop_parent ); 330 } 331 var meets_condition = this.field_meets_condition( field_value, condition.rule, condition.value ); 332 if( meets_condition && match == 'any' ) { 333 return true; 334 } else if( ! meets_condition && match =='all' ) { 335 return false; 336 } 337 } 338 339 return is_visible; 340 341 }, 342 343 // Get the value of the specified field 344 get_field_value: function( field_id, field_type, parent ) { 345 346 if( typeof field_id == 'undefined' ) { 347 return; 348 } 349 350 // var field_wrapper = $( '.' + field_id.replace( 'field', 'group' ) ); 351 var input_fields = ['text','number','advanced-preview']; 352 353 var field = $( parent ).find( '.pewc-field-' + field_id ); 354 355 // since 3.11.5 356 if ( field.hasClass( 'pewc-hidden-field') || field.closest( '.pewc-group-wrap' ).hasClass( 'pewc-group-hidden' ) ) { 357 if ( ! field.hasClass( 'pewc-reset-me' ) ) { 358 field.addClass( 'pewc-reset-me' ); // so that we can reset 359 } 360 return ''; // field is hidden so return a blank value 361 } 362 363 if( input_fields.includes( field_type ) ) { 364 return $( field ).find( 'input' ).val(); 365 } else if( field_type == 'select' || field_type == 'select-box' ) { 366 return $( field ).find( 'select' ).val(); 367 } else if( field_type == 'checkbox_group' ) { 368 var field_value = []; 369 $( field ).find( 'input:checked' ).each( function() { 370 field_value.push( $( this ).val() ); 371 }); 372 return field_value; 373 } else if( field_type == 'products' || field_type == 'product-categories' ) { 374 var field_value = []; 375 if ( field.hasClass( 'pewc-item-products-select' ) ) { 376 return $( field ).find( 'select' ).val(); 377 } 378 else { 379 $( field ).find( 'input:checked' ).each( function() { 380 field_value.push( Number( $( this ).val() ) ); 381 }); 382 } 383 return field_value; 384 } else if( field_type == 'image_swatch' ) { 385 if( $( field ).hasClass( 'pewc-item-image-swatch-checkbox' ) ) { 386 // Array 387 var field_value = []; 388 $( field ).find( 'input:checked' ).each( function() { 389 field_value.push( $( this ).val() ); 390 }); 391 return field_value; 392 } else { 393 return $( field ).find( 'input:radio:checked' ).val(); 394 } 395 } else if( field_type == 'checkbox' ) { 396 if( $( field ).find( 'input' ).prop( 'checked' ) ) { 397 return '__checked__'; 398 } 399 return false;
400 } else if( field_type == 'radio' ) { 401 return $( field ).find( 'input:radio:checked' ).val(); 402 } else if( field_type == 'quantity' ) { 403 return $( 'form.cart .quantity input.qty' ).val(); 404 } else if( field_type == 'cost' ) { 405 return $( '#pewc_total_calc_price' ).val(); 406 } else if( field_type == 'upload' ) { 407 return $( field ).find( '.pewc-number-uploads' ).val(); 408 } else if( field_type == 'calculation' ) { 409 return $( field ).find( '.pewc-calculation-value' ).val(); 410 } 411 412 }, 413 414 field_meets_condition: function( value, rule, required_value ) { 415 if (value == undefined ) { 416 return false; 417 } else if( rule == 'is') { 418 return value == required_value; 419 } else if( rule == 'is-not' ) { 420 return value != required_value; 421 } else if( rule == 'contains' ) { 422 if ( typeof required_value === 'string' && required_value.indexOf( ',' ) !== -1 ) { 423 // 3.13.7, comma-separated product categories IDs 424 return this.csv_required_value_in_field_value( value, required_value ); 425 } else { 426 // sometimes value is an array of numbers (e.g. product IDs) so we have to parse required_value first 427 return value.includes( required_value ) || value.includes( parseFloat( required_value ) ); 428 } 429 } else if( rule == 'does-not-contain' ) { 430 if ( typeof required_value === 'string' && required_value.indexOf( ',' ) !== -1 ) { 431 // 3.13.7, comma-separated product categories IDs 432 return ! this.csv_required_value_in_field_value( value, required_value ); 433 } else { 434 return ! value.includes( required_value ) && ! value.includes( parseFloat( required_value ) ); 435 } 436 } else if ( rule == 'cost-equals' ) { 437 return parseFloat(value) == parseFloat(required_value); 438 } else if( rule == 'greater-than' || rule == 'cost-greater' ) { 439 return parseFloat(value) > parseFloat(required_value); 440 } else if( rule == 'greater-than-equals' ) { 441 return parseFloat(value) >= parseFloat(required_value); 442 } else if( rule == 'less-than' || rule == 'cost-less' ) { 443 return parseFloat(value) < parseFloat(required_value); 444 } else if( rule == 'less-than-equals' ) { 445 return parseFloat(value) <= parseFloat(required_value); 446 } 447 448 }, 449 450 // 3.13.7 451 csv_required_value_in_field_value: function( field_value, required_value ) { 452 var required_values = required_value.split( ',' ); 453 for ( var i in required_values ) { 454 if ( ! field_value.includes( required_values[i] ) && ! field_value.includes( parseFloat( required_values[i] ) ) ) { 455 return false; 456 } 457 } 458 return true; 459 }, 460 461 assign_group_classes: function( conditions_obtain, action, group_id ) { 462 463 if( conditions_obtain ) { 464 if( action == 'show' ) { 465 $( '#pewc-group-' + group_id ).removeClass( 'pewc-group-hidden' ); 466 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-group-hidden' ); 467 $( '#pewc-group-' + group_id ).removeClass( 'pewc-reset-group' ); 468 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-reset-group' ); 469 } else { 470 $( '#pewc-group-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 471 $( '#pewc-tab-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 472 } 473 } else { 474 if( action == 'show' ) { 475 $( '#pewc-group-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 476 $( '#pewc-tab-' + group_id ).addClass( 'pewc-group-hidden pewc-reset-group' ); 477 478 // $( '#pewc-group-' + group_id ).find( '.pewc-field-triggers-condition' ).each( function() { 479 // Check each field in this group, in case of conditions on the fields 480 // $( this ).find( 'input' ).trigger( 'change' ); 481 // pewc_conditions.trigger_field_condition_check_by_id( $( this ).attr( 'data-field-id' ) ); 482 // }); 483 } else { 484 $( '#pewc-group-' + group_id ).removeClass( 'pewc-group-hidden' ); 485 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-group-hidden' ); 486 $( '#pewc-group-' + group_id ).removeClass( 'pewc-reset-group' ); 487 $( '#pewc-tab-' + group_id ).removeClass( 'pewc-reset-group' ); 488 } 489 } 490 491 // moved here since 3.11.5. Let's always trigger this because sometimes another field is dependent on a just-hidden field 492 pewc_conditions.trigger_fields_within_hidden_groups( group_id ); 493 // also moved here since 3.11.5 because the replace main image function for image swatch depends on this 494 $( 'body' ).trigger( 'pewc_group_visibility_updated', [ group_id, action ] ); 495 $( 'body' ).trigger('pewc_force_update_total_js'); // added in 3.11.9 496 497 // Iterate through each field in the group to check for layered swatches 498 $( '#pewc-group-' + group_id ).find( '.pewc-item' ).each( function( layer_index, element ) { 499 pewc_conditions.hide_layered_images( $( this ), $( this ).attr( 'data-field-id' ) ); 500 // 3.26.6, trigger this in case group has condition but field does not, needed when editing options from the cart 501 $( 'body' ).trigger( 'pewc_field_visibility_updated', [ $( this ).attr('data-id'), action ] ); 502 }); 503 504 }, 505 506 trigger_fields_within_hidden_groups: function( group_id ) { 507 508 $( '#pewc-group-' + group_id ).find( '.pewc-field-triggers-condition' ).each( function() { 509 // Check each field in this group, in case of conditions on the fields 510 var field = $( '.pewc-field-' + $( this ).attr( 'data-field-id' ) ); 511 var parent = pewc_conditions.get_field_parent( field ); 512 var field_value = pewc_conditions.get_field_value( $( field ).attr( 'data-field-id' ), $( field ).attr( 'data-field-type' ), parent ); 513 var triggers_for = JSON.parse( $( field ).attr( 'data-triggers-for' ) ); 514 515 // 3.26.5 516 pewc_conditions.check_triggered_fields( $( field ), field_value, triggers_for, parent ); 517 518 // Iterate through each field that is conditional on the updated field 519 //for( var g in triggers_for ) { 520 // conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, parent ); 521 // var group = $( '.pewc-field-' + triggers_for[g] ).closest( '.pewc-group-wrap' ); 522 // var action = $( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 523 // pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], parent ); 524 //} 525 526 }); 527 528 if( pewc_vars.reset_fields == 'yes' ) { 529 // pewc_conditions.reset_fields(); 530 } 531 532 }, 533 534 assign_field_classes: function( conditions_obtain, action, field_id, parent ) { 535 536 var field = $( parent ).find( '.pewc-field-' + field_id ); 537 //$( 'body' ).trigger( 'pewc_field_visibility_updated', [ field.attr('data-id'), action ] ); // commented out on 3.21.7 538 539 if( conditions_obtain ) { 540 if( action == 'show' ) { 541 $( field ).removeClass( 'pewc-hidden-field' ); 542 $( field ).removeClass( 'pewc-reset-me' ); // 3.26.6 543 $( parent ).removeClass( 'pewc-hidden-field-' + $( field ).attr( 'data-field-id' ) ); 544 } else { 545 if( ! $( field ).hasClass( 'pewc-hidden-field' ) ) { 546 $( field ).addClass( 'pewc-hidden-field pewc-reset-me' ); 547 $( parent ).addClass( 'pewc-hidden-field-' + $( field ).attr( 'data-field-id' ) ); 548 } 549 } 550 } else { 551 if( action == 'show' ) { 552 if( $( field ).hasClass( 'pewc-item-advanced-preview' ) ) { 553 $( parent ).addClass( 'pewc-hidden-field-' + $( field ).attr( 'data-field-id' ) ); 554 } 555 // 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 556 // 3.25.3, updated this again, because the changes in 3.24.8 didn't work for some calculation fields with conditions 557 if( ! $( field).hasClass( 'pewc-reset-me' ) || ! $( field).hasClass( 'pewc-hidden-field' ) ) { 558 // avoid having duplicates 559 $( field ).removeClass( 'pewc-hidden-field' ); 560 $( field ).removeClass( 'pewc-reset-me' ); 561 // add both back 562 $( field ).addClass( 'pewc-hidden-field pewc-reset-me' ); 563 } 564 } else { 565 $( field).removeClass( 'pewc-hidden-field' ); 566 $( parent ).removeClass( 'pewc-hidden-field-' + $( field ).attr( 'data-field-id' ) ); 567 } 568 } 569 570 // Hide layered images 571 if( $( field ).hasClass( 'pewc-layered-image' ) ) { 572 pewc_conditions.hide_layered_images( field, field_id ); 573 } 574 575 // 3.21.7, moved here so that field visibility has already been updated before other plugins hook into this 576 $( 'body' ).trigger( 'pewc_field_visibility_updated', [ field.attr('data-id'), action ] ); 577 578 }, 579 580 hide_layered_images: function( field, field_id ) { 581 582 var group_id = pewc_conditions.get_field_group_id( field ); 583 var is_field_hidden = $( field ).hasClass( 'pewc-hidden-field' ); 584 var is_group_hidden = $( '.pewc-group-wrap-' + group_id ).hasClass( 'pewc-group-hidden' ); 585 586 if( is_field_hidden || is_group_hidden ) { 587 // Hide the layer too 588 $( '.pewc-layer-' + field_id ).fadeOut( 150 ); 589 } else { 590 $( '.pewc-layer-' + field_id ).fadeIn( 150 ); 591 } 592 593 }, 594 595 reset_fields: function() { 596 597 if( $( '.pewc-reset-me' ).length < 1 && $( '.pewc-reset-group' ).length < 1 ) { 598 return; 599 } 600 601 $( '.pewc-reset-me' ).each( function() { 602 603 var field = $( this ); 604 pewc_conditions.reset_field_value( field ); 605 $( field ).removeClass( 'pewc-reset-me' ).addClass( 'pewc-reset' ); 606 607 }); 608 609 $( '.pewc-reset-group' ).each( function() { 610 611 $( this ).find( '.pewc-item' ).each( function() { 612 613 var field = $( this ); 614 pewc_conditions.reset_field_value( field ); 615 616 }); 617 618 }); 619 620 }, 621 622 // 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 623 reset_field_value: function( field ) { 624 625 // Iterate through all fields with pewc-reset-me class 626 var inputs = ['date', 'name_price', 'number', 'text', 'textarea', 'advanced-preview']; 627 var checks = ['checkbox', 'checkbox_group', 'radio']; 628 var field_type = $( field ).attr( 'data-field-type' ); 629 var default_value = $( field ).attr( 'data-default-value' ); 630 $( field ).attr( 'data-field-value', default_value ); 631
632 if( inputs.includes( field_type ) ) { 633 // 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 634 if ( ( default_value || default_value == '' ) && $( field ).find( '.pewc-form-field' ).val() != default_value ) { 635 $( field ).find( '.pewc-form-field' ).val( default_value ).trigger( 'change' ); 636 } 637 } else if( field_type == 'image_swatch' ) { 638 // 3.17.2 version, removes the swatch from the main image if swatch field is hidden 639 $( field ).find( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).each(function(){ 640 if ( $(this).hasClass( 'checked' ) ) { 641 $(this).removeClass( 'checked' ); // needed for swatch fields where "allow multiple" is enabled 642 $(this).trigger( 'click' ); // this triggers the update_add_on_image() function in pewc.js 643 } 644 if ( $(this).find( 'input' ).val() == default_value ) { 645 // this field has a default value, add checked class back 646 $(this).addClass( 'checked' ); 647 $(this).trigger( 'click' ); // 3.24.8, added to trigger swatches with conditions and layers 648 } else { 649 $(this).find( 'input' ).prop( 'checked', false ); 650 } 651 }); 652 // 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 653 if ( default_value === '' && $( field ).hasClass( 'pewc-layered-image' ) ) { 654 $( '.pewc-layer-' + $( field ).attr( 'data-field-id' ) ).remove(); 655 } 656 } else if( field_type == 'products' || field_type == 'product-categories' ) { 657 if ( ( field ).hasClass( 'pewc-item-products-select' ) ) { 658 // 3.25.5 659 $( field ).find( '.pewc-form-field' ).prop( 'selectedIndex', 0 ); 660 } else { 661 $( field ).find( 'input' ).prop( 'checked', false ); 662 $( field ).find( '.pewc-form-field' ).val( 0 ); 663 $( field ).find( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).removeClass( 'checked' ); 664 } 665 // 3.13.0, put back default value 666 // 3.27.2, this is now also done in put_back_default_products(), called by put_back_default when a product is shown 667 // put_back_default_products for now only works for checkboxes and radio layout, but maybe transition the function below when the need arises? 668 if ( default_value ) { 669 // convert new value to a valid ID string first (e.g. no spaces, lower case) 670 var default_value2 = default_value.toLowerCase().replaceAll(' ', '_'); 671 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).prop( 'checked', true ); 672 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).closest( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).addClass( 'checked' ); 673 } 674 } else if( checks.includes( field_type ) ) { 675 $( field ).find( 'input' ).prop( 'checked', false ); 676 // 3.23.1 677 if ( $( field ).hasClass( 'pewc-text-swatch' ) ) { 678 $( field ).find( '.active-swatch' ).removeClass( 'active-swatch' ); 679 } 680 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 681 if ( default_value ) { 682 if ( field_type === 'checkbox' ) { 683 $( field ).find( 'input' ).prop( 'checked', true ); // 3.17.2 684 // 3.23.1 685 if ( $( field ).hasClass( 'pewc-text-swatch' ) ) { 686 $( field ).find( 'input' ).closest( '.pewc-checkbox-form-label' ).addClass( 'active-swatch' ); 687 } 688 } else { 689 // convert new value to a valid ID string first (e.g. no spaces, lower case) 690 var default_value2 = default_value.toLowerCase().replaceAll(' ', '_'); 691 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).prop( 'checked', true ); 692 // 3.23.1 693 if ( field_type === 'radio' && $( field ).hasClass( 'pewc-text-swatch' ) ) { 694 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).closest( '.pewc-radio-form-label' ).addClass( 'active-swatch' ); // 3.23.1 695 } 696 } 697 } 698 } else if ( field_type == 'select' ) { 699 if( default_value ) { 700 $( field ).find( '.pewc-form-field' ).val( default_value ); 701 } else { 702 $( field ).find( '.pewc-form-field' ).prop( 'selectedIndex', 0 ); 703 } 704 } else if( field_type == 'select-box' ) { 705 // 3.25.2, separated this from the reset process for Select fields 706 // Reselect option for Select Box 707 var select_box_id = $( field ).attr( 'data-id' ); 708 if ( $( '#' + select_box_id + '_select_box' ).find( 'ul.dd-options' ).length > 0 ) { 709 // select box ready? 710 var selected_value = $( field ).find( '.dd-selected-value' ).val(); 711 var select_option_index = 0; 712 // find the index of the current value 713 select_option_index = $( 'select#' + select_box_id + ' option[value="' + selected_value.replace( /"/g, '\\"' ) + '"]').index(); 714 if ( default_value ) { 715 if ( default_value != selected_value ) { 716 // find index of the default value 717 select_option_index = $( 'select#' + select_box_id + ' option[value="' + default_value.replace( /"/g, '\\"' ) + '"]').index();
718 $( '#' + select_box_id + '_select_box' ).ddslick( 'select', {index: select_option_index} ); 719 } 720 } else if ( select_option_index > 0 ) { 721 // reset to the first value 722 $( '#' + select_box_id + '_select_box' ).ddslick( 'select', {index: 0} ); 723 } 724 } 725 if( default_value ) { 726 $( field ).find( '.pewc-form-field' ).val( default_value ); 727 } else { 728 $( field ).find( '.pewc-form-field' ).val( '' ); 729 $( field ).attr( 'data-value', '' ); 730 } 731 732 } else if( field_type == 'calculation' ) { 733 $( field ).attr( 'data-price', 0 ).attr( 'data-field-price', 0 ); 734 var action = $( field ).find( '.pewc-action' ).val(); 735 if( pewc_vars.conditions_timer > 0 ) { 736 if( action == 'price' ) { 737 $( '#pewc_calc_set_price' ).val( 0 ); 738 $( field ).find( '.pewc-calculation-value' ).val( 0 ).trigger( 'change' ); 739 } else { 740 $( field ).find( '.pewc-calculation-value' ).val( 0 ); 741 } 742 } else { 743 // This is an older method with some performance issues 744 $( field ).find( '.pewc-calculation-value' ).val( 0 ).trigger( 'change' ); 745 if( action == 'price' ) { 746 $( '#pewc_calc_set_price' ).val( 0 ); 747 } 748 } 749 } else if ( field_type == 'color-picker' ) { 750 // 3.17.2 751 if ( default_value ) { 752 $( field ).find( '.pewc-color-picker-field' ).val( default_value ).trigger( 'change' ); 753 } else { 754 $( field ).find( '.pewc-color-picker-field' ).val( '' ).trigger( 'change' ); 755 } 756 } 757 758 // Does this trigger a group? 759 if( $( field ).attr( 'data-trigger-groups' ) ) { 760 var groups = JSON.parse( $( field ).attr( 'data-trigger-groups' ) ); 761 pewc_conditions.trigger_group_conditions( groups ); 762 } 763 764 if ( $( field ).attr( 'data-field-value') != '' && ! $( field ).hasClass( 'pewc-active-field') ) { 765 // this is added so that the summary panel can detect the field 766 $( field ).addClass( 'pewc-active-field' ); 767 } 768 // 3.12.2 769 if ( $( field ).attr( 'data-field-value') == '' && $( field ).attr( 'data-field-price' ) != 0 ) { 770 // maybe also reset price 771 $( field ).attr( 'data-field-price', 0 ); 772 } 773 // we force update_total_js so that the summary panel is also updated 774 $( 'body' ).trigger('pewc_force_update_total_js'); 775 $( 'body' ).trigger( 'pewc_reset_field_condition' ); 776 777 }, 778 779 // since 3.11.9, not sure if this is necessary if the group already has the class? 780 group_has_attribute_conditions: function( group ) { 781 782 if ( group.attr( 'data-condition-action') != '' && group.attr( 'data-conditions-match' ) != '' && group.attr( 'data-conditions' ) != '' ) { 783 var data_conditions = JSON.parse( group.attr( 'data-conditions' ) ); 784 if ( data_conditions.length > 0 ) { 785 var has_attribute_condition = false; 786 for ( var i in data_conditions ) { 787 if ( data_conditions[i].field.substring( 0, 3 ) == 'pa_' ) { 788 has_attribute_condition = true; 789 break; 790 } 791 } 792 return has_attribute_condition; 793 } 794 } 795 return false; 796 797 }, 798 799 // since 3.11.9 800 trigger_groups_with_attribute_conditions: function( event, variation, purchasable ) { 801 802 $( '.pewc-group-wrap.pewc-has-attribute-condition' ).each( function() { 803 var group = $( this ); 804 805 // check if this group has conditions 806 //if ( pewc_conditions.group_has_attribute_conditions( group ) ) { 807 // this group is dependent on attributes, check now 808 var group_id = parseFloat( group.attr( 'id' ).replace( 'pewc-group-', '' ) ); 809 conditions_obtain = pewc_conditions.check_group_conditions( group_id ); 810 var action = $( '#pewc-group-' + group_id ).attr( 'data-condition-action' ); 811 pewc_conditions.assign_group_classes( conditions_obtain, action, group_id ); 812 //} 813 }); 814 815 }, 816 817 // since 3.11.9 818 trigger_fields_with_attribute_conditions: function( event, variation, purchasable ) { 819 820 $( '.pewc-item.pewc-field-has-attribute-condition' ).each( function() { 821 822 var field = $( this ); 823 var field_id = field.attr( 'data-field-id' ); 824 var parent = pewc_conditions.get_field_parent( field ); 825 826 conditions_obtain = pewc_conditions.check_field_conditions( field_id, '', parent ); 827 var action = $( '.pewc-field-' + field_id ).attr( 'data-field-conditions-action' ); 828 829 pewc_conditions.assign_field_classes( conditions_obtain, action, field_id, parent ); 830 831 if( pewc_vars.reset_fields == 'yes' ) { 832 pewc_conditions.reset_fields(); 833 } 834 835 }); 836 837 }, 838 839 // since 3.11.9 840 trigger_attribute_condition_check: function( event, variation, purchasable ) { 841 842 pewc_conditions.trigger_groups_with_attribute_conditions( event, variation, purchasable ); 843 pewc_conditions.trigger_fields_with_attribute_conditions( event, variation, purchasable ); 844 845 }, 846 847 // 3.26.5, created a separate function for this loop 848 check_triggered_fields: function( field, field_value, triggers_for, parent=false ) { 849 850 var is_repeatable_field = false;
851 var is_cloned_field = false; 852 var group_wrapper = $( field ).closest( '.pewc-group-wrap' ); 853 854 if ( field != 'cost' && field != 'quantity' ) { 855 // was this triggered from a field inside a repeatable group? 856 is_repeatable_field = $( group_wrapper ).hasClass( 'pewc-repeatable-group' ); 857 // was this triggered from a cloned field? 858 is_cloned_field = $( group_wrapper ).hasClass( 'pewc-cloned-group' ); 859 } 860 861 // we use a different var so that it doesn't get overwritten in the loop 862 // trigger_quantity_condition_check and trigger_cost_condition_check sets the parent var inside the loop 863 var loop_parent = parent; 864 865 // Iterate through each field that is conditional on the updated field 866 for( var g in triggers_for ) { 867 if ( ! parent ) { 868 loop_parent = pewc_conditions.get_field_parent( $( '.pewc-field-'+triggers_for[g] ) ); 869 } else if ( is_repeatable_field ) { 870 // this was triggered from a repeatable field, check if the current field is a sibling 871 if ( $( group_wrapper ).find( '.pewc-field-' + triggers_for[g] ).length > 0 ) { 872 // this is a sibling, change the loop_parent to the group_wrapper so that we only look for this field within this group 873 loop_parent = group_wrapper; 874 } else { 875 // not a sibling, point back to the original parent 876 loop_parent = parent; 877 } 878 } 879 conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, loop_parent ); 880 var action = $( loop_parent ).find( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' ); 881 pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], loop_parent ); 882 } 883 884 }, 885 886 // 3.26.11, put back a field's default value if it has been displayed 887 put_back_default: function( pewc_id ) { 888 889 var field = $( '.pewc-item.' + pewc_id ); 890 if ( field.length > 0 && ! field.hasClass( 'pewc-hidden-field' ) && ! field.closest( '.pewc-group-wrap' ).hasClass( 'pewc-group-hidden' ) && field.attr( 'data-default-value' ) != undefined && field.attr( 'data-default-value' ) != '' ) { 891 var default_value = field.attr( 'data-default-value' ); 892 893 if ( 'checkbox' === field.attr( 'data-field-type' ) && 'checked' === default_value ) {
894 field.find( 'input#' + pewc_id ).prop( 'checked', true ).trigger( 'change' ); 895 } else if ( 'products' === field.attr( 'data-field-type' ) || 'product-categories' === field.attr( 'data-field-type' ) ) { 896 // 3.27.2 897 pewc_conditions.put_back_default_products( field, default_value ); 898 } 899 } 900 901 }, 902 903 // 3.27.2, we will support other layouts when the need arises 904 put_back_default_products: function( field, default_value ) { 905 906 var is_independent = field.hasClass( 'pewc-item-products-independent'); 907 var default_value2; 908 909 if ( field.hasClass( 'pewc-item-products-checkboxes' ) || field.hasClass( 'pewc-item-products-checkboxes-list' ) ) { 910 // default could be a comma-separated list 911 default_value2 = default_value.split( ',' ); 912 913 for ( var i in default_value2 ) { 914 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2[i] ).prop( 'checked', true ); 915 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2[i] ).closest( '.pewc-checkbox-wrapper' ).addClass( 'checked' ); 916 if ( is_independent ) { 917 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2[i] ).closest( '.pewc-checkbox-wrapper' ).find( '.pewc-child-quantity-field' ).val( 1 ); 918 } 919 } 920 } else if ( field.hasClass( 'pewc-item-products-radio' ) || field.hasClass( 'pewc-item-products-radio-list' ) ) { 921 default_value2 = default_value.toLowerCase().replaceAll(' ', '_'); // maybe not needed if this is an ID? 922 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).prop( 'checked', true ); 923 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).closest( '.pewc-radio-wrapper' ).addClass( 'checked' ); 924 if ( is_independent ) { 925 $( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).closest( '.pewc-item-field-wrapper' ).find( '.pewc-child-quantity-field' ).val( 1 ); 926 } 927 } 928 929 }, 930 931 } 932 933 pewc_conditions.init(); 934 935 }); 936 937})(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.