PageSourceSearch

https://www.dunstan.co.nz/edit/modules/products/products.js?version=1.0.10

js dunstan.co.nz collected 2026-10-08 07:13:08 UTC 18,269 bytes, 669 lines download raw bytes

1// Products JavaScript Document
2
3	$(document).ready(function() {
4		"use strict";
5        
6        // Hilde Layout-side when empty
7        if($("#product-details-container .content.layout-three-col > *:first-child").children().length == 1 
8           && $("#product-details-container .content.layout-three-col > *:first-child > b.clearer").length == 1 ) {
9           //
10           $("#product-details-container .content > *:first-child > *").parent().addClass("no-content");
11        }
12     
13		$('.feedfinder-submit').hide();
14
15		$('.feedfinder-radio-option input, .feedfinder-grid .grid-item input').on('click', function(){
16
17			$(this).closest('form').submit();
18		});
19
20		// Product tabs
21		$('.product-description-tabs .tabs a').on('click', function(e){
22
23			e.preventDefault();
24			var id = $(this).attr('href');
25
26			$('.product-description-tabs .tabs li.active').removeClass('active');
27			$(this).parent().addClass('active');
28
29			$('.product-description-tabs .content li.show').removeClass('show');
30			$(id).addClass('show');
31		});
32
33		// Check for product items then match height
34		if($.fn.matchHeight && $(".products .product-item").length > 0) {
35			
36			//
37			$(".products .product-item").matchHeight({
38				byRow: true,
39				property: "height"
40			});
41		}
42		
43		// Open product images in magnific popup
44		if($.fn.magnificPopup) {
45			//
46			$('a.product-details-image, a.product-details-images-img').magnificPopup({
47				type:'image'
48			});	
49		}
50		
51		// Open product images in colorbox
52		else if($.fn.colorbox) { 
53			//
54			$("a[class=\'product-details-image\'], a[class=\'product-details-images-img\']").colorbox({photo:true},function(){ 
55				var titleheight = $("#cboxTitle").height(); 
56				$("#colorbox").height($("#colorbox").height() + titleheight); 
57				$("#cboxWrapper").height($("#cboxWrapper").height() + titleheight); 
58				$("#cboxMiddleLeft").height($("#cboxMiddleLeft").height() + titleheight); 
59				$("#cboxMiddleRight").height($("#cboxMiddleRight").height() + titleheight); 
60				$("#cboxContent").height($("#cboxContent").height() + titleheight); 
61				$("#cboxLoadedContent").height($("#cboxLoadedContent").height() + titleheight); 
62			});
63		}
64		
65		
66		// Change image
67		if($(".product-details-thumbnails").length > 0) {
68			
69			//
70			$(".product-details-thumbnails a").on("click",function(event) {
71				
72				//
73				event.preventDefault();
74				
75				// Image
76				var image = $(this).data('image');
77				
78				change_product_image(image);
79			});
80		}
81		
82		
83		// Product details buy
84		if($(".product-details-buy").length > 0) {
85				
86			// Show hidden areas
87			$(".product-details-buy-price").show();
88			$(".product-details-buy-add-quantity").show();
89			
90			// Create options dropdown/dropdowns/radio buttons in product details if more than one sub part
91			if($(".product-details-buy .product-details-buy-add").length > 1) {
92				
93				// Master part
94				var master = $(".product-details-buy-options").data('masterpart');
95				
96				//
97				var displayType = 'standard';
98				//
99				if($(".product-details-buy-options").hasClass('radio-options')) {displayType = 'radio';}
100				if($(".product-details-buy-options").hasClass('dropdown-options')) {displayType = 'dropdown';}
101				
102				
103				// Create options
104				var options = new Array();
105				var optionFields = $(".product-details-buy-options").data('options');
106				var optionTitles = $(".product-details-buy-options").data('option-titles');
107				
108				// First options
109				var firstOptions = new Array();
110				
111				//
112				var price = '';
113				var count = 0;
114				
115				// Show hidden areas
116				$(".product-details-buy-options").show();
117				
118				// For each option
119				$(".product-details-buy .product-details-buy-add").each(function() {
120							
121					// Get part options
122					var partOptions = $(this).data('options');
123					
124					// Switch display type
125					switch(displayType) {
126						case 'radio' :
127						case 'dropdown' : {
128							
129							// Set titles of first options only
130							var title = partOptions[0];
131							
132							// Create option
133							if(firstOptions.indexOf(title) < 0) {
134								//
135								var optionsObject = {
136									master:		master,
137									recordid: 	$(this).data('recordid'),
138									title: 		title,
139									first:		partOptions[0],
140									options:	partOptions,
141								};
142								
143								// Add object to options array
144								options.push(optionsObject);
145								
146								// Add title to first options array
147								firstOptions.push(title);
148							}
149							
150							//
151							break;	
152						}
153						case 'standard' :
154						default : {
155							// Title
156							{
157								var title = '';
158								
159								// Add options to title
160								if(partOptions.length > 0) {
161									partOptions.forEach(function(item,index) {
162										//
163										title += (title !== '' && item !== '') ? ' - ' : '';
164										title += (item !== '') ? item : '';
165									});
166								}
167								
168								// Set title
169								else {
170									title += $(this).data('title');
171								}
172								
173								// Set price
174								if($(this).data('price') !== '') {
175									//
176									title += ' - '+$(this).data('price');
177									title += $(this).data('pricepost');
178								}
179								
180								// Set out of stock
181								if($(this).data('outofstock') !== '') {
182									//
183									title += ' - '+$(this).data('outofstock');
184								}
185							}
186							
187							// Create option
188							{
189								//
190								var optionsObject = {
191									master:		master,
192									recordid: 	$(this).data('recordid'),
193									title: 		title,
194									first:		partOptions[0],
195									options:	partOptions,
196								};
197								
198								// Add object to options array
199								options.push(optionsObject);
200							}
201							
202							//
203							break;
204						}
205					}
206					
207					// Set price to show
208					if(count === 0) {
209						//
210						price = get_formatted_pricing($(this));
211						
212						// Hide all options if display type isn't standard
213						if(displayType !== 'standard') {
214							$(this).hide();
215						}
216					}
217					else {
218						// Hide others
219						$(this).hide();
220					}
221					
222					// Hide stacked titles
223					$(this).find(".product-details-buy-add-title").hide();
224					$(this).removeClass('stacked');
225					
226					count++;
227				});
228				
229				
230				// If there are options the generate dropdowns/radio buttons
231				if(options.length > 0) {
232					
233					var html = '';
234					
235					switch(displayType) {
236						// Create radio button options
237						case 'radio' : {
238							//
239							html += '<div class="buy-radio-group '+optionFields[0]+'-option">';
240							
241							// Build title
242							html += '<span class="buy-radio-group-title">'+optionTitles[0]+'</span>';
243							
244							// Build options
245							options.forEach(function(item,index) {
246								
247								//
248								html += '<input type="radio" id="'+optionFields[0]+'-'+item.recordid+'" name="'+optionFields[0]+'-option" value="'+item.recordid+'" class="buy-radio" data-option="'+optionFields[0]+'" />';
249								html += '<label for="'+optionFields[0]+'-'+item.recordid+'">'+item.first+'</label>';
250							});
251								
252							//
253							html += '</div>';
254							
255							break;
256						}
257						// Create dropdown options
258						case 'dropdown' : {
259							//
260							html += '<div class="buy-dropdowns '+optionFields[0]+'-option">';
261							
262							// Build title
263							html += '<span class="buy-dropdowns-title">'+optionTitles[0]+'</span>';
264							
265							// Build select box
266							html += '<select name="'+optionFields[0]+'-option" class="buy-dropdown" data-option="'+optionFields[0]+'">';
267							html += '<option value="">Select a '+optionTitles[0].toLowerCase()+'...</option>';
268							
269							// Build options
270							options.forEach(function(item,index) {
271								
272								//
273								html += '<option value="'+item.recordid+'">'+item.title+'</option>';
274							});
275							
276							//
277							html += '</select>';
278								
279							//
280							html += '</div>';
281							
282							break;
283						}
284						// Create single dropdown
285						case 'standard' :
286						default : {
287							
288							// Build select box
289							html += '<select class="buy-standard" name="option">';
290							
291							// Build options
292							options.forEach(function(item,index) {
293								//
294								html += '<option value="'+item.recordid+'">'+item.title+'</option>';
295							});
296							
297							//
298							html += '</select>';
299					
300							// Add price to dom
301							$(".product-details-buy-price").html(price);
302							
303							break;
304						}
305					}	
306					
307					// Add dropdown to dom
308					$(".product-details-buy-options").html(html);	
309				}
310			}
311			
312			// Else show single option
313			else if($(".product-details-buy .product-details-buy-add").length === 1) {
314				
315				// Show price
316				$(".product-details-buy .product-details-buy-add").each(function() {
317					
318					//
319					var price = get_formatted_pricing($(this));
320					
321					// Add price to dom
322					$(".product-details-buy-price").html(price);
323					$(this).find(".product-details-buy-add-title .price").hide();
324				});
325			}
326			
327			
328			// On dropdowns / radio button option select
329			{
330				$(".product-details-buy").on("change",".buy-radio, .buy-dropdown",function() {
331					
332					// Hide all add buttons
333					$(".product-details-buy .product-details-buy-add").hide();
334					
335					//
336					var value  = $(this).val();
337					var option = $(this).data("option");
338					var displayType = ($(this).hasClass("buy-radio")) ? 'radio' : 'dropdown';
339					
340					// Get list of option title
341					var optionTitles = $(".product-details-buy-options").data('option-titles');
342					
343					// Get list of option fields
344					var optionFields = $(".product-details-buy-options").data('options');
345					var currentFound = false;
346					
347					// Delete all after current option
348					optionFields.forEach(function(item,index) {
349						//
350						if(currentFound) {
351							$("."+item+"-option").remove();
352						}
353						else if(item === option) {
354							currentFound = true;	
355						}
356					});
357					
358					// Build list of options selected
359					build_options_selected();
360					
361					// Get selected
362					var master   = $(".product-details-buy-options").data("masterpart");
363					var selected = $(".product-details-buy-options").data("selected");
364					
365					// Get available options
366					$.ajax({
367						type: "GET",
368						url: pageRelativity+'edit/modules/products/products.ajax.php',
369						dataType: "jsonp",
370						jsonp: "callback",
371						timeout: 5000,
372						data: {
373							type: 'get-product-options',
374							option: option,
375							master: master,
376							selected: selected,
377							titles: optionTitles,
378							displayType: displayType,
379							relative: pageRelativity
380						},
381						success: function(data) {
382							// If data
383							if(data.count === 1 && data.itemId !== '') {
384								
385								// Show add button
386								show_add_button(data.itemId);
387							}
388							else if(data.count > 0 && data.html) {
389								// Default price
390								default_pricing();
391								//
392								$(".product-details-buy-options").append(data.html);
393								
394								// Check for single option
395								if($(".product-details-buy ."+data.option+"-option input").length === 1) {
396									
397									// Select single option
398									$(".product-details-buy ."+data.option+"-option input").prop("checked",true);
399									$(".product-details-buy ."+data.option+"-option input").trigger("change");
400								}
401							}
402							else {
403								
404							}
405						},
406						error: function(request, status, error) {
407							//
408							alert(request.responseText);
409						}
410					});
411				});
412			}
413			
414			
415			// On single dropdown select
416			{
417				$(".product-details-buy").on("change",".buy-standard",function() {
418					//
419					var itemId = $(this).val();
420					
421					// Show add button
422					show_add_button(itemId);
423				});
424			}
425			
426			
427			// On quanity change
428			{
429				$(".product-details-buy-add-quantity .buy-quantity").on("keyup",function() {
430					
431					var value = parseInt($(this).val());
432					
433					//
434					if(!isNaN(value) && value > 0) {
435						
436						//
437						$(".buy-quantity").val(value);
438					}
439				});
440			}
441			
442			
443			// Automatically add item to cart
444			{
445				// Ajax action for product buy form
446				$(".buy-product").submit(function(event) {
447					
448					// Stop submit
449					event.preventDefault();
450					
451					// Time process
452					var start = new Date();
453					var minDelay = 1000;
454					
455					// Set button title
456					var form      = $(this);
457					var buyButton = $(form).children("input[type=submit]");
458					
459					$(buyButton).attr("value","Adding product to cart...");
460					$(buyButton).attr("disabled","disabled");
461					
462					$(".message-input").hide();
463					
464					// Get form data
465					var dataString = $(form).serialize();
466					  
467					$.ajax({  
468						type: "POST",  
469						url: pageRelativity+'edit/shop/cart.ajax.php?add=1',  
470						data: dataString,  
471						success: function(data) {
472							
473							// For timing
474							var end = new Date();
475							var timeInMilliseconds = end-start;
476							
477							// Delay till minimum time is reached
478							if(timeInMilliseconds < minDelay) {
479								setTimeout(function() {
480									add_item_to_summary_cart(data,buyButton);
481									},minDelay-timeInMilliseconds);
482							}
483							else {
484								add_item_to_summary_cart(data,buyButton);
485							}
486						},
487						error: function(request, status, error) {
488							//
489							$(form).submit();
490						}
491					});
492					
493					//
494					return false;
495				});
496			}
497		}
498	});
499	
500	
501	// Build options selected data array
502	function build_options_selected() {
503		"use strict";
504		
505		// Get display type
506		var displayType = 'standard';
507		//
508		if($(".product-details-buy-options").hasClass('radio-options')) {displayType = 'radio';}
509		if($(".product-details-buy-options").hasClass('dropdown-options')) {displayType = 'dropdown';}
510		
511		// Set options array
512		var options = [];
513		
514		switch(displayType) {
515			case 'radio' : {
516				//
517				$(".product-details-buy .buy-radio-group").each(function() {
518					//
519					var option = $(this).find("input[type=radio]:checked").val();
520					
521					// Add option to array
522					options.push(option);
523				});
524				
525				//
526				break;
527			}
528			case 'dropdown' : {
529				//
530				$(".product-details-buy .buy-dropdowns").each(function() {
531					//
532					var option = $(this).find("select").val();
533					
534					// Add option to array
535					options.push(option);
536				});
537				
538				//
539				break;
540			}
541		}
542		
543		// Update selected list
544		$(".product-details-buy-options").data("selected",options);
545	}
546	
547	
548	// Show add button
549	function show_add_button(itemId) {
550		"use strict";
551		
552		//
553		var option = $("#"+itemId);
554		var price  = get_formatted_pricing($(option));
555		
556		// Add price to dom
557		$(".product-details-buy-price").html(price);
558		
559		// Change image
560		if($(option).data('image')) {
561			
562			change_product_image($(option).data('image'));
563		}
564		
565		// Hide all add buttons
566		$(".product-details-buy .product-details-buy-add").hide();
567		
568		// Show option add button
569		$(option).show();
570	}
571	
572	
573	// Change image
574	function change_product_image(image) {
575		"use strict";
576		
577		if($(".product-details-images #"+image).length > 0) {
578			// Remove current
579			$(".product-details-images .product-details-images-img").removeClass("current-image");
580			$(".product-details-thumbnails a").removeClass("current-image");
581			
582			// Change current
583			$(".product-details-images #"+image).addClass("current-image");
584			$(".product-details-thumbnails #"+image+"_thumbnail").addClass("current-image");
585		}
586	}
587	
588	
589	// Get formatted price
590	function default_pricing() {
591		"use strict";
592		
593		//
594		var price     	= '';
595		var priceVal  	= $(".product-details-buy-options").data('default-price');
596		var normalVal 	= $(".product-details-buy-options").data('default-normal');
597		var priceText	= $(".product-details-buy-options").data('default-pricetext');
598		var specialText	= $(".product-details-buy-options").data('default-specialtext');
599		var wasText   	= $(".product-details-buy-options").data('default-wastext');
600		var pricePost 	= $(".product-details-buy-options").data('default-pricepost');
601		
602		//
603		price += (priceVal !== normalVal) ? '<h4>'+specialText+'</h4>' : '<h4>'+priceText+'</h4>';
604		price += '<h2>'+priceVal+'<span class="price-post">'+pricePost+'</span></h2>';
605		price += (priceVal !== normalVal) ? '<span class="was">'+wasText+'<del>'+normalVal+'</del></span>' : '';	
606		
607		// Add price to dom
608		$(".product-details-buy-price").html(price);
609	}
610	
611	
612	// Get formatted price
613	function get_formatted_pricing(option) {
614		"use strict";
615		
616		//
617		var price     	= '';
618		var priceVal  	= $(option).data('price');
619		var normalVal 	= $(option).data('normal');
620		var priceText	= $(option).data('pricetext');
621		var specialText	= $(option).data('specialtext');
622		var wasText   	= $(option).data('wastext');
623		var pricePost 	= $(option).data('pricepost');
624		
625		//
626		price += (priceVal !== normalVal) ? '<h4>'+specialText+'</h4>' : '<h4>'+priceText+'</h4>';
627		price += '<h2>'+priceVal+'<span class="price-post">'+pricePost+'</span></h2>';
628		price += (priceVal !== normalVal) ? '<span class="was">'+wasText+'<del>'+normalVal+'</del></span>' : '';	
629		
630		//
631		return price;
632	}
633	
634	
635	// Add product to cart
636	function add_item_to_summary_cart(data,buyButton) {
637		"use strict";
638		
639		if(data === "error") {
640			
641			// Show error message
642			update_messages();
643			$(".message-input").show();
644			
645			$(buyButton).attr("value","Add to Cart");
646			$(buyButton).attr("disabled","");
647		}
648		else if(data !== "") {
649			
650			// Change button to added
651			$(buyButton).attr("value","Product added");
652			$(".view-cart-link").show();
653			
654			// Add to side cart
655			updateCartSummery();
656			update_messages();
657			$(".message-input").show();
658		}
659		else {
660			
661			//
662			$(".message-input").show();
663			$(buyButton).attr("value","Add to Cart");
664			$(buyButton).attr("disabled","");
665		}
666	}
667	
668	
669	

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.