File: D:/HostingSpaces/SBogers10/spire.komma-mediadesign.nl/wwwroot/js/shoppingCart.js
/* ==========================================================================
Helper functions or classes
========================================================================== */
var Cookie = {
set: function (name, value, days) {
var domain, domainParts, date, expires, host;
if (days) {
date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
host = location.host;
if (host.split('.').length === 1) {
// no "." in a domain - it's localhost or something similar
document.cookie = name + "=" + value + expires + "; path=/";
console.log('no . in domain');
}
else {
// Remember the cookie on all subdomains.
//
// Start with trying to set cookie to the top domain.
// (example: if user is on foo.com, try to set
// cookie to domain ".com")
//
// If the cookie will not be set, it means ".com"
// is a top level domain and we need to
// set the cookie to ".foo.com"
domainParts = host.split('.');
domainParts.shift();
domain = '.' + domainParts.join('.');
domain = '.komma-mediadesign.nl';
document.cookie = name + "=" + value + expires + "; path=/; domain=" + domain;
console.log('cookie set to: ' + domain);
// check if cookie was successfuly set to the given domain
// (otherwise it was a Top-Level Domain)
if (Cookie.get(name) == null || Cookie.get(name) != value) {
// append "." to current domain
// domain = '.'+host; //
document.cookie = name + "=" + value + expires + "; path=/; domain=" + domain;
console.log('append "." to current domain');
console.log('cookie set to: ' + domain);
}
}
},
get: function (name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
},
erase: function (name) {
Cookie.set(name, '', -1);
}
};
/*
* Simple isset method for this does not exist in javascript
*/
var isset = function(obj)
{
return typeof obj !== 'undefined' && obj !== null;
};
/* ==========================================================================
Shopping Cart Handler
========================================================================== */
var ShoppingCartHandler = {
cookieName: 'shopping-cart',
shoppingCart: null,
shoppingCartUrl: 'http://www.spire-checkout.komma-mediadesign.nl',
shoppingCartX2Url: 'http://www.spirex2-checkout.komma-mediadesign.nl',
productUnitsInput: null,
productUnits: 1,
init: function(){
// Get the shopping cart cookie
ShoppingCartHandler.getShoppingCart();
// Add event listener for adding to the shopping cart
var addToShoppingCartButton = document.getElementById('add-to-shopping-cart');
if(isset(addToShoppingCartButton)){
addToShoppingCartButton.addEventListener('click', function () {
ShoppingCartHandler.addToShoppingCart(this);
});
// Check if spire shop route is needed of the spire one
if(addToShoppingCartButton.getAttribute('data-shop-x2') == 'true'){
console.log('set shop url to x2');
ShoppingCartHandler.shoppingCartUrl = ShoppingCartHandler.shoppingCartX2Url;
}
}
ShoppingCartHandler.productUnitsInput = document.querySelector('.shop-now-side .units input');
var incrementProductUnitsButton = document.querySelector('.shop-now-side .units .increment');
var decrementProductUnitsButton = document.querySelector('.shop-now-side .units .decrement');
// Add the event listeners from the input and belonging buttons
if(isset(ShoppingCartHandler.productUnitsInput) && isset(incrementProductUnitsButton) && isset(decrementProductUnitsButton)){
incrementProductUnitsButton.addEventListener('click', function () {
ShoppingCartHandler.changeProductUnit(1);
});
decrementProductUnitsButton.addEventListener('click', function () {
ShoppingCartHandler.changeProductUnit(-1);
});
ShoppingCartHandler.productUnitsInput.addEventListener('change', function () {
ShoppingCartHandler.changeProductUnit(0);
});
}
},
changeProductUnit: function(changeValue){
var decrementProductUnitsButton = document.querySelector('.shop-now-side .units .decrement');
// Get the product units input
var shoppingCartItemUnit = parseInt(ShoppingCartHandler.productUnitsInput.value);
// Check if the unit is a number else reset it to 1
if (isNaN(shoppingCartItemUnit)) {
shoppingCartItemUnit = 1;
}
else {
// Change the product unit with the desired change value
shoppingCartItemUnit += changeValue;
}
// But it can't be below zero then you should remove it, and add/remove disabled on the decrement button
if (shoppingCartItemUnit <= 1) {
shoppingCartItemUnit = 1;
decrementProductUnitsButton.classList.add('disabled');
}
else {
decrementProductUnitsButton.classList.remove('disabled');
}
// Set the limit to 100 for a product
if (shoppingCartItemUnit >= 100) shoppingCartItemUnit = 100;
// Set the product unit
ShoppingCartHandler.productUnitsInput.value = shoppingCartItemUnit;
ShoppingCartHandler.productUnits = shoppingCartItemUnit;
},
// Create or get shopping cart
getShoppingCart: function(){
var shoppingCart = Cookie.get(ShoppingCartHandler.cookieName);
if (shoppingCart !== null) ShoppingCartHandler.shoppingCart = JSON.parse(shoppingCart);
else ShoppingCartHandler.shoppingCart = [];
},
addToShoppingCart: function (button) {
// Generate new shopping cart item
var item = {
'id': button.getAttribute('data-product-id'),
'category': button.getAttribute('data-category-id'),
// 'url': button.getAttribute('data-product-link'),
};
// Add item(s) to shopping cart
for(var i = 0; i < ShoppingCartHandler.productUnits; i++){
ShoppingCartHandler.shoppingCart.push(item);
}
// Export
ShoppingCartHandler.exportShoppingCartCookie();
},
// Export Shopping Cart to cookie
exportShoppingCartCookie: function () {
if(ShoppingCartHandler.shoppingCart.length !== 0){
Cookie.set(ShoppingCartHandler.cookieName, JSON.stringify(ShoppingCartHandler.shoppingCart), 1);
}
// Redirect after setting cookie
window.location.href = ShoppingCartHandler.shoppingCartUrl;
},
};
ShoppingCartHandler.init();