diff --git a/dist/shopping-cart.min.js b/dist/shopping-cart.min.js index 35c021b..8b54181 100644 --- a/dist/shopping-cart.min.js +++ b/dist/shopping-cart.min.js @@ -1,2 +1,2 @@ -var EcomCart={};!function(){"use strict";EcomCart.cart={subtotal:0,items:[]};var a=[];EcomCart.addCallback=function(t){"function"==typeof t&&a.push(t)},EcomCart.saveCart=function(){var t,r;for(t=EcomCart.cart.subtotal=0;to&&(t.quantity=o),EcomCart.saveCart(),t._id},"object"==typeof window&&(window.EcomCart=EcomCart),module&&module.exports&&(module.exports=EcomCart)}(),function(){"use strict";EcomCart.addItem=function(t){if(!("string"==typeof t.product_id&&"number"==typeof t.quantity&&0<=t.quantity&&"number"==typeof t.price&&0<=t.price))return console.error(new Error("Trying to add invalid item object")),null;for(var r=0;rc&&(t.quantity=c),EcomCart.saveCart(),t._id},"object"==typeof window&&(window.EcomCart=EcomCart),"undefined"!=typeof module&&module.exports&&(module.exports=EcomCart)}(),function(){"use strict";EcomCart.addItem=function(t){if(!("string"==typeof t.product_id&&"number"==typeof t.quantity&&0<=t.quantity&&"number"==typeof t.price&&0<=t.price))return console.error(new Error("Trying to add invalid item object")),null;for(var r=0;r\n * @license MIT\n */\n\n/**\n * EcomCart object with shopping cart methods.\n * @namespace\n */\n\nvar EcomCart = {}\n\n;(function () {\n 'use strict'\n\n /**\n * Current cart object.\n * @type {object}\n * @see {@link https://developers.e-com.plus/docs/api/#/store/carts/carts|Object model}\n */\n\n EcomCart.cart = {\n subtotal: 0,\n items: []\n }\n\n // trigger callbacks after cart changes (events)\n var callbacks = []\n\n /**\n * Add callback function for cart events.\n * @param {function} callback - The callback function\n */\n\n EcomCart.addCallback = function (callback) {\n if (typeof callback === 'function') {\n callbacks.push(callback)\n }\n }\n\n /**\n * Fix subtotal and save cart object to browser localStorage.\n */\n\n EcomCart.saveCart = function () {\n // fix cart subtotal\n EcomCart.cart.subtotal = 0\n var i, item\n for (i = 0; i < EcomCart.cart.items.length; i++) {\n item = EcomCart.cart.items[i]\n EcomCart.cart.subtotal += item.quantity * (item.final_price || item.price)\n }\n\n /* global localStorage */\n if (typeof localStorage === 'object' && localStorage) {\n localStorage.setItem('cart', JSON.stringify(EcomCart.cart))\n }\n // trigger callbacks\n for (i = 0; i < callbacks.length; i++) {\n callbacks[i](null, EcomCart.cart)\n }\n }\n\n /*\n * Check item quantity and save new cart object.\n * @param {object} item - Cart item object\n * @returns {string} Returns the item object ID\n */\n\n EcomCart.handleItem = function (item) {\n if (!item._id) {\n // generate random ObjectID\n // 24 chars hexadecimal string\n item._id = '1234'\n var hexa = '0123456789abcdef'\n for (var i = item._id.length; i < 24; i++) {\n item._id += hexa.charAt(Math.floor(Math.random() * hexa.length))\n }\n }\n\n // fix item quantity if needed\n // use minimun quantity 1 by default\n var min = item.min_quantity || 1\n var max = item.max_quantity\n if (typeof item.quantity !== 'number' || isNaN(item.quantity) || item.quantity < min) {\n item.quantity = min\n } else if (max && item.quantity > max) {\n item.quantity = max\n }\n // save the cart object again and return current item ID\n EcomCart.saveCart()\n return item._id\n }\n\n /**\n * Exports EcomCart object.\n * @module @ecomplus/shopping-cart\n * @returns {object} {@link EcomCart}\n */\n\n if (typeof window === 'object') {\n // on browser\n // set EcomCart object globally\n window.EcomCart = EcomCart\n }\n if (module && module.exports) {\n // Node.js\n module.exports = EcomCart\n }\n}())\n\n// require('methods/*')\n// require('reload')\n","/* global EcomCart */\n\n;(function () {\n 'use strict'\n\n /**\n * Add item to cart.\n * @param {object} newItem - New cart item object\n * @returns {string|null} Returns the item ID or null when object is invalid\n */\n\n EcomCart.addItem = function (newItem) {\n // check required fields\n if (typeof newItem.product_id === 'string' &&\n typeof newItem.quantity === 'number' && newItem.quantity >= 0 &&\n typeof newItem.price === 'number' && newItem.price >= 0) {\n // do the stuf\n } else {\n console.error(new Error('Trying to add invalid item object'))\n return null\n }\n\n for (var i = 0; i < EcomCart.cart.items.length; i++) {\n var item = EcomCart.cart.items[i]\n // check IDs\n if (item.product_id === newItem.product_id && item.variation_id === newItem.variation_id) {\n // same product and variation\n // update quantity and price\n item.quantity += newItem.quantity\n if (newItem.price) {\n item.price = newItem.price\n }\n if (newItem.final_price) {\n item.final_price = newItem.final_price\n }\n return EcomCart.handleItem(item)\n }\n }\n\n // add item to cart\n EcomCart.cart.items.push(newItem)\n return EcomCart.handleItem(newItem)\n }\n}())\n","/* global EcomCart */\n\n;(function () {\n 'use strict'\n\n /**\n * Remove all items from cart.\n */\n\n EcomCart.clearCart = function () {\n // empty the items array and save\n EcomCart.cart.items = []\n EcomCart.saveCart()\n }\n}())\n","/* global EcomCart */\n\n;(function () {\n 'use strict'\n\n /**\n * Increase quantity of specific item by ID.\n * @param {string} itemId - The unique object ID of item\n * @param {integer} qnt - Quantity to increase\n * @returns {boolean} True if the item was found\n */\n\n EcomCart.increaseItemQnt = function (itemId, qnt) {\n // find respective item on list by ID\n for (var i = 0; i < EcomCart.cart.items.length; i++) {\n var item = EcomCart.cart.items[i]\n if (item._id === itemId) {\n // item found\n item.quantity += qnt\n // fix new quantity if needed and save cart object\n EcomCart.handleItem(item)\n return true\n }\n }\n return false\n }\n}())\n","/* global EcomCart */\n\n;(function () {\n 'use strict'\n\n /**\n * Remove item from cart.\n * @param {string} itemId - The object ID of item to be removed\n * @returns {boolean} True if the item was found and removed\n */\n\n EcomCart.removeItem = function (itemId) {\n // find respective item on list by ID\n for (var i = 0; i < EcomCart.cart.items.length; i++) {\n if (EcomCart.cart.items[i]._id === itemId) {\n // item found\n // remove from items array\n EcomCart.cart.items.splice(i, 1)\n EcomCart.saveCart()\n return true\n }\n }\n return false\n }\n}())\n","/* global EcomCart */\n/* global localStorage */\n\n;(function () {\n 'use strict'\n\n if (typeof localStorage === 'object' && localStorage) {\n // try to load stored cart object\n var json = localStorage.getItem('cart')\n try {\n var cart = JSON.parse(json)\n if (typeof cart === 'object' && cart !== null && Array.isArray(cart.items)) {\n cart.items.forEach(function (item) {\n EcomCart.addItem(item)\n })\n }\n } catch (err) {\n // ignore invalid cart JSON\n }\n }\n}())\n"]} \ No newline at end of file +{"version":3,"sources":["index.js","addItem.js","clearCart.js","increaseItemQnt.js","removeItem.js","reload.js"],"names":["EcomCart","cart","subtotal","items","callbacks","addCallback","callback","push","saveCart","i","item","length","quantity","final_price","price","localStorage","setItem","JSON","stringify","handleItem","_id","hexa","charAt","Math","floor","random","min","min_quantity","max","max_quantity","isNaN","window","module","exports","addItem","newItem","product_id","console","error","Error","variation_id","clearCart","increaseItemQnt","itemId","qnt","removeItem","splice","json","getItem","parse","Array","isArray","forEach","err"],"mappings":"AAWA,IAAAA,SAAA,IAEA,WACA,aAQAA,SAAAC,KAAA,CACAC,SAAA,EACAC,MAAA,IAIA,IAAAC,EAAA,GAOAJ,SAAAK,YAAA,SAAAC,GACA,mBAAAA,GACAF,EAAAG,KAAAD,IAQAN,SAAAQ,SAAA,WAGA,IAAAC,EAAAC,EACA,IAAAD,EAFAT,SAAAC,KAAAC,SAAA,EAEAO,EAAAT,SAAAC,KAAAE,MAAAQ,OAAAF,IACAC,EAAAV,SAAAC,KAAAE,MAAAM,GACAT,SAAAC,KAAAC,UAAAQ,EAAAE,UAAAF,EAAAG,aAAAH,EAAAI,OAQA,IAJA,iBAAAC,cAAAA,cACAA,aAAAC,QAAA,OAAAC,KAAAC,UAAAlB,SAAAC,OAGAQ,EAAA,EAAAA,EAAAL,EAAAO,OAAAF,IACAL,EAAAK,GAAA,KAAAT,SAAAC,OAUAD,SAAAmB,WAAA,SAAAT,GACA,IAAAA,EAAAU,IAAA,CAGAV,EAAAU,IAAA,OAEA,IADA,IAAAC,EAAA,mBACAZ,EAAAC,EAAAU,IAAAT,OAAAF,EAAA,GAAAA,IACAC,EAAAU,KAAAC,EAAAC,OAAAC,KAAAC,MAAAD,KAAAE,SAAAJ,EAAAV,SAMA,IAAAe,EAAAhB,EAAAiB,cAAA,EACAC,EAAAlB,EAAAmB,aAQA,MAPA,iBAAAnB,EAAAE,UAAAkB,MAAApB,EAAAE,WAAAF,EAAAE,SAAA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AA,EAAAA,EAAAT,SAAAC,KAAAE,MAAAQ,OAAAF,IACA,GAAAT,SAAAC,KAAAE,MAAAM,GAAAW,MAAAuB,EAKA,OAFA3C,SAAAC,KAAAE,MAAA2C,OAAArC,EAAA,GACAT,SAAAQ,YACA,EAGA,OAAA,GApBA,GCCA,WACA,aAEA,GAAA,iBAAAO,cAAAA,aAAA,CAEA,IAAAgC,EAAAhC,aAAAiC,QAAA,QACA,IACA,IAAA/C,EAAAgB,KAAAgC,MAAAF,GACA,iBAAA9C,GAAA,OAAAA,GAAAiD,MAAAC,QAAAlD,EAAAE,QACAF,EAAAE,MAAAiD,QAAA,SAAA1C,GACAV,SAAAkC,QAAAxB,KAGA,MAAA2C,MAbA","file":"shopping-cart.min.js","sourcesContent":["/**\n * https://github.com/ecomclub/shopping-cart\n * @author E-Com Club \n * @license MIT\n */\n\n/**\n * EcomCart object with shopping cart methods.\n * @namespace\n */\n\nvar EcomCart = {}\n\n;(function () {\n 'use strict'\n\n /**\n * Current cart object.\n * @type {object}\n * @see {@link https://developers.e-com.plus/docs/api/#/store/carts/carts|Object model}\n */\n\n EcomCart.cart = {\n subtotal: 0,\n items: []\n }\n\n // trigger callbacks after cart changes (events)\n var callbacks = []\n\n /**\n * Add callback function for cart events.\n * @param {function} callback - The callback function\n */\n\n EcomCart.addCallback = function (callback) {\n if (typeof callback === 'function') {\n callbacks.push(callback)\n }\n }\n\n /**\n * Fix subtotal and save cart object to browser localStorage.\n */\n\n EcomCart.saveCart = function () {\n // fix cart subtotal\n EcomCart.cart.subtotal = 0\n var i, item\n for (i = 0; i < EcomCart.cart.items.length; i++) {\n item = EcomCart.cart.items[i]\n EcomCart.cart.subtotal += item.quantity * (item.final_price || item.price)\n }\n\n /* global localStorage */\n if (typeof localStorage === 'object' && localStorage) {\n localStorage.setItem('cart', JSON.stringify(EcomCart.cart))\n }\n // trigger callbacks\n for (i = 0; i < callbacks.length; i++) {\n callbacks[i](null, EcomCart.cart)\n }\n }\n\n /*\n * Check item quantity and save new cart object.\n * @param {object} item - Cart item object\n * @returns {string} Returns the item object ID\n */\n\n EcomCart.handleItem = function (item) {\n if (!item._id) {\n // generate random ObjectID\n // 24 chars hexadecimal string\n item._id = '1234'\n var hexa = '0123456789abcdef'\n for (var i = item._id.length; i < 24; i++) {\n item._id += hexa.charAt(Math.floor(Math.random() * hexa.length))\n }\n }\n\n // fix item quantity if needed\n // use minimun quantity 1 by default\n var min = item.min_quantity || 1\n var max = item.max_quantity\n if (typeof item.quantity !== 'number' || isNaN(item.quantity) || item.quantity < min) {\n item.quantity = min\n } else if (max && item.quantity > max) {\n item.quantity = max\n }\n // save the cart object again and return current item ID\n EcomCart.saveCart()\n return item._id\n }\n\n /**\n * Exports EcomCart object.\n * @module @ecomplus/shopping-cart\n * @returns {object} {@link EcomCart}\n */\n\n if (typeof window === 'object') {\n // on browser\n // set EcomCart object globally\n window.EcomCart = EcomCart\n }\n if (typeof module !== 'undefined' && module.exports) {\n // Node.js\n module.exports = EcomCart\n }\n}())\n\n// require('methods/*')\n// require('reload')\n","/* global EcomCart */\n\n;(function () {\n 'use strict'\n\n /**\n * Add item to cart.\n * @param {object} newItem - New cart item object\n * @returns {string|null} Returns the item ID or null when object is invalid\n */\n\n EcomCart.addItem = function (newItem) {\n // check required fields\n if (typeof newItem.product_id === 'string' &&\n typeof newItem.quantity === 'number' && newItem.quantity >= 0 &&\n typeof newItem.price === 'number' && newItem.price >= 0) {\n // do the stuf\n } else {\n console.error(new Error('Trying to add invalid item object'))\n return null\n }\n\n for (var i = 0; i < EcomCart.cart.items.length; i++) {\n var item = EcomCart.cart.items[i]\n // check IDs\n if (item.product_id === newItem.product_id && item.variation_id === newItem.variation_id) {\n // same product and variation\n // update quantity and price\n item.quantity += newItem.quantity\n if (newItem.price) {\n item.price = newItem.price\n }\n if (newItem.final_price) {\n item.final_price = newItem.final_price\n }\n return EcomCart.handleItem(item)\n }\n }\n\n // add item to cart\n EcomCart.cart.items.push(newItem)\n return EcomCart.handleItem(newItem)\n }\n}())\n","/* global EcomCart */\n\n;(function () {\n 'use strict'\n\n /**\n * Remove all items from cart.\n */\n\n EcomCart.clearCart = function () {\n // empty the items array and save\n EcomCart.cart.items = []\n EcomCart.saveCart()\n }\n}())\n","/* global EcomCart */\n\n;(function () {\n 'use strict'\n\n /**\n * Increase quantity of specific item by ID.\n * @param {string} itemId - The unique object ID of item\n * @param {integer} qnt - Quantity to increase\n * @returns {boolean} True if the item was found\n */\n\n EcomCart.increaseItemQnt = function (itemId, qnt) {\n // find respective item on list by ID\n for (var i = 0; i < EcomCart.cart.items.length; i++) {\n var item = EcomCart.cart.items[i]\n if (item._id === itemId) {\n // item found\n item.quantity += qnt\n // fix new quantity if needed and save cart object\n EcomCart.handleItem(item)\n return true\n }\n }\n return false\n }\n}())\n","/* global EcomCart */\n\n;(function () {\n 'use strict'\n\n /**\n * Remove item from cart.\n * @param {string} itemId - The object ID of item to be removed\n * @returns {boolean} True if the item was found and removed\n */\n\n EcomCart.removeItem = function (itemId) {\n // find respective item on list by ID\n for (var i = 0; i < EcomCart.cart.items.length; i++) {\n if (EcomCart.cart.items[i]._id === itemId) {\n // item found\n // remove from items array\n EcomCart.cart.items.splice(i, 1)\n EcomCart.saveCart()\n return true\n }\n }\n return false\n }\n}())\n","/* global EcomCart */\n/* global localStorage */\n\n;(function () {\n 'use strict'\n\n if (typeof localStorage === 'object' && localStorage) {\n // try to load stored cart object\n var json = localStorage.getItem('cart')\n try {\n var cart = JSON.parse(json)\n if (typeof cart === 'object' && cart !== null && Array.isArray(cart.items)) {\n cart.items.forEach(function (item) {\n EcomCart.addItem(item)\n })\n }\n } catch (err) {\n // ignore invalid cart JSON\n }\n }\n}())\n"]} \ No newline at end of file diff --git a/package.json b/package.json index d1c97f7..f7fc45d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ecomplus/shopping-cart", - "version": "0.4.0", + "version": "0.5.0", "description": "Simple vanilla JS library to handle shopping cart object", "main": "dist/shopping-cart.min.js", "scripts": {