@etsoo/appscript 1.6.55 → 1.6.57

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.
@@ -1,499 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ShoppingCart = void 0;
4
- const shared_1 = require("@etsoo/shared");
5
- const ShoppingCartKeyField = "ETSOO-CART-KEYS";
6
- /**
7
- * Shopping cart
8
- * 购物篮
9
- */
10
- class ShoppingCart {
11
- /**
12
- * Create identifier key
13
- * 创建识别键
14
- * @param currency Currency
15
- * @param culture Culture
16
- * @param key Additional key
17
- * @returns Result
18
- */
19
- static createKey(currency, culture, key) {
20
- return `ETSOO-CART-${culture}-${key}-${currency}`;
21
- }
22
- /**
23
- * Clear shopping cart
24
- * 清除购物篮
25
- * @param identifier Identifier
26
- * @param storage Storage
27
- */
28
- static clear(identifier, storage) {
29
- try {
30
- storage.setData(identifier, null);
31
- storage.setPersistedData(identifier, null);
32
- }
33
- catch (error) {
34
- console.warn(`ShoppingCart clear ${identifier} error`, error);
35
- }
36
- }
37
- /**
38
- * Get cart data
39
- * 获取购物篮数据
40
- * @param storage Storage
41
- * @param id Cart id
42
- * @returns Result
43
- */
44
- static getCartData(storage, id) {
45
- try {
46
- return (storage.getPersistedObject(id) ??
47
- storage.getObject(id));
48
- }
49
- catch (error) {
50
- console.warn(`ShoppingCart getCartData ${id} error`, error);
51
- }
52
- }
53
- /**
54
- * ISO currency id
55
- * 标准货币编号
56
- */
57
- get currency() {
58
- return this._currency;
59
- }
60
- set currency(value) {
61
- this._currency = value;
62
- }
63
- /**
64
- * ISO culture id, like zh-Hans
65
- * 标准语言文化编号
66
- */
67
- get culture() {
68
- return this._culture;
69
- }
70
- set culture(value) {
71
- this._culture = value;
72
- }
73
- /**
74
- * Items
75
- * 项目
76
- */
77
- get items() {
78
- return this._items;
79
- }
80
- set items(value) {
81
- this._items = value;
82
- }
83
- /**
84
- * Order level promotions
85
- * 订单层面促销
86
- */
87
- get promotions() {
88
- return this._promotions;
89
- }
90
- set promotions(value) {
91
- this._promotions = value;
92
- }
93
- /**
94
- * Currency symbol
95
- * 币种符号
96
- */
97
- get symbol() {
98
- return this._symbol;
99
- }
100
- set symbol(value) {
101
- this._symbol = value;
102
- }
103
- /**
104
- * Cart identifier
105
- * 购物篮标识
106
- */
107
- get identifier() {
108
- const o = this.owner;
109
- return ShoppingCart.createKey(this.currency, this.culture, this.key);
110
- }
111
- /**
112
- * All data keys
113
- * 所有的数据键
114
- */
115
- get keys() {
116
- return this.storage.getPersistedData(ShoppingCartKeyField, []);
117
- }
118
- set keys(items) {
119
- this.storage.setPersistedData(ShoppingCartKeyField, items);
120
- }
121
- /**
122
- * Lines count
123
- * 项目数量
124
- */
125
- get lines() {
126
- return this.items.length;
127
- }
128
- /**
129
- * Total qty
130
- * 总数量
131
- */
132
- get totalQty() {
133
- return this.items.map((item) => item.qty).sum();
134
- }
135
- /**
136
- * Total amount
137
- * 总金额
138
- */
139
- get totalAmount() {
140
- const subtotal = this.items
141
- .map((item) => item.subtotal - item.discount)
142
- .sum();
143
- const discount = this.promotions.sum("amount");
144
- return subtotal - discount;
145
- }
146
- /**
147
- * Total amount string
148
- * 总金额字符串
149
- */
150
- get totalAmountStr() {
151
- return this.formatAmount(this.totalAmount);
152
- }
153
- /**
154
- * Constructor
155
- * 构造函数
156
- * @param key Key for identifier
157
- * @param currency Currency ISO code
158
- * @param storage Data storage
159
- */
160
- constructor(key, currencyOrState, storage = new shared_1.WindowStorage()) {
161
- this.storage = storage;
162
- this._items = [];
163
- this._promotions = [];
164
- /**
165
- * Cached prices
166
- * 缓存的价格
167
- */
168
- this.prices = {};
169
- this.key = key;
170
- if (Array.isArray(currencyOrState)) {
171
- this.reset(currencyOrState[0], currencyOrState[1]);
172
- }
173
- else {
174
- this.setCartData(currencyOrState);
175
- this.changeCurrency(currencyOrState.currency);
176
- this.changeCulture(currencyOrState.culture);
177
- }
178
- }
179
- getCartData() {
180
- return (this.storage.getPersistedObject(this.identifier) ??
181
- this.storage.getObject(this.identifier));
182
- }
183
- setCartData(state) {
184
- const { owner, items = [], promotions = [], formData, cache } = state ?? {};
185
- this.owner = owner;
186
- this.items = items;
187
- this.promotions = promotions;
188
- this.formData = formData;
189
- this.cache = cache;
190
- }
191
- doChange(reason, changedItems) {
192
- if (this.onChange)
193
- this.onChange(reason, changedItems);
194
- }
195
- /**
196
- * Add item
197
- * 添加项目
198
- * @param item New item
199
- */
200
- addItem(item) {
201
- this.addItems([item]);
202
- }
203
- /**
204
- * Add items
205
- * @param items New items
206
- */
207
- addItems(items) {
208
- this.items.push(...items);
209
- this.doChange("add", items);
210
- }
211
- /**
212
- * Cache price
213
- * @param id Item id
214
- * @param price Price
215
- * @param overrideExisting Override existing price
216
- */
217
- cachePrice(id, price, overrideExisting = false) {
218
- if (overrideExisting || this.prices[id] == null)
219
- this.prices[id] = price;
220
- }
221
- /**
222
- * Change currency
223
- * @param currency Currency
224
- */
225
- changeCurrency(currency) {
226
- this.currency = currency;
227
- this.symbol = shared_1.NumberUtils.getCurrencySymbol(this.currency);
228
- }
229
- /**
230
- * Change culture
231
- * @param culture Culture
232
- */
233
- changeCulture(culture) {
234
- this.culture = culture;
235
- }
236
- /**
237
- * Clear storage
238
- * @param keepOwner Keep owner data
239
- */
240
- clear(keepOwner) {
241
- this.items.length = 0;
242
- this.promotions.length = 0;
243
- this.prices = {};
244
- this.cache = undefined;
245
- if (keepOwner) {
246
- this.save();
247
- }
248
- else {
249
- ShoppingCart.clear(this.identifier, this.storage);
250
- this.keys.remove(this.identifier);
251
- }
252
- this.doChange("clear", []);
253
- }
254
- /**
255
- * Format amount
256
- * @param amount Amount
257
- * @returns Result
258
- */
259
- formatAmount(amount) {
260
- return shared_1.NumberUtils.formatMoney(amount, this.currency);
261
- }
262
- /**
263
- * Get item
264
- * @param id Item id
265
- * @returns Result
266
- */
267
- getItem(id) {
268
- return this.items.find((item) => item.id === id);
269
- }
270
- /**
271
- * Push item
272
- * 推送项目
273
- * @param data Item data
274
- * @returns Added or not
275
- */
276
- pushItem(data) {
277
- if (this.items.some((item) => item.id === data.id)) {
278
- return false;
279
- }
280
- else {
281
- this.addItem(data);
282
- return true;
283
- }
284
- }
285
- /**
286
- * Reset currency and culture
287
- * @param currency New currency
288
- * @param culture New culture
289
- */
290
- reset(currency, culture) {
291
- this.changeCurrency(currency);
292
- this.changeCulture(culture);
293
- this.setCartData(this.getCartData());
294
- }
295
- /**
296
- * Remove item from the index
297
- * @param index Item index
298
- */
299
- removeItem(index) {
300
- const removedItems = this.items.splice(index, 1);
301
- this.doChange("remove", removedItems);
302
- }
303
- /**
304
- * Reset item
305
- * @param item Shopping cart item
306
- */
307
- resetItem(item) {
308
- item.discount = 0;
309
- item.currentPrice = undefined;
310
- item.promotions = [];
311
- }
312
- /**
313
- * Save cart data
314
- * @param persisted For persisted storage
315
- */
316
- save(persisted = true) {
317
- if (this.owner == null)
318
- return;
319
- const { currency, culture, owner, items, promotions, formData, cache } = this;
320
- const data = {
321
- currency,
322
- culture,
323
- owner,
324
- items,
325
- promotions,
326
- formData,
327
- cache
328
- };
329
- try {
330
- if (persisted) {
331
- this.storage.setPersistedData(this.identifier, data);
332
- const keys = this.keys;
333
- if (!keys.includes(this.identifier)) {
334
- keys.push(this.identifier);
335
- this.keys = keys;
336
- }
337
- }
338
- else {
339
- this.storage.setData(this.identifier, data);
340
- }
341
- }
342
- catch (error) {
343
- console.warn(`ShoppingCart save ${this.identifier} error`, error);
344
- }
345
- return data;
346
- }
347
- /**
348
- * Trigger update
349
- * 触发更新
350
- */
351
- update() {
352
- this.doChange("update", []);
353
- }
354
- /**
355
- * Update discount
356
- * @param item Shopping cart item
357
- */
358
- updateDiscount(item) {
359
- item.discount = item.promotions.sum("amount");
360
- }
361
- /**
362
- * Update asset item
363
- * 更新资产项目
364
- * @param id Product id
365
- * @param qty Asset qty
366
- * @param itemCreator New item creator
367
- * @returns Updated or not
368
- */
369
- updateAssetItem(id, assetQty, itemCreator) {
370
- if (assetQty == null || assetQty <= 0)
371
- assetQty = 1;
372
- const index = this.items.findIndex((item) => item.id === id);
373
- if (index === -1) {
374
- // New
375
- if (itemCreator) {
376
- const price = this.prices[id];
377
- const data = itemCreator();
378
- const qty = data.qty;
379
- const newItem = {
380
- ...data,
381
- id,
382
- price,
383
- assetQty,
384
- subtotal: price * qty * assetQty,
385
- discount: 0,
386
- promotions: Array()
387
- };
388
- this.addItem(newItem);
389
- }
390
- return false;
391
- }
392
- else {
393
- // Update
394
- const item = this.items[index];
395
- // Price may be cached first
396
- const price = this.prices[id] ?? item.price;
397
- const qty = item.qty;
398
- const newItem = {
399
- ...item,
400
- price,
401
- assetQty,
402
- subtotal: price * qty * assetQty,
403
- discount: 0
404
- };
405
- this.items.splice(index, 1, newItem);
406
- this.doChange("update", [item, newItem]);
407
- }
408
- return true;
409
- }
410
- /**
411
- * Update item
412
- * 更新项目
413
- * @param id Product id
414
- * @param qty Qty
415
- * @param itemCreator New item creator
416
- * @returns Updated or not
417
- */
418
- updateItem(id, qty, itemCreator) {
419
- const index = this.items.findIndex((item) => item.id === id);
420
- if (qty == null) {
421
- // Remove the item
422
- if (index !== -1) {
423
- this.removeItem(index);
424
- }
425
- }
426
- else if (index === -1) {
427
- // New
428
- if (itemCreator) {
429
- const price = this.prices[id];
430
- const data = itemCreator();
431
- const newItem = {
432
- ...data,
433
- id,
434
- price,
435
- qty,
436
- subtotal: price * qty * (data.assetQty || 1),
437
- discount: 0,
438
- promotions: Array()
439
- };
440
- this.addItem(newItem);
441
- }
442
- return false;
443
- }
444
- else {
445
- // Update
446
- const item = this.items[index];
447
- // Price may be cached first
448
- const price = this.prices[id] ?? item.price;
449
- const newItem = {
450
- ...item,
451
- qty,
452
- price,
453
- subtotal: price * qty * (item.assetQty || 1),
454
- discount: 0
455
- };
456
- this.items.splice(index, 1, newItem);
457
- this.doChange("update", [item, newItem]);
458
- }
459
- return true;
460
- }
461
- /**
462
- * Update price
463
- * @param id Item id
464
- * @param price New price
465
- */
466
- updatePrice(id, price) {
467
- this.cachePrice(id, price, true);
468
- const index = this.items.findIndex((item) => item.id === id);
469
- if (index !== -1) {
470
- const item = this.items[index];
471
- const qty = item.qty;
472
- const assetQty = item.assetQty || 1;
473
- const newItem = {
474
- ...item,
475
- price,
476
- subtotal: price * qty * assetQty
477
- };
478
- this.items.splice(index, 1, newItem);
479
- this.doChange("update", [item, newItem]);
480
- }
481
- }
482
- /**
483
- * Update title
484
- * @param id Item id
485
- * @param title New title
486
- */
487
- updateTitle(id, title) {
488
- const index = this.items.findIndex((item) => item.id === id);
489
- if (index !== -1) {
490
- const item = this.items[index];
491
- const newItem = { ...item, title };
492
- if (newItem.name === newItem.title)
493
- newItem.title = undefined;
494
- this.items.splice(index, 1, newItem);
495
- this.doChange("title", [item, newItem]);
496
- }
497
- }
498
- }
499
- exports.ShoppingCart = ShoppingCart;