PageSourceSearch

https://joahbox.com/wp-content/plugins/insight-swatches/assets/js/scripts.js

js joahbox.com collected 2026-10-05 10:45:33 UTC 15,439 bytes, 543 lines download raw bytes

1'use strict';
2
3window.isw = {};
4
5(
6	function( isw, $ ) {
7		isw = isw || {};
8		$.extend( isw, {
9			ajax_url: isw_vars.ajax,
10			product_selector: isw_vars.product_selector,
11			price_selector: isw_vars.price_selector,
12			localization: isw_vars.localization,
13		} );
14	}
15).apply( this, [window.isw, jQuery] );
16
17(
18	function( isw, $ ) {
19		isw = isw || {};
20		$.extend( isw, {
21			Swatches: {
22				init: function() {
23
24					this.$form = $( 'form.isw-swatches.variations_form' );
25					this.$swatches = $( 'div.isw-swatches' );
26
27					this.initSingle();
28					this.initLoop();
29
30					$( '.isw-term' ).each( function() {
31						if ( $( this ).css( 'background-color' ) ==
32						     'rgb(255, 255, 255)' ) {
33							$( this ).addClass( 'isw-white' );
34						}
35					} );
36
37				},
38
39				initSingle: function() {
40
41					var self = this,
42						$term = self.$form.find( '.isw-term' ),
43						$activeTerm = self.$form.find(
44							'.isw-term:not(.isw-disabled)' );
45
46					// load default value
47					$term.each( function() {
48						var $this = $( this ),
49							term = $this.attr( 'data-term' ),
50							attr = $this.parent().attr( 'data-attribute' ),
51							$selectbox = self.$form.find( 'select#' + attr ),
52							val = $selectbox.val();
53
54						if ( val != '' && term == val ) {
55							$( this ).addClass( 'isw-selected' );
56						}
57					} );
58
59					$activeTerm.unbind( 'click' ).on( 'click', function( e ) {
60						var $this = $( this ),
61							term = $this.attr( 'data-term' ),
62							title = $this.attr( 'title' ),
63							attr = $this.parent().attr( 'data-attribute' ),
64							$selectbox = self.$form.find( 'select#' + attr );
65
66						if ( $this.hasClass( 'isw-disabled' ) ) {
67							return false;
68						}
69
70						$selectbox.val( term ).trigger( 'change' );
71
72						$this.parent( '.isw-swatch' ).find( '.isw-selected' ).removeClass( 'isw-selected' );
73						$this.addClass( 'isw-selected' );
74
75						$( 'body' ).trigger( 'isw_selected', [attr, term, title] );
76
77						e.preventDefault();
78					} );
79
80					self.$form.on( 'woocommerce_update_variation_values',
81						function() {
82							self.$form.find( 'select' ).each( function() {
83								var $this = $( this );
84								var $swatch = $this.parent().find( '.isw-swatch' );
85
86								$swatch.find( '.isw-term' ).removeClass( 'isw-enabled' ).addClass( 'isw-disabled' );
87
88								$this.find( 'option.enabled' ).each( function() {
89									var val = $( this ).val();
90									$swatch.find(
91										'.isw-term[data-term="' + val + '"]' ).removeClass( 'isw-disabled' ).addClass( 'isw-enabled' );
92								} );
93							} );
94						} );
95
96					self.$form.on( 'reset_data', function() {
97						$( 'body' ).trigger( 'isw_reset' );
98						$( this ).find( '.isw-selected' ).removeClass( 'isw-selected' );
99						$( this ).find( 'select' ).each( function() {
100							var attr = $( this ).attr( 'id' );
101							var title = $( this ).find( 'option:selected' ).text();
102							var term = $( this ).val();
103							if ( term != '' ) {
104								$( this ).parent().find( '.isw-term[data-term="' + term + '"]' ).addClass( 'isw-selected' );
105								$( 'body' ).trigger( 'isw_reset_attr', [attr, term, title] );
106							}
107						} );
108
109					} );
110
111				},
112
113				initLoop: function() {
114
115					var self = this;
116
117					self.$swatches.each( function() {
118
119						var $swatches = $( this ),
120							$term = $swatches.find(
121								'.isw-term:not(.isw-disabled)' ),
122							$resetBtn = $swatches.find(
123								'.reset_variations--loop' ),
124							$product = $swatches.closest(
125								isw.product_selector ),
126							variationData = $.parseJSON(
127								$swatches.attr( 'data-product_variations' ) );
128
129						// add class if empty
130						if ( $swatches.find( '.isw-swatch' ).length == 0 ) {
131							$swatches.addClass( 'isw-empty' );
132						}
133
134						$term.unbind( 'click' ).on( 'click', function( e ) {
135
136							var $this = $( this );
137
138							if ( $this.hasClass( 'isw-disabled' ) ) {
139								return false;
140							}
141
142							var term = $this.attr( 'data-term' );
143
144							$product.find( '.isw-term' ).removeClass( 'isw-disabled isw-enabled' );
145							$this.parent().find( '.isw-term.isw-selected' ).removeClass( 'isw-selected' );
146
147							if ( $this.hasClass( 'isw-selected' ) ) {
148								$this.parent().removeClass( 'isw-activated' );
149								$product.removeClass( 'isw-product-swatched' );
150
151								if ( !$product.find( '.isw-selected' ).length ) {
152									$resetBtn.trigger( 'click' );
153								}
154							} else {
155								$this.parent().addClass( 'isw-activated' );
156								$this.addClass( 'isw-selected' );
157
158								$product.addClass( 'isw-product-swatched' );
159								$resetBtn.addClass( 'show' ).show();
160							}
161
162							var attributes = self.getChosenAttributes(
163								$swatches ),
164								currentAttributes = attributes.data;
165
166							if ( attributes.count === attributes.chosenCount ) {
167								self.updateAttributes( $swatches, variationData );
168
169								var matching_variations = self.findMatchingVariations(
170									variationData, currentAttributes ),
171									variation = matching_variations.shift();
172
173								if ( variation ) {
174									// Found variation
175									self.foundVariation( $swatches, variation );
176								} else {
177									$resetBtn.trigger( 'click' );
178								}
179							} else {
180								self.updateAttributes( $swatches, variationData );
181							}
182
183							e.preventDefault()
184
185						} );
186
187						$resetBtn.unbind( 'click' ).on( 'click', function() {
188
189							$product.removeClass( 'isw-product-swatched' );
190
191							$swatches.removeAttr( 'data-variation_id' );
192							$swatches.find( '.isw-swatch' ).removeClass( 'isw-activated' );
193							$swatches.find( '.isw-term' ).removeClass(
194								'isw-enabled isw-disabled isw-selected' );
195
196							$( 'body' ).trigger( 'isw_reset_add_to_cart_button_text', [$product] );
197
198							$product.find( '.add_to_cart_button' ).removeClass(
199								'isw-ready isw-readmore isw-text-changed added loading' ).text( isw.localization.select_options_text );
200
201							// reset price
202							var $price = $product.find(
203								isw.price_selector ).not( '.price-cloned' ),
204								$price_cloned = $product.find( '.price-cloned' );
205
206							if ( $price_cloned.length ) {
207								$price.html( $price_cloned.html() );
208								$price_cloned.remove();
209							}
210
211							// reset image
212							self.variationsImageUpdate( false, $product );
213
214							$( this ).removeClass( 'show' ).hide();
215
216							return false;
217						} );
218					} );
219
220					// Ajax add to cart
221					$( document ).on( 'click',
222						'.add_to_cart_button.product_type_variable.isw-ready',
223						function() {
224
225							var $this = $( this ),
226								$swatches = $this.closest(
227									isw.product_selector ).find( '.isw-swatches' );
228
229							var variation_id = $swatches.attr(
230								'data-variation_id' );
231
232							if ( typeof variation_id == 'undefined' ||
233							     variation_id == '' ) {
234								return true;
235							}
236
237							var product_id = $this.attr( 'data-product_id' ),
238								quantity = $this.attr( 'data-quantity' ),
239								item = {};
240
241							$swatches.find( '.isw-swatch' ).each( function() {
242								var attr = $( this ).attr( 'data-attribute' );
243								var attr_value = $( this ).find( 'span.isw-selected' ).attr( 'aria-label' );
244
245								item[attr] = attr_value;
246							} );
247
248							$this.removeClass( 'added' );
249
250							var data = {
251								action: 'isw_add_to_cart',
252								product_id: product_id,
253								quantity: quantity,
254								variation_id: variation_id,
255								variation: item,
256							};
257
258							$( 'body' ).trigger( 'adding_to_cart', [$this, data] );
259
260							$.ajax( {
261								type: 'POST',
262								url: isw.ajax_url,
263								data: data,
264								success: function( response ) {
265
266									if ( !response ) {
267										return false;
268									}
269
270									if ( response.error &&
271									     response.product_url ) {
272										window.location = response.product_url;
273										return false;
274									}
275
276									// update cart fragment
277									var fragments = response.fragments,
278										cart_hash = response.cart_hash;
279
280									if ( fragments ) {
281										$.each( fragments,
282											function( key, value ) {
283												$( key ).replaceWith( value );
284											} );
285									}
286
287									$this.addClass( 'added' );
288									$( 'body' ).trigger( 'added_to_cart', [fragments, cart_hash] );
289
290								}, error: function( error ) {
291									console.log( error );
292								}
293							} );
294
295							return false;
296						} );
297
298				},
299
300				updateAttributes: function( $swatches, variationData ) {
301
302					var self = this,
303						attributes = self.getChosenAttributes( $swatches ),
304						currentAttributes = attributes.data,
305						available_options_count = 0,
306						$swatch = $swatches.find( '.isw-swatch' );
307
308					$swatch.each( function( idx, el ) {
309
310						var current_attr_sw = $( el ),
311							current_attr_name = 'attribute_' +
312							                    current_attr_sw.attr(
313								                    'data-attribute' ),
314							selected_attr_val = current_attr_sw.find(
315								'.isw-term.isw-selected' ).attr( 'data-term' ),
316							selected_attr_val_valid = true,
317							checkAttributes = $.extend( true, {},
318								currentAttributes );
319
320						checkAttributes[current_attr_name] = '';
321
322						var variations = self.findMatchingVariations(
323							variationData, checkAttributes );
324
325						// Loop through variations.
326						for ( var num in variations ) {
327							if ( typeof variations[num] !== 'undefined' ) {
328								var variationAttributes = variations[num].attributes;
329
330								for ( var attr_name in variationAttributes ) {
331									if ( variationAttributes.hasOwnProperty(
332										attr_name ) ) {
333										var attr_val = variationAttributes[attr_name],
334											variation_active = '';
335
336										if ( attr_name === current_attr_name ) {
337											if ( variations[num].variation_is_active ) {
338												variation_active = 'enabled';
339											}
340
341											if ( attr_val ) {
342												// available
343												current_attr_sw.find(
344													'.isw-term[data-term="' + attr_val + '"]' ).addClass( 'isw-' + variation_active );
345											} else {
346												// apply for all swatches
347												current_attr_sw.find( '.isw-term' ).addClass( 'isw-' + variation_active );
348											}
349										}
350									}
351								}
352							}
353						}
354
355						available_options_count = current_attr_sw.find(
356							'.isw-term.isw-enabled' ).length;
357
358						if ( selected_attr_val && (
359							available_options_count === 0 || current_attr_sw.find(
360								'.isw-term.isw-enabled[data-term="' +
361								self.addSlashes( selected_attr_val ) + '"]' ).length ===
362							0
363						) ) {
364							selected_attr_val_valid = false;
365						}
366
367						// Disable terms not available
368						current_attr_sw.find( '.isw-term:not(.isw-enabled)' ).addClass( 'isw-disabled' );
369					} );
370				},
371
372				addSlashes: function( string ) {
373					string = string.replace( /'/g, '\\\'' );
374					string = string.replace( /"/g, '\\\"' );
375					return string;
376				},
377
378				getChosenAttributes: function( $swatches ) {
379
380					var data = {},
381						count = 0,
382						chosen = 0,
383						$swatch = $swatches.find( '.isw-swatch' );
384
385					$swatch.each( function() {
386
387						var attribute_name = 'attribute_' +
388						                     $( this ).attr( 'data-attribute' ),
389							value = $( this ).find( '.isw-term.isw-selected' ).attr( 'data-term' ) || '';
390
391						if ( value.length > 0 ) {
392							chosen ++;
393						}
394
395						count ++;
396						data[attribute_name] = value;
397					} );
398
399					return {
400						'count': count,
401						'chosenCount': chosen,
402						'data': data,
403					};
404				},
405
406				findMatchingVariations: function( variationData, settings ) {
407					var matching = [];
408					for ( var i = 0; i < variationData.length; i ++ ) {
409						var variation = variationData[i];
410
411						if ( this.isMatch( variation.attributes, settings ) ) {
412							matching.push( variation );
413						}
414					}
415					return matching;
416				},
417
418				isMatch: function( variation_attributes, attributes ) {
419					var match = true;
420					for ( var attr_name in variation_attributes ) {
421						if ( variation_attributes.hasOwnProperty( attr_name ) ) {
422							var val1 = variation_attributes[attr_name];
423							var val2 = attributes[attr_name];
424							if ( val1 !== undefined && val2 !== undefined &&
425							     val1.length !== 0 && val2.length !== 0 &&
426							     val1 !== val2 ) {
427								match = false;
428							}
429						}
430					}
431					return match;
432				},
433
434				foundVariation: function( $swatches, variation ) {
435
436					var self = this,
437						$product = $swatches.closest( '.product' ),
438						$price = $product.find( isw.price_selector ).not( '.price-cloned' ),
439						$price_clone = $price.clone().addClass( 'price-cloned' ).css( 'display', 'none' );
440
441					if ( variation.price_html ) {
442
443						if ( !$product.find( '.price-cloned' ).length ) {
444							$product.append( $price_clone );
445						}
446
447						$price.replaceWith( variation.price_html );
448					} else {
449						if ( $product.find( '.price-cloned' ).length ) {
450							$price.replaceWith( $price_clone.html() );
451							$price_clone.remove();
452						}
453					}
454
455					// add variation id
456					$swatches.attr( 'data-variation_id', variation.variation_id );
457
458					// update image
459					self.variationsImageUpdate( variation, $product );
460
461					// change add to cart button text
462					self.changeAddToCartBtnText( variation, $product );
463				},
464
465				setVariationAttr: function( $el, attr, value ) {
466					if ( undefined === $el.attr( 'data-o_' + attr ) ) {
467						$el.attr( 'data-o_' + attr, (
468							!$el.attr( attr )
469						) ? '' : $el.attr( attr ) );
470					}
471					if ( false === value ) {
472						$el.removeAttr( attr );
473					} else {
474						$el.attr( attr, value );
475					}
476				},
477
478				resetVariationAttr: function( $el, attr ) {
479					if ( undefined !== $el.attr( 'data-o_' + attr ) ) {
480						$el.attr( attr, $el.attr( 'data-o_' + attr ) );
481					}
482				},
483
484				variationsImageUpdate: function( variation, $product ) {
485
486					var self = this,
487						$product_img = $product.find( '.wp-post-image' );
488
489					if ( variation && variation.image.full_src ) {
490						self.setVariationAttr( $product_img, 'src',
491							variation.image.full_src );
492						self.setVariationAttr( $product_img, 'srcset',
493							variation.image.full_src );
494						self.setVariationAttr( $product_img, 'sizes',
495							variation.image.sizes );
496					} else {
497						self.resetVariationAttr( $product_img, 'src' );
498						self.resetVariationAttr( $product_img, 'srcset' );
499						self.resetVariationAttr( $product_img, 'sizes' );
500					}
501
502				},
503
504				changeAddToCartBtnText: function( variation, $product ) {
505
506					var $atcBtn = $product.find( '.add_to_cart_button' ),
507						text = '';
508
509					$atcBtn.removeClass( 'added' );
510
511					if ( Object.keys( variation.attributes ).length ==
512					     $product.find( '.isw-swatch' ).length ) {
513						if ( variation.is_in_stock === true ) {
514							text = isw.localization.add_to_cart_text;
515							$atcBtn.addClass( 'isw-ready' ).removeClass( 'isw-readmore' );
516						} else {
517							// out of stock
518							text = isw.localization.read_more_text;
519							$atcBtn.addClass( 'isw-readmore' ).removeClass( 'isw-ready' );
520						}
521					} else {
522						text = isw.localization.select_options_text;
523						$atcBtn.removeClass( 'isw-ready isw-readmore' );
524					}
525
526					$atcBtn.addClass( 'isw-text-changed' ).text( text );
527
528					$( 'body' ).trigger( 'isw_change_add_to_cart_button_text', [$product] );
529				},
530			},
531		} )
532	}
533).apply( this, [window.isw, jQuery] );
534
535(
536	function( isw, $ ) {
537		$( document ).ready( function() {
538			if ( typeof isw.Swatches !== 'undefined' ) {
539				isw.Swatches.init();
540			}
541		} );
542	}
543).apply( this, [window.isw, 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.