1/** 2 This script is for GUI behavior when limiting quantity of cart items. 3 */ 4var cartdata; 5 6// Only acdc-cart.js should call this with the proper AJAX cart data provided. 7function initCartLimit(_cartdata) { 8 console.log(_cartdata); 9 cartdata = _cartdata; 10 updateCartLimit(); 11} 12 13// Call this to begin updating the webpage with qty limiting GUI behavior 14function updateCartLimit() { 15 console.log("update cart limit"); 16 17 if ( $('.acdc-cart-limit').length == 0 ) { 18 // nothing needs limitation 19 console.log("nothing to limit"); 20 return; 21 } 22 23 var qtyMap = getCartQuantities(); 24 25 // Run through all elements that need GUI limitation 26 $('.acdc-cart-limit').each(function(index) { 27 var itemId = $(this).data('sku-id'); 28 29 // Only perform GUI updates IFF the cart qty limit is reached 30 if (typeof qtyMap[itemId] != 'undefined') { 31 console.log('capping itemId::' + itemId + ' with limit::' + $(this).data('qty-limit') + ' and cartQty::' + qtyMap[itemId]); 32 renderCartLimit( $(this), $(this).data('qty-limit'), qtyMap[itemId] ); 33 } else { 34 // assuming the item was removed from minicart 35 renderCartLimit( $(this), $(this).data('qty-limit'), 0 ); 36 } 37 }); 38} 39 40function getCartQuantities() { 41 // Aggregate what's already in the cart by itemId and qty 42 var qtyMap = {}; 43 44 if (cartdata) { 45 cartdata.cart.cartItems.forEach(function (cartItem) { 46 var itemId = cartItem.itemId; 47 if (typeof qtyMap[itemId] == 'undefined') { 48 qtyMap[itemId] = 1; 49 } else { 50 qtyMap[itemId] = qtyMap[itemId] + 1; 51 } 52 }); 53 } 54 55 return qtyMap; 56} 57 58// Performs the actual GUI behavior changes 59function renderCartLimit ( target, _limit, _current ) { 60 limit = parseInt(_limit); 61 current = parseInt(_current); 62 var remaining = limit - current; 63 64 // qty dropdown - pdp, samples page 65 if (target.is('select')) { 66 target.dropdown("destroy"); 67 target.html(''); 68 let start = 0 69 if ($(target).hasClass('qty-cartlimit-alt')){ 70 start = 1; 71 } 72 73 if (start > remaining) { 74 var $option = $(document.createElement('option')); 75 $option.val(0); 76 $option.html(0); 77 target.append($option); 78 } else { 79 for (var x = start; x <= remaining; x++) { 80 var $option = $(document.createElement('option')); 81 $option.val(x); 82 $option.html(x); 83 target.append($option); 84 } 85 } 86 87 if ($(target).hasClass('acdc-cart-limit')){ 88 $(target).data("remaining-limit", remaining); 89 } 90 91 target.dropdown(); 92 // plus and minus signs - view cart, mini cart 93 } else if (target.is('div')) { 94 if (remaining <= 0 ) { 95 target.children('.quantity-increase').hide(); 96 } 97 // atc link - quickview atc 98 } else if (target.is('a')) { 99 if (remaining <= 0 ) { 100 target.hide(); 101 } else { 102 target.show(); 103 } 104 } 105 106} 107 108function getCartQuantities() { 109 // Aggregate what's already in the cart by itemId and qty 110 var qtyMap = {}; 111 112 if (cartdata) { 113 cartdata.cart.cartItems.forEach(function (cartItem) { 114 var itemId = cartItem.itemId; 115 if (typeof qtyMap[itemId] == 'undefined') { 116 qtyMap[itemId] = 1; 117 } else { 118 qtyMap[itemId] = qtyMap[itemId] + 1; 119 } 120 }); 121 } 122 123 return qtyMap; 124}
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.