@etsoo/appscript 1.4.63 → 1.4.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,7 +5,6 @@ import { Currency } from './Currency';
|
|
|
5
5
|
* 购物篮所有人
|
|
6
6
|
*/
|
|
7
7
|
export type ShoppingCartOwner = DataTypes.IdNameItem & {
|
|
8
|
-
isSupplier?: boolean;
|
|
9
8
|
culture?: string;
|
|
10
9
|
currency?: Currency;
|
|
11
10
|
};
|
|
@@ -20,6 +19,7 @@ export type ShoppingCartData<T extends ShoppingCartItem> = {
|
|
|
20
19
|
items: T[];
|
|
21
20
|
promotions: ShoppingPromotion[];
|
|
22
21
|
formData?: any;
|
|
22
|
+
cache?: Record<string, unknown>;
|
|
23
23
|
};
|
|
24
24
|
/**
|
|
25
25
|
* Shopping promotion
|
|
@@ -122,7 +122,7 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
122
122
|
* @param key Additional key
|
|
123
123
|
* @returns Result
|
|
124
124
|
*/
|
|
125
|
-
static createKey(currency: Currency, culture: string, key
|
|
125
|
+
static createKey(currency: Currency, culture: string, key: string): string;
|
|
126
126
|
/**
|
|
127
127
|
* Clear shopping cart
|
|
128
128
|
* 清除购物篮
|
|
@@ -130,14 +130,6 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
130
130
|
* @param storage Storage
|
|
131
131
|
*/
|
|
132
132
|
static clear(identifier: string, storage: IStorage): void;
|
|
133
|
-
/**
|
|
134
|
-
* Clear shopping cart
|
|
135
|
-
* 清除购物篮
|
|
136
|
-
* @param currency Currency
|
|
137
|
-
* @param culture Culture
|
|
138
|
-
* @param storage Storage
|
|
139
|
-
*/
|
|
140
|
-
static clearWith(currency: Currency, culture: string, storage?: IStorage): void;
|
|
141
133
|
/**
|
|
142
134
|
* Get cart data
|
|
143
135
|
* 获取购物篮数据
|
|
@@ -146,13 +138,11 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
146
138
|
* @returns Result
|
|
147
139
|
*/
|
|
148
140
|
static getCartData<D extends ShoppingCartItem>(storage: IStorage, id: string): ShoppingCartData<D> | undefined;
|
|
149
|
-
_owner?: ShoppingCartOwner;
|
|
150
141
|
/**
|
|
151
142
|
* Owner data
|
|
152
143
|
* 所有者信息
|
|
153
144
|
*/
|
|
154
|
-
|
|
155
|
-
set owner(value: ShoppingCartOwner | undefined);
|
|
145
|
+
owner?: ShoppingCartOwner;
|
|
156
146
|
_currency: Currency;
|
|
157
147
|
/**
|
|
158
148
|
* ISO currency id
|
|
@@ -186,6 +176,11 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
186
176
|
* 关联的表单数据
|
|
187
177
|
*/
|
|
188
178
|
formData: any;
|
|
179
|
+
/**
|
|
180
|
+
* Cache
|
|
181
|
+
* 缓存对象
|
|
182
|
+
*/
|
|
183
|
+
cache?: Record<string, unknown>;
|
|
189
184
|
_symbol: string | undefined;
|
|
190
185
|
/**
|
|
191
186
|
* Currency symbol
|
|
@@ -193,6 +188,10 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
193
188
|
*/
|
|
194
189
|
get symbol(): string | undefined;
|
|
195
190
|
private set symbol(value);
|
|
191
|
+
/**
|
|
192
|
+
* Key for identifier
|
|
193
|
+
*/
|
|
194
|
+
readonly key: string;
|
|
196
195
|
/**
|
|
197
196
|
* Cart identifier
|
|
198
197
|
* 购物篮标识
|
|
@@ -237,17 +236,20 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
237
236
|
/**
|
|
238
237
|
* Constructor
|
|
239
238
|
* 构造函数
|
|
239
|
+
* @param key Key for identifier
|
|
240
240
|
* @param init Currency & culture ISO code array
|
|
241
241
|
* @param storage Data storage
|
|
242
242
|
*/
|
|
243
|
-
constructor(init: [Currency, string], storage?: IStorage);
|
|
243
|
+
constructor(key: string, init: [Currency, string], storage?: IStorage);
|
|
244
244
|
/**
|
|
245
245
|
* Constructor
|
|
246
246
|
* 构造函数
|
|
247
|
+
* @param key Key for identifier
|
|
247
248
|
* @param state Initialization state
|
|
248
249
|
* @param storage Data storage
|
|
249
250
|
*/
|
|
250
|
-
constructor(state: ShoppingCartData<T>, storage?: IStorage);
|
|
251
|
+
constructor(key: string, state: ShoppingCartData<T>, storage?: IStorage);
|
|
252
|
+
private getCartData;
|
|
251
253
|
private setCartData;
|
|
252
254
|
private doChange;
|
|
253
255
|
/**
|
|
@@ -16,7 +16,7 @@ class ShoppingCart {
|
|
|
16
16
|
* @param key Additional key
|
|
17
17
|
* @returns Result
|
|
18
18
|
*/
|
|
19
|
-
static createKey(currency, culture, key
|
|
19
|
+
static createKey(currency, culture, key) {
|
|
20
20
|
return `ETSOO-CART-${culture}-${key}-${currency}`;
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
@@ -34,17 +34,6 @@ class ShoppingCart {
|
|
|
34
34
|
console.log('ShoppingCart clear', error);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
/**
|
|
38
|
-
* Clear shopping cart
|
|
39
|
-
* 清除购物篮
|
|
40
|
-
* @param currency Currency
|
|
41
|
-
* @param culture Culture
|
|
42
|
-
* @param storage Storage
|
|
43
|
-
*/
|
|
44
|
-
static clearWith(currency, culture, storage = new shared_1.WindowStorage()) {
|
|
45
|
-
const identifier = this.createKey(currency, culture);
|
|
46
|
-
this.clear(identifier, storage);
|
|
47
|
-
}
|
|
48
37
|
/**
|
|
49
38
|
* Get cart data
|
|
50
39
|
* 获取购物篮数据
|
|
@@ -61,24 +50,6 @@ class ShoppingCart {
|
|
|
61
50
|
console.log('ShoppingCart constructor', error);
|
|
62
51
|
}
|
|
63
52
|
}
|
|
64
|
-
/**
|
|
65
|
-
* Owner data
|
|
66
|
-
* 所有者信息
|
|
67
|
-
*/
|
|
68
|
-
get owner() {
|
|
69
|
-
return this._owner;
|
|
70
|
-
}
|
|
71
|
-
set owner(value) {
|
|
72
|
-
var _a;
|
|
73
|
-
if (((_a = this._owner) === null || _a === void 0 ? void 0 : _a.id) === (value === null || value === void 0 ? void 0 : value.id))
|
|
74
|
-
return;
|
|
75
|
-
this._owner = value;
|
|
76
|
-
if (value) {
|
|
77
|
-
const data = ShoppingCart.getCartData(this.storage, this.identifier);
|
|
78
|
-
if (data)
|
|
79
|
-
this.setCartData(data);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
53
|
/**
|
|
83
54
|
* ISO currency id
|
|
84
55
|
* 标准货币编号
|
|
@@ -135,7 +106,7 @@ class ShoppingCart {
|
|
|
135
106
|
*/
|
|
136
107
|
get identifier() {
|
|
137
108
|
const o = this.owner;
|
|
138
|
-
return ShoppingCart.createKey(this.currency, this.culture,
|
|
109
|
+
return ShoppingCart.createKey(this.currency, this.culture, this.key);
|
|
139
110
|
}
|
|
140
111
|
/**
|
|
141
112
|
* All data keys
|
|
@@ -182,10 +153,11 @@ class ShoppingCart {
|
|
|
182
153
|
/**
|
|
183
154
|
* Constructor
|
|
184
155
|
* 构造函数
|
|
156
|
+
* @param key Key for identifier
|
|
185
157
|
* @param currency Currency ISO code
|
|
186
158
|
* @param storage Data storage
|
|
187
159
|
*/
|
|
188
|
-
constructor(currencyOrState, storage = new shared_1.WindowStorage()) {
|
|
160
|
+
constructor(key, currencyOrState, storage = new shared_1.WindowStorage()) {
|
|
189
161
|
this.storage = storage;
|
|
190
162
|
this._items = [];
|
|
191
163
|
this._promotions = [];
|
|
@@ -194,9 +166,9 @@ class ShoppingCart {
|
|
|
194
166
|
* 缓存的价格
|
|
195
167
|
*/
|
|
196
168
|
this.prices = {};
|
|
169
|
+
this.key = key;
|
|
197
170
|
if (Array.isArray(currencyOrState)) {
|
|
198
|
-
this.
|
|
199
|
-
this.changeCulture(currencyOrState[1]);
|
|
171
|
+
this.reset(currencyOrState[0], currencyOrState[1]);
|
|
200
172
|
}
|
|
201
173
|
else {
|
|
202
174
|
this.setCartData(currencyOrState);
|
|
@@ -204,12 +176,17 @@ class ShoppingCart {
|
|
|
204
176
|
this.changeCulture(currencyOrState.culture);
|
|
205
177
|
}
|
|
206
178
|
}
|
|
179
|
+
getCartData() {
|
|
180
|
+
var _a;
|
|
181
|
+
return ((_a = this.storage.getPersistedObject(this.identifier)) !== null && _a !== void 0 ? _a : this.storage.getObject(this.identifier));
|
|
182
|
+
}
|
|
207
183
|
setCartData(state) {
|
|
208
|
-
const { owner, items = [], promotions = [], formData } = state !== null && state !== void 0 ? state : {};
|
|
184
|
+
const { owner, items = [], promotions = [], formData, cache } = state !== null && state !== void 0 ? state : {};
|
|
209
185
|
this.owner = owner;
|
|
210
186
|
this.items = items;
|
|
211
187
|
this.promotions = promotions;
|
|
212
188
|
this.formData = formData;
|
|
189
|
+
this.cache = cache;
|
|
213
190
|
}
|
|
214
191
|
doChange(reason, changedItems) {
|
|
215
192
|
if (this.onChange)
|
|
@@ -315,9 +292,9 @@ class ShoppingCart {
|
|
|
315
292
|
* @param culture New culture
|
|
316
293
|
*/
|
|
317
294
|
reset(currency, culture) {
|
|
318
|
-
this.clear(true);
|
|
319
295
|
this.changeCurrency(currency);
|
|
320
296
|
this.changeCulture(culture);
|
|
297
|
+
this.setCartData(this.getCartData());
|
|
321
298
|
}
|
|
322
299
|
/**
|
|
323
300
|
* Remove item from the index
|
|
@@ -343,14 +320,15 @@ class ShoppingCart {
|
|
|
343
320
|
save(persisted = true) {
|
|
344
321
|
if (this.owner == null)
|
|
345
322
|
return;
|
|
346
|
-
const { currency, culture, owner, items, promotions, formData } = this;
|
|
323
|
+
const { currency, culture, owner, items, promotions, formData, cache } = this;
|
|
347
324
|
const data = {
|
|
348
325
|
currency,
|
|
349
326
|
culture,
|
|
350
327
|
owner,
|
|
351
328
|
items,
|
|
352
329
|
promotions,
|
|
353
|
-
formData
|
|
330
|
+
formData,
|
|
331
|
+
cache
|
|
354
332
|
};
|
|
355
333
|
try {
|
|
356
334
|
if (persisted) {
|
|
@@ -5,7 +5,6 @@ import { Currency } from './Currency';
|
|
|
5
5
|
* 购物篮所有人
|
|
6
6
|
*/
|
|
7
7
|
export type ShoppingCartOwner = DataTypes.IdNameItem & {
|
|
8
|
-
isSupplier?: boolean;
|
|
9
8
|
culture?: string;
|
|
10
9
|
currency?: Currency;
|
|
11
10
|
};
|
|
@@ -20,6 +19,7 @@ export type ShoppingCartData<T extends ShoppingCartItem> = {
|
|
|
20
19
|
items: T[];
|
|
21
20
|
promotions: ShoppingPromotion[];
|
|
22
21
|
formData?: any;
|
|
22
|
+
cache?: Record<string, unknown>;
|
|
23
23
|
};
|
|
24
24
|
/**
|
|
25
25
|
* Shopping promotion
|
|
@@ -122,7 +122,7 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
122
122
|
* @param key Additional key
|
|
123
123
|
* @returns Result
|
|
124
124
|
*/
|
|
125
|
-
static createKey(currency: Currency, culture: string, key
|
|
125
|
+
static createKey(currency: Currency, culture: string, key: string): string;
|
|
126
126
|
/**
|
|
127
127
|
* Clear shopping cart
|
|
128
128
|
* 清除购物篮
|
|
@@ -130,14 +130,6 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
130
130
|
* @param storage Storage
|
|
131
131
|
*/
|
|
132
132
|
static clear(identifier: string, storage: IStorage): void;
|
|
133
|
-
/**
|
|
134
|
-
* Clear shopping cart
|
|
135
|
-
* 清除购物篮
|
|
136
|
-
* @param currency Currency
|
|
137
|
-
* @param culture Culture
|
|
138
|
-
* @param storage Storage
|
|
139
|
-
*/
|
|
140
|
-
static clearWith(currency: Currency, culture: string, storage?: IStorage): void;
|
|
141
133
|
/**
|
|
142
134
|
* Get cart data
|
|
143
135
|
* 获取购物篮数据
|
|
@@ -146,13 +138,11 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
146
138
|
* @returns Result
|
|
147
139
|
*/
|
|
148
140
|
static getCartData<D extends ShoppingCartItem>(storage: IStorage, id: string): ShoppingCartData<D> | undefined;
|
|
149
|
-
_owner?: ShoppingCartOwner;
|
|
150
141
|
/**
|
|
151
142
|
* Owner data
|
|
152
143
|
* 所有者信息
|
|
153
144
|
*/
|
|
154
|
-
|
|
155
|
-
set owner(value: ShoppingCartOwner | undefined);
|
|
145
|
+
owner?: ShoppingCartOwner;
|
|
156
146
|
_currency: Currency;
|
|
157
147
|
/**
|
|
158
148
|
* ISO currency id
|
|
@@ -186,6 +176,11 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
186
176
|
* 关联的表单数据
|
|
187
177
|
*/
|
|
188
178
|
formData: any;
|
|
179
|
+
/**
|
|
180
|
+
* Cache
|
|
181
|
+
* 缓存对象
|
|
182
|
+
*/
|
|
183
|
+
cache?: Record<string, unknown>;
|
|
189
184
|
_symbol: string | undefined;
|
|
190
185
|
/**
|
|
191
186
|
* Currency symbol
|
|
@@ -193,6 +188,10 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
193
188
|
*/
|
|
194
189
|
get symbol(): string | undefined;
|
|
195
190
|
private set symbol(value);
|
|
191
|
+
/**
|
|
192
|
+
* Key for identifier
|
|
193
|
+
*/
|
|
194
|
+
readonly key: string;
|
|
196
195
|
/**
|
|
197
196
|
* Cart identifier
|
|
198
197
|
* 购物篮标识
|
|
@@ -237,17 +236,20 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
237
236
|
/**
|
|
238
237
|
* Constructor
|
|
239
238
|
* 构造函数
|
|
239
|
+
* @param key Key for identifier
|
|
240
240
|
* @param init Currency & culture ISO code array
|
|
241
241
|
* @param storage Data storage
|
|
242
242
|
*/
|
|
243
|
-
constructor(init: [Currency, string], storage?: IStorage);
|
|
243
|
+
constructor(key: string, init: [Currency, string], storage?: IStorage);
|
|
244
244
|
/**
|
|
245
245
|
* Constructor
|
|
246
246
|
* 构造函数
|
|
247
|
+
* @param key Key for identifier
|
|
247
248
|
* @param state Initialization state
|
|
248
249
|
* @param storage Data storage
|
|
249
250
|
*/
|
|
250
|
-
constructor(state: ShoppingCartData<T>, storage?: IStorage);
|
|
251
|
+
constructor(key: string, state: ShoppingCartData<T>, storage?: IStorage);
|
|
252
|
+
private getCartData;
|
|
251
253
|
private setCartData;
|
|
252
254
|
private doChange;
|
|
253
255
|
/**
|
|
@@ -13,7 +13,7 @@ export class ShoppingCart {
|
|
|
13
13
|
* @param key Additional key
|
|
14
14
|
* @returns Result
|
|
15
15
|
*/
|
|
16
|
-
static createKey(currency, culture, key
|
|
16
|
+
static createKey(currency, culture, key) {
|
|
17
17
|
return `ETSOO-CART-${culture}-${key}-${currency}`;
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
@@ -31,17 +31,6 @@ export class ShoppingCart {
|
|
|
31
31
|
console.log('ShoppingCart clear', error);
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Clear shopping cart
|
|
36
|
-
* 清除购物篮
|
|
37
|
-
* @param currency Currency
|
|
38
|
-
* @param culture Culture
|
|
39
|
-
* @param storage Storage
|
|
40
|
-
*/
|
|
41
|
-
static clearWith(currency, culture, storage = new WindowStorage()) {
|
|
42
|
-
const identifier = this.createKey(currency, culture);
|
|
43
|
-
this.clear(identifier, storage);
|
|
44
|
-
}
|
|
45
34
|
/**
|
|
46
35
|
* Get cart data
|
|
47
36
|
* 获取购物篮数据
|
|
@@ -58,24 +47,6 @@ export class ShoppingCart {
|
|
|
58
47
|
console.log('ShoppingCart constructor', error);
|
|
59
48
|
}
|
|
60
49
|
}
|
|
61
|
-
/**
|
|
62
|
-
* Owner data
|
|
63
|
-
* 所有者信息
|
|
64
|
-
*/
|
|
65
|
-
get owner() {
|
|
66
|
-
return this._owner;
|
|
67
|
-
}
|
|
68
|
-
set owner(value) {
|
|
69
|
-
var _a;
|
|
70
|
-
if (((_a = this._owner) === null || _a === void 0 ? void 0 : _a.id) === (value === null || value === void 0 ? void 0 : value.id))
|
|
71
|
-
return;
|
|
72
|
-
this._owner = value;
|
|
73
|
-
if (value) {
|
|
74
|
-
const data = ShoppingCart.getCartData(this.storage, this.identifier);
|
|
75
|
-
if (data)
|
|
76
|
-
this.setCartData(data);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
50
|
/**
|
|
80
51
|
* ISO currency id
|
|
81
52
|
* 标准货币编号
|
|
@@ -132,7 +103,7 @@ export class ShoppingCart {
|
|
|
132
103
|
*/
|
|
133
104
|
get identifier() {
|
|
134
105
|
const o = this.owner;
|
|
135
|
-
return ShoppingCart.createKey(this.currency, this.culture,
|
|
106
|
+
return ShoppingCart.createKey(this.currency, this.culture, this.key);
|
|
136
107
|
}
|
|
137
108
|
/**
|
|
138
109
|
* All data keys
|
|
@@ -179,10 +150,11 @@ export class ShoppingCart {
|
|
|
179
150
|
/**
|
|
180
151
|
* Constructor
|
|
181
152
|
* 构造函数
|
|
153
|
+
* @param key Key for identifier
|
|
182
154
|
* @param currency Currency ISO code
|
|
183
155
|
* @param storage Data storage
|
|
184
156
|
*/
|
|
185
|
-
constructor(currencyOrState, storage = new WindowStorage()) {
|
|
157
|
+
constructor(key, currencyOrState, storage = new WindowStorage()) {
|
|
186
158
|
this.storage = storage;
|
|
187
159
|
this._items = [];
|
|
188
160
|
this._promotions = [];
|
|
@@ -191,9 +163,9 @@ export class ShoppingCart {
|
|
|
191
163
|
* 缓存的价格
|
|
192
164
|
*/
|
|
193
165
|
this.prices = {};
|
|
166
|
+
this.key = key;
|
|
194
167
|
if (Array.isArray(currencyOrState)) {
|
|
195
|
-
this.
|
|
196
|
-
this.changeCulture(currencyOrState[1]);
|
|
168
|
+
this.reset(currencyOrState[0], currencyOrState[1]);
|
|
197
169
|
}
|
|
198
170
|
else {
|
|
199
171
|
this.setCartData(currencyOrState);
|
|
@@ -201,12 +173,17 @@ export class ShoppingCart {
|
|
|
201
173
|
this.changeCulture(currencyOrState.culture);
|
|
202
174
|
}
|
|
203
175
|
}
|
|
176
|
+
getCartData() {
|
|
177
|
+
var _a;
|
|
178
|
+
return ((_a = this.storage.getPersistedObject(this.identifier)) !== null && _a !== void 0 ? _a : this.storage.getObject(this.identifier));
|
|
179
|
+
}
|
|
204
180
|
setCartData(state) {
|
|
205
|
-
const { owner, items = [], promotions = [], formData } = state !== null && state !== void 0 ? state : {};
|
|
181
|
+
const { owner, items = [], promotions = [], formData, cache } = state !== null && state !== void 0 ? state : {};
|
|
206
182
|
this.owner = owner;
|
|
207
183
|
this.items = items;
|
|
208
184
|
this.promotions = promotions;
|
|
209
185
|
this.formData = formData;
|
|
186
|
+
this.cache = cache;
|
|
210
187
|
}
|
|
211
188
|
doChange(reason, changedItems) {
|
|
212
189
|
if (this.onChange)
|
|
@@ -312,9 +289,9 @@ export class ShoppingCart {
|
|
|
312
289
|
* @param culture New culture
|
|
313
290
|
*/
|
|
314
291
|
reset(currency, culture) {
|
|
315
|
-
this.clear(true);
|
|
316
292
|
this.changeCurrency(currency);
|
|
317
293
|
this.changeCulture(culture);
|
|
294
|
+
this.setCartData(this.getCartData());
|
|
318
295
|
}
|
|
319
296
|
/**
|
|
320
297
|
* Remove item from the index
|
|
@@ -340,14 +317,15 @@ export class ShoppingCart {
|
|
|
340
317
|
save(persisted = true) {
|
|
341
318
|
if (this.owner == null)
|
|
342
319
|
return;
|
|
343
|
-
const { currency, culture, owner, items, promotions, formData } = this;
|
|
320
|
+
const { currency, culture, owner, items, promotions, formData, cache } = this;
|
|
344
321
|
const data = {
|
|
345
322
|
currency,
|
|
346
323
|
culture,
|
|
347
324
|
owner,
|
|
348
325
|
items,
|
|
349
326
|
promotions,
|
|
350
|
-
formData
|
|
327
|
+
formData,
|
|
328
|
+
cache
|
|
351
329
|
};
|
|
352
330
|
try {
|
|
353
331
|
if (persisted) {
|
package/package.json
CHANGED
|
@@ -6,7 +6,6 @@ import { Currency } from './Currency';
|
|
|
6
6
|
* 购物篮所有人
|
|
7
7
|
*/
|
|
8
8
|
export type ShoppingCartOwner = DataTypes.IdNameItem & {
|
|
9
|
-
isSupplier?: boolean;
|
|
10
9
|
culture?: string;
|
|
11
10
|
currency?: Currency;
|
|
12
11
|
};
|
|
@@ -22,6 +21,7 @@ export type ShoppingCartData<T extends ShoppingCartItem> = {
|
|
|
22
21
|
items: T[];
|
|
23
22
|
promotions: ShoppingPromotion[];
|
|
24
23
|
formData?: any;
|
|
24
|
+
cache?: Record<string, unknown>;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
/**
|
|
@@ -145,7 +145,7 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
145
145
|
* @param key Additional key
|
|
146
146
|
* @returns Result
|
|
147
147
|
*/
|
|
148
|
-
static createKey(currency: Currency, culture: string, key: string
|
|
148
|
+
static createKey(currency: Currency, culture: string, key: string) {
|
|
149
149
|
return `ETSOO-CART-${culture}-${key}-${currency}`;
|
|
150
150
|
}
|
|
151
151
|
|
|
@@ -164,22 +164,6 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
/**
|
|
168
|
-
* Clear shopping cart
|
|
169
|
-
* 清除购物篮
|
|
170
|
-
* @param currency Currency
|
|
171
|
-
* @param culture Culture
|
|
172
|
-
* @param storage Storage
|
|
173
|
-
*/
|
|
174
|
-
static clearWith(
|
|
175
|
-
currency: Currency,
|
|
176
|
-
culture: string,
|
|
177
|
-
storage: IStorage = new WindowStorage()
|
|
178
|
-
) {
|
|
179
|
-
const identifier = this.createKey(currency, culture);
|
|
180
|
-
this.clear(identifier, storage);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
167
|
/**
|
|
184
168
|
* Get cart data
|
|
185
169
|
* 获取购物篮数据
|
|
@@ -201,25 +185,11 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
201
185
|
}
|
|
202
186
|
}
|
|
203
187
|
|
|
204
|
-
_owner?: ShoppingCartOwner;
|
|
205
188
|
/**
|
|
206
189
|
* Owner data
|
|
207
190
|
* 所有者信息
|
|
208
191
|
*/
|
|
209
|
-
|
|
210
|
-
return this._owner;
|
|
211
|
-
}
|
|
212
|
-
set owner(value) {
|
|
213
|
-
if (this._owner?.id === value?.id) return;
|
|
214
|
-
this._owner = value;
|
|
215
|
-
if (value) {
|
|
216
|
-
const data = ShoppingCart.getCartData<T>(
|
|
217
|
-
this.storage,
|
|
218
|
-
this.identifier
|
|
219
|
-
);
|
|
220
|
-
if (data) this.setCartData(data);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
192
|
+
owner?: ShoppingCartOwner;
|
|
223
193
|
|
|
224
194
|
_currency!: Currency;
|
|
225
195
|
|
|
@@ -277,6 +247,12 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
277
247
|
*/
|
|
278
248
|
formData: any;
|
|
279
249
|
|
|
250
|
+
/**
|
|
251
|
+
* Cache
|
|
252
|
+
* 缓存对象
|
|
253
|
+
*/
|
|
254
|
+
cache?: Record<string, unknown>;
|
|
255
|
+
|
|
280
256
|
_symbol: string | undefined;
|
|
281
257
|
/**
|
|
282
258
|
* Currency symbol
|
|
@@ -289,17 +265,18 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
289
265
|
this._symbol = value;
|
|
290
266
|
}
|
|
291
267
|
|
|
268
|
+
/**
|
|
269
|
+
* Key for identifier
|
|
270
|
+
*/
|
|
271
|
+
readonly key: string;
|
|
272
|
+
|
|
292
273
|
/**
|
|
293
274
|
* Cart identifier
|
|
294
275
|
* 购物篮标识
|
|
295
276
|
*/
|
|
296
277
|
get identifier() {
|
|
297
278
|
const o = this.owner;
|
|
298
|
-
return ShoppingCart.createKey(
|
|
299
|
-
this.currency,
|
|
300
|
-
this.culture,
|
|
301
|
-
o ? `${o.isSupplier ? 'S' : 'C'}${o.id}` : undefined
|
|
302
|
-
);
|
|
279
|
+
return ShoppingCart.createKey(this.currency, this.culture, this.key);
|
|
303
280
|
}
|
|
304
281
|
|
|
305
282
|
/**
|
|
@@ -369,45 +346,63 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
369
346
|
/**
|
|
370
347
|
* Constructor
|
|
371
348
|
* 构造函数
|
|
349
|
+
* @param key Key for identifier
|
|
372
350
|
* @param init Currency & culture ISO code array
|
|
373
351
|
* @param storage Data storage
|
|
374
352
|
*/
|
|
375
|
-
constructor(init: [Currency, string], storage?: IStorage);
|
|
353
|
+
constructor(key: string, init: [Currency, string], storage?: IStorage);
|
|
376
354
|
|
|
377
355
|
/**
|
|
378
356
|
* Constructor
|
|
379
357
|
* 构造函数
|
|
358
|
+
* @param key Key for identifier
|
|
380
359
|
* @param state Initialization state
|
|
381
360
|
* @param storage Data storage
|
|
382
361
|
*/
|
|
383
|
-
constructor(state: ShoppingCartData<T>, storage?: IStorage);
|
|
362
|
+
constructor(key: string, state: ShoppingCartData<T>, storage?: IStorage);
|
|
384
363
|
|
|
385
364
|
/**
|
|
386
365
|
* Constructor
|
|
387
366
|
* 构造函数
|
|
367
|
+
* @param key Key for identifier
|
|
388
368
|
* @param currency Currency ISO code
|
|
389
369
|
* @param storage Data storage
|
|
390
370
|
*/
|
|
391
371
|
constructor(
|
|
372
|
+
key: string,
|
|
392
373
|
currencyOrState: [Currency, string] | ShoppingCartData<T>,
|
|
393
374
|
private readonly storage: IStorage = new WindowStorage()
|
|
394
375
|
) {
|
|
376
|
+
this.key = key;
|
|
377
|
+
|
|
395
378
|
if (Array.isArray(currencyOrState)) {
|
|
396
|
-
this.
|
|
397
|
-
this.changeCulture(currencyOrState[1]);
|
|
379
|
+
this.reset(currencyOrState[0], currencyOrState[1]);
|
|
398
380
|
} else {
|
|
399
381
|
this.setCartData(currencyOrState);
|
|
400
382
|
this.changeCurrency(currencyOrState.currency);
|
|
401
383
|
this.changeCulture(currencyOrState.culture);
|
|
402
384
|
}
|
|
403
385
|
}
|
|
386
|
+
private getCartData(): ShoppingCartData<T> | undefined {
|
|
387
|
+
return (
|
|
388
|
+
this.storage.getPersistedObject(this.identifier) ??
|
|
389
|
+
this.storage.getObject(this.identifier)
|
|
390
|
+
);
|
|
391
|
+
}
|
|
404
392
|
|
|
405
393
|
private setCartData(state: ShoppingCartData<T> | undefined) {
|
|
406
|
-
const {
|
|
394
|
+
const {
|
|
395
|
+
owner,
|
|
396
|
+
items = [],
|
|
397
|
+
promotions = [],
|
|
398
|
+
formData,
|
|
399
|
+
cache
|
|
400
|
+
} = state ?? {};
|
|
407
401
|
this.owner = owner;
|
|
408
402
|
this.items = items;
|
|
409
403
|
this.promotions = promotions;
|
|
410
404
|
this.formData = formData;
|
|
405
|
+
this.cache = cache;
|
|
411
406
|
}
|
|
412
407
|
|
|
413
408
|
private doChange(reason: ShoppingCartChangeReason, changedItems: T[]) {
|
|
@@ -524,9 +519,9 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
524
519
|
* @param culture New culture
|
|
525
520
|
*/
|
|
526
521
|
reset(currency: Currency, culture: string) {
|
|
527
|
-
this.clear(true);
|
|
528
522
|
this.changeCurrency(currency);
|
|
529
523
|
this.changeCulture(culture);
|
|
524
|
+
this.setCartData(this.getCartData());
|
|
530
525
|
}
|
|
531
526
|
|
|
532
527
|
/**
|
|
@@ -555,14 +550,16 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
555
550
|
save(persisted: boolean = true) {
|
|
556
551
|
if (this.owner == null) return;
|
|
557
552
|
|
|
558
|
-
const { currency, culture, owner, items, promotions, formData } =
|
|
553
|
+
const { currency, culture, owner, items, promotions, formData, cache } =
|
|
554
|
+
this;
|
|
559
555
|
const data: ShoppingCartData<T> = {
|
|
560
556
|
currency,
|
|
561
557
|
culture,
|
|
562
558
|
owner,
|
|
563
559
|
items,
|
|
564
560
|
promotions,
|
|
565
|
-
formData
|
|
561
|
+
formData,
|
|
562
|
+
cache
|
|
566
563
|
};
|
|
567
564
|
|
|
568
565
|
try {
|