1// namespaced cart functions 2var AcdcCart = (function($) { 3 var obj = {}; 4 5 /** 6 * gets the cart as JSON and then calls the the provided function 7 * 8 * @param func 9 */ 10 obj.doSomethingWithCart = function(func) { 11 $.get(js_site_var.context_path + '/app/cart/view.json', { 12 rand: Math.floor(Math.random() * 1000000) 13 }, func); 14 } 15 16 /** 17 * get total items in cart, disregarding shipments 18 * 19 * @param cart 20 * @returns 21 */ 22 obj.getTotalItemsInCart = function(cart) { 23 return cart.cartItems.length; 24 } 25 26 /** 27 * items = [ { itemId: '1234', quantity: 1, listItemId: '334' }, ... ] 28 * 29 * @param url either to cart/addItemsDo (old) or cart/addItems (new) flow 30 * @param items 31 * @param callback 32 * @param remove_list_id list ID to remove the items from (if we are e.g., adding to cart from wishlist) 33 * @param remove_cart_item_ids can also remove items (CSVs) 34 * @param addItemsCallback an optional callback for clients to populate addItems with additional data 35 */ 36 obj.addToCart = function(url, items, callback, remove_list_id, remove_cart_item_ids, addItemsCallback) { 37 var data = {}; 38 39 if (typeof remove_list_id !== 'undefined') { 40 data['remove_list_id'] = remove_list_id; 41 } 42 43 if (typeof remove_cart_item_ids !== 'undefined') { 44 data['remove_cart_item_ids'] = remove_cart_item_ids; 45 } 46 47 for (var i = 0; i < items.length; i++) { 48 data['addItems[' + i + '].itemId'] = items[i].itemId; 49 50 if (items[i].hasOwnProperty('quantity')) { 51 data['addItems[' + i + '].quantity'] = items[i].quantity; 52 } else { 53 data['addItems[' + i + '].quantity'] = 1; 54 } 55 56 if (items[i].hasOwnProperty('note') && items[i].note != null) { 57 data['addItems[' + i + '].note'] = items[i].note; 58 } 59 60 if (items[i].hasOwnProperty('linkKey') && items[i].linkKey != null) { 61 data['addItems[' + i + '].linkKey'] = items[i].linkKey; 62 } 63 64 if (items[i].hasOwnProperty('linkPrimary') && items[i].linkPrimary != null) { 65 data['addItems[' + i + '].linkPrimary'] = items[i].linkPrimary; 66 } 67 68 if (items[i].hasOwnProperty('notes')) { 69 for (j = 0; j < items[i].notes.length; j++) { 70 data['addItems[' + i + '].notes[' + j + ']'] = items[i].notes[j]; 71 } 72 } 73 74 if (items[i].hasOwnProperty('subscriptionTerm') && items[i].subscriptionTerm != null) { 75 data['addItems[' + i + '].subscriptionTerm'] = items[i].subscriptionTerm; 76 } 77 78 // add additional client-specific data to the request 79 if (typeof addItemsCallback !== 'undefined' && typeof (addItemsCallback) == 'function') { 80 // the callback should return a JSON-like object in the form { keyA: value1, keyB: value2, ... } 81 var additionalData = addItemsCallback(items[i]); 82 83 if (typeof additionalData !== 'undefined' && typeof additionalData === 'object') { 84 $.each(additionalData, function(key, value) { 85 data['addItems[' + i + '].' + key] = value; 86 }); 87 } 88 } 89 } 90 91 addCsrfTokenToPostData(data); 92 93 /* 94 * The below requests will be tried again if they fail, with a new CSRF token that should have been 95 * returned with the blocked request 96 */ 97 if (typeof callback !== 'undefined' && typeof (callback) == 'function') { 98 $.post(url, data, callback).fail(function(){ 99 addCsrfTokenToPostData(data); 100 $.post(url, data, callback) 101 }); 102 } else { 103 $.post(url, data).fail(function(){ 104 addCsrfTokenToPostData(data); 105 $.post(url, data); 106 }); 107 } 108 } 109 110 /** 111 * useful when we store items added to cart in cookie for later retrieval 112 * 113 * @param url either to cart/addItemsDo (old) or cart/addItems (new) flow 114 * @param already_loaded_cookie_name name of the cookie used to remember that items were already loaded 115 * @param cookie_name name of the cookie where items are stored as JSON 116 * @param callback 117 */ 118 obj.addItemsFromCookieToCart = function(url, already_loaded_cookie_name, cookie_name, callback) { 119 var already_loaded = getCookieWithHelper(false, already_loaded_cookie_name); 120 121 if (typeof callback === 'undefined') { 122 callback = function() { 123 124 }; 125 } 126 127 if (typeof already_loaded !== 'undefined' && already_loaded == 't') { 128 console.log('already added items from cookie "' + cookie_name + '" to cart'); 129 callback(); 130 } else { 131 setCookieWithHelper(false, already_loaded_cookie_name, 't', { 132 path: '/' 133 }); 134 var items_str = getCookieWithHelper(false, cookie_name); 135 console.log(items_str); 136 137 if (typeof items_str !== 'undefined' && items_str != null) { 138 console.log('going to add items from cookie "' + cookie_name + '" to cart'); 139 obj.addToCart(url, JSON.parse(items_str), callback); 140 } else { 141 142 // we still call "callback" just in case 143 callback(); 144 } 145 } 146 } 147
148 /** 149 * move items to a list (e.g., wishlist) 150 * 151 * @param url 152 * @param index 153 * @returns {Boolean} 154 */ 155 obj.moveToList = function(url, index) { 156 var $hidden = $('input[type="hidden"][name="cartUpdate.itemUpdates[' + index + '].sendToList"]'); 157 158 if ($hidden.length != 1) { 159 console.log("couldn't find hidden \"sendToList\" input with index " + index); 160 return; 161 } 162 163 $hidden.val('true'); 164 document.location = url + '&_eventId=checkout_list_item_add&' + $('#command input[name$="sendToList"]').serialize(); 165 return false; 166 } 167 168 /** 169 * update cart (after a delay). this would refresh the page. useful if they don't have an "update" button on the 170 * cart page and they want it to be auto-updated after quantity is changed 171 * 172 * @param delay in ms 173 * @param on_selector 174 * @param selector filter for event 175 * @param event - event triggger the update, default to click 176 */ 177 obj.updateCart = function(delay, on_selector, selector, event) { 178 179 var evt = 'click'; 180 181 if (event) { 182 evt = event; 183 } 184 185 $(on_selector).on(evt, selector, $.debounce(delay, function() { 186 submitEvent('_eventId_checkout_update'); 187 })); 188 } 189 190 obj.estimateShippingCosts = function(address, callback) { 191 console.log(address); 192 193 if (typeof address.country === 'undefined' || address.country == null || address.country == '') { 194 return; 195 } 196 197 if (typeof address.postal_code === 'undefined' || address.postal_code == null) { 198 address.postal_code = ''; 199 } 200 201 if (typeof address.province === 'undefined' || address.province == null) { 202 address.province = ''; 203 } 204 205 ajaxWithoutAuth(js_site_var['context_path'] + '/app/cart/estimateDo?ta=rates&country=' + address.country + '&postalCode=' + address.postal_code + '&province=' + address.province, null, 'GET', function(data) { 206 callback(address, data); 207 }); 208 } 209 210 return obj; 211})(jQuery); 212 213/** 214 * @deprecated 215 */ 216function doSomethingWithCart(func) { 217 AcdcCart.doSomethingWithCart(func); 218} 219 220/** 221 * @deprecated 222 */ 223function getTotalItemsInCart(cart) { 224 return AcdcCart.getTotalItemsInCart(cart); 225} 226 227/** 228 * @deprecated 229 */ 230function addToCart(items, callback, remove_list_id, remove_cart_item_ids, addItemsCallback) { 231 var url = js_site_var['cart_domain'] + js_site_var['context_path'] + '/app/cart/addItemsDo';
232 AcdcCart.addToCart(url, items, callback, remove_list_id, remove_cart_item_ids, addItemsCallback); 233} 234 235/** 236 * @deprecated 237 */ 238function addItemsFromCookieToCart(already_loaded_cookie_name, cookie_name, callback) { 239 var url = js_site_var['cart_domain'] + js_site_var['context_path'] + '/app/cart/addItemsDo'; 240 AcdcCart.addItemsFromCookieToCart(url, already_loaded_cookie_name, cookie_name, callback); 241} 242 243/** 244 * @deprecated 245 */ 246function cart_moveToList(url, index) { 247 return AcdcCart.moveToList(url, index); 248} 249 250/** 251 * @deprecated 252 */ 253function cart_updateCart(delay, on_selector, selector) { 254 AcdcCart.updateCart(delay, on_selector, selector); 255} 256 257/** 258 * right now, it's US-only 259 * 260 * DEPRECATED! use AcdcCart.estimateShippingCosts() 261 * 262 * @deprecated 263 * @param coutry_code 264 * @param postal_code 265 * @param callback function is passed three args: country code, postal code, and the data 266 */ 267function cart_estimateShippingRates(country_code, postal_code, callback) { 268 if (typeof country_code === 'undefined' || country_code == null || country_code == '' || typeof postal_code === 'undefined' || postal_code == null || postal_code == '' || typeof callback === 'undefined') { 269 return; 270 } 271 272 var re = /^[0-9]{5}$/; 273 274 if (country_code == 'US' && postal_code.match(re)) { 275 ajaxWithoutAuth(js_site_var['context_path'] + '/app/cart/estimateDo?ta=rates&country=' + country_code + '&postalCode=' + postal_code, null, 'GET', function(data) { 276 callback(country_code, postal_code, data); 277 }); 278 } else { 279 console.log('US-only with 5-digit zip code allowed'); 280 } 281} 282 283/** 284 * @deprecated 285 */ 286function cart_estimateShippingCosts(address, callback) { 287 AcdcCart.estimateShippingCosts(address, callback); 288} 289 290/** 291 * @deprecated use AcdcCart.estimateShippingCosts() 292 * @param country_code 293 * @param postal_code 294 * @param callback 295 */ 296function cart_estimateInternationalShippingRates(country_code, postal_code, callback) { 297 if (typeof country_code === 'undefined' || country_code == null || country_code == '' || typeof postal_code === 'undefined' || postal_code == null || postal_code == '' || typeof callback === 'undefined') { 298 return; 299 } 300 ajaxWithoutAuth(js_site_var['context_path'] + '/app/cart/estimateDo?ta=rates&country=' + country_code + '&postalCode=' + postal_code, null, 'GET', function(data) { 301 callback(country_code, postal_code, data); 302 }); 303} 304 305/** 306 * Adds a a csrf token to the data object if the cookie exists 307 * @param data 308 * @returns 309 */ 310function addCsrfTokenToPostData(data){ 311 var csrfCookieValue = getCookieWithHelper(false, 'csrfToken'); 312 if (csrfCookieValue != undefined) { 313 data['_csrfToken'] = csrfCookieValue; 314 } 315} 316 317/** 318 * jQuery plugin to deal with "same as shipping" checkboxes 319 * 320 * "options.shipping" is optional and is the shipping address. if it's undefined, then it will try to get it from the page itself. 321 * "options.callback" is optional and is called afterwards 322 * 323 * IMPORTANT: do not use this directly for security reasons. always use the <checkout:sameAsShipping> tag which will use this 324 */ 325(function($) { 326 $.fn.cart_sameAsShipping = function(options) { 327 var settings = $.extend({ 328 callback: function() { 329 }, 330 precheck: false, // if true, the checkbox is automatically checked (w/o actually copying the address) 331 credit_card_address_model: 'creditCardAddress', // defaults to "creditCardAddress" but can be "address" 332 shipping_address_model: 'shippingAddress', // defaults to "shippingAddress" but can be "address" 333 name_on_card: false, // if true, copy the credit card's "name on card" to the shipping first name and last name 334 skip_email: false, // if true, skips email field and does not populate it 335 billing: false //if true, copy to billing instead of creditcard address 336 }, options); 337 338 var copyShipping = function(target) { 339 var checked = $(target).is(':checked'); 340 console.log(checked); 341 var shipping = settings.shipping; 342 343 if (typeof shipping === 'undefined') { 344 shipping = {}; 345 shipping.address = {}; 346 shipping.address.firstName = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.firstName"]').val(); 347 shipping.address.lastName = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.lastName"]').val(); 348 shipping.address.address1 = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.address1"]').val(); 349 shipping.address.address2 = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.address2"]').val(); 350 shipping.address.city = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.city"]').val(); 351 shipping.address.province = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.province"]').val(); 352 shipping.address.postalCode = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.postalCode"]').val(); 353 shipping.address.phoneNumber = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.phoneNumber"]').val(); 354 if (!settings.skip_email) { 355 shipping.address.email = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.email"]').val(); 356 } 357 shipping.address.company = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.company"]').val(); 358 shipping.address.country = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.country"]').val(); 359 } else { 360 shipping = JSON.parse(shipping); 361 } 362 363 if (settings.name_on_card) { 364 if (shipping.address.firstName != '' && shipping.address.lastName != '') { 365 $('[name="checkoutCreditCard\\.creditCard\\.nameOnCard"]').val(checked ? shipping.address.firstName + ' ' + shipping.address.lastName : ''); 366 } 367 } 368 369 $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.firstName"]').val(checked ? shipping.address.firstName : ''); 370 $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.lastName"]').val(checked ? shipping.address.lastName : ''); 371 $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.address1"]').val(checked ? shipping.address.address1 : ''); 372 $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.address2"]').val(checked ? shipping.address.address2 : ''); 373 $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.city"]').val(checked ? shipping.address.city : ''); 374 var $province = $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.province"]'); 375 376 if (typeof $province.selectize != 'undefined' && typeof $province.selectize()[0] != 'undefined') { 377 // selectize.js hides the native select dropdown, hence this if clause 378 $province.selectize()[0].selectize.setValue(checked ? shipping.address.province : ''); 379 } else if ($province.is('select')) { 380 if (checked) { 381 $province.val(shipping.address.province); 382 } else { 383 $province.val($province.find('option:first').val()); 384 } 385 386 $province.trigger('change'); 387 } else { 388 $province.val(checked ? shipping.address.province : ''); 389 } 390 391 $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.postalCode"]').val(checked ? shipping.address.postalCode : ''); 392 $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.phoneNumber"]').val(checked ? shipping.address.phoneNumber : ''); 393 if (!settings.skip_email) { 394 $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.email"]').val(checked ? shipping.address.email : ''); 395 } 396 $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.company"]').val(checked ? shipping.address.company : ''); 397 var $country = $('[name="checkoutCreditCard\\.creditCard\\.' + settings.credit_card_address_model + '\\.country"]'); 398 399 if (typeof $country.selectize != 'undefined' && typeof $country.selectize()[0] != 'undefined') { 400 // selectize.js hides the native select dropdown, hence this if clause 401 $country.selectize()[0].selectize.setValue(checked ? shipping.address.country : ''); 402 } else if ($country.is('select')) { 403 if (checked) { 404 $country.val(shipping.address.country); 405 } else { 406 $country.val($country.find('option:first').val()); 407 } 408 409 $country.trigger('change'); 410 } else if ($country.is(":hidden")) { 411 // don't do anything; sometimes country is hidden and not meant to be changed (and set to US always) 412 } else { 413 $country.val(checked ? shipping.address.country : ''); 414 } 415 }; 416 var copyShippingtoBilling = function(target) { 417 var checked = $(target).is(':checked'); 418 console.log(checked); 419 var shipping = settings.shipping; 420 421 if (typeof shipping === 'undefined') { 422 shipping = {}; 423 shipping.address = {}; 424 shipping.address.firstName = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.firstName"]').val(); 425 shipping.address.lastName = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.lastName"]').val(); 426 shipping.address.address1 = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.address1"]').val(); 427 shipping.address.address2 = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.address2"]').val(); 428 shipping.address.city = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.city"]').val(); 429 shipping.address.province = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.province"]').val(); 430 shipping.address.postalCode = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.postalCode"]').val(); 431 shipping.address.phoneNumber = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.phoneNumber"]').val(); 432 if (!settings.skip_email) { 433 shipping.address.email = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.email"]').val(); 434 } 435 shipping.address.company = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.company"]').val(); 436 shipping.address.country = $('[name="checkoutShippingAddress\\.' + settings.shipping_address_model + '\\.country"]').val(); 437 } else { 438 shipping = JSON.parse(shipping); 439 } 440 441 $('[name="billingAddress\\.firstName"]').val(checked ? shipping.address.firstName : ''); 442 $('[name="billingAddress\\.lastName"]').val(checked ? shipping.address.lastName : ''); 443 $('[name="billingAddress\\.address1"]').val(checked ? shipping.address.address1 : ''); 444 $('[name="billingAddress\\.address2"]').val(checked ? shipping.address.address2 : ''); 445 $('[name="billingAddress\\.city"]').val(checked ? shipping.address.city : ''); 446 var $province = $('[name="billingAddress\\.province"]'); 447 448 if (typeof $province.selectize != 'undefined' && typeof $province.selectize()[0] != 'undefined') { 449 // selectize.js hides the native select dropdown, hence this if clause 450 $province.selectize()[0].selectize.setValue(checked ? shipping.address.province : ''); 451 } else if ($province.is('select')) { 452 if (checked) { 453 $province.val(shipping.address.province); 454 } else { 455 $province.val($province.find('option:first').val()); 456 } 457 458 $province.trigger('change'); 459 } else { 460 $province.val(checked ? shipping.address.province : ''); 461 } 462 463 $('[name="billingAddress\\.postalCode"]').val(checked ? shipping.address.postalCode : ''); 464 $('[name="billingAddress\\.phoneNumber"]').val(checked ? shipping.address.phoneNumber : ''); 465 if (!settings.skip_email) { 466 $('[name="billingAddress\\.email"]').val(checked ? shipping.address.email : ''); 467 } 468 $('[name="billingAddress\\.company"]').val(checked ? shipping.address.company : ''); 469 var $country = $('[name="billingAddress\\.country"]'); 470 471 if (typeof $country.selectize != 'undefined' && typeof $country.selectize()[0] != 'undefined') { 472 // selectize.js hides the native select dropdown, hence this if clause 473 $country.selectize()[0].selectize.setValue(checked ? shipping.address.country : ''); 474 } else if ($country.is('select')) { 475 if (checked) { 476 $country.val(shipping.address.country); 477 } else { 478 $country.val($country.find('option:first').val()); 479 } 480 481 $country.trigger('change'); 482 } else if ($country.is(":hidden")) { 483 // don't do anything; sometimes country is hidden and not meant to be changed (and set to US always) 484 } else { 485 $country.val(checked ? shipping.address.country : ''); 486 } 487 }; 488 489 $(this).on('click', function(event) { 490 if(settings.billing) { 491 copyShippingtoBilling(this) 492 } else { 493 copyShipping(this); 494 } 495 var checked = $(this).is(':checked'); 496 497 settings.callback(checked); 498 }); 499 500 $(this).prop('checked', settings.precheck); 501 502 if (settings.precheck) { 503 if(settings.billing) { 504 copyShippingtoBilling(this) 505 } else { 506 copyShipping(this); 507 } 508 } 509 510 settings.callback(settings.precheck); 511 }; 512}(jQuery));
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.