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-2015 X.commerce, Inc. (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 var quantity = parseInt(input.val(), 10); 147 cart.hideMessage(); 148 cart.showOverlay(); 149 $j.ajax({ 150 type: 'POST', 151 dataType: 'json', 152 url: input.data('link'), 153 data: {qty: quantity, form_key: cart.formKey} 154 }).done(function(result) { 155 cart.hideOverlay(); 156 if (result.success) { 157 cart.updateCartQty(result.qty); 158 if (quantity !== 0) { 159 cart.updateContentOnUpdate(result); 160 } else { 161 cart.updateContentOnRemove(result, input.closest('li')); 162 } 163 } else { 164 cart.showMessage(result); 165 } 166 }).error(function() { 167 cart.hideOverlay(); 168 cart.showError(cart.defaultErrorMessage); 169 }); 170 return false; 171 }, 172 173 updateContentOnRemove: function(result, el) { 174 var cart = this; 175 el.hide('slow', function() { 176 $j(cart.selectors.container).html(result.content); 177 cart.showMessage(result); 178 179 }); 180 }, 181 182 updateContentOnUpdate: function(result) { 183 $j(this.selectors.container).html(result.content); 184 this.showMessage(result); 185 }, 186 187 updateCartQty: function(qty) { 188 if (typeof qty != 'undefined') { 189 $j(this.selectors.qty).text(qty); 190 } 191 }, 192 193 isValidQty: function(val) { 194 return (val.length > 0) && (val - 0 == val) && (val - 0 > 0); 195 }, 196 197 showOverlay: function() { 198 $j(this.selectors.overlay).addClass('loading'); 199 }, 200 201 hideOverlay: function() { 202 $j(this.selectors.overlay).removeClass('loading'); 203 }, 204 205 showMessage: function(result) { 206 if (typeof result.notice != 'undefined') { 207 this.showError(result.notice); 208 } else if (typeof result.error != 'undefined') { 209 this.showError(result.error); 210 } else if (typeof result.message != 'undefined') { 211 this.showSuccess(result.message); 212 } 213 }, 214 215 hideMessage: function() { 216 $j(this.selectors.error).fadeOut('slow'); 217 $j(this.selectors.success).fadeOut('slow'); 218 }, 219 220 showError: function(message) { 221 $j(this.selectors.error).text(message).fadeIn('slow'); 222 }, 223 224 showSuccess: function(message) { 225 $j(this.selectors.success).text(message).fadeIn('slow'); 226 } 227};
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.