1window.favorite = { 2 init: function () { 3 const products = favorite.getProducts(); 4 const likesCount = products[0] ?? {}; 5 const liked = products[1] ?? {}; 6 7 $('.js-favorite[data-product]').each(function () { 8 const productID = $(this).attr('data-product') || false; 9 if (!productID) { 10 return; 11 } 12 13 let count = Number.parseInt(likesCount[productID]); 14 if (Number.isNaN(count) || count < 0) { 15 count = 0; 16 } 17 const isActive = liked[productID] === true; 18 if (count) { 19 favorite.setCount(this, count); 20 } 21 if (isActive) { 22 favorite.setActive(this, isActive); 23 } 24 }); 25 }, 26 27 setProducts: function (products) { 28 localStorage.setItem('favoriteProducts', JSON.stringify(products)); 29 }, 30 31 getProducts: function () { 32 return JSON.parse(localStorage.getItem('favoriteProducts') ?? '{}'); 33 }, 34 35 like: async function (productID) { 36 return await BX.ajax.runComponentAction('mts:favorite', 'like', { 37 mode: 'ajax', 38 data: { 39 productID 40 } 41 }); 42 }, 43 44 dislike: async function (productID) { 45 return await BX.ajax.runComponentAction('mts:favorite', 'dislike', { 46 mode: 'ajax', 47 data: { 48 productID 49 } 50 }); 51 }, 52 53 setCount: async function (element, count) { 54 if (count === 0) { 55 $(element).find('svg.icon use').attr('href', '#heart-fav-empty'); 56 $(element).find('.item__controls__unit__icon__counter').remove(); 57 $(element).find('.products__unit__likes__amount').remove(); 58 } else { 59 const iconWrapper = $(element).find('.item__controls__unit__icon'); 60 if (iconWrapper.length) { 61 $(element).find('svg.icon use').attr('href', '#heart-fav2'); 62 const divCount = $(iconWrapper).find('.item__controls__unit__icon__counter'); 63 if (divCount.length) { 64 divCount.html(count); 65 } else { 66 $(iconWrapper).append(`<div class="item__controls__unit__icon__counter">${count}</div>`); 67 } 68 } else { 69 $(element).find('svg.icon use').attr('href', '#heart-fav'); 70 const spanCount = $(element).find('.products__unit__likes__amount'); 71 if (spanCount.length) { 72 spanCount.html(count); 73 } else { 74 $(element).append(`<span class="products__unit__likes__amount num">${count}</span>`); 75 } 76 } 77 } 78 let products = favorite.getProducts(); 79 const productID = $(element).attr('data-product'); 80 products[0][productID] = count; 81 favorite.setProducts(products); 82 }, 83 84 setActive: async function (element, active) { 85 if (active) { 86 $(element).addClass('item__controls__unit--active'); 87 $(element).addClass('products__unit__likes--active'); 88 } else { 89 $(element).removeClass('item__controls__unit--active'); 90 $(element).removeClass('products__unit__likes--active'); 91 } 92 93 $(element).attr('data-active', active); 94 let products = favorite.getProducts(); 95 const productID = $(element).attr('data-product'); 96 products[1][productID] = active; 97 favorite.setProducts(products); 98 }, 99}; 100 101$(function () { 102 $(document).on('click', '.js-favorite', async function () { 103 const productID = $(this).attr('data-product'); 104 if (productID <= 0) { 105 return; 106 } 107 108 const response = $(this).attr('data-active') === 'true' 109 ? await favorite.dislike(productID) 110 : await favorite.like(productID); 111 const data = response.data; 112 const count = Number.parseInt(data.count); 113 const active = data.active === true; 114 115 Number.isNaN(count) || favorite.setCount(this, count); 116 favorite.setActive(this, active);
117 $(document).trigger('favorite:update', { block: this, count, active }); 118 }); 119 120 $('.js-favorite.item__controls__unit--likes').each(function () { 121 $(this).data('setCount', favorite.setCount.bind(this)); 122 $(this).data('setActive', favorite.setActive.bind(this)); 123 }); 124 125 $('.js-favorite.products__unit__likes').each(function () { 126 $(this).data('setCount', favorite.setCount.bind(this)); 127 $(this).data('setActive', favorite.setActive.bind(this)); 128 }); 129 130 favorite.init(); 131});
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.