Guest User

Untitled

a guest
Nov 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. var hidewish = function() {
  2. $(".wishlist--empty").show();
  3. $(".wishlist__items").hide();
  4. };
  5. var showwish = function() {
  6. $(".wishlist--empty").hide();
  7. $(".wishlist__items").show();
  8. };
  9. $(".wishlist__items").hide();
  10. // Wish Function
  11. var wish = {
  12. items: []
  13. };
  14. var update_product = function(product) {};
  15. $(function() {
  16. //Add to wish
  17. var addToWish = function(product, qty) {
  18. qty = qty || 1;
  19. var wish = getWish();
  20. var indexOfId = wish.items.findIndex(x => x.id == product.id);
  21. if (indexOfId === -1) {
  22. wish.items.push({
  23. id: product.id,
  24. img: product.img,
  25. name: product.name,
  26. location: product.location,
  27. price: product.price
  28. });
  29. $parent = $("#" + product.id).closest(".items__cart");
  30. $parent
  31. .find(".wish-icon")
  32. .addClass("active")
  33. .attr("data-prefix", "fas");
  34. } else {
  35. wish.items[indexOfId].qty++;
  36. wish.items[indexOfId].stock = Number(product.stock);
  37. }
  38. //Update popup wish
  39. updateWish(wish);
  40. if (wish.items.length > 0) {
  41. showwish();
  42. }
  43. };
  44. //Remove from wish on id
  45. var removeFromWish = function(id) {
  46. var wish = getWish();
  47. var wishIndex = wish.items.findIndex(x => x.id == id);
  48. wish.items.splice(wishIndex, 1);
  49. $parent = $("#" + id).closest(".items__cart");
  50. $parent
  51. .find(".wish-icon")
  52. .first()
  53. .removeClass("active")
  54. .attr("data-prefix", "far");
  55. //Update popup wish
  56. updateWish(wish);
  57. if (wish.items.length < 1) {
  58. hidewish();
  59. }
  60. };
  61.  
  62. var getProductValues = function(element) {
  63. var productId = $(element)
  64. .closest(".items__cart")
  65. .find(".item__tile")
  66. .attr("id");
  67. var productImg = $(element)
  68. .closest(".items__cart")
  69. .find(".item__img")
  70. .attr("src");
  71. var productName = $(element)
  72. .closest(".items__cart")
  73. .find(".item__title")
  74. .html();
  75. var productPrice = $(element)
  76. .closest(".items__cart")
  77. .find(".cost")
  78. .html();
  79. var productLocation = $(element)
  80. .closest(".items__cart")
  81. .find(".item__location")
  82. .html();
  83. return {
  84. id: productId,
  85. img: productImg,
  86. name: productName,
  87. location: productLocation,
  88. price: productPrice
  89. };
  90. };
  91.  
  92. $(".my-wish-add").on("change", function() {
  93. var product = getProductValues(this);
  94. if ($(this).is(":checked")) {
  95. addToWish({
  96. id: product.id,
  97. img: product.img,
  98. name: product.name,
  99. location: product.location,
  100. price: product.price
  101. });
  102. } else {
  103. removeFromWish(product.id);
  104. }
  105. });
  106. //Update wish html to reflect changes
  107. var updateWish = function(wish) {
  108. //Add to shopping wish dropdown
  109. $(".wishlist__items").html("");
  110. for (var i = 0; i < wish.items.length; i++) {
  111. $(".wishlist__items").append(
  112. "<li class='wish__item'>" +
  113. '<div class="wish__thumb">' +
  114. "<img src='" +
  115. wish.items[i].img +
  116. "' />" +
  117. "</div>" +
  118. '<div class="wish__info">' +
  119. '<div class="wish-name">' +
  120. wish.items[i].name +
  121. "</div>" +
  122. '<div class="wish__title">' +
  123. wish.items[i].location +
  124. "</div>" +
  125. ' <div class="wish__value">' +
  126. "<span>Price: </span> $ " +
  127. wish.items[i].price +
  128. "</div>" +
  129. "</div>" +
  130. '<div class="wish__remove">' +
  131. '<label class="wish__label">' +
  132. '<input type="checkbox" id="my-wish-remove' +
  133. i +
  134. '" class="my-wish-remove" aria-hidden="true">' +
  135. "<i class='fas fa-heart'></i>" +
  136. "</div>" +
  137. "</div>"
  138. );
  139. (function() {
  140. var currentIndex = i;
  141. $("#my-wish-remove" + currentIndex).on("change", function() {
  142. $(this)
  143. .closest("li")
  144. .hide(400);
  145. setTimeout(function() {
  146. wish.items[currentIndex].stock = "";
  147. update_product(wish.items[currentIndex]);
  148. $("#"+wish.items[currentIndex].id).parents().find($(".wish-btn > input")).prop("checked", false);
  149. removeFromWish(wish.items[currentIndex].id);
  150. }, 400);
  151. });
  152. })();
  153. }
  154. };
  155.  
  156.  
  157. //Get Wish
  158. var getWish = function() {
  159. var myWish = wish;
  160. return myWish;
  161. };
  162. });
Add Comment
Please, Sign In to add comment