Browse Source

Front-end Shop module JS, buy button, basket button, JS optimization

Vova Tkach 5 years ago
parent
commit
6da4524d46

+ 1 - 1
hosts/localhost/template/header.html

@@ -47,7 +47,7 @@
 								<a class="nav-link{{if eq $.Data.Module "404"}} active{{end}}" href="/not-existent-page/">404</a>
 							</li>
 							<li class="nav-item">
-								<a class="nav-link basket" href="#">Basket <span class="badge badge-pill badge-primary">0</span></a>
+								<a id="basket-nav-btn" class="nav-link" href="" onclick="window&&window.frontend&&frontend.ShopOpenBasket(this);return false;">Basket <span class="badge badge-pill badge-primary">0</span></a>
 							</li>
 						</ul>
 					</div>

+ 81 - 27
hosts/localhost/template/scripts.js

@@ -1,28 +1,82 @@
-var GlobalLastProductImage = '';
-
-function ShopProductImagesLightGallery() {
-	$('#product_thumbnails').lightGallery({
-		pager: false,
-		showThumbByDefault: false,
-		toogleThumb: false,
-		controls: false,
-		download: false
-	});
-	$('#product_image img').css('cursor', 'pointer').click(function() {
-		$($('#product_thumbnails a.thumbnail').get($(this).data('index'))).click();
-	});
-	$('#product_thumbnails a.thumbnail').each(function() {
-		$(this).mouseover(function() {
-			if($(this).data('hover') != GlobalLastProductImage) {
-				GlobalLastProductImage = $(this).data('hover');
-				$('#product_image img').attr('src', $(this).data('hover'));
-				$('#product_image img').data('index', $(this).data('index'));
+(function(window, $) {
+	var frontend = function(window, $) {
+		var ShopProductsLightGalleryLastImage = '';
+
+		// Private
+		function ShopProductsInitLightGallery() {
+			$('#product_thumbnails').lightGallery({
+				pager: false,
+				showThumbByDefault: false,
+				toogleThumb: false,
+				controls: false,
+				download: false
+			});
+			$('#product_image img').css('cursor', 'pointer').click(function() {
+				$($('#product_thumbnails a.thumbnail').get($(this).data('index'))).click();
+			});
+			$('#product_thumbnails a.thumbnail').each(function() {
+				$(this).mouseover(function() {
+					if($(this).data('hover') != ShopProductsLightGalleryLastImage) {
+						ShopProductsLightGalleryLastImage = $(this).data('hover');
+						$('#product_image img').attr('src', $(this).data('hover'));
+						$('#product_image img').data('index', $(this).data('index'));
+					}
+				});
+			});
+		};
+
+		function ShopSetBasketNavBtnProductsCount(value) {
+			$('#basket-nav-btn .badge').html(value);
+		};
+
+		function Initialize() {
+			// Check if jQuery was loaded
+			if(typeof $ == 'function') {
+				ShopProductsInitLightGallery();
+			} else {
+				console.log('Error: jQuery is not loaded!');
 			}
-		});
-	});
-}
-
-// Init all
-$(document).ready(function() {
-	ShopProductImagesLightGallery();
-});
+		};
+
+		// Initialize
+		if(window.addEventListener) {
+			// W3C standard
+			window.addEventListener('load', Initialize, false);
+		} else if(window.attachEvent) {
+			// Microsoft
+			window.attachEvent('onload', Initialize);
+		};
+
+		// Public
+		return {
+			ShopOpenBasket: function(object) {
+				if(!$(object).hasClass('click-blocked')) {
+					$(object).addClass('click-blocked');
+
+					ShopSetBasketNavBtnProductsCount(0);
+					console.log('ShopOpenBasket', object);
+
+					$(object).removeClass('click-blocked');
+				}
+				return false;
+			},
+
+			// TODO: add product to basket or count++ if already in basket
+			// Update products counter in header nav bar button
+			// Automatically open basket popup
+			ShopAddProductToBasket: function(object, product_id) {
+				if(!$(object).hasClass('click-blocked')) {
+					$(object).addClass('click-blocked');
+
+					ShopSetBasketNavBtnProductsCount(product_id);
+					console.log('ShopAddProductToBasket', object, product_id);
+
+					$(object).removeClass('click-blocked');
+				}
+				return false;
+			},
+		};
+	}(window, $);
+
+	window.frontend = frontend;
+}(window, jQuery));

+ 1 - 1
hosts/localhost/template/shop-category.html

@@ -26,7 +26,7 @@
 						<div class="card-text">{{.Briefly}}</div>
 					</div>
 					<div class="card-footer">
-						<span class="price">{{.PriceFormat "%.2f"}} {{.Currency.Code}}</span><a href="{{.Permalink}}" class="btn btn-primary">View</a>
+						<span class="price">{{.PriceFormat "%.2f"}} {{.Currency.Code}}</span><a href="{{.Permalink}}" class="btn btn-success" onclick="window&&window.frontend&&frontend.ShopAddProductToBasket(this, {{.Id}});return false;">Buy</a>
 					</div>
 				</div>
 			{{end}}

+ 2 - 2
hosts/localhost/template/shop-product.html

@@ -47,7 +47,7 @@
 						{{end}}
 						<div class="card mt-3{{if not $.Data.Shop.Product.HaveVariations}} mt-sm-3 mt-md-0 mt-lg-0{{end}}">
 							<div class="card-body">
-								<h3 class="price mb-0 mr-4">{{$.Data.Shop.Product.PriceFormat "%.2f"}} {{$.Data.Shop.Product.Currency.Code}}</h3><a href="" class="btn btn-success btn-buy">Buy</a>
+								<h3 class="price mb-0 mr-4">{{$.Data.Shop.Product.PriceFormat "%.2f"}} {{$.Data.Shop.Product.Currency.Code}}</h3><a href="" class="btn btn-success btn-buy" onclick="window&&window.frontend&&frontend.ShopAddProductToBasket(this, {{$.Data.Shop.Product.Id}});return false;">Buy</a>
 							</div>
 						</div>
 						<div class="card mt-3">
@@ -115,7 +115,7 @@
 							</div>
 							<div class="card mt-3">
 								<div class="card-body">
-									<h3 class="price mb-0 mr-4">{{$.Data.Shop.Product.PriceFormat "%.2f"}} {{$.Data.Shop.Product.Currency.Code}}</h3><a href="" class="btn btn-success btn-buy">Buy</a>
+									<h3 class="price mb-0 mr-4">{{$.Data.Shop.Product.PriceFormat "%.2f"}} {{$.Data.Shop.Product.Currency.Code}}</h3><a href="" class="btn btn-success btn-buy" onclick="window&&window.frontend&&frontend.ShopAddProductToBasket(this, {{$.Data.Shop.Product.Id}});return false;">Buy</a>
 								</div>
 							</div>
 						</div>

+ 1 - 1
hosts/localhost/template/shop.html

@@ -26,7 +26,7 @@
 						<div class="card-text">{{.Briefly}}</div>
 					</div>
 					<div class="card-footer">
-						<span class="price">{{.PriceFormat "%.2f"}} {{.Currency.Code}}</span><a href="{{.Permalink}}" class="btn btn-primary">View</a>
+						<span class="price">{{.PriceFormat "%.2f"}} {{.Currency.Code}}</span><a href="{{.Permalink}}" class="btn btn-success" onclick="window&&window.frontend&&frontend.ShopAddProductToBasket(this, {{.Id}});return false;">Buy</a>
 					</div>
 				</div>
 			{{end}}

+ 1 - 1
hosts/localhost/template/styles.css

@@ -100,7 +100,7 @@ footer {
 }
 
 /* Basket button */
-a.nav-link.basket .badge {
+#basket-nav-btn .badge {
 	vertical-align: top;
 	margin-top: 3px;
 }