PageSourceSearch

https://rplanetshop.com/cdn/shop/t/10/assets/component-product-form_v-168234936048216057231672274852.js

js rplanetshop.com collected 2026-10-08 19:30:39 UTC 17,102 bytes, 513 lines download raw bytes

1/** Shopify CDN: Minification failed
2
3Line 17:1 Transforming class syntax to the configured target environment ("es5") is not supported yet
4Line 19:13 Transforming object literal extensions to the configured target environment ("es5") is not supported yet
5Line 53:17 Transforming object literal extensions to the configured target environment ("es5") is not supported yet
6Line 92:15 Transforming object literal extensions to the configured target environment ("es5") is not supported yet
7Line 105:16 Transforming object literal extensions to the configured target environment ("es5") is not supported yet
8Line 113:13 Transforming object literal extensions to the configured target environment ("es5") is not supported yet
9Line 140:13 Transforming object literal extensions to the configured target environment ("es5") is not supported yet
10Line 146:5 Transforming let to the configured target environment ("es5") is not supported yet
11Line 147:11 Transforming const to the configured target environment ("es5") is not supported yet
12Line 147:25 Transforming for-of loops to the configured target environment ("es5") is not supported yet
13... and 34 more hidden warnings
14
15**/
16if ( typeof ProductVariants !== 'function' ) {
17	class ProductVariants extends HTMLElement {
18
19		constructor() {
20
21			super();
22			this.price = document.querySelector(`#product-price-${this.dataset.id}`);
23
24			if ( this.price ) {
25				this.priceOriginal = this.price.querySelector('[data-js-product-price-original]');
26				this.priceCompare = this.price.querySelector('[data-js-product-price-compare]');
27				this.priceUnit = this.price.querySelector('[data-js-product-price-unit]');
28			}
29
30			this.productForm = document.querySelector(`#product-form-${this.dataset.id}`);
31			if ( this.productForm ) {
32				this.productQty = this.productForm.querySelector('[data-js-product-quantity]');
33				this.addToCart = this.productForm.querySelector('[data-js-product-add-to-cart]');
34				this.addToCartText = this.productForm.querySelector('[data-js-product-add-to-cart-text]');
35			}
36
37			this.addEventListener('change', this.onVariantChange);
38
39			this.updateOptions();
40			this.updateMasterId();
41			this.updateUnavailableVariants();
42
43			this.productStock = this.querySelector('[data-js-variant-quantity]');
44			if ( this.productStock && this.querySelector('[data-js-variant-quantity-data]') ) {
45				this.productStockData = JSON.parse(this.querySelector('[data-js-variant-quantity-data]').dataset.inventory);
46			}
47			this.updateStock();
48
49			this._event = new Event('VARIANT_CHANGE');
50
51		}
52
53		onVariantChange(){
54
55			this.updateOptions();
56			this.updateMasterId();
57			this.updateVariantInput();
58			if ( this.price ) {
59				this.updatePrice();
60			}
61			this.updateStock();
62			this.updateUnavailableVariants();
63			this.updatePickupAvailability();
64			if ( ! this.hasAttribute('data-no-history') ) {
65				this.updateURL();
66			}
67			this.updateDetails();
68
69			if ( this.productForm ) {
70				if ( ! this.currentVariant || ! this.currentVariant.available ) {
71					if ( this.productQty ) this.productQty.style.display = 'none';
72					this.addToCart.classList.add('disabled');
73					this.productForm.classList.add('disabled-cart');
74					this.addToCartText.textContent = KROWN.settings.locales.products_sold_out_variant;
75				} else {
76					if ( this.productQty ) this.productQty.style.display = '';
77					this.addToCart.classList.remove('disabled');
78					this.productForm.classList.remove('disabled-cart');
79					this.addToCartText.textContent = KROWN.settings.locales.products_add_to_cart_button;
80				}
81				if ( ! this.currentVariant ) {
82					this.productForm.classList.add('unavailable-variant');
83					this.addToCartText.textContent = KROWN.settings.locales.products_unavailable_variant;
84				} else {
85					this.productForm.classList.remove('unavailable-variant');
86				}
87			}
88			this.dispatchEvent(this._event);
89
90		}
91
92		updateOptions(){
93			if ( this.dataset.type == 'select' ) {
94				this.options = Array.from(this.querySelectorAll('.product-variant-container'), (select) => select.value);
95			} else if ( this.dataset.type == 'radio' ) {
96				this.options = [];
97				this.querySelectorAll('.product-variant__input').forEach(elm=>{
98					if ( elm.checked ) {
99						this.options.push(elm.value);
100					}
101				});
102			}
103		}
104
105		updateMasterId(){
106			this.currentVariant = this.getVariantData().find((variant) => {
107				return !variant.options.map((option, index) => {
108					return this.options[index] === option;
109				}).includes(false);
110			});
111		}
112
113		updatePrice(){
114			
115			if (!this.currentVariant) {
116				this.priceOriginal.innerHTML = '';
117				this.priceCompare.innerHTML = '';
118				this.priceUnit.innerHTML = '';
119			} else {
120				this.priceOriginal.innerHTML = this._formatMoney(this.currentVariant.price, KROWN.settings.shop_money_format);
121				if ( this.currentVariant.compare_at_price > this.currentVariant.price ) {
122					this.priceCompare.innerHTML = this._formatMoney(this.currentVariant.compare_at_price, KROWN.settings.shop_money_format);
123				} else {
124					this.priceCompare.innerHTML = '';
125				}
126
127				if ( this.currentVariant.unit_price_measurement ) {
128					this.priceUnit.innerHTML = `
129						${this._formatMoney(this.currentVariant.unit_price, KROWN.settings.shop_money_format)} / 
130						${( this.currentVariant.unit_price_measurement.reference_value != 1 ? this.currentVariant.unit_price_measurement.reference_value + ' ' : '' )}
131						${this.currentVariant.unit_price_measurement.reference_unit}
132					`;
133				} else {
134					this.priceUnit.innerHTML = '';
135				}
136			}
137		
138		}
139
140		updateStock(){
141			if (!this.currentVariant) {
142				if ( this.productStock )
143				this.productStock.innerHTML = '';
144			} else {
145				if ( this.productStock && this.productStockData ) {
146					let currentVariant = false;
147					for ( const variant of this.productStockData ) {
148						if ( variant.id == this.currentVariant.id ) {
149							currentVariant = variant;
150							break;
151						}
152					}
153					this.productStock.innerHTML = '';
154					if ( currentVariant ) {
155						if ( currentVariant.quantity <= 0 ) {
156							if ( currentVariant.inventory == 'continue' ) {
157								this.productStock.innerHTML = KROWN.settings.locales.products_preorder;
158							} else if ( currentVariant.inventory == 'deny' ) {
159								this.productStock.innerHTML = KROWN.settings.locales.products_no_products;
160							}
161						} else if ( currentVariant.quantity == '1' ) {
162							this.productStock.innerHTML = KROWN.settings.locales.products_one_product;
163						} else if ( currentVariant.quantity <= 5 ) {
164							this.productStock.innerHTML = KROWN.settings.locales.products_few_products.replace('{{ count }}', currentVariant.quantity);
165						} else if ( currentVariant.unavailable ) {
166							this.productStock.innerHTML = KROWN.settings.locales.products_no_products;
167						} else if ( currentVariant.quantity > 5 && this.productStock.dataset.type == "always" ) {
168							this.productStock.innerHTML = KROWN.settings.locales.products_few_products.replace('{{ count }}', currentVariant.quantity);
169						}
170					}
171				}
172			}
173		}
174
175		updateDetails(){
176
177			document.querySelectorAll(`#product-${this.dataset.id} [data-js-product-sku]`).forEach(elm=>{
178				if ( this.currentVariant && this.currentVariant.sku ) {
179					elm.innerHTML = KROWN.settings.locales.product_sku + this.currentVariant.sku;
180				} else {
181					elm.innerHTML = '';
182				}
183			});
184
185			document.querySelectorAll(`#product-${this.dataset.id} [data-js-product-barcode]`).forEach(elm=>{
186				if ( this.currentVariant && this.currentVariant.barcode ) {
187					elm.innerHTML = KROWN.settings.locales.product_barcode + this.currentVariant.barcode;
188				} else {
189					elm.innerHTML = '';
190				}
191			});
192			
193		}
194
195		updatePickupAvailability() {
196			const pickUpAvailability = document.querySelector('pickup-availability');
197			if (!pickUpAvailability) return;
198
199			if (this.currentVariant && this.currentVariant.available) {
200				pickUpAvailability.fetchAvailability(this.currentVariant.id);
201			} else {
202				pickUpAvailability.removeAttribute('available');
203				pickUpAvailability.innerHTML = '';
204			}
205		}
206
207		updateUnavailableVariants(){
208
209			if ( this.dataset.hideVariants == 'true' ) {
210
211				let options1 = {},
212						options2 = {},
213						options3 = {},
214
215						option1Selected = null,
216						option2Selected = null;
217
218				if ( this.dataset.variants > 1 ) {
219					option1Selected = this._getSelectedOption(0);
220					this._resetDisabledOption(1);
221				}
222				if ( this.dataset.variants > 2 ) {
223					option2Selected = this._getSelectedOption(1);
224					this._resetDisabledOption(2);
225				}
226
227				this.getVariantData().forEach((el) => {
228
229					if ( this.dataset.variants > 0 ) {
230						if ( ! options1[el.option1] ) {
231							options1[el.option1] = [];
232						}
233						options1[el.option1].push(String(el.available));
234					}
235					if ( this.dataset.variants > 1 ) {
236						if ( ! options2[el.option2] ) {
237							options2[el.option2] = [];
238						}
239						options2[el.option2].push(String(el.available));
240					}
241					if ( this.dataset.variants == 2 ) {
242						if ( el.option1 == option1Selected && ! el.available ) {
243							this._setDisabledOption(1, el.option2);
244						}
245					}
246					if ( this.dataset.variants > 2 ) {
247						if ( ! options3[el.option3] ) {
248							options3[el.option3] = [];
249						}
250						options3[el.option3].push(String(el.available));
251						if ( el.option2 == option2Selected && el.option1 == option1Selected && ! el.available ) {
252							this._setDisabledOption(2, el.option3);
253						}
254					}
255
256				});
257
258				if ( this.dataset.variants > 0 ) {
259					Object.keys(options1).forEach((key)=>{
260						if ( ! options1[key].includes('true') ){
261							this.querySelectorAll('.product-variant')[0].querySelector(`.product-variant-value[value="${key}"]`).classList.add('disabled');
262						}
263					});
264				}
265				if ( this.dataset.variants > 1 ) {
266					Object.keys(options2).forEach((key)=>{
267						if ( ! options2[key].includes('true') ){
268							this.querySelectorAll('.product-variant')[1].querySelector(`.product-variant-value[value="${key}"]`).classList.add('disabled');
269						}
270					});
271				}
272				if ( this.dataset.variants > 2 ) {
273					Object.keys(options3).forEach((key)=>{
274						if ( ! options3[key].includes('true') ){
275							this.querySelectorAll('.product-variant')[2].querySelector(`.product-variant-value[value="${key}"]`).classList.add('disabled');
276						}
277					});
278					Object.keys(options2).forEach((key)=>{
279						if ( ! options2[key].includes('true') ){
280							if ( option2Selected == key ) {
281								this.querySelectorAll('.product-variant')[2].querySelector('.product-variant-value').classList.add('disabled');
282							}
283						}
284					});
285				}
286
287			}
288		}
289
290		_getSelectedOption(i){
291			if ( this.dataset.type == 'select' ) {
292				return this.querySelectorAll('.product-variant')[i].querySelector('.product-variant-container').value;
293			} else if ( this.dataset.type == 'radio' ) {
294				let selectedOption = null;
295				this.querySelectorAll('.product-variant')[i].querySelectorAll('.product-variant__input').forEach(elm=>{ if (elm.checked) selectedOption = elm.value });
296				return selectedOption;
297			}
298		}
299
300		_resetDisabledOption(i){
301			this.querySelectorAll('.product-variant')[i].querySelectorAll('.product-variant-value').forEach((elm)=>{
302				elm.classList.remove('disabled');
303			})
304		}
305
306		_setDisabledOption(i,option){
307			this.querySelectorAll('.product-variant')[i].querySelector(`.product-variant-value[value="${option}"]`).classList.add('disabled');
308		}
309
310		updateURL(){
311			if (!this.currentVariant) return;
312			window.history.propertyIsEnumerable({ }, '', `${this.dataset.url}?variant=${this.currentVariant.id}`);
313		}
314
315		updateVariantInput() {
316			if (!this.currentVariant) return;
317			const productForms = document.querySelectorAll(`#product-form-${this.dataset.id}, #product-form-installment`);
318			productForms.forEach((productForm) => {
319				const input = productForm.querySelector('input[name="id"]');
320				input.value = this.currentVariant.id;
321				input.dispatchEvent(new Event('change', { bubbles: true }));
322			});
323		}
324
325		getVariantData() {
326			this.variantData = this.variantData || JSON.parse(this.querySelector('[type="application/json"]').textContent);
327			return this.variantData;
328		}
329
330		_formatMoney(cents, format) {
331
332			if (typeof cents === 'string') {
333				cents = cents.replace('.', '');
334			}
335	
336			let value = '';
337			const placeholderRegex = /\{\{\s*(\w+)\s*\}\}/;
338			const formatString = format || moneyFormat;
339	
340			function formatWithDelimiters(number, precision, thousands, decimal) {
341				thousands = thousands || ',';
342				decimal = decimal || '.';
343	
344				if (isNaN(number) || number === null) {
345					return 0;
346				}
347	
348				number = (number / 100.0).toFixed(precision);
349	
350				const parts = number.split('.');
351				const dollarsAmount = parts[0].replace(
352					/(\d)(?=(\d\d\d)+(?!\d))/g,
353					'$1' + thousands
354				);
355				const centsAmount = parts[1] ? decimal + parts[1] : '';
356	
357				return dollarsAmount + centsAmount;
358			}
359	
360			switch (formatString.match(placeholderRegex)[1]) {
361				case 'amount':
362					value = formatWithDelimiters(cents, 2);
363					break;
364				case 'amount_no_decimals':
365					value = formatWithDelimiters(cents, 0);
366					break;
367				case 'amount_with_comma_separator':
368					value = formatWithDelimiters(cents, 2, '.', ',');
369					break;
370				case 'amount_no_decimals_with_comma_separator':
371					value = formatWithDelimiters(cents, 0, '.', ',');
372					break;
373				case 'amount_no_decimals_with_space_separator':
374					value = formatWithDelimiters(cents, 0, ' ');
375					break;
376				case 'amount_with_apostrophe_separator':
377					value = formatWithDelimiters(cents, 2, "'");
378					break;
379			}
380	
381			return formatString.replace(placeholderRegex, value);
382	
383		}
384
385	}
386
387  if ( typeof customElements.get('product-variants') == 'undefined' ) {
388		customElements.define('product-variants', ProductVariants);
389	}
390
391}
392
393/* ---
394	Product Form
395--- */
396
397
398if ( typeof ProductForm !== 'function' ) {
399	class ProductForm extends HTMLElement {
400		constructor() {
401			super();   
402			this.init();
403		}
404
405		init(){
406			if ( this.hasAttribute('data-ajax-cart') && ! document.body.classList.contains('template-cart') ) {
407				this.form = this.querySelector('form');
408				this.form.addEventListener('submit', this.onSubmitHandler.bind(this));
409				this.ADD_TO_CART = new Event('add-to-cart');
410			}
411		}
412
413		onSubmitHandler(e) {
414
415			e.preventDefault();
416			
417			const submitButton = this.querySelector('[type="submit"]');
418
419			submitButton.classList.add('working');
420
421			const body = this._serializeForm(this.form);
422			let alert = '';
423
424			fetch(`${KROWN.settings.routes.cart_add_url}.js`, {
425				body,
426				headers: {
427					'Content-Type': 'application/x-www-form-urlencoded',
428					'X-Requested-With': 'XMLHttpRequest'
429				},
430				method: 'POST'
431			})
432				.then(response => response.json())
433				.then(response => {
434					if ( response.status == 422 ) {
435						// wrong stock logic alert
436						alert = document.createElement('span');
437						alert.className = 'alert alert--error';
438						alert.innerHTML = response.description
439						return fetch('?section_id=helper-cart');
440					} else {
441						return fetch('?section_id=helper-cart');
442					}
443				})
444				.then(response => response.text())
445				.then(text => {
446
447					const sectionInnerHTML = new DOMParser().parseFromString(text, 'text/html');
448					const cartFormInnerHTML = sectionInnerHTML.getElementById('AjaxCartForm').innerHTML;
449					const cartSubtotalInnerHTML = sectionInnerHTML.getElementById('AjaxCartSubtotal').innerHTML;
450
451					const cartItems = document.getElementById('AjaxCartForm');
452					cartItems.innerHTML = cartFormInnerHTML;
453					cartItems.ajaxifyCartItems();
454
455					if ( alert != '' ) {
456						document.getElementById('AjaxCartForm').querySelector('form').prepend(alert);
457					}
458
459					document.getElementById('AjaxCartSubtotal').innerHTML = cartSubtotalInnerHTML;
460
461					document.querySelectorAll('[data-header-cart-count]').forEach(elm=>{
462						elm.textContent = document.querySelector('#AjaxCartForm [data-cart-count]').textContent;
463					});
464					document.querySelectorAll('[data-header-cart-total').forEach(elm=>{
465						elm.textContent = document.querySelector('#AjaxCartForm [data-cart-total]').textContent;
466					});
467					this.dispatchEvent(this.ADD_TO_CART);
468
469				})
470				.catch(e => {
471					console.log(e);
472				})
473				.finally(() => {
474					submitButton.classList.remove('working');
475				});
476		}
477
478		_serializeForm(form) {
479			let arr = [];
480			Array.prototype.slice.call(form.elements).forEach(function(field) {
481				if (
482					!field.name ||
483					field.disabled ||
484					['file', 'reset', 'submit', 'button'].indexOf(field.type) > -1
485				)
486					return;
487				if (field.type === 'select-multiple') {
488					Array.prototype.slice.call(field.options).forEach(function(option) {
489						if (!option.selected) return;
490						arr.push(
491							encodeURIComponent(field.name) +
492								'=' +
493								encodeURIComponent(option.value)
494						);
495					});
496					return;
497				}
498				if (['checkbox', 'radio'].indexOf(field.type) > -1 && !field.checked)
499					return;
500				arr.push(
501					encodeURIComponent(field.name) + '=' + encodeURIComponent(field.value)
502				);
503			});
504			return arr.join('&');
505		}
506
507	}
508
509  if ( typeof customElements.get('product-form') == 'undefined' ) {
510		customElements.define('product-form', ProductForm);
511	}
512
513}

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.