PageSourceSearch

https://pinacoteca.org.br/wp-content/plugins/infixs-correios-automatico/assets/front/js/main.js?ver=1785937477

js pinacoteca.org.br collected 2026-10-01 12:03:39 UTC 8,278 bytes, 349 lines download raw bytes

1/**
2 * Infixs Correios Automático - Main JS Front-End.
3 *
4 * @version 1.0.0
5 * @since   1.0.0
6 */
7
8/**
9 * @global {Object} infxsCorreiosAutomatico - Global object for Infixs Correios Automático.
10 * @property {string} product_id - The product ID.
11 * @property {string} nonce - The nonce for AJAX requests.
12 */
13
14jQuery( function ( $ ) {
15	/**
16	 * Infixs Correios Automático Input Text.
17	 *
18	 * @since 1.0.0
19	 * @version 1.0.0
20	 *
21	 * @param {JQuery<HTMLElement>} element
22	 */
23	const InfixsCorreiosAutomaticoInputText = ( inputSelector ) => {
24		const element = $( inputSelector );
25
26		return {
27			element: element,
28			input: element.find( 'input' ),
29			/**
30			 * Error element.
31			 * @type {JQuery<HTMLElement>|null}
32			 * @default null
33			 */
34			error: null,
35			getValue() {
36				return this.input.val();
37			},
38			setValue( value ) {
39				this.input.val( value );
40			},
41			setLoading() {
42				this.element
43					.find( '.infixs-correios-automatico-loading' )
44					.show();
45			},
46			unsetLoading() {
47				this.element
48					.find( '.infixs-correios-automatico-loading' )
49					.hide();
50			},
51			/**
52			 * Set the input as invalid.
53			 *
54			 * @param {string} message
55			 * @param {JQuery<HTMLElement>|null} element
56			 */
57			setError( message, element = null ) {
58				this.unsetError();
59				this.element.addClass( 'infixs-correios-automatico-invalid' );
60				$( '<div>' )
61					.addClass( 'infixs-correios-automatico-error-message' )
62					.text( message )
63					.css( {
64						opacity: '0',
65						'font-size': '12px',
66						'max-height': '0',
67						color: '#ff0000',
68					} )
69					.insertAfter( element ?? this.element )
70					.animate(
71						{
72							opacity: '1',
73							'max-height': '100px',
74						},
75						300
76					);
77			},
78			unsetError() {
79				this.element.removeClass(
80					'infixs-correios-automatico-invalid'
81				);
82				$( '.infixs-correios-automatico-error-message' ).remove();
83			},
84		};
85	};
86
87	function maskPostcodeByElement( element ) {
88		let value = $( element ).val().replace( /\D/g, '' );
89		if ( value.length > 8 ) {
90			value = value.substring( 0, 8 );
91		}
92		if ( value.length > 5 ) {
93			value = value.replace( /^(\d{5})(\d)/, '$1-$2' );
94		}
95		$( element ).val( value );
96	}
97
98	function postcodeMask() {
99		maskPostcodeByElement( this );
100	}
101
102	/**
103	 * Admin class.
104	 */
105	const InfixsCorreiosAutomaticoFront = {
106		/**
107		 * Initialize the class.
108		 */
109		init() {
110			this.applyListners();
111			if ( $( 'select#billing_country' ).length > 0 ) {
112				$( 'select#billing_country' ).trigger( 'change' );
113			}
114			if ( $( 'select#shipping_country' ).length > 0 ) {
115				$( 'select#shipping_country' ).trigger( 'change' );
116			}
117			this.applyPostcodeMask();
118		},
119
120		/**
121		 * Apply listners.
122		 *
123		 * @since 1.0.0
124		 * @version 1.0.0
125		 *
126		 * @return {void}
127		 */
128		applyListners() {
129			$( document.body ).on(
130				'click',
131				'.infixs-correios-automatico-calculate-submit',
132				this.calculateShipping.bind( this )
133			);
134
135			$( document.body ).on(
136				'change',
137				'select#billing_country',
138				this.changeCountry.bind( this, 'billing' )
139			);
140
141			$( document.body ).on(
142				'change',
143				'select#shipping_country',
144				this.changeCountry.bind( this, 'shipping' )
145			);
146
147			if (
148				infxsCorreiosAutomatico.options
149					?.autoCalculateProductShippingPostcode
150			) {
151				$( document.body ).on(
152					'input',
153					'input.infixs-correios-automatico-input',
154					this.updateProductShippingPostcode.bind( this )
155				);
156			}
157
158			const self = this;
159
160			$( document.body ).on(
161				'keydown',
162				'input.infixs-correios-automatico-input',
163				function ( e ) {
164					if ( e.key === 'Enter' ) {
165						e.preventDefault();
166						self.calculateShipping( e );
167					}
168				}
169			);
170
171			$( document ).on(
172				'wc_variation_form',
173				function ( event, variationFormInstance ) {
174					self.changeVariation( variationFormInstance );
175					$( document ).on(
176						'change.wc-variation-form',
177						'.variations select',
178						self.changeVariation.bind( self, variationFormInstance )
179					);
180				}
181			);
182		},
183
184		changeVariation( form ) {
185			if ( form && form.variationData ) {
186				const variationData = form.variationData;
187				const currentVariationId = this.getVariantion();
188
189				if ( currentVariationId ) {
190					const found = variationData.find(
191						( variation ) =>
192							variation.variation_id == currentVariationId
193					);
194					if ( found && found.is_virtual === true ) {
195						$( '.infixs-correios-automatico-calculator' ).hide();
196					}
197					if ( found && ! found.is_virtual ) {
198						$( '.infixs-correios-automatico-calculator' ).show();
199					}
200				}
201			}
202		},
203
204		/**
205		 * Apply PostCode mask to inputs with the class 'infixs-correios-automatico-postcode-mask'.
206		 */
207		applyPostcodeMask() {
208			$( document.body ).on(
209				'input',
210				'.infixs-correios-automatico-postcode-mask',
211				postcodeMask
212			);
213
214			$( document.body ).on(
215				'input',
216				'#calc_shipping_postcode',
217				postcodeMask
218			);
219
220			//this.bindPostcodeMask("input#shipping-postcode");
221			//this.bindPostcodeMask("input#0-postcode");
222		},
223
224		bindPostcodeMask( selector ) {
225			$( document.body ).on( 'input', selector, postcodeMask );
226			$( document.body ).on( 'blur', selector, postcodeMask );
227		},
228
229		unbindPostcodeMask( selector ) {
230			$( document.body ).off( 'input', selector, postcodeMask );
231			$( document.body ).off( 'blur', selector, postcodeMask );
232		},
233
234		updateProductShippingPostcode( event ) {
235			$element = $( event.target );
236			const postcode = $element.val().replace( /\D/g, '' );
237
238			if ( postcode.length === 8 ) {
239				this.calculateShipping( event );
240			}
241		},
242
243		/**
244		 * Submit and Calculate the shipping.
245		 *
246		 * @since 1.0.0
247		 * @version 1.0.0
248		 *
249		 * @param {Event} event
250		 *
251		 * @return {void}
252		 */
253		calculateShipping( event ) {
254			event.preventDefault();
255
256			if ( typeof woocommerce_params === 'undefined' ) {
257				console.error( 'woocommerce_params.ajax_url is undefined.' );
258				return;
259			}
260
261			const box = $( event.target ).closest(
262				'.infixs-correios-automatico-calculator'
263			);
264			const postcodeInput = InfixsCorreiosAutomaticoInputText(
265				box.find( '.infixs-correios-automatico-input-text' ).first()
266			);
267
268			const submitButton = $(
269				box
270					.find( '.infixs-correios-automatico-calculate-submit' )
271					.first()
272			);
273
274			const postcode = postcodeInput.getValue().replace( /\D/g, '' );
275			if ( postcode.length !== 8 ) {
276				postcodeInput.setError(
277					'CEP inválido, tente novamente.',
278					'.infixs-correios-automatico-calculate-box'
279				);
280				return;
281			}
282
283			postcodeInput.unsetError();
284
285			postcodeInput.setLoading();
286			submitButton.prop( 'disabled', true );
287
288			const variationId = this.getVariantion();
289
290			const _quantity = $( '.quantity input[name="quantity"]' ).val();
291			const quantity = Number.isFinite( Number( _quantity ) )
292				? Number( _quantity )
293				: null;
294
295			$.ajax( {
296				url: woocommerce_params.ajax_url,
297				type: 'POST',
298				data: {
299					action: 'infixs_correios_automatico_calculate_shipping',
300					postcode: postcode,
301					product_id: infxsCorreiosAutomatico.productId,
302					...( variationId && { variation_id: variationId } ),
303					nonce: infxsCorreiosAutomatico.nonce,
304					...( quantity && { quantity } ),
305				},
306				success: ( response ) => {
307					box.find( '#infixs-correios-automatico-calculate-results' )
308						.first()
309						.html( response );
310				},
311				error: ( error ) => {
312					box.find( '#infixs-correios-automatico-calculate-results' )
313						.first()
314						.html(
315							error.responseJSON?.message ||
316								'Houve um problema ao calcular, verifique o CEP e tente novamente.'
317						);
318				},
319				complete: () => {
320					submitButton.prop( 'disabled', false );
321					postcodeInput.unsetLoading();
322				},
323			} );
324		},
325
326		changeCountry( field, event ) {
327			const target = $( event.target );
328			const country = target.val();
329
330			if ( country === 'BR' ) {
331				this.bindPostcodeMask( `input#${ field }_postcode` );
332			} else {
333				this.unbindPostcodeMask( `input#${ field }_postcode` );
334			}
335		},
336
337		getVariantion() {
338			const variationInput = $( 'input[name="variation_id"]' );
339			if ( variationInput.length === 0 ) return false;
340			const variationId = parseInt( variationInput.val(), 10 );
341			return isNaN( variationId ) ? false : variationId;
342		},
343
344		getInput( element ) {},
345	};
346
347	// Initialize the class.
348	InfixsCorreiosAutomaticoFront.init();
349} );

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.