1/** 2 * Magento 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_AFL.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 Magento to newer 17 * versions in the future. If you wish to customize Magento for your 18 * needs please refer to http://www.magento.com for more information. 19 * 20 * @category design 21 * @package rwd_default 22 * @copyright Copyright (c) 2006-2017 X.commerce, Inc. and affiliates (http://www.magento.com) 23 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) 24 */ 25function Minicart(options) { 26 this.formKey = options.formKey; 27 this.previousVal = null; 28 29 this.defaultErrorMessage = 'Error occurred. Try to refresh page.'; 30 31 this.selectors = { 32 itemRemove: '#cart-sidebar .remove', 33 container: '#header-cart', 34 inputQty: '.cart-item-quantity', 35 qty: 'div.header-minicart span.count', 36 overlay: '.minicart-wrapper', 37 error: '#minicart-error-message', 38 success: '#minicart-success-message', 39 quantityButtonPrefix: '#qbutton-', 40 quantityInputPrefix: '#qinput-', 41 quantityButtonClass: '.quantity-button' 42 }; 43 44 if (options.selectors) { 45 $j.extend(this.selectors, options.selectors); 46 } 47} 48 49Minicart.prototype = { 50 initAfterEvents : {}, 51 removeItemAfterEvents : {}, 52 init: function() { 53 var cart = this; 54 55 // bind remove event 56 $j(this.selectors.itemRemove).unbind('click.minicart').bind('click.minicart', function(e) { 57 e.preventDefault(); 58 cart.removeItem($j(this)); 59 }); 60 61 // bind update qty event 62 $j(this.selectors.inputQty) 63 .unbind('blur.minicart') 64 .unbind('focus.minicart') 65 .bind('focus.minicart', function() { 66 cart.previousVal = $j(this).val(); 67 cart.displayQuantityButton($j(this)); 68 }) 69 .bind('blur.minicart', function() { 70 cart.revertInvalidValue(this); 71 }); 72 73 $j(this.selectors.quantityButtonClass) 74 .unbind('click.quantity') 75 .bind('click.quantity', function() { 76 cart.processUpdateQuantity(this); 77 }); 78 79 for (var i in this.initAfterEvents) { 80 if (this.initAfterEvents.hasOwnProperty(i) && typeof(this.initAfterEvents[i]) === "function") { 81 this.initAfterEvents[i](); 82 } 83 } 84 85 }, 86 87 removeItem: function(el) { 88 var cart = this; 89 if (confirm(el.data('confirm'))) { 90 cart.hideMessage(); 91 cart.showOverlay(); 92 $j.ajax({ 93 type: 'POST', 94 dataType: 'json', 95 data: {form_key: cart.formKey}, 96 url: el.attr('href') 97 }).done(function(result) { 98 cart.hideOverlay(); 99 if (result.success) { 100 cart.updateCartQty(result.qty); 101 cart.updateContentOnRemove(result, el.closest('li')); 102 } else { 103 cart.showMessage(result); 104 } 105 }).error(function() { 106 cart.hideOverlay(); 107 cart.showError(cart.defaultErrorMessage); 108 }); 109 } 110 for (var i in this.removeItemAfterEvents) { 111 if (this.removeItemAfterEvents.hasOwnProperty(i) && typeof(this.removeItemAfterEvents[i]) === "function") { 112 this.removeItemAfterEvents[i](); 113 } 114 } 115 }, 116 117 revertInvalidValue: function(el) { 118 if (!this.isValidQty($j(el).val()) || $j(el).val() == this.previousVal) { 119 $j(el).val(this.previousVal); 120 this.hideQuantityButton(el); 121 } 122 }, 123 124 displayQuantityButton: function(el) { 125 var buttonId = this.selectors.quantityButtonPrefix + $j(el).data('item-id'); 126 $j(buttonId).addClass('visible').attr('disabled',null); 127 }, 128 129 hideQuantityButton: function(el) { 130 var buttonId = this.selectors.quantityButtonPrefix + $j(el).data('item-id'); 131 $j(buttonId).removeClass('visible').attr('disabled','disabled'); 132 }, 133 134 processUpdateQuantity: function(el) {
135 var input = $j(this.selectors.quantityInputPrefix + $j(el).data('item-id')); 136 if (this.isValidQty(input.val()) && input.val() != this.previousVal) { 137 this.updateItem(el); 138 } else { 139 this.revertInvalidValue(input); 140 } 141 }, 142 143 updateItem: function(el) { 144 var cart = this; 145 var input = $j(this.selectors.quantityInputPrefix + $j(el).data('item-id')); 146 147 if (!$j.isNumeric(input.val())) { 148 cart.hideOverlay(); 149 cart.showError(cart.defaultErrorMessage); 150 return false; 151 } 152 153 var quantity = input.val(); 154 cart.hideMessage(); 155 cart.showOverlay(); 156 $j.ajax({ 157 type: 'POST', 158 dataType: 'json', 159 url: input.data('link'), 160 data: {qty: quantity, form_key: cart.formKey} 161 }).done(function(result) { 162 cart.hideOverlay(); 163 if (result.success) { 164 cart.updateCartQty(result.qty); 165 if (quantity !== 0) { 166 cart.updateContentOnUpdate(result); 167 } else { 168 cart.updateContentOnRemove(result, input.closest('li')); 169 } 170 } else { 171 cart.showMessage(result); 172 } 173 }).error(function() { 174 cart.hideOverlay(); 175 cart.showError(cart.defaultErrorMessage); 176 }); 177 return false; 178 }, 179 180 updateContentOnRemove: function(result, el) { 181 var cart = this; 182 el.hide('slow', function() { 183 $j(cart.selectors.container).html(result.content); 184 cart.showMessage(result); 185 186 }); 187 }, 188 189 updateContentOnUpdate: function(result) { 190 $j(this.selectors.container).html(result.content); 191 this.showMessage(result); 192 }, 193 194 updateCartQty: function(qty) { 195 if (typeof qty != 'undefined') { 196 $j(this.selectors.qty).text(qty); 197 } 198 }, 199 200 isValidQty: function(val) { 201 return (val.length > 0) && (val - 0 == val) && (val - 0 > 0); 202 }, 203 204 showOverlay: function() { 205 $j(this.selectors.overlay).addClass('loading'); 206 }, 207 208 hideOverlay: function() { 209 $j(this.selectors.overlay).removeClass('loading'); 210 }, 211 212 showMessage: function(result) { 213 if (typeof result.notice != 'undefined') { 214 this.showError(result.notice); 215 } else if (typeof result.error != 'undefined') { 216 this.showError(result.error); 217 } else if (typeof result.message != 'undefined') { 218 this.showSuccess(result.message); 219 } 220 }, 221 222 hideMessage: function() { 223 $j(this.selectors.error).fadeOut('slow'); 224 $j(this.selectors.success).fadeOut('slow'); 225 }, 226 227 showError: function(message) { 228 $j(this.selectors.error).text(message).fadeIn('slow'); 229 }, 230 231 showSuccess: function(message) { 232 $j(this.selectors.success).text(message).fadeIn('slow'); 233 } 234};
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.