@etsoo/appscript
Version:
Applications shared TypeScript framework
369 lines (368 loc) • 8.27 kB
TypeScript
import { DataTypes, IStorage, IdType } from "@etsoo/shared";
import { Currency } from "./Currency";
/**
* Shopping cart owner
* 购物篮所有人
*/
export type ShoppingCartOwner = DataTypes.IdNameItem & {
culture?: string;
currency?: Currency;
};
/**
* Shopping cart data
* 购物篮数据
*/
export type ShoppingCartData<T extends ShoppingCartItem> = {
culture: string;
currency: Currency;
owner: ShoppingCartOwner;
items: T[];
promotions: ShoppingPromotion[];
formData?: any;
cache?: Record<string, unknown>;
};
/**
* Shopping promotion
* 购物促销
*/
export type ShoppingPromotion = {
/**
* Promotion id
* 促销编号
*/
id: number;
/**
* Promotion title
* 促销标题
*/
title: string;
/**
* Discount amount
* 折扣金额
*/
amount: number;
};
/**
* Shopping cart base item
* 购物篮基础项目
*/
export type ShoppingCartItemBase = {
/**
* Product id
* 产品编号
*/
id: IdType;
/**
* Product title, default is name
* 产品标题,默认为name
*/
title?: string;
/**
* Sale price
* 销售价格
*/
price: number;
/**
* Qty
* 数量
*/
qty: number;
/**
* Asset qty
*/
assetQty?: number;
/**
* Product level promotions
* 产品层次促销
*/
promotions: ShoppingPromotion[];
};
/**
* Shopping cart item
* 购物篮项目
*/
export type ShoppingCartItem = ShoppingCartItemBase & {
/**
* Product name
* 产品名称
*/
name: string;
/**
* Current price for cache
* 当前缓存价格
*/
currentPrice?: number;
/**
* Subtotal
* 小计
*/
subtotal: number;
/**
* Total discount amount
* 总折扣金额
*/
discount: number;
};
/**
* Shopping cart change reason
* 购物篮改变原因
*/
export type ShoppingCartChangeReason = "add" | "clear" | "remove" | "title" | "update";
/**
* Shopping cart
* 购物篮
*/
export declare class ShoppingCart<T extends ShoppingCartItem> {
private readonly storage;
/**
* Create identifier key
* 创建识别键
* @param currency Currency
* @param culture Culture
* @param key Additional key
* @returns Result
*/
static createKey(currency: Currency, culture: string, key: string): string;
/**
* Clear shopping cart
* 清除购物篮
* @param identifier Identifier
* @param storage Storage
*/
static clear(identifier: string, storage: IStorage): void;
/**
* Get cart data
* 获取购物篮数据
* @param storage Storage
* @param id Cart id
* @returns Result
*/
static getCartData<D extends ShoppingCartItem>(storage: IStorage, id: string): ShoppingCartData<D> | undefined;
/**
* Owner data
* 所有者信息
*/
owner?: ShoppingCartOwner;
_currency: Currency;
/**
* ISO currency id
* 标准货币编号
*/
get currency(): Currency;
private set currency(value);
_culture: string;
/**
* ISO culture id, like zh-Hans
* 标准语言文化编号
*/
get culture(): string;
private set culture(value);
_items: T[];
/**
* Items
* 项目
*/
get items(): T[];
private set items(value);
_promotions: ShoppingPromotion[];
/**
* Order level promotions
* 订单层面促销
*/
get promotions(): ShoppingPromotion[];
private set promotions(value);
/**
* Related form data
* 关联的表单数据
*/
formData: any;
/**
* Cache
* 缓存对象
*/
cache?: Record<string, unknown>;
_symbol: string | undefined;
/**
* Currency symbol
* 币种符号
*/
get symbol(): string | undefined;
private set symbol(value);
/**
* Key for identifier
*/
readonly key: string;
/**
* Cart identifier
* 购物篮标识
*/
get identifier(): string;
/**
* All data keys
* 所有的数据键
*/
get keys(): string[];
set keys(items: string[]);
/**
* Lines count
* 项目数量
*/
get lines(): number;
/**
* Total qty
* 总数量
*/
get totalQty(): number;
/**
* Total amount
* 总金额
*/
get totalAmount(): number;
/**
* Total amount string
* 总金额字符串
*/
get totalAmountStr(): string;
/**
* Cached prices
* 缓存的价格
*/
private prices;
/**
* Onchange callback
* 改变时回调
*/
onChange?: (reason: ShoppingCartChangeReason, changedItems: T[]) => void;
/**
* Constructor
* 构造函数
* @param key Key for identifier
* @param init Currency & culture ISO code array
* @param storage Data storage
*/
constructor(key: string, init: [Currency, string], storage?: IStorage);
/**
* Constructor
* 构造函数
* @param key Key for identifier
* @param state Initialization state
* @param storage Data storage
*/
constructor(key: string, state: ShoppingCartData<T>, storage?: IStorage);
private getCartData;
private setCartData;
private doChange;
/**
* Add item
* 添加项目
* @param item New item
*/
addItem(item: T): void;
/**
* Add items
* @param items New items
*/
addItems(items: T[]): void;
/**
* Cache price
* @param id Item id
* @param price Price
* @param overrideExisting Override existing price
*/
cachePrice(id: T["id"], price: number, overrideExisting?: boolean): void;
/**
* Change currency
* @param currency Currency
*/
changeCurrency(currency: Currency): void;
/**
* Change culture
* @param culture Culture
*/
changeCulture(culture: string): void;
/**
* Clear storage
* @param keepOwner Keep owner data
*/
clear(keepOwner?: boolean): void;
/**
* Format amount
* @param amount Amount
* @returns Result
*/
formatAmount(amount: number): string;
/**
* Get item
* @param id Item id
* @returns Result
*/
getItem(id: T["id"]): T | undefined;
/**
* Push item
* 推送项目
* @param data Item data
* @returns Added or not
*/
pushItem(data: T): boolean;
/**
* Reset currency and culture
* @param currency New currency
* @param culture New culture
*/
reset(currency: Currency, culture: string): void;
/**
* Remove item from the index
* @param index Item index
*/
removeItem(index: number): void;
/**
* Reset item
* @param item Shopping cart item
*/
resetItem(item: ShoppingCartItem): void;
/**
* Save cart data
* @param persisted For persisted storage
*/
save(persisted?: boolean): ShoppingCartData<T> | undefined;
/**
* Trigger update
* 触发更新
*/
update(): void;
/**
* Update discount
* @param item Shopping cart item
*/
updateDiscount(item: ShoppingCartItem): void;
/**
* Update asset item
* 更新资产项目
* @param id Product id
* @param qty Asset qty
* @param itemCreator New item creator
* @returns Updated or not
*/
updateAssetItem(id: T["id"], assetQty: number | undefined, itemCreator?: () => Omit<T, "id" | "price" | "assetQty" | "subtotal" | "discount" | "promotions">): boolean;
/**
* Update item
* 更新项目
* @param id Product id
* @param qty Qty
* @param itemCreator New item creator
* @returns Updated or not
*/
updateItem(id: T["id"], qty: number | undefined, itemCreator?: () => Omit<T, "id" | "price" | "qty" | "subtotal" | "discount" | "promotions">): boolean;
/**
* Update price
* @param id Item id
* @param price New price
*/
updatePrice(id: T["id"], price: number): void;
/**
* Update title
* @param id Item id
* @param title New title
*/
updateTitle(id: T["id"], title: string): void;
}