decentralized-internet
Version:
An NPM library of programs to create decentralized web and distributed computing projects
1 lines • 1.3 kB
JavaScript
import{$ecomConfig,randomObjectId}from"@ecomplus/utils";import*as EventEmitter from"eventemitter3";import addItem from"./methods/add-item";import addPoduct from"./methods/add-product";import increaseItemQnt from"./methods/increase-item-qnt";import removeItem from"./methods/remove-item";import save from"./methods/save";import clear from"./methods/clear";import reset from"./methods/reset";const defaultStorage="object"==typeof window&&window.localStorage,EcomCart=function(t,e="ecomShoppingCart",o=defaultStorage){const r=this;r.Constructor=EcomCart,r.storeId=t||$ecomConfig.get("store_id"),r.storageKey=e,r.localStorage=o,r.data={items:[],subtotal:0};const m=new EventEmitter;["on","off","once"].forEach(t=>{r[t]=((e,o)=>{m[t](e,o)})});const a=(t,e=[],o=!0)=>{const a=t(r,m,e);if(a&&o){const{data:t}=r;m.emit("change",{data:t})}return a};if(this.addItem=((t,e)=>a(addItem,[t,e])),this.addProduct=((t,e,o,r)=>a(addPoduct,[t,e,o,r])),this.increaseItemQnt=((t,e,o)=>a(increaseItemQnt,[t,e,o])),this.removeItem=((t,e)=>a(removeItem,[t,e])),this.clear=(t=>a(clear,[t])),this.reset=(t=>a(reset,[t])),this.save=(t=>a(save,[t],!1)),o&&e){const t=o.getItem(e);if(t){let e;try{e=JSON.parse(t)}catch(t){}e&&Array.isArray(e.items)&&(r.data=e)}}r.data._id||(r.data._id=randomObjectId())};export default EcomCart;