Files

339 lines
9.2 KiB
JavaScript
Raw Permalink Normal View History

2021-03-16 16:19:46 +07:00
$(document).ready(function () {
$(window).scroll(function () {
if ($(window).scrollTop() > 206) {
$(".header").addClass("header-fix");
} else {
$(".header").removeClass("header-fix");
}
});
})
function tooltip() {
var w_tooltip = $("#tooltip").width();
var h_tooltip = 0;
var pad = 10;
var x_mouse = 0;
var y_mouse = 0;
var wrap_left = 0;
var wrap_right = 0;
var wrap_top = 0;
var wrap_bottom = 0;
$(".item_product .item_img").mousemove(function (e) {
content_tooltip = $(this).parents(".item_product").find(".hover_content_pro");
if (content_tooltip.length == 0) {
return;
$("#tooltip").hide();
}
$("#tooltip").html(content_tooltip.html());
wrap_left = 0;
wrap_top = $(window).scrollTop();
wrap_bottom = $(window).height();
wrap_right = $(window).width();
x_mouse = e.pageX;
y_mouse = e.pageY;
h_tooltip = $("#tooltip").height();
if (x_mouse + w_tooltip > wrap_right) $("#tooltip").css("left", x_mouse - w_tooltip - pad);
else $("#tooltip").css("left", x_mouse + pad);
if (y_mouse - h_tooltip < wrap_top) $("#tooltip").css("top", wrap_top);
else $("#tooltip").css("top", y_mouse - h_tooltip - pad);
$("#tooltip").show();
});
$(".item_product .item_img").mouseout(function () {
$("#tooltip").hide();
});
}
tooltip();
function getImageFancybox() {
$("#sync2").owlCarousel({
items: 4,
loop: false,
nav: false,
dots: false,
margin: 10
});
}
getImageFancybox();
// <!------ cart---->
$(document).ready(function () {
listenDeleteCartItem()
//hien thi tong don hang
calculateCartTotalPrice();
//thay doi so luong chon mua
listenQuantityChange();
//xoa sp
listenDeleteCartItem();
});
function check_field_new() {
var number_regex1 = /^[0]\d{9}$/i;
var number_regex2 = /^[0]\d{10}$/i;
var error = "";
var check_name = document.getElementById('buyer_name').value;
if (check_name.length < 2) error += "- Bạn chưa nhập tên\n";
else if (check_name.indexOf('<script') > -1) {
error += "- Họ tên chứa các ký tự không hợp lệ, bạn vui lòng kiểm tra lại\n";
}
var sex = $(".sex input[type='radio']:checked").val();
var check_email = document.getElementById('buyer_email').value;
if (check_email.length < 4) {
error += "- Bạn chưa nhập email\n";
}
if (check_email.length > 4) {
if (validateEmail(check_email) == false) error += "- Email không hợp lệ\n";
}
var check_tel = document.getElementById('buyer_tel').value;
if (check_tel.length < 4) error += "- Bạn chưa nhập SĐT\n";
else {
if (number_regex1.test(check_tel) == false && number_regex2.test(check_tel) == false) error += "- Số điện thoại chưa chính xác\n";
}
var check_province = document.getElementById('ship_to_province').value;
if (check_province == 0) error += "- Bạn chưa chọn Tỉnh/TP\n";
if (document.getElementById('ship_to_district')) {
var check_district = document.getElementById('ship_to_district').value;
if (check_district == 0) error += "- Bạn chưa chọn Quận/Huyện\n";
}
var check_add = document.getElementById('buyer_address').value;
if (check_add.length < 5) error += "- Bạn chưa nhập địa chỉ\n";
else if (check_add.indexOf('<script') > -1) {
error += "- Địa chỉ chứa các ký tự không hợp lệ, bạn vui lòng kiểm tra lại\n";
}
var check_note = document.getElementById('buyer_note').value;
if (check_note.indexOf('<script') > -1) {
error += "- Ghi chú chứa các ký tự không hợp lệ, bạn vui lòng kiểm tra lại\n";
}
if (error != "") {
alert(error);
// location.reload();
return false;
} else {
//$('#buyer_name').val(sex+" "+check_name);
$("#btn-submit").attr("disabled", "disabled").html("Đang xử lý...");
var dealInCart = checkDeal2020('.js-item-deal');
if (dealInCart.length == 0) {
$('#formCart').submit();
} else {
checkDealFromAjax(dealInCart);
}
}
//$('#buyer_name').val(sex+" "+check_name);
//$("#btn-submit").attr("disabled","disabled").html("Đang xử lý...");
return true;
}
var TOTAL_CART_VALUE_NODE = "#js-total-cart-price";
function listenDeleteCartItem() {
$(".delete-from-cart").on("click", function () {
if (confirm("Bạn muốn xóa sản phẩm này khỏi giỏ hàng!") == false) {
return;
}
var $row = $(this).closest(".js-item-row");
var item_type = $row.data("item_type");
var item_id = $row.data("item_id");
calculateCartTotalPrice();
console.log("deleting item from cart = " + item_type);
var deleteStatus;
//save to cart
switch (item_type) {
case "product":
deleteStatus = Hura.Cart.Product.remove(item_id, $row.data("variant_id"));
break;
case "deal":
deleteStatus = Hura.Cart.Deal.remove(item_id);
break;
case "combo":
deleteStatus = Hura.Cart.Combo.remove(item_id);
break;
}
if (deleteStatus) {
deleteStatus.then(function () {
$row.remove();
calculateCartTotalPrice();
if ($(".js-item-row").length == 0) location.reload();
})
}
})
}
//on page ready
//thay doi tong gia cua 1 san pham
function changeItemTotalPrice() {
var $row = $(this).closest(".js-item-row");
var item_type = $row.data("item_type");
var price = $row.find(".js-buy-price").attr("data-price");
var quantity = $row.find(".js-buy-quantity").val();
var total_price = price * quantity;
//loai bo so luong vo ly
if (quantity < 0) {
$row.find(".js-quantity-change").val(0);
total_price = 0;
}
$row.find(".js-total-item-price").html(formatCurrency(total_price));
calculateCartTotalPrice();
//console.log("updating cart = " + item_type);
//save to cart
switch (item_type) {
case "product":
Hura.Cart.Product.update($row.data("item_id"), $row.data("variant_id"), {
quantity: quantity
});
break;
case "deal":
Hura.Cart.Deal.update($row.data("item_id"), {
quantity: quantity
});
break;
case "combo":
Hura.Cart.Combo.update($row.data("item_id"), {
quantity: quantity
});
break;
}
}
//tinh tong gia don hang
function calculateCartTotalPrice() {
var dom_target = ".total-cart-price";
var dom_target_2 = ".total-cart-payment";
var total_cart_price = 0;
$(".total-item-price").each(function () {
total_cart_price += Hura.Util.strToNumber($(this).html());
});
//log for other reference
$("#js-total-before-fee-discount").val(total_cart_price);
//giam gia khac: voucher, membership, loyalty point conversion
var total_discount = 0;
total_discount += parseInt($("#js-discount-voucher").val());
total_discount += parseInt($("#js-discount-membership").val());
//Lưu giá trị voucher cho trang in:
var voucher_value = parseInt($("#js-discount-voucher").val());
//phi khac: shipping, COD...
var total_other_fee = 0;
total_other_fee += parseInt($("#js-fee-shipping").val());
total_other_fee += parseInt($("#js-fee-cod").val());
$(TOTAL_CART_VALUE_NODE).html(formatCurrency(total_cart_price));
}
//nghe thay doi so luong sp
function listenQuantityChange() {
var $track_change = $(".js-quantity-change");
//thay doi so luong sp mua, neu nhap so luong
$track_change.on("change", function (e) {
changeItemTotalPrice.call(this);
});
//thay doi so luong sp theo - hoac +
$track_change.on("click", function (e) {
if (e.target.nodeName === 'INPUT') return;
var quantity_change = parseInt(this.getAttribute("data-value"));
var $row = $(this).closest(".js-item-row");
var current_quantity = parseInt($row.find(".js-buy-quantity").val());
//loai bo so luong vo ly
if (current_quantity + quantity_change < 1 && 1 < 2) {
$row.find(".js-buy-quantity").val(1);
return;
}
$row.find(".js-buy-quantity").val(current_quantity + quantity_change);
//then update
changeItemTotalPrice.call(this);
});
}
//nghe xoa san pham
function listenDeleteCartItem() {
$(".delete-from-cart").on("click", function () {
var $row = $(this).closest(".js-item-row");
var item_type = $row.data("item_type");
var item_id = $row.data("item_id");
// calculateCartTotalPrice();
location.reload();
console.log("deleting item from cart = " + item_type);
var deleteStatus;
//save to cart
switch (item_type) {
case "product":
deleteStatus = Hura.Cart.Product.remove(item_id, $row.data("variant_id"));
break;
case "deal":
deleteStatus = Hura.Cart.Deal.remove(item_id);
break;
case "combo":
deleteStatus = Hura.Cart.Combo.remove(item_id);
break;
}
if (deleteStatus) {
deleteStatus.then(function () {
$row.remove();
})
}
})
}
function _strToNumber(str) {
str += ''; //convert to str incase it's already a number
while (str.indexOf(".") > 0) {
str = str.replace('.', '');
}
var result = parseFloat(str);
return isNaN(result) ? 0 : result;
}
// <!----- end cart------>