1define([ 2 'jquery', 3 'underscore', 4 'Magento_Customer/js/customer-data', 5 'jquery/ui', 6 'jquery/jquery-storageapi' 7], function ($, _, customerData) { 8 $.widget('mage.productItems', { 9 _create: function () { 10 var self = this; 11 self.wishlistBtn = self.element.find('.towishlist'); 12 self.wishlistPopup = self.element.find('.pop-up-box'); 13 14 /* On click wishlist button */ 15 self.wishlistBtn.on('click', function (event) { 16 if (self._isUserLoggedIn()) { 17 /* Check if user logged in - Add to wishlist */ 18 event.preventDefault(); 19 event.stopImmediatePropagation(); 20 self._toggleWishList(this); 21 } 22 }); 23 24 /* Listen for trigger for closing wish list. This is the *only* code that hides the popup */ 25 $(window).on('close-wishlist', function () { 26 $('.wishlist-container').children().fadeOut(200).filter('.wishlist').remove(); 27 $('body').off('click.snb.wishlist.close'); 28 }); 29 30 self._checkWishListActive(); 31 }, 32 33 _toggleWishList: function (button) { 34 var self = this; 35 var remove = false; 36 var wishListData = $(button).data('post'); 37 var formKey = $("input[name=form_key]").val(); 38 wishListData.data.form_key = formKey; 39 window.currentProduct = $(self.element).parents('.product-item-information').find('.product-item-link').text().trim(); 40 var whishlistId = jQuery(self.element).find('.towishlist').attr('id'); 41 if (jQuery('.wl-' + whishlistId).hasClass('wishlisted') == true) { 42 wishListData.action = window.location.origin+'/wishlist/index/remove/'; 43 var wishlistItemId = null; 44 var wishlistCollection = JSON.parse(localStorage.getItem('mage-cache-storage')).wishlist; 45 _.each(wishlistCollection.items, function(item) { 46 if (item.product_id == wishListData.data.product) { 47 wishlistItemId = item.wishlist_item_id 48 } 49 }); 50 wishListData.data.item = wishlistItemId; 51 remove = true; 52 } else { 53 wishListData.action = window.location.origin+'/wishlist/index/add/'; 54 } 55 56 let $body = $('body'); 57 $body.trigger('processStart'); 58 59 $.post(wishListData.action, wishListData.data) 60 .done(function (response) { 61 const isRemove = wishListData.action.includes("remove"); 62 63 if (response && response.result) { 64 self.element 65 .addClass('added-to-wishlist') 66 .find('.towishlist') 67 .toggleClass('wishlisted', !isRemove); 68 69 let msg = isRemove 70 ? $.mage.__(' has been deleted from your Wish List.') 71 : $.mage.__(' has been added to your Wish List.'); 72 73 let wishlistHtmlMsg = '<div class="message-success success message show"> ' + window.currentProduct + msg + '</div>'; 74 $('.page.messages').html(wishlistHtmlMsg); 75 76 setTimeout(function () { 77 let wishlistStorage = JSON.parse(localStorage.getItem('mage-cache-storage') || '{}').wishlist; 78 if (wishlistStorage && wishlistStorage.counter !== undefined) { 79 $('.link.wishlist .counter.qty').text(wishlistStorage.counter); 80 } 81 }, 3000); 82 83 let storage = $.initNamespaceStorage('mage-cache-storage').localStorage; 84 storage.remove('messages'); 85 $.cookieStorage.set('mage-messages', ''); 86 $('.link.wishlist a').removeClass('empty'); 87 } else { 88 showWishlistError(response.message || 'Failed to add product to wishlist.'); 89 } 90 91 window.currentProduct = undefined; 92 }) 93 .fail(function (jqXHR, textStatus, errorThrown) { 94 console.error('Wishlist AJAX failed:', textStatus, errorThrown); 95 showWishlistError('Something went wrong. Please try again.'); 96 }) 97 .always(function () { 98 self.element.trigger('processStop'); 99 $body.trigger('processStop'); 100 }); 101 function showWishlistError(message) { 102 let wishlistHtmlMsg = '<div class="message-error error message show">' + $.mage.__(message) + '</div>'; 103 $('.page.messages').html(wishlistHtmlMsg); 104 self.element.find('.towishlist').removeClass('wishlisted added-to-wishlist'); 105 } 106 107 }, 108 109 _isUserLoggedIn: function () { 110 var customer = customerData.get('customer'); 111 return _.isString(customer().firstname); 112 }, 113 114 _checkWishListActive: function () { 115 var self = this; 116 // get 'kemana_wishlist' section from local storage. reference module: Kemana_Wishlist 117 var interval = setInterval(function () { 118 var wishlistStorage = JSON.parse(localStorage.getItem('mage-cache-storage')).kemana_wishlist; 119 if (typeof wishlistStorage !== 'undefined' && wishlistStorage !== '{}') { 120 var productIdValue = self.element.find('.towishlist').attr('id'); 121 if (!_.isEmpty(wishlistStorage) && _.isArray(wishlistStorage.items) && wishlistStorage.items.length) { 122 _.each(wishlistStorage.items, function(item) { 123 if (item === productIdValue) { 124 self.element.addClass('added-to-wishlist'); 125 const wishlistElements = document.getElementsByClassName('wl-' + item); 126 127 if (wishlistElements && typeof wishlistElements.length === 'number' && wishlistElements.length > 0) { 128 Array.from(wishlistElements).forEac
128h(el => { 129 el.classList.add('wishlisted'); 130 }); 131 } else { 132 var wishlist = document.getElementById(item); 133 wishlist.classList.add("wishlisted"); 134 } 135 } 136 }); 137 } 138 139 clearInterval(interval); 140 } 141 }, 500); 142 } 143 }); 144 return $.mage.productItems; 145});
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.