1/** 2 * 2007-2019 PrestaShop SA and Contributors 3 * 4 * NOTICE OF LICENSE 5 * 6 * This source file is subject to the Academic Free License (AFL 3.0) 7 * that is bundled with this package in the file LICENSE.txt. 8 * It is also available through the world-wide-web at this URL: 9 * http://opensource.org/licenses/afl-3.0.php 10 * If you did not receive a copy of the license and are unable to 11 * obtain it through the world-wide-web, please send an email 12 * to [email protected] so we can send you a copy immediately. 13 * 14 * DISCLAIMER 15 * 16 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 17 * versions in the future. If you wish to customize PrestaShop for your 18 * needs please refer to http://www.prestashop.com for more information. 19 * 20 * @author PrestaShop SA <[email protected]> 21 * @copyright 2007-2019 PrestaShop SA and Contributors 22 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) 23 * International Registered Trademark & Property of PrestaShop SA 24 */ 25 26 27$(document).ready(function() { 28 productListingComments.init(); 29 productListingComments.load(); 30}); 31 32 33var productListingComments = (function () { 34 35 var data = { 36 productIDs: [], 37 commentsLoadingInProgress: false, 38 ajaxIDsLimit: 50, 39 ajaxUrl: '' 40 } 41 42 var DOMStrings = { 43 productListReviewsContainer: '.product-list-reviews', 44 productListReviewsNumberOfComments: '.comments-nb', 45 productListReviewsStarsContainer: '.grade-stars', 46 productContainer: '.js-product-miniature' 47 }; 48 49 var DOMClasses = { 50 inProgress: 'reviews-loading', 51 reviewsLoaded: 'reviews-loaded', 52 hasReviews: 'has-reviews' 53 }; 54 55 function setEvents() { 56 prestashop.on('updateProductList', function() { 57 addProductsIDs(); 58 }); 59 60 prestashop.on("updatedProduct", function () { 61 updateRatings(); 62 }); 63 } 64 65 66 function setAjaxUrl() { 67 if (data.ajaxUrl !== '') 68 return; 69 70 var url = $(DOMStrings.productListReviewsContainer).first().data('url'); 71 data.ajaxUrl = url; 72 } 73 74 function getNewProductsReviewsElements() { 75 var $productListReviews = $(DOMStrings.productContainer) 76 .not('.' + DOMClasses.reviewsLoaded + ', .' + DOMClasses.inProgress) 77 .addClass(DOMClasses.inProgress) 78 .find(DOMStrings.productListReviewsContainer); 79 80 return $productListReviews; 81 } 82 83 function addProductsIDs() { 84 85 var $productsList = getNewProductsReviewsElements(), 86 seenIds = {}; 87 88 $productsList.each(function () { 89 var id = $(this).data('id'); 90 seenIds[id] = true; 91 }); 92 93 94 var IDsArray = Object.keys(seenIds).filter(e => e !== 'undefined'); 95 var prevDataIDs = data.productIDs.splice(0); 96 data.productIDs = prevDataIDs.concat(IDsArray); 97 98 if (!data.commentsLoadingInProgress) { 99 loadProductsData(); 100 } 101 } 102 103 function loadProductsData() { 104 if (data.productIDs.length === 0) 105 return; 106 107 data.commentsLoadingInProgress = true; 108 109 var dataIDsCopy = data.productIDs.slice(0); 110 selectedProductIDs = dataIDsCopy.splice(0, data.ajaxIDsLimit); 111 112 113 $.get(data.ajaxUrl, { id_products: selectedProductIDs }, function (jsonData) { 114 if (jsonData) { 115 $.each(jsonData.products, function(i, elem) { 116 var productData = elem; 117 var $productsReviewsContainer = $('.product-list-reviews[data-id="' + productData.id_product + '"]'); 118 119 $productsReviewsContainer.each(function () { 120 var $self = $(this); 121 122 if (productData.comments_nb > 0) { 123 $self.find(DOMStrings.productListReviewsStarsContainer).rating({ grade: productData.average_grade, starWidth: 16 }); 124 $self.find(DOMStrings.productListReviewsNumberOfComments).text('(' + productData.comments_nb + ')');
125 $self.closest(DOMStrings.productContainer).addClass(DOMClasses.hasReviews); 126 $self.css('visibility', 'visible'); 127 } 128 129 $self.closest(DOMStrings.productContainer).addClass(DOMClasses.reviewsLoaded); 130 $self.closest(DOMStrings.productContainer).removeClass(DOMClasses.inProgress); 131 132 }); 133 data.productIDs.shift(); 134 }); 135 136 data.commentsLoadingInProgress = false; 137 if (data.productIDs.length > 0) { 138 loadProductsData(); 139 } 140 141 } 142 }); 143 } 144 145 function updateRatings() { 146 // Collect all product IDs that need to be updated 147 var productIDs = []; 148 var $productsList = $(DOMStrings.productContainer); 149 150 $productsList.each(function () { 151 var productID = $(this).find(DOMStrings.productListReviewsContainer).data("id"); 152 if (productID) { 153 productIDs.push(productID); 154 } 155 }); 156 157 if (productIDs.length > 0) { 158 $.get( 159 data.ajaxUrl, 160 { id_products: productIDs }, 161 function (jsonData) { 162 if (jsonData && jsonData.products.length > 0) { 163 // Create a map of product data 164 var productDataMap = {}; 165 jsonData.products.forEach(function (product) { 166 productDataMap[product.id_product] = product; 167 }); 168 169 // Update each product's ratings 170 $productsList.each(function () { 171 var $self = $(this); 172 var productID = $self.find(DOMStrings.productListReviewsContainer).data("id"); 173 174 if (productDataMap[productID]) { 175 var productData = productDataMap[productID]; 176 var $productReviewContainer = $self.find(DOMStrings.productListReviewsContainer); 177 178 if (productData.comments_nb > 0) { 179 $productReviewContainer 180 .find(DOMStrings.productListReviewsStarsContainer) 181 .rating({ grade: productData.average_grade, starWidth: 16 }); 182 $productReviewContainer 183 .find(DOMStrings.productListReviewsNumberOfComments) 184 .text("(" + productData.comments_nb + ")"); 185 $self.addClass(DOMClasses.hasReviews); 186 $productReviewContainer.css("visibility", "visible"); 187 } 188 } 189 }); 190 } 191 } 192 ); 193 } 194 } 195 196 return { 197 load: function () { 198 addProductsIDs(); 199 }, 200 init: function () { 201 setAjaxUrl(); 202 setEvents(); 203 } 204 } 205})();
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.