@etsoo/appscript 1.3.94 → 1.3.96
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.
|
@@ -128,26 +128,40 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
128
128
|
* @param storage Storage
|
|
129
129
|
*/
|
|
130
130
|
static clearWith(currency: string, storage?: IStorage): void;
|
|
131
|
+
/**
|
|
132
|
+
* Get cart data
|
|
133
|
+
* 获取购物篮数据
|
|
134
|
+
* @param storage Storage
|
|
135
|
+
* @param id Cart id
|
|
136
|
+
* @returns Result
|
|
137
|
+
*/
|
|
138
|
+
static getCartData<D extends ShoppingCartItem>(storage: IStorage, id: string): ShoppingCartData<D> | undefined;
|
|
139
|
+
_owner?: ShoppingCartOwner;
|
|
131
140
|
/**
|
|
132
141
|
* Owner data
|
|
133
142
|
* 所有者信息
|
|
134
143
|
*/
|
|
135
|
-
owner
|
|
144
|
+
get owner(): ShoppingCartOwner | undefined;
|
|
145
|
+
set owner(value: ShoppingCartOwner | undefined);
|
|
136
146
|
/**
|
|
137
147
|
* ISO currency id
|
|
138
148
|
* 标准货币编号
|
|
139
149
|
*/
|
|
140
150
|
readonly currency: string;
|
|
151
|
+
_items: T[];
|
|
141
152
|
/**
|
|
142
153
|
* Items
|
|
143
154
|
* 项目
|
|
144
155
|
*/
|
|
145
|
-
|
|
156
|
+
get items(): T[];
|
|
157
|
+
private set items(value);
|
|
158
|
+
_promotions: ShoppingPromotion[];
|
|
146
159
|
/**
|
|
147
160
|
* Order level promotions
|
|
148
161
|
* 订单层面促销
|
|
149
162
|
*/
|
|
150
|
-
|
|
163
|
+
get promotions(): ShoppingPromotion[];
|
|
164
|
+
private set promotions(value);
|
|
151
165
|
/**
|
|
152
166
|
* Related form data
|
|
153
167
|
* 关联的表单数据
|
|
@@ -162,7 +176,13 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
162
176
|
* Cart identifier
|
|
163
177
|
* 购物篮标识
|
|
164
178
|
*/
|
|
165
|
-
|
|
179
|
+
get identifier(): string;
|
|
180
|
+
/**
|
|
181
|
+
* All data keys
|
|
182
|
+
* 所有的数据键
|
|
183
|
+
*/
|
|
184
|
+
get keys(): string[];
|
|
185
|
+
set keys(items: string[]);
|
|
166
186
|
/**
|
|
167
187
|
* Lines count
|
|
168
188
|
* 项目数量
|
|
@@ -198,17 +218,16 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
198
218
|
* 构造函数
|
|
199
219
|
* @param currency Currency ISO code
|
|
200
220
|
* @param storage Data storage
|
|
201
|
-
* @param key Additional key
|
|
202
221
|
*/
|
|
203
|
-
constructor(currency: string, storage?: IStorage
|
|
222
|
+
constructor(currency: string, storage?: IStorage);
|
|
204
223
|
/**
|
|
205
224
|
* Constructor
|
|
206
225
|
* 构造函数
|
|
207
226
|
* @param state Initialization state
|
|
208
227
|
* @param storage Data storage
|
|
209
|
-
* @param key Additional key
|
|
210
228
|
*/
|
|
211
|
-
constructor(state: ShoppingCartData<T>, storage?: IStorage
|
|
229
|
+
constructor(state: ShoppingCartData<T>, storage?: IStorage);
|
|
230
|
+
private setCartData;
|
|
212
231
|
private doChange;
|
|
213
232
|
/**
|
|
214
233
|
* Add item
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ShoppingCart = void 0;
|
|
4
4
|
const shared_1 = require("@etsoo/shared");
|
|
5
|
+
const ShoppingCartKeyField = 'ETSOO-CART-KEYS';
|
|
5
6
|
/**
|
|
6
7
|
* Shopping cart
|
|
7
8
|
* 购物篮
|
|
@@ -42,6 +43,78 @@ class ShoppingCart {
|
|
|
42
43
|
const identifier = this.createKey(currency);
|
|
43
44
|
this.clear(identifier, storage);
|
|
44
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Get cart data
|
|
48
|
+
* 获取购物篮数据
|
|
49
|
+
* @param storage Storage
|
|
50
|
+
* @param id Cart id
|
|
51
|
+
* @returns Result
|
|
52
|
+
*/
|
|
53
|
+
static getCartData(storage, id) {
|
|
54
|
+
var _a;
|
|
55
|
+
try {
|
|
56
|
+
return ((_a = storage.getPersistedObject(id)) !== null && _a !== void 0 ? _a : storage.getObject(id));
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.log('ShoppingCart constructor', error);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Owner data
|
|
64
|
+
* 所有者信息
|
|
65
|
+
*/
|
|
66
|
+
get owner() {
|
|
67
|
+
return this._owner;
|
|
68
|
+
}
|
|
69
|
+
set owner(value) {
|
|
70
|
+
var _a;
|
|
71
|
+
if (((_a = this._owner) === null || _a === void 0 ? void 0 : _a.id) === (value === null || value === void 0 ? void 0 : value.id))
|
|
72
|
+
return;
|
|
73
|
+
this._owner = value;
|
|
74
|
+
if (value) {
|
|
75
|
+
const data = ShoppingCart.getCartData(this.storage, this.identifier);
|
|
76
|
+
if (data)
|
|
77
|
+
this.setCartData(data);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Items
|
|
82
|
+
* 项目
|
|
83
|
+
*/
|
|
84
|
+
get items() {
|
|
85
|
+
return this._items;
|
|
86
|
+
}
|
|
87
|
+
set items(value) {
|
|
88
|
+
this._items = value;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Order level promotions
|
|
92
|
+
* 订单层面促销
|
|
93
|
+
*/
|
|
94
|
+
get promotions() {
|
|
95
|
+
return this._promotions;
|
|
96
|
+
}
|
|
97
|
+
set promotions(value) {
|
|
98
|
+
this._promotions = value;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Cart identifier
|
|
102
|
+
* 购物篮标识
|
|
103
|
+
*/
|
|
104
|
+
get identifier() {
|
|
105
|
+
const o = this.owner;
|
|
106
|
+
return ShoppingCart.createKey(this.currency, o ? `${o.isSupplier ? 'S' : 'C'}${o.id}` : undefined);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* All data keys
|
|
110
|
+
* 所有的数据键
|
|
111
|
+
*/
|
|
112
|
+
get keys() {
|
|
113
|
+
return this.storage.getPersistedData(ShoppingCartKeyField, []);
|
|
114
|
+
}
|
|
115
|
+
set keys(items) {
|
|
116
|
+
this.storage.setPersistedData(ShoppingCartKeyField, items);
|
|
117
|
+
}
|
|
45
118
|
/**
|
|
46
119
|
* Lines count
|
|
47
120
|
* 项目数量
|
|
@@ -79,34 +152,26 @@ class ShoppingCart {
|
|
|
79
152
|
* 构造函数
|
|
80
153
|
* @param currency Currency ISO code
|
|
81
154
|
* @param storage Data storage
|
|
82
|
-
* @param key Additional key
|
|
83
155
|
*/
|
|
84
|
-
constructor(currencyOrState, storage = new shared_1.WindowStorage()
|
|
85
|
-
var _a;
|
|
156
|
+
constructor(currencyOrState, storage = new shared_1.WindowStorage()) {
|
|
86
157
|
this.storage = storage;
|
|
158
|
+
this._items = [];
|
|
159
|
+
this._promotions = [];
|
|
87
160
|
/**
|
|
88
161
|
* Cached prices
|
|
89
162
|
* 缓存的价格
|
|
90
163
|
*/
|
|
91
164
|
this.prices = {};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const id = ShoppingCart.createKey(this.currency, key);
|
|
95
|
-
this.identifier = id;
|
|
96
|
-
this.symbol = shared_1.NumberUtils.getCurrencySymbol(this.currency);
|
|
97
|
-
let state;
|
|
98
|
-
if (isCurrency) {
|
|
99
|
-
try {
|
|
100
|
-
state =
|
|
101
|
-
(_a = storage.getPersistedObject(id)) !== null && _a !== void 0 ? _a : storage.getObject(id);
|
|
102
|
-
}
|
|
103
|
-
catch (error) {
|
|
104
|
-
console.log('ShoppingCart constructor', error);
|
|
105
|
-
}
|
|
165
|
+
if (typeof currencyOrState === 'string') {
|
|
166
|
+
this.currency = currencyOrState;
|
|
106
167
|
}
|
|
107
168
|
else {
|
|
108
|
-
|
|
169
|
+
this.setCartData(currencyOrState);
|
|
170
|
+
this.currency = currencyOrState.currency;
|
|
109
171
|
}
|
|
172
|
+
this.symbol = shared_1.NumberUtils.getCurrencySymbol(this.currency);
|
|
173
|
+
}
|
|
174
|
+
setCartData(state) {
|
|
110
175
|
const { owner, items = [], promotions = [], formData } = state !== null && state !== void 0 ? state : {};
|
|
111
176
|
this.owner = owner;
|
|
112
177
|
this.items = items;
|
|
@@ -153,6 +218,12 @@ class ShoppingCart {
|
|
|
153
218
|
}
|
|
154
219
|
else {
|
|
155
220
|
ShoppingCart.clear(this.identifier, this.storage);
|
|
221
|
+
const keys = this.keys;
|
|
222
|
+
const index = keys.indexOf(this.identifier);
|
|
223
|
+
if (index !== -1) {
|
|
224
|
+
keys.splice(index, 1);
|
|
225
|
+
this.keys = keys;
|
|
226
|
+
}
|
|
156
227
|
}
|
|
157
228
|
this.doChange('clear', []);
|
|
158
229
|
}
|
|
@@ -199,6 +270,11 @@ class ShoppingCart {
|
|
|
199
270
|
try {
|
|
200
271
|
if (persisted) {
|
|
201
272
|
this.storage.setPersistedData(this.identifier, data);
|
|
273
|
+
const keys = this.keys;
|
|
274
|
+
if (!keys.includes(this.identifier)) {
|
|
275
|
+
keys.push(this.identifier);
|
|
276
|
+
this.keys = keys;
|
|
277
|
+
}
|
|
202
278
|
}
|
|
203
279
|
else {
|
|
204
280
|
this.storage.setData(this.identifier, data);
|
|
@@ -128,26 +128,40 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
128
128
|
* @param storage Storage
|
|
129
129
|
*/
|
|
130
130
|
static clearWith(currency: string, storage?: IStorage): void;
|
|
131
|
+
/**
|
|
132
|
+
* Get cart data
|
|
133
|
+
* 获取购物篮数据
|
|
134
|
+
* @param storage Storage
|
|
135
|
+
* @param id Cart id
|
|
136
|
+
* @returns Result
|
|
137
|
+
*/
|
|
138
|
+
static getCartData<D extends ShoppingCartItem>(storage: IStorage, id: string): ShoppingCartData<D> | undefined;
|
|
139
|
+
_owner?: ShoppingCartOwner;
|
|
131
140
|
/**
|
|
132
141
|
* Owner data
|
|
133
142
|
* 所有者信息
|
|
134
143
|
*/
|
|
135
|
-
owner
|
|
144
|
+
get owner(): ShoppingCartOwner | undefined;
|
|
145
|
+
set owner(value: ShoppingCartOwner | undefined);
|
|
136
146
|
/**
|
|
137
147
|
* ISO currency id
|
|
138
148
|
* 标准货币编号
|
|
139
149
|
*/
|
|
140
150
|
readonly currency: string;
|
|
151
|
+
_items: T[];
|
|
141
152
|
/**
|
|
142
153
|
* Items
|
|
143
154
|
* 项目
|
|
144
155
|
*/
|
|
145
|
-
|
|
156
|
+
get items(): T[];
|
|
157
|
+
private set items(value);
|
|
158
|
+
_promotions: ShoppingPromotion[];
|
|
146
159
|
/**
|
|
147
160
|
* Order level promotions
|
|
148
161
|
* 订单层面促销
|
|
149
162
|
*/
|
|
150
|
-
|
|
163
|
+
get promotions(): ShoppingPromotion[];
|
|
164
|
+
private set promotions(value);
|
|
151
165
|
/**
|
|
152
166
|
* Related form data
|
|
153
167
|
* 关联的表单数据
|
|
@@ -162,7 +176,13 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
162
176
|
* Cart identifier
|
|
163
177
|
* 购物篮标识
|
|
164
178
|
*/
|
|
165
|
-
|
|
179
|
+
get identifier(): string;
|
|
180
|
+
/**
|
|
181
|
+
* All data keys
|
|
182
|
+
* 所有的数据键
|
|
183
|
+
*/
|
|
184
|
+
get keys(): string[];
|
|
185
|
+
set keys(items: string[]);
|
|
166
186
|
/**
|
|
167
187
|
* Lines count
|
|
168
188
|
* 项目数量
|
|
@@ -198,17 +218,16 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
198
218
|
* 构造函数
|
|
199
219
|
* @param currency Currency ISO code
|
|
200
220
|
* @param storage Data storage
|
|
201
|
-
* @param key Additional key
|
|
202
221
|
*/
|
|
203
|
-
constructor(currency: string, storage?: IStorage
|
|
222
|
+
constructor(currency: string, storage?: IStorage);
|
|
204
223
|
/**
|
|
205
224
|
* Constructor
|
|
206
225
|
* 构造函数
|
|
207
226
|
* @param state Initialization state
|
|
208
227
|
* @param storage Data storage
|
|
209
|
-
* @param key Additional key
|
|
210
228
|
*/
|
|
211
|
-
constructor(state: ShoppingCartData<T>, storage?: IStorage
|
|
229
|
+
constructor(state: ShoppingCartData<T>, storage?: IStorage);
|
|
230
|
+
private setCartData;
|
|
212
231
|
private doChange;
|
|
213
232
|
/**
|
|
214
233
|
* Add item
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NumberUtils, WindowStorage } from '@etsoo/shared';
|
|
2
|
+
const ShoppingCartKeyField = 'ETSOO-CART-KEYS';
|
|
2
3
|
/**
|
|
3
4
|
* Shopping cart
|
|
4
5
|
* 购物篮
|
|
@@ -39,6 +40,78 @@ export class ShoppingCart {
|
|
|
39
40
|
const identifier = this.createKey(currency);
|
|
40
41
|
this.clear(identifier, storage);
|
|
41
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Get cart data
|
|
45
|
+
* 获取购物篮数据
|
|
46
|
+
* @param storage Storage
|
|
47
|
+
* @param id Cart id
|
|
48
|
+
* @returns Result
|
|
49
|
+
*/
|
|
50
|
+
static getCartData(storage, id) {
|
|
51
|
+
var _a;
|
|
52
|
+
try {
|
|
53
|
+
return ((_a = storage.getPersistedObject(id)) !== null && _a !== void 0 ? _a : storage.getObject(id));
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.log('ShoppingCart constructor', error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Owner data
|
|
61
|
+
* 所有者信息
|
|
62
|
+
*/
|
|
63
|
+
get owner() {
|
|
64
|
+
return this._owner;
|
|
65
|
+
}
|
|
66
|
+
set owner(value) {
|
|
67
|
+
var _a;
|
|
68
|
+
if (((_a = this._owner) === null || _a === void 0 ? void 0 : _a.id) === (value === null || value === void 0 ? void 0 : value.id))
|
|
69
|
+
return;
|
|
70
|
+
this._owner = value;
|
|
71
|
+
if (value) {
|
|
72
|
+
const data = ShoppingCart.getCartData(this.storage, this.identifier);
|
|
73
|
+
if (data)
|
|
74
|
+
this.setCartData(data);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Items
|
|
79
|
+
* 项目
|
|
80
|
+
*/
|
|
81
|
+
get items() {
|
|
82
|
+
return this._items;
|
|
83
|
+
}
|
|
84
|
+
set items(value) {
|
|
85
|
+
this._items = value;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Order level promotions
|
|
89
|
+
* 订单层面促销
|
|
90
|
+
*/
|
|
91
|
+
get promotions() {
|
|
92
|
+
return this._promotions;
|
|
93
|
+
}
|
|
94
|
+
set promotions(value) {
|
|
95
|
+
this._promotions = value;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Cart identifier
|
|
99
|
+
* 购物篮标识
|
|
100
|
+
*/
|
|
101
|
+
get identifier() {
|
|
102
|
+
const o = this.owner;
|
|
103
|
+
return ShoppingCart.createKey(this.currency, o ? `${o.isSupplier ? 'S' : 'C'}${o.id}` : undefined);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* All data keys
|
|
107
|
+
* 所有的数据键
|
|
108
|
+
*/
|
|
109
|
+
get keys() {
|
|
110
|
+
return this.storage.getPersistedData(ShoppingCartKeyField, []);
|
|
111
|
+
}
|
|
112
|
+
set keys(items) {
|
|
113
|
+
this.storage.setPersistedData(ShoppingCartKeyField, items);
|
|
114
|
+
}
|
|
42
115
|
/**
|
|
43
116
|
* Lines count
|
|
44
117
|
* 项目数量
|
|
@@ -76,34 +149,26 @@ export class ShoppingCart {
|
|
|
76
149
|
* 构造函数
|
|
77
150
|
* @param currency Currency ISO code
|
|
78
151
|
* @param storage Data storage
|
|
79
|
-
* @param key Additional key
|
|
80
152
|
*/
|
|
81
|
-
constructor(currencyOrState, storage = new WindowStorage()
|
|
82
|
-
var _a;
|
|
153
|
+
constructor(currencyOrState, storage = new WindowStorage()) {
|
|
83
154
|
this.storage = storage;
|
|
155
|
+
this._items = [];
|
|
156
|
+
this._promotions = [];
|
|
84
157
|
/**
|
|
85
158
|
* Cached prices
|
|
86
159
|
* 缓存的价格
|
|
87
160
|
*/
|
|
88
161
|
this.prices = {};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const id = ShoppingCart.createKey(this.currency, key);
|
|
92
|
-
this.identifier = id;
|
|
93
|
-
this.symbol = NumberUtils.getCurrencySymbol(this.currency);
|
|
94
|
-
let state;
|
|
95
|
-
if (isCurrency) {
|
|
96
|
-
try {
|
|
97
|
-
state =
|
|
98
|
-
(_a = storage.getPersistedObject(id)) !== null && _a !== void 0 ? _a : storage.getObject(id);
|
|
99
|
-
}
|
|
100
|
-
catch (error) {
|
|
101
|
-
console.log('ShoppingCart constructor', error);
|
|
102
|
-
}
|
|
162
|
+
if (typeof currencyOrState === 'string') {
|
|
163
|
+
this.currency = currencyOrState;
|
|
103
164
|
}
|
|
104
165
|
else {
|
|
105
|
-
|
|
166
|
+
this.setCartData(currencyOrState);
|
|
167
|
+
this.currency = currencyOrState.currency;
|
|
106
168
|
}
|
|
169
|
+
this.symbol = NumberUtils.getCurrencySymbol(this.currency);
|
|
170
|
+
}
|
|
171
|
+
setCartData(state) {
|
|
107
172
|
const { owner, items = [], promotions = [], formData } = state !== null && state !== void 0 ? state : {};
|
|
108
173
|
this.owner = owner;
|
|
109
174
|
this.items = items;
|
|
@@ -150,6 +215,12 @@ export class ShoppingCart {
|
|
|
150
215
|
}
|
|
151
216
|
else {
|
|
152
217
|
ShoppingCart.clear(this.identifier, this.storage);
|
|
218
|
+
const keys = this.keys;
|
|
219
|
+
const index = keys.indexOf(this.identifier);
|
|
220
|
+
if (index !== -1) {
|
|
221
|
+
keys.splice(index, 1);
|
|
222
|
+
this.keys = keys;
|
|
223
|
+
}
|
|
153
224
|
}
|
|
154
225
|
this.doChange('clear', []);
|
|
155
226
|
}
|
|
@@ -196,6 +267,11 @@ export class ShoppingCart {
|
|
|
196
267
|
try {
|
|
197
268
|
if (persisted) {
|
|
198
269
|
this.storage.setPersistedData(this.identifier, data);
|
|
270
|
+
const keys = this.keys;
|
|
271
|
+
if (!keys.includes(this.identifier)) {
|
|
272
|
+
keys.push(this.identifier);
|
|
273
|
+
this.keys = keys;
|
|
274
|
+
}
|
|
199
275
|
}
|
|
200
276
|
else {
|
|
201
277
|
this.storage.setData(this.identifier, data);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/appscript",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.96",
|
|
4
4
|
"description": "Applications shared TypeScript framework",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"@babel/plugin-transform-runtime": "^7.21.4",
|
|
65
65
|
"@babel/preset-env": "^7.21.4",
|
|
66
66
|
"@babel/runtime-corejs3": "^7.21.0",
|
|
67
|
-
"@types/jest": "^29.5.
|
|
67
|
+
"@types/jest": "^29.5.1",
|
|
68
68
|
"jest": "^29.5.0",
|
|
69
69
|
"jest-environment-jsdom": "^29.5.0",
|
|
70
70
|
"ts-jest": "^29.1.0",
|
|
@@ -121,6 +121,8 @@ export type ShoppingCartChangeReason =
|
|
|
121
121
|
| 'title'
|
|
122
122
|
| 'update';
|
|
123
123
|
|
|
124
|
+
const ShoppingCartKeyField = 'ETSOO-CART-KEYS';
|
|
125
|
+
|
|
124
126
|
/**
|
|
125
127
|
* Shopping cart
|
|
126
128
|
* 购物篮
|
|
@@ -166,11 +168,46 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
166
168
|
this.clear(identifier, storage);
|
|
167
169
|
}
|
|
168
170
|
|
|
171
|
+
/**
|
|
172
|
+
* Get cart data
|
|
173
|
+
* 获取购物篮数据
|
|
174
|
+
* @param storage Storage
|
|
175
|
+
* @param id Cart id
|
|
176
|
+
* @returns Result
|
|
177
|
+
*/
|
|
178
|
+
static getCartData<D extends ShoppingCartItem>(
|
|
179
|
+
storage: IStorage,
|
|
180
|
+
id: string
|
|
181
|
+
) {
|
|
182
|
+
try {
|
|
183
|
+
return (
|
|
184
|
+
storage.getPersistedObject<ShoppingCartData<D>>(id) ??
|
|
185
|
+
storage.getObject<ShoppingCartData<D>>(id)
|
|
186
|
+
);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
console.log('ShoppingCart constructor', error);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
_owner?: ShoppingCartOwner;
|
|
169
193
|
/**
|
|
170
194
|
* Owner data
|
|
171
195
|
* 所有者信息
|
|
172
196
|
*/
|
|
173
|
-
owner
|
|
197
|
+
get owner() {
|
|
198
|
+
return this._owner;
|
|
199
|
+
}
|
|
200
|
+
set owner(value) {
|
|
201
|
+
if (this._owner?.id === value?.id) return;
|
|
202
|
+
this._owner = value;
|
|
203
|
+
if (value) {
|
|
204
|
+
const data = ShoppingCart.getCartData<T>(
|
|
205
|
+
this.storage,
|
|
206
|
+
this.identifier
|
|
207
|
+
);
|
|
208
|
+
if (data) this.setCartData(data);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
174
211
|
|
|
175
212
|
/**
|
|
176
213
|
* ISO currency id
|
|
@@ -178,17 +215,30 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
178
215
|
*/
|
|
179
216
|
readonly currency: string;
|
|
180
217
|
|
|
218
|
+
_items: T[] = [];
|
|
219
|
+
|
|
181
220
|
/**
|
|
182
221
|
* Items
|
|
183
222
|
* 项目
|
|
184
223
|
*/
|
|
185
|
-
|
|
224
|
+
get items() {
|
|
225
|
+
return this._items;
|
|
226
|
+
}
|
|
227
|
+
private set items(value) {
|
|
228
|
+
this._items = value;
|
|
229
|
+
}
|
|
186
230
|
|
|
231
|
+
_promotions: ShoppingPromotion[] = [];
|
|
187
232
|
/**
|
|
188
233
|
* Order level promotions
|
|
189
234
|
* 订单层面促销
|
|
190
235
|
*/
|
|
191
|
-
|
|
236
|
+
get promotions() {
|
|
237
|
+
return this._promotions;
|
|
238
|
+
}
|
|
239
|
+
private set promotions(value) {
|
|
240
|
+
this._promotions = value;
|
|
241
|
+
}
|
|
192
242
|
|
|
193
243
|
/**
|
|
194
244
|
* Related form data
|
|
@@ -206,7 +256,27 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
206
256
|
* Cart identifier
|
|
207
257
|
* 购物篮标识
|
|
208
258
|
*/
|
|
209
|
-
|
|
259
|
+
get identifier() {
|
|
260
|
+
const o = this.owner;
|
|
261
|
+
return ShoppingCart.createKey(
|
|
262
|
+
this.currency,
|
|
263
|
+
o ? `${o.isSupplier ? 'S' : 'C'}${o.id}` : undefined
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* All data keys
|
|
269
|
+
* 所有的数据键
|
|
270
|
+
*/
|
|
271
|
+
get keys() {
|
|
272
|
+
return this.storage.getPersistedData<string[]>(
|
|
273
|
+
ShoppingCartKeyField,
|
|
274
|
+
[]
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
set keys(items: string[]) {
|
|
278
|
+
this.storage.setPersistedData(ShoppingCartKeyField, items);
|
|
279
|
+
}
|
|
210
280
|
|
|
211
281
|
/**
|
|
212
282
|
* Lines count
|
|
@@ -263,53 +333,38 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
263
333
|
* 构造函数
|
|
264
334
|
* @param currency Currency ISO code
|
|
265
335
|
* @param storage Data storage
|
|
266
|
-
* @param key Additional key
|
|
267
336
|
*/
|
|
268
|
-
constructor(currency: string, storage?: IStorage
|
|
337
|
+
constructor(currency: string, storage?: IStorage);
|
|
269
338
|
|
|
270
339
|
/**
|
|
271
340
|
* Constructor
|
|
272
341
|
* 构造函数
|
|
273
342
|
* @param state Initialization state
|
|
274
343
|
* @param storage Data storage
|
|
275
|
-
* @param key Additional key
|
|
276
344
|
*/
|
|
277
|
-
constructor(state: ShoppingCartData<T>, storage?: IStorage
|
|
345
|
+
constructor(state: ShoppingCartData<T>, storage?: IStorage);
|
|
278
346
|
|
|
279
347
|
/**
|
|
280
348
|
* Constructor
|
|
281
349
|
* 构造函数
|
|
282
350
|
* @param currency Currency ISO code
|
|
283
351
|
* @param storage Data storage
|
|
284
|
-
* @param key Additional key
|
|
285
352
|
*/
|
|
286
353
|
constructor(
|
|
287
354
|
currencyOrState: string | ShoppingCartData<T>,
|
|
288
|
-
private readonly storage: IStorage = new WindowStorage()
|
|
289
|
-
key?: string
|
|
355
|
+
private readonly storage: IStorage = new WindowStorage()
|
|
290
356
|
) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const id = ShoppingCart.createKey(this.currency, key);
|
|
295
|
-
this.identifier = id;
|
|
296
|
-
this.symbol = NumberUtils.getCurrencySymbol(this.currency);
|
|
297
|
-
|
|
298
|
-
let state: ShoppingCartData<T> | undefined;
|
|
299
|
-
if (isCurrency) {
|
|
300
|
-
try {
|
|
301
|
-
state =
|
|
302
|
-
storage.getPersistedObject<ShoppingCartData<T>>(id) ??
|
|
303
|
-
storage.getObject<ShoppingCartData<T>>(id);
|
|
304
|
-
} catch (error) {
|
|
305
|
-
console.log('ShoppingCart constructor', error);
|
|
306
|
-
}
|
|
357
|
+
if (typeof currencyOrState === 'string') {
|
|
358
|
+
this.currency = currencyOrState;
|
|
307
359
|
} else {
|
|
308
|
-
|
|
360
|
+
this.setCartData(currencyOrState);
|
|
361
|
+
this.currency = currencyOrState.currency;
|
|
309
362
|
}
|
|
363
|
+
this.symbol = NumberUtils.getCurrencySymbol(this.currency);
|
|
364
|
+
}
|
|
310
365
|
|
|
366
|
+
private setCartData(state: ShoppingCartData<T> | undefined) {
|
|
311
367
|
const { owner, items = [], promotions = [], formData } = state ?? {};
|
|
312
|
-
|
|
313
368
|
this.owner = owner;
|
|
314
369
|
this.items = items;
|
|
315
370
|
this.promotions = promotions;
|
|
@@ -359,6 +414,13 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
359
414
|
this.save();
|
|
360
415
|
} else {
|
|
361
416
|
ShoppingCart.clear(this.identifier, this.storage);
|
|
417
|
+
|
|
418
|
+
const keys = this.keys;
|
|
419
|
+
const index = keys.indexOf(this.identifier);
|
|
420
|
+
if (index !== -1) {
|
|
421
|
+
keys.splice(index, 1);
|
|
422
|
+
this.keys = keys;
|
|
423
|
+
}
|
|
362
424
|
}
|
|
363
425
|
|
|
364
426
|
this.doChange('clear', []);
|
|
@@ -411,6 +473,12 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
411
473
|
try {
|
|
412
474
|
if (persisted) {
|
|
413
475
|
this.storage.setPersistedData(this.identifier, data);
|
|
476
|
+
|
|
477
|
+
const keys = this.keys;
|
|
478
|
+
if (!keys.includes(this.identifier)) {
|
|
479
|
+
keys.push(this.identifier);
|
|
480
|
+
this.keys = keys;
|
|
481
|
+
}
|
|
414
482
|
} else {
|
|
415
483
|
this.storage.setData(this.identifier, data);
|
|
416
484
|
}
|