@etsoo/appscript 1.3.94 → 1.3.95
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,7 +218,6 @@ 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
222
|
constructor(currency: string, storage?: IStorage, key?: string);
|
|
204
223
|
/**
|
|
@@ -206,9 +225,9 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
206
225
|
* 构造函数
|
|
207
226
|
* @param state Initialization state
|
|
208
227
|
* @param storage Data storage
|
|
209
|
-
* @param key Additional key
|
|
210
228
|
*/
|
|
211
229
|
constructor(state: ShoppingCartData<T>, storage?: IStorage, key?: string);
|
|
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,77 @@ 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
|
+
this.setCartData(data);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Items
|
|
81
|
+
* 项目
|
|
82
|
+
*/
|
|
83
|
+
get items() {
|
|
84
|
+
return this._items;
|
|
85
|
+
}
|
|
86
|
+
set items(value) {
|
|
87
|
+
this._items = value;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Order level promotions
|
|
91
|
+
* 订单层面促销
|
|
92
|
+
*/
|
|
93
|
+
get promotions() {
|
|
94
|
+
return this._promotions;
|
|
95
|
+
}
|
|
96
|
+
set promotions(value) {
|
|
97
|
+
this._promotions = value;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Cart identifier
|
|
101
|
+
* 购物篮标识
|
|
102
|
+
*/
|
|
103
|
+
get identifier() {
|
|
104
|
+
const o = this.owner;
|
|
105
|
+
return ShoppingCart.createKey(this.currency, o ? `${o.isSupplier ? 'S' : 'C'}${o.id}` : undefined);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* All data keys
|
|
109
|
+
* 所有的数据键
|
|
110
|
+
*/
|
|
111
|
+
get keys() {
|
|
112
|
+
return this.storage.getPersistedData(ShoppingCartKeyField, []);
|
|
113
|
+
}
|
|
114
|
+
set keys(items) {
|
|
115
|
+
this.storage.setPersistedData(ShoppingCartKeyField, items);
|
|
116
|
+
}
|
|
45
117
|
/**
|
|
46
118
|
* Lines count
|
|
47
119
|
* 项目数量
|
|
@@ -79,34 +151,26 @@ class ShoppingCart {
|
|
|
79
151
|
* 构造函数
|
|
80
152
|
* @param currency Currency ISO code
|
|
81
153
|
* @param storage Data storage
|
|
82
|
-
* @param key Additional key
|
|
83
154
|
*/
|
|
84
|
-
constructor(currencyOrState, storage = new shared_1.WindowStorage()
|
|
85
|
-
var _a;
|
|
155
|
+
constructor(currencyOrState, storage = new shared_1.WindowStorage()) {
|
|
86
156
|
this.storage = storage;
|
|
157
|
+
this._items = [];
|
|
158
|
+
this._promotions = [];
|
|
87
159
|
/**
|
|
88
160
|
* Cached prices
|
|
89
161
|
* 缓存的价格
|
|
90
162
|
*/
|
|
91
163
|
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
|
-
}
|
|
164
|
+
if (typeof currencyOrState === 'string') {
|
|
165
|
+
this.currency = currencyOrState;
|
|
106
166
|
}
|
|
107
167
|
else {
|
|
108
|
-
|
|
168
|
+
this.setCartData(currencyOrState);
|
|
169
|
+
this.currency = currencyOrState.currency;
|
|
109
170
|
}
|
|
171
|
+
this.symbol = shared_1.NumberUtils.getCurrencySymbol(this.currency);
|
|
172
|
+
}
|
|
173
|
+
setCartData(state) {
|
|
110
174
|
const { owner, items = [], promotions = [], formData } = state !== null && state !== void 0 ? state : {};
|
|
111
175
|
this.owner = owner;
|
|
112
176
|
this.items = items;
|
|
@@ -153,6 +217,12 @@ class ShoppingCart {
|
|
|
153
217
|
}
|
|
154
218
|
else {
|
|
155
219
|
ShoppingCart.clear(this.identifier, this.storage);
|
|
220
|
+
const keys = this.keys;
|
|
221
|
+
const index = keys.indexOf(this.identifier);
|
|
222
|
+
if (index !== -1) {
|
|
223
|
+
keys.splice(index, 1);
|
|
224
|
+
this.keys = keys;
|
|
225
|
+
}
|
|
156
226
|
}
|
|
157
227
|
this.doChange('clear', []);
|
|
158
228
|
}
|
|
@@ -199,6 +269,11 @@ class ShoppingCart {
|
|
|
199
269
|
try {
|
|
200
270
|
if (persisted) {
|
|
201
271
|
this.storage.setPersistedData(this.identifier, data);
|
|
272
|
+
const keys = this.keys;
|
|
273
|
+
if (!keys.includes(this.identifier)) {
|
|
274
|
+
keys.push(this.identifier);
|
|
275
|
+
this.keys = keys;
|
|
276
|
+
}
|
|
202
277
|
}
|
|
203
278
|
else {
|
|
204
279
|
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,7 +218,6 @@ 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
222
|
constructor(currency: string, storage?: IStorage, key?: string);
|
|
204
223
|
/**
|
|
@@ -206,9 +225,9 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
206
225
|
* 构造函数
|
|
207
226
|
* @param state Initialization state
|
|
208
227
|
* @param storage Data storage
|
|
209
|
-
* @param key Additional key
|
|
210
228
|
*/
|
|
211
229
|
constructor(state: ShoppingCartData<T>, storage?: IStorage, key?: string);
|
|
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,77 @@ 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
|
+
this.setCartData(data);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Items
|
|
78
|
+
* 项目
|
|
79
|
+
*/
|
|
80
|
+
get items() {
|
|
81
|
+
return this._items;
|
|
82
|
+
}
|
|
83
|
+
set items(value) {
|
|
84
|
+
this._items = value;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Order level promotions
|
|
88
|
+
* 订单层面促销
|
|
89
|
+
*/
|
|
90
|
+
get promotions() {
|
|
91
|
+
return this._promotions;
|
|
92
|
+
}
|
|
93
|
+
set promotions(value) {
|
|
94
|
+
this._promotions = value;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Cart identifier
|
|
98
|
+
* 购物篮标识
|
|
99
|
+
*/
|
|
100
|
+
get identifier() {
|
|
101
|
+
const o = this.owner;
|
|
102
|
+
return ShoppingCart.createKey(this.currency, o ? `${o.isSupplier ? 'S' : 'C'}${o.id}` : undefined);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* All data keys
|
|
106
|
+
* 所有的数据键
|
|
107
|
+
*/
|
|
108
|
+
get keys() {
|
|
109
|
+
return this.storage.getPersistedData(ShoppingCartKeyField, []);
|
|
110
|
+
}
|
|
111
|
+
set keys(items) {
|
|
112
|
+
this.storage.setPersistedData(ShoppingCartKeyField, items);
|
|
113
|
+
}
|
|
42
114
|
/**
|
|
43
115
|
* Lines count
|
|
44
116
|
* 项目数量
|
|
@@ -76,34 +148,26 @@ export class ShoppingCart {
|
|
|
76
148
|
* 构造函数
|
|
77
149
|
* @param currency Currency ISO code
|
|
78
150
|
* @param storage Data storage
|
|
79
|
-
* @param key Additional key
|
|
80
151
|
*/
|
|
81
|
-
constructor(currencyOrState, storage = new WindowStorage()
|
|
82
|
-
var _a;
|
|
152
|
+
constructor(currencyOrState, storage = new WindowStorage()) {
|
|
83
153
|
this.storage = storage;
|
|
154
|
+
this._items = [];
|
|
155
|
+
this._promotions = [];
|
|
84
156
|
/**
|
|
85
157
|
* Cached prices
|
|
86
158
|
* 缓存的价格
|
|
87
159
|
*/
|
|
88
160
|
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
|
-
}
|
|
161
|
+
if (typeof currencyOrState === 'string') {
|
|
162
|
+
this.currency = currencyOrState;
|
|
103
163
|
}
|
|
104
164
|
else {
|
|
105
|
-
|
|
165
|
+
this.setCartData(currencyOrState);
|
|
166
|
+
this.currency = currencyOrState.currency;
|
|
106
167
|
}
|
|
168
|
+
this.symbol = NumberUtils.getCurrencySymbol(this.currency);
|
|
169
|
+
}
|
|
170
|
+
setCartData(state) {
|
|
107
171
|
const { owner, items = [], promotions = [], formData } = state !== null && state !== void 0 ? state : {};
|
|
108
172
|
this.owner = owner;
|
|
109
173
|
this.items = items;
|
|
@@ -150,6 +214,12 @@ export class ShoppingCart {
|
|
|
150
214
|
}
|
|
151
215
|
else {
|
|
152
216
|
ShoppingCart.clear(this.identifier, this.storage);
|
|
217
|
+
const keys = this.keys;
|
|
218
|
+
const index = keys.indexOf(this.identifier);
|
|
219
|
+
if (index !== -1) {
|
|
220
|
+
keys.splice(index, 1);
|
|
221
|
+
this.keys = keys;
|
|
222
|
+
}
|
|
153
223
|
}
|
|
154
224
|
this.doChange('clear', []);
|
|
155
225
|
}
|
|
@@ -196,6 +266,11 @@ export class ShoppingCart {
|
|
|
196
266
|
try {
|
|
197
267
|
if (persisted) {
|
|
198
268
|
this.storage.setPersistedData(this.identifier, data);
|
|
269
|
+
const keys = this.keys;
|
|
270
|
+
if (!keys.includes(this.identifier)) {
|
|
271
|
+
keys.push(this.identifier);
|
|
272
|
+
this.keys = keys;
|
|
273
|
+
}
|
|
199
274
|
}
|
|
200
275
|
else {
|
|
201
276
|
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.95",
|
|
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
|
+
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,7 +333,6 @@ 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
337
|
constructor(currency: string, storage?: IStorage, key?: string);
|
|
269
338
|
|
|
@@ -272,7 +341,6 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
272
341
|
* 构造函数
|
|
273
342
|
* @param state Initialization state
|
|
274
343
|
* @param storage Data storage
|
|
275
|
-
* @param key Additional key
|
|
276
344
|
*/
|
|
277
345
|
constructor(state: ShoppingCartData<T>, storage?: IStorage, key?: string);
|
|
278
346
|
|
|
@@ -281,35 +349,22 @@ export class ShoppingCart<T extends ShoppingCartItem> {
|
|
|
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
|
}
|