ng-shopping-cart
Version:
An Angular component library to create shopping carts
120 lines (119 loc) • 3.88 kB
TypeScript
import { EventEmitter } from '@angular/core';
import { CartItem } from '../classes/cart-item';
import { CartChangeEvent } from '../interfaces/cart-change-event';
import { LocaleFormat } from '../interfaces/locale-format';
/**
* The base class for storing items in your cart
*
* @service
* @note {warning} Do not modify the items `id` after they are added to the cart. Doing so could result in duplicates which can cause
* undefined behaviour
* @order 1
*/
export declare abstract class CartService<T extends CartItem> {
private localeFormat;
private format;
/**
* Emits an event every time items, tax, shipping cost or currency formats are changed in the cart.
*/
onChange: EventEmitter<CartChangeEvent>;
/**
* Emits an event every time an item is added to the cart.
*/
onItemAdded: EventEmitter<T>;
/**
* Emits an event every time an item is removed from the cart.
*
* > This event only fires when a single item is removed by id. If you want to be notified of any removal (eg: clearing the cart) listen
* to the `onChange` or the `onItemsChanged` event instead.
*/
onItemRemoved: EventEmitter<T>;
/**
* Emits an event every time an item is added or removed from the cart.
*/
onItemsChanged: EventEmitter<number>;
/**
* Emits an event every time taxes for the cart are changed.
*/
onTaxChange: EventEmitter<number>;
/**
* Emits an event every time shipping costs for the cart are changed.
*/
onShippingChange: EventEmitter<number>;
/**
* Finds an item by id
*/
abstract getItem(id: any): T;
/**
* Gets all the items in the cart.
*/
abstract getItems(): T[];
/**
* Add a new item to the cart.
*/
abstract addItem(item: T): void;
/**
* Remove an item from the cart by id.
*/
abstract removeItem(id: any): void;
/**
* Returns the number of unique items in the cart.
*/
abstract itemCount(): number;
/**
* Returns the number of items including each item's quantity.
*/
abstract entries(): number;
/**
* Returns the total cost of the shopping cart without shipping and taxes.
*/
abstract cost(): number;
/**
* Removes all items from the cart.
*/
abstract clear(): void;
/**
* Returns if the carts has any items in it.
*/
abstract isEmpty(): boolean;
/**
* Returns the shipping cost of the shopping cart.
*/
abstract getShipping(): number;
/**
* Sets the shipping cost of the shopping cart.
*/
abstract setShipping(shipping: number): void;
/**
* Returns the tax rate of the shopping cart.
*/
abstract getTaxRate(): number;
/**
* Sets the tax rate of the shopping cart.
*/
abstract setTaxRate(tax: number): void;
/**
* Returns the tax computation of the shopping cart.
*/
getTax(): number;
/**
* Returns the total cost of the shopping cart including shipping and taxes.
*/
totalCost(): number;
/**
* Changes the currency symbol and number format for all components associated to this service instance.
*
* Check the Angular `CurrencyPipe` and the Internationalization guide for more info.
*/
setLocaleFormat(format: string): void;
/**
* Returns the currency format as set with `setCurrencyFormat` or `'auto'` if no value is set.
*
* Passing true as parameter will return an object instead of a string.
*/
getLocaleFormat(object?: boolean): string | LocaleFormat;
/**
* Returns an object with all the cart information in it, useful for serialization of the cart.
*/
toObject(): any;
}