Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var hidewish = function() {
- $(".wishlist--empty").show();
- $(".wishlist__items").hide();
- };
- var showwish = function() {
- $(".wishlist--empty").hide();
- $(".wishlist__items").show();
- };
- $(".wishlist__items").hide();
- // Wish Function
- var wish = {
- items: []
- };
- var update_product = function(product) {};
- $(function() {
- //Add to wish
- var addToWish = function(product, qty) {
- qty = qty || 1;
- var wish = getWish();
- var indexOfId = wish.items.findIndex(x => x.id == product.id);
- if (indexOfId === -1) {
- wish.items.push({
- id: product.id,
- img: product.img,
- name: product.name,
- location: product.location,
- price: product.price
- });
- $parent = $("#" + product.id).closest(".items__cart");
- $parent
- .find(".wish-icon")
- .addClass("active")
- .attr("data-prefix", "fas");
- } else {
- wish.items[indexOfId].qty++;
- wish.items[indexOfId].stock = Number(product.stock);
- }
- //Update popup wish
- updateWish(wish);
- if (wish.items.length > 0) {
- showwish();
- }
- };
- //Remove from wish on id
- var removeFromWish = function(id) {
- var wish = getWish();
- var wishIndex = wish.items.findIndex(x => x.id == id);
- wish.items.splice(wishIndex, 1);
- $parent = $("#" + id).closest(".items__cart");
- $parent
- .find(".wish-icon")
- .first()
- .removeClass("active")
- .attr("data-prefix", "far");
- //Update popup wish
- updateWish(wish);
- if (wish.items.length < 1) {
- hidewish();
- }
- };
- var getProductValues = function(element) {
- var productId = $(element)
- .closest(".items__cart")
- .find(".item__tile")
- .attr("id");
- var productImg = $(element)
- .closest(".items__cart")
- .find(".item__img")
- .attr("src");
- var productName = $(element)
- .closest(".items__cart")
- .find(".item__title")
- .html();
- var productPrice = $(element)
- .closest(".items__cart")
- .find(".cost")
- .html();
- var productLocation = $(element)
- .closest(".items__cart")
- .find(".item__location")
- .html();
- return {
- id: productId,
- img: productImg,
- name: productName,
- location: productLocation,
- price: productPrice
- };
- };
- $(".my-wish-add").on("change", function() {
- var product = getProductValues(this);
- if ($(this).is(":checked")) {
- addToWish({
- id: product.id,
- img: product.img,
- name: product.name,
- location: product.location,
- price: product.price
- });
- } else {
- removeFromWish(product.id);
- }
- });
- //Update wish html to reflect changes
- var updateWish = function(wish) {
- //Add to shopping wish dropdown
- $(".wishlist__items").html("");
- for (var i = 0; i < wish.items.length; i++) {
- $(".wishlist__items").append(
- "<li class='wish__item'>" +
- '<div class="wish__thumb">' +
- "<img src='" +
- wish.items[i].img +
- "' />" +
- "</div>" +
- '<div class="wish__info">' +
- '<div class="wish-name">' +
- wish.items[i].name +
- "</div>" +
- '<div class="wish__title">' +
- wish.items[i].location +
- "</div>" +
- ' <div class="wish__value">' +
- "<span>Price: </span> $ " +
- wish.items[i].price +
- "</div>" +
- "</div>" +
- '<div class="wish__remove">' +
- '<label class="wish__label">' +
- '<input type="checkbox" id="my-wish-remove' +
- i +
- '" class="my-wish-remove" aria-hidden="true">' +
- "<i class='fas fa-heart'></i>" +
- "</div>" +
- "</div>"
- );
- (function() {
- var currentIndex = i;
- $("#my-wish-remove" + currentIndex).on("change", function() {
- $(this)
- .closest("li")
- .hide(400);
- setTimeout(function() {
- wish.items[currentIndex].stock = "";
- update_product(wish.items[currentIndex]);
- $("#"+wish.items[currentIndex].id).parents().find($(".wish-btn > input")).prop("checked", false);
- removeFromWish(wish.items[currentIndex].id);
- }, 400);
- });
- })();
- }
- };
- //Get Wish
- var getWish = function() {
- var myWish = wish;
- return myWish;
- };
- });
Add Comment
Please, Sign In to add comment