PageSourceSearch

https://www.juvaly.com.tw/wp-content/plugins/product-extras-for-wo…/assets/js/conditions.js?ver=3.26.15

js juvaly.com.tw collected 2026-10-10 00:09:37 UTC 37,819 bytes, 903 lines download raw bytes

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					if ( default_value ) {
667						// convert new value to a valid ID string first (e.g. no spaces, lower case)
668						var default_value2 = default_value.toLowerCase().replaceAll(' ', '_');
669						$( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).prop( 'checked', true );
670						$( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).closest( '.pewc-radio-image-wrapper, .pewc-checkbox-image-wrapper' ).addClass( 'checked' );
671					}
672				} else if( checks.includes( field_type ) ) {
673					$( field ).find( 'input' ).prop( 'checked', false );
674					// 3.23.1
675					if ( $( field ).hasClass( 'pewc-text-swatch' ) ) {
676						$( field ).find( '.active-swatch' ).removeClass( 'active-swatch' );
677					}
678					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
679					if ( default_value ) {
680						if ( field_type === 'checkbox' ) {
681							$( field ).find( 'input' ).prop( 'checked', true ); // 3.17.2
682							// 3.23.1
683							if ( $( field ).hasClass( 'pewc-text-swatch' ) ) {
684								$( field ).find( 'input' ).closest( '.pewc-checkbox-form-label' ).addClass( 'active-swatch' );
685							}
686						} else {
687							// convert new value to a valid ID string first (e.g. no spaces, lower case)
688							var default_value2 = default_value.toLowerCase().replaceAll(' ', '_');
689							$( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).prop( 'checked', true );
690							// 3.23.1
691							if ( field_type === 'radio' && $( field ).hasClass( 'pewc-text-swatch' ) ) {
692								$( '#' + $( field ).attr( 'data-id' ) + '_' + default_value2 ).closest( '.pewc-radio-form-label' ).addClass( 'active-swatch' ); // 3.23.1
693							}
694						}
695					}
696				} else if ( field_type == 'select' ) {
697					if( default_value ) {
698						$( field ).find( '.pewc-form-field' ).val( default_value );
699					} else {
700						$( field ).find( '.pewc-form-field' ).prop( 'selectedIndex', 0 );
701					}
702				} else if( field_type == 'select-box' ) {
703					// 3.25.2, separated this from the reset process for Select fields
704					// Reselect option for Select Box
705					var select_box_id = $( field ).attr( 'data-id' );
706					if ( $( '#' + select_box_id + '_select_box' ).find( 'ul.dd-options' ).length > 0 ) {
707						// select box ready?
708						var selected_value = $( field ).find( '.dd-selected-value' ).val();
709						var select_option_index = 0;
710						// find the index of the current value
711						select_option_index = $( 'select#' + select_box_id + ' option[value="' + selected_value.replace( /"/g, '\\"' ) + '"]').index();
712						if ( default_value ) {
713							if ( default_value != selected_value ) {
714								// find index of the default value
715								select_option_index = $( 'select#' + select_box_id + ' option[value="' + default_value.replace( /"/g, '\\"' ) + '"]').index();
716								$( '#' + select_box_id + '_select_box' ).ddslick( 'select', {index: select_option_index} );
717							}
718						} else if ( select_option_index > 0 ) {
719							// reset to the first value
720							$( '#' + select_box_id + '_select_box' ).ddslick( 'select', {index: 0} );
721						}
722					}
723					if( default_value ) {
724						$( field ).find( '.pewc-form-field' ).val( default_value );
725					} else {
726						$( field ).find( '.pewc-form-field' ).val( '' );
727						$( field ).attr( 'data-value', '' );
728					}
729					
730				} else if( field_type == 'calculation' ) {
731					$( field ).attr( 'data-price', 0 ).attr( 'data-field-price', 0 );
732					var action = $( field ).find( '.pewc-action' ).val();
733					if( pewc_vars.conditions_timer > 0 ) {
734						if( action == 'price' ) {
735							$( '#pewc_calc_set_price' ).val( 0 );
736							$( field ).find( '.pewc-calculation-value' ).val( 0 ).trigger( 'change' );
737						} else {
738							$( field ).find( '.pewc-calculation-value' ).val( 0 );
739						}
740					} else {
741						// This is an older method with some performance issues
742						$( field ).find( '.pewc-calculation-value' ).val( 0 ).trigger( 'change' );
743						if( action == 'price' ) {
744							$( '#pewc_calc_set_price' ).val( 0 );
745						}
746					}
747				} else if ( field_type == 'color-picker' ) {
748					// 3.17.2
749					if ( default_value ) {
750						$( field ).find( '.pewc-color-picker-field' ).val( default_value ).trigger( 'change' );
751					} else {
752						$( field ).find( '.pewc-color-picker-field' ).val( '' ).trigger( 'change' );
753					}
754				}
755
756				// Does this trigger a group?
757				if( $( field ).attr( 'data-trigger-groups' ) ) {
758					var groups = JSON.parse( $( field ).attr( 'data-trigger-groups' ) );
759					pewc_conditions.trigger_group_conditions( groups );
760				}
761
762				if ( $( field ).attr( 'data-field-value') != '' && ! $( field ).hasClass( 'pewc-active-field') ) {
763					// this is added so that the summary panel can detect the field
764					$( field ).addClass( 'pewc-active-field' );
765				}
766				// 3.12.2
767				if ( $( field ).attr( 'data-field-value') == '' && $( field ).attr( 'data-field-price' ) != 0 ) {
768					// maybe also reset price
769					$( field ).attr( 'data-field-price', 0 );
770				}
771				// we force update_total_js so that the summary panel is also updated
772				$( 'body' ).trigger('pewc_force_update_total_js');
773				$( 'body' ).trigger( 'pewc_reset_field_condition' );
774
775			},
776
777			// since 3.11.9, not sure if this is necessary if the group already has the class?
778			group_has_attribute_conditions: function( group ) {
779
780				if ( group.attr( 'data-condition-action') != '' && group.attr( 'data-conditions-match' ) != '' && group.attr( 'data-conditions' ) != '' ) {
781					var data_conditions = JSON.parse( group.attr( 'data-conditions' ) );
782					if ( data_conditions.length > 0 ) {
783						var has_attribute_condition = false;
784						for ( var i in data_conditions ) {
785							if ( data_conditions[i].field.substring( 0, 3 ) == 'pa_' ) {
786								has_attribute_condition = true;
787								break;
788							}
789						}
790						return has_attribute_condition;
791					}
792				}
793				return false;
794
795			},
796
797			// since 3.11.9
798			trigger_groups_with_attribute_conditions: function( event, variation, purchasable ) {
799
800				$( '.pewc-group-wrap.pewc-has-attribute-condition' ).each( function() {
801					var group = $( this );
802
803					// check if this group has conditions
804					//if ( pewc_conditions.group_has_attribute_conditions( group ) ) {
805						// this group is dependent on attributes, check now
806						var group_id = parseFloat( group.attr( 'id' ).replace( 'pewc-group-', '' ) );
807						conditions_obtain = pewc_conditions.check_group_conditions( group_id );
808						var action = $( '#pewc-group-' + group_id ).attr( 'data-condition-action' );
809						pewc_conditions.assign_group_classes( conditions_obtain, action, group_id );
810					//}
811				});
812
813			},
814
815			// since 3.11.9
816			trigger_fields_with_attribute_conditions: function( event, variation, purchasable ) {
817
818				$( '.pewc-item.pewc-field-has-attribute-condition' ).each( function() {
819
820					var field = $( this );
821					var field_id = field.attr( 'data-field-id' );
822					var parent = pewc_conditions.get_field_parent( field );
823
824					conditions_obtain = pewc_conditions.check_field_conditions( field_id, '', parent );
825					var action = $( '.pewc-field-' + field_id ).attr( 'data-field-conditions-action' );
826
827					pewc_conditions.assign_field_classes( conditions_obtain, action, field_id, parent );
828
829					if( pewc_vars.reset_fields == 'yes' ) {
830						pewc_conditions.reset_fields();
831					}
832
833				});
834
835			},
836
837			// since 3.11.9
838			trigger_attribute_condition_check: function( event, variation, purchasable ) {
839
840				pewc_conditions.trigger_groups_with_attribute_conditions( event, variation, purchasable );
841				pewc_conditions.trigger_fields_with_attribute_conditions( event, variation, purchasable );
842
843			},
844
845			// 3.26.5, created a separate function for this loop
846			check_triggered_fields: function( field, field_value, triggers_for, parent=false ) {
847
848				var is_repeatable_field = false;
849				var is_cloned_field = false;
850				var group_wrapper = $( field ).closest( '.pewc-group-wrap' );
851
852				if ( field != 'cost' && field != 'quantity' ) {
853					// was this triggered from a field inside a repeatable group?
854					is_repeatable_field = $( group_wrapper ).hasClass( 'pewc-repeatable-group' );
855					// was this triggered from a cloned field?
856					is_cloned_field = $( group_wrapper ).hasClass( 'pewc-cloned-group' );
857				}
858
859				// we use a different var so that it doesn't get overwritten in the loop
860				// trigger_quantity_condition_check and trigger_cost_condition_check sets the parent var inside the loop
861				var loop_parent = parent;
862
863				// Iterate through each field that is conditional on the updated field
864				for( var g in triggers_for ) {
865					if ( ! parent ) {
866						loop_parent = pewc_conditions.get_field_parent( $( '.pewc-field-'+triggers_for[g] ) );
867					} else if ( is_repeatable_field ) {
868						// this was triggered from a repeatable field, check if the current field is a sibling
869						if ( $( group_wrapper ).find( '.pewc-field-' + triggers_for[g] ).length > 0 ) {
870							// this is a sibling, change the loop_parent to the group_wrapper so that we only look for this field within this group
871							loop_parent = group_wrapper;
872						} else {
873							// not a sibling, point back to the original parent
874							loop_parent = parent;
875						}
876					}
877					conditions_obtain = pewc_conditions.check_field_conditions( triggers_for[g], field_value, loop_parent );
878					var action = $( loop_parent ).find( '.pewc-field-' + triggers_for[g] ).attr( 'data-field-conditions-action' );
879					pewc_conditions.assign_field_classes( conditions_obtain, action, triggers_for[g], loop_parent );
880				}
881
882			},
883
884			// 3.26.11, put back a field's default value if it has been displayed
885			put_back_default: function( pewc_id ) {
886
887				var field = $( '.pewc-item.' + pewc_id );
888				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' ) != '' ) {
889					// checkbox
890					if ( 'checkbox' === field.attr( 'data-field-type' ) && 'checked' === field.attr( 'data-default-value' ) ) {
891						field.find( 'input#' + pewc_id ).prop( 'checked', true ).trigger( 'change' );
892					}
893				}
894
895			},
896
897		}
898
899		pewc_conditions.init();
900
901	});
902
903})(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.