@vectoriox/iox-ui 5.93.0 → 5.95.0
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.
- package/actions-core/index.d.ts +7 -2
- package/commerce-core/index.d.ts +863 -3
- package/composite-core/index.d.ts +290 -2
- package/design-core/index.d.ts +1049 -105
- package/fesm2022/vectoriox-iox-ui-actions-core.mjs +15 -4
- package/fesm2022/vectoriox-iox-ui-actions-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-commerce-core.mjs +1509 -5
- package/fesm2022/vectoriox-iox-ui-commerce-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-composite-core.mjs +1170 -177
- package/fesm2022/vectoriox-iox-ui-composite-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-design-core.mjs +1147 -43
- package/fesm2022/vectoriox-iox-ui-design-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs +2 -2
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui.mjs +8 -1
- package/fesm2022/vectoriox-iox-ui.mjs.map +1 -1
- package/generation-core/index.d.ts +2 -2
- package/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/actions-core/action.model.ts +1 -1
- package/src/actions-core/element-actions.ts +17 -3
- package/src/commerce-core/cart-transport.ts +211 -0
- package/src/commerce-core/cart.ts +305 -0
- package/src/commerce-core/catalog.ts +42 -0
- package/src/commerce-core/checkout.ts +202 -0
- package/src/commerce-core/decimal.ts +28 -0
- package/src/commerce-core/labels.ts +29 -0
- package/src/commerce-core/limits.ts +9 -0
- package/src/commerce-core/money-error.ts +1 -0
- package/src/commerce-core/order-snapshot.ts +151 -0
- package/src/commerce-core/orders.ts +172 -0
- package/src/commerce-core/payments.ts +196 -0
- package/src/commerce-core/public-api.ts +19 -4
- package/src/commerce-core/quantity.ts +56 -0
- package/src/commerce-core/shipping.ts +168 -0
- package/src/commerce-core/shop-display.ts +117 -0
- package/src/commerce-core/text.ts +13 -0
- package/src/commerce-core/variants.ts +296 -0
- package/src/commerce-core/vat.ts +38 -0
- package/src/composite-core/exclusive-runtime.ts +20 -7
- package/src/composite-core/public-api.ts +2 -0
- package/src/composite-core/shop-runtime.ts +626 -0
- package/src/composite-core/shop.ts +433 -0
- package/src/design-core/flavor-spec.generated.ts +1147 -43
- package/src/generation-core/site-spec.generated.ts +2 -2
- package/styles/iox-design.css +95 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { Availability, CatalogEntry } from './catalog';
|
|
2
|
+
import { CurrencyCode, isCurrencyCode } from './currency';
|
|
3
|
+
import { isMoney, majorToMinor, migrateMajorPrice, money, Money } from './money';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A product's OPTIONS and its VARIANTS as the storefront serves them (iox-cms `storefront-projection.ts`), read into
|
|
7
|
+
* what a product page needs (commerce plan §3i, trap T26): which values each option offers, which of them can still be
|
|
8
|
+
* bought given what is already chosen, and the variant a full choice names.
|
|
9
|
+
*
|
|
10
|
+
* A stored variant has no option map, only a flat NAME — the option values in option order joined by `-` (the admin's
|
|
11
|
+
* `product.component.ts` `generateCombinations`: "Red-M"). A name is matched against every combination of the options'
|
|
12
|
+
* values, never split on `-`, so a value that holds a `-` ("Off-white") still reads.
|
|
13
|
+
*/
|
|
14
|
+
export const VARIANT_NAME_SEPARATOR = '-';
|
|
15
|
+
/** The one variant row a product with no options keeps (iox-cms `DEFAULT_VARIANT_NAME`). */
|
|
16
|
+
export const DEFAULT_VARIANT_NAME = 'Default';
|
|
17
|
+
|
|
18
|
+
export interface ProductOption {
|
|
19
|
+
name: string;
|
|
20
|
+
values: string[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** One variant row as the storefront serves it: prices in MAJOR units of the product's `currency`. */
|
|
24
|
+
export interface StorefrontVariant {
|
|
25
|
+
_id?: string;
|
|
26
|
+
name?: string;
|
|
27
|
+
enable?: boolean;
|
|
28
|
+
image?: string | null;
|
|
29
|
+
price?: number | string | null;
|
|
30
|
+
compareat?: number | string | null;
|
|
31
|
+
availability?: string | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A product as the storefront serves it (the allowlist in iox-cms `STOREFRONT_FIELDS`). */
|
|
35
|
+
export interface StorefrontProduct {
|
|
36
|
+
_id?: string;
|
|
37
|
+
title?: string;
|
|
38
|
+
slug?: string;
|
|
39
|
+
mainImage?: string | null;
|
|
40
|
+
productVariants?: boolean;
|
|
41
|
+
pricing?: { price?: number | string | null; compareat?: number | string | null } | null;
|
|
42
|
+
options?: ReadonlyArray<{ name?: string; values?: ReadonlyArray<unknown> }> | null;
|
|
43
|
+
variants?: ReadonlyArray<StorefrontVariant> | null;
|
|
44
|
+
currency?: string | null;
|
|
45
|
+
availability?: string | null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Option name → the value chosen. */
|
|
49
|
+
export type VariantChoice = Readonly<Record<string, string>>;
|
|
50
|
+
|
|
51
|
+
/** One variant, read: its id (null for a product with no variant row), its choice, its money and whether it sells. */
|
|
52
|
+
export interface ProductVariantView {
|
|
53
|
+
variantId: string | null;
|
|
54
|
+
name: string | null;
|
|
55
|
+
choice: VariantChoice;
|
|
56
|
+
price: Money | null;
|
|
57
|
+
/** Only when it is above the price (shown struck through). */
|
|
58
|
+
compareAt: Money | null;
|
|
59
|
+
image: string | null;
|
|
60
|
+
availability: Availability;
|
|
61
|
+
/** The variant's own switch (a product with no options: true). */
|
|
62
|
+
enabled: boolean;
|
|
63
|
+
/** Can be put in a cart: switched on, priced, not `out`. */
|
|
64
|
+
sellable: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface OptionValueState {
|
|
68
|
+
value: string;
|
|
69
|
+
/** Some sellable variant has this value, given the choices of the options BEFORE this one. */
|
|
70
|
+
available: boolean;
|
|
71
|
+
selected: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface OptionState {
|
|
75
|
+
name: string;
|
|
76
|
+
values: OptionValueState[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const isRecord = (value: unknown): value is Record<string, any> => !!value && typeof value === 'object' && !Array.isArray(value);
|
|
80
|
+
const text = (value: unknown): string => (typeof value === 'string' ? value.trim() : typeof value === 'number' && Number.isFinite(value) ? String(value) : '');
|
|
81
|
+
|
|
82
|
+
/** True for something the storefront could have served as a product: a title and a price or variants. */
|
|
83
|
+
export function isStorefrontProduct(value: unknown): value is StorefrontProduct {
|
|
84
|
+
return isRecord(value) && typeof value['title'] === 'string' && (isRecord(value['pricing']) || Array.isArray(value['variants']));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The product's currency, or null when it names none we know (a price is then never shown — never guessed). */
|
|
88
|
+
export function productCurrency(product: StorefrontProduct | null | undefined): CurrencyCode | null {
|
|
89
|
+
const code = text(product?.currency).toUpperCase();
|
|
90
|
+
return isCurrencyCode(code) ? code : null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The options a product offers, cleaned: named, each value once, no empty value. None when it has no variants. */
|
|
94
|
+
export function productOptions(product: StorefrontProduct | null | undefined): ProductOption[] {
|
|
95
|
+
if (!product?.productVariants || !Array.isArray(product.options)) return [];
|
|
96
|
+
const out: ProductOption[] = [];
|
|
97
|
+
for (const raw of product.options) {
|
|
98
|
+
const name = text(raw?.name);
|
|
99
|
+
if (!name || out.some(o => o.name === name)) continue;
|
|
100
|
+
const values: string[] = [];
|
|
101
|
+
for (const v of Array.isArray(raw?.values) ? raw!.values! : []) {
|
|
102
|
+
const value = text(v);
|
|
103
|
+
if (value && !values.includes(value)) values.push(value);
|
|
104
|
+
}
|
|
105
|
+
if (values.length) out.push({ name, values });
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Every full choice of `options`, in the admin's order, with the variant name it writes. */
|
|
111
|
+
function combinations(options: ReadonlyArray<ProductOption>): Array<{ name: string; choice: VariantChoice }> {
|
|
112
|
+
let acc: Array<{ parts: string[]; choice: Record<string, string> }> = [{ parts: [], choice: {} }];
|
|
113
|
+
for (const option of options) {
|
|
114
|
+
const next: typeof acc = [];
|
|
115
|
+
for (const partial of acc) {
|
|
116
|
+
for (const value of option.values) next.push({ parts: [...partial.parts, value], choice: { ...partial.choice, [option.name]: value } });
|
|
117
|
+
}
|
|
118
|
+
acc = next;
|
|
119
|
+
}
|
|
120
|
+
return acc.map(c => ({ name: c.parts.join(VARIANT_NAME_SEPARATOR), choice: c.choice }));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The choice a stored variant's flat name stands for, or null when no combination of the options writes it. */
|
|
124
|
+
export function variantChoiceOf(variant: StorefrontVariant | null | undefined, options: ReadonlyArray<ProductOption>): VariantChoice | null {
|
|
125
|
+
const name = text(variant?.name);
|
|
126
|
+
if (!name || !options.length) return null;
|
|
127
|
+
return combinations(options).find(c => c.name === name)?.choice ?? null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** A stored major-unit price as Money, or null (no price, a currency we do not know, a value that is no price). */
|
|
131
|
+
export function storefrontMoney(value: unknown, currency: CurrencyCode | null): Money | null {
|
|
132
|
+
if (!currency) return null;
|
|
133
|
+
if (isMoney(value)) return value.currency === currency ? value : null;
|
|
134
|
+
if (typeof value !== 'number' && typeof value !== 'string') return null;
|
|
135
|
+
if (typeof value === 'string' && value.trim() === '') return null;
|
|
136
|
+
try {
|
|
137
|
+
return money(majorToMinor(value, currency), currency);
|
|
138
|
+
} catch {
|
|
139
|
+
const minor = migrateMajorPrice(value, currency);
|
|
140
|
+
return minor === null ? null : money(minor, currency);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const AVAILABILITIES = Object.values(Availability) as string[];
|
|
145
|
+
|
|
146
|
+
function availabilityOf(value: unknown, enabled: boolean): Availability {
|
|
147
|
+
if (!enabled) return Availability.Out;
|
|
148
|
+
return AVAILABILITIES.includes(text(value)) ? text(value) as Availability : Availability.InStock;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function aboveOnly(compareAt: Money | null, price: Money | null): Money | null {
|
|
152
|
+
return compareAt && price && compareAt.amount > price.amount ? compareAt : null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Every variant of a product, read. A product with no options is ONE view: its Default row's id (or null when it has
|
|
157
|
+
* no row) and the product's own price. A variant whose name matches no combination of the options is left out — the
|
|
158
|
+
* page cannot offer what it cannot name.
|
|
159
|
+
*/
|
|
160
|
+
export function productVariants(product: StorefrontProduct | null | undefined): ProductVariantView[] {
|
|
161
|
+
if (!product) return [];
|
|
162
|
+
const currency = productCurrency(product);
|
|
163
|
+
const options = productOptions(product);
|
|
164
|
+
const rows: StorefrontVariant[] = Array.isArray(product.variants) ? product.variants.filter(isRecord) as StorefrontVariant[] : [];
|
|
165
|
+
const view = (row: StorefrontVariant | null, choice: VariantChoice, priceRaw: unknown, compareRaw: unknown, image: unknown, availability: unknown): ProductVariantView => {
|
|
166
|
+
const enabled = row?.enable !== false;
|
|
167
|
+
const price = storefrontMoney(priceRaw, currency);
|
|
168
|
+
const state = availabilityOf(availability, enabled);
|
|
169
|
+
return {
|
|
170
|
+
variantId: text(row?._id) || null,
|
|
171
|
+
name: text(row?.name) || null,
|
|
172
|
+
choice,
|
|
173
|
+
price,
|
|
174
|
+
compareAt: aboveOnly(storefrontMoney(compareRaw, currency), price),
|
|
175
|
+
image: text(image) || null,
|
|
176
|
+
availability: state,
|
|
177
|
+
enabled,
|
|
178
|
+
sellable: enabled && !!price && state !== Availability.Out,
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
if (!options.length) {
|
|
182
|
+
const row = rows.find(r => r.name === DEFAULT_VARIANT_NAME) ?? (rows.length === 1 ? rows[0] : null);
|
|
183
|
+
const pricing = product.pricing ?? {};
|
|
184
|
+
return [view(row, {}, row?.price ?? pricing.price, row?.compareat ?? pricing.compareat, row?.image || product.mainImage,
|
|
185
|
+
row?.availability ?? product.availability)];
|
|
186
|
+
}
|
|
187
|
+
const out: ProductVariantView[] = [];
|
|
188
|
+
for (const row of rows) {
|
|
189
|
+
const choice = variantChoiceOf(row, options);
|
|
190
|
+
if (choice) out.push(view(row, choice, row.price, row.compareat, row.image || product.mainImage, row.availability));
|
|
191
|
+
}
|
|
192
|
+
return out;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const matches = (variant: ProductVariantView, choice: VariantChoice, names: ReadonlyArray<string>): boolean =>
|
|
196
|
+
names.every(name => choice[name] === undefined || variant.choice[name] === choice[name]);
|
|
197
|
+
|
|
198
|
+
/** The variant a FULL choice names, or null (a choice that misses an option, or names no stored variant). */
|
|
199
|
+
export function findVariant(options: ReadonlyArray<ProductOption>, variants: ReadonlyArray<ProductVariantView>, choice: VariantChoice): ProductVariantView | null {
|
|
200
|
+
if (!options.length) return variants[0] ?? null;
|
|
201
|
+
if (options.some(o => !choice[o.name])) return null;
|
|
202
|
+
return variants.find(v => options.every(o => v.choice[o.name] === choice[o.name])) ?? null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* What each option offers now. Availability CASCADES in option order (the way a buyer reads the page, top to bottom):
|
|
207
|
+
* a value of the first option is available when ANY sellable variant has it; a value of a later option, when a
|
|
208
|
+
* sellable variant has it together with the values chosen for the options before it. So a buyer can always change an
|
|
209
|
+
* earlier option and the later ones follow (`chooseValue`) — never stuck behind a combination that is sold out.
|
|
210
|
+
*/
|
|
211
|
+
export function optionValueStates(options: ReadonlyArray<ProductOption>, variants: ReadonlyArray<ProductVariantView>, choice: VariantChoice): OptionState[] {
|
|
212
|
+
const sellable = variants.filter(v => v.sellable);
|
|
213
|
+
return options.map((option, index) => {
|
|
214
|
+
const before = options.slice(0, index).map(o => o.name);
|
|
215
|
+
return {
|
|
216
|
+
name: option.name,
|
|
217
|
+
values: option.values.map(value => ({
|
|
218
|
+
value,
|
|
219
|
+
available: sellable.some(v => v.choice[option.name] === value && matches(v, choice, before)),
|
|
220
|
+
selected: choice[option.name] === value,
|
|
221
|
+
})),
|
|
222
|
+
};
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** The choice a page opens with: the first sellable variant's, else the first value of every option. */
|
|
227
|
+
export function defaultChoice(options: ReadonlyArray<ProductOption>, variants: ReadonlyArray<ProductVariantView>): VariantChoice {
|
|
228
|
+
const first = variants.find(v => v.sellable && options.every(o => v.choice[o.name]));
|
|
229
|
+
if (first) return { ...first.choice };
|
|
230
|
+
return Object.fromEntries(options.map(o => [o.name, o.values[0]]));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The buyer picks `value` for option `name`. The options AFTER it keep their value while it is still available after
|
|
235
|
+
* the change, else move to their first available value (else keep it — the page then says the combination is sold out).
|
|
236
|
+
* An unknown option or value changes nothing.
|
|
237
|
+
*/
|
|
238
|
+
export function chooseValue(options: ReadonlyArray<ProductOption>, variants: ReadonlyArray<ProductVariantView>, choice: VariantChoice,
|
|
239
|
+
name: string, value: string): VariantChoice {
|
|
240
|
+
const at = options.findIndex(o => o.name === name);
|
|
241
|
+
if (at < 0 || !options[at].values.includes(value)) return choice;
|
|
242
|
+
const next: Record<string, string> = { ...choice, [name]: value };
|
|
243
|
+
for (let i = at + 1; i < options.length; i++) {
|
|
244
|
+
const state = optionValueStates(options, variants, next)[i];
|
|
245
|
+
const keeps = state.values.find(v => v.value === next[options[i].name])?.available;
|
|
246
|
+
if (keeps) continue;
|
|
247
|
+
const first = state.values.find(v => v.available);
|
|
248
|
+
if (first) next[options[i].name] = first.value;
|
|
249
|
+
}
|
|
250
|
+
return next;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The catalog entries a storefront product sells (one per variant, or one for a product with no options) — what
|
|
254
|
+
* `priceCart` reads where only the storefront is known (the builder canvas's simulated cart). iox-cms builds its own
|
|
255
|
+
* from the stored row, with the stock COUNT. */
|
|
256
|
+
export function storefrontCatalog(product: StorefrontProduct | null | undefined): CatalogEntry[] {
|
|
257
|
+
const productId = text(product?._id);
|
|
258
|
+
if (!product || !productId) return [];
|
|
259
|
+
const hasOptions = productOptions(product).length > 0;
|
|
260
|
+
return productVariants(product).map(v => ({
|
|
261
|
+
productId,
|
|
262
|
+
variantId: hasOptions ? v.variantId : null,
|
|
263
|
+
title: text(product.title),
|
|
264
|
+
variantName: hasOptions ? Object.values(v.choice).join(' · ') : null,
|
|
265
|
+
unitPrice: v.price,
|
|
266
|
+
compareAt: v.compareAt,
|
|
267
|
+
availability: v.availability,
|
|
268
|
+
enabled: v.enabled,
|
|
269
|
+
published: true,
|
|
270
|
+
requiresShipping: true,
|
|
271
|
+
image: v.image,
|
|
272
|
+
}));
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The words a chosen variant is shown with in a cart line ("Sand · 32 cm"). */
|
|
276
|
+
export function variantWords(choice: VariantChoice, options: ReadonlyArray<ProductOption>): string {
|
|
277
|
+
return options.map(o => choice[o.name]).filter(Boolean).join(' · ');
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ── the product in scope ─────────────────────────────────────────────────────────────────────
|
|
281
|
+
|
|
282
|
+
/** The data keys a product page's own item sits under (the route's item first — relative data sources). */
|
|
283
|
+
const ROUTE_KEYS: ReadonlyArray<string> = ['$current', '$route'];
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The product an element is about, the SAME rule on the builder canvas and the live site: the repeated row it sits in
|
|
287
|
+
* when that row is a product (a card in a product list), else the page's route item, else the first product the page's
|
|
288
|
+
* data holds (a product page bound to one product). Null when the page has none.
|
|
289
|
+
*/
|
|
290
|
+
export function productInScope(rowItem: unknown, data: Readonly<Record<string, unknown>> | null | undefined): StorefrontProduct | null {
|
|
291
|
+
if (isStorefrontProduct(rowItem)) return rowItem;
|
|
292
|
+
const all = data ?? {};
|
|
293
|
+
for (const key of ROUTE_KEYS) if (isStorefrontProduct(all[key])) return all[key] as StorefrontProduct;
|
|
294
|
+
for (const value of Object.values(all)) if (isStorefrontProduct(value)) return value;
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { currencyExponent } from './currency';
|
|
2
|
+
import { decimalToScaledInteger, DecimalRounding, mulDivHalfEven, parseDecimal } from './decimal';
|
|
3
|
+
import { Money } from './money';
|
|
4
|
+
import { MoneyError, MoneyErrorCode } from './money-error';
|
|
5
|
+
|
|
6
|
+
export const MAX_VAT_PERCENT = 100;
|
|
7
|
+
/** 17, 18, 7.7, 8.1 — four decimals is more than any rate in force; it also keeps the arithmetic below 2^53. */
|
|
8
|
+
export const VAT_PERCENT_MAX_DECIMALS = 4;
|
|
9
|
+
|
|
10
|
+
export function isVatPercent(value: unknown): value is number {
|
|
11
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > MAX_VAT_PERCENT) return false;
|
|
12
|
+
const decimal = parseDecimal(String(value));
|
|
13
|
+
return !!decimal && decimal.frac.length <= VAT_PERCENT_MAX_DECIMALS;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The VAT a VAT-INCLUSIVE amount already holds (C-D8): amount × v / (100 + v), rounded ONCE to a minor unit,
|
|
18
|
+
* half-to-even. Exact: v is read as the integer R = v·10^s (s ≤ 4 decimals), so the share is
|
|
19
|
+
* amount × R / (100·10^s + R) in whole numbers. 118.00 at 18 % → 18.00; 100.00 at 17 % → 14.53.
|
|
20
|
+
*/
|
|
21
|
+
export function includedVat(total: Money, vatPercent: number): Money {
|
|
22
|
+
currencyExponent(total.currency);
|
|
23
|
+
if (typeof total.amount !== 'number' || !Number.isSafeInteger(total.amount)) {
|
|
24
|
+
throw new MoneyError(MoneyErrorCode.NotInteger, `total must be a whole number of minor units, got ${String(total.amount)}`);
|
|
25
|
+
}
|
|
26
|
+
if (!isVatPercent(vatPercent)) {
|
|
27
|
+
throw new MoneyError(MoneyErrorCode.OutOfRange, `VAT must be 0–${MAX_VAT_PERCENT} with at most ${VAT_PERCENT_MAX_DECIMALS} decimals, got ${String(vatPercent)}`);
|
|
28
|
+
}
|
|
29
|
+
const decimal = parseDecimal(String(vatPercent))!;
|
|
30
|
+
const scale = decimal.frac.length;
|
|
31
|
+
const rate = decimalToScaledInteger(decimal, scale, DecimalRounding.Exact);
|
|
32
|
+
const magnitude = Math.abs(total.amount);
|
|
33
|
+
if (magnitude > Number.MAX_SAFE_INTEGER / 2) {
|
|
34
|
+
throw new MoneyError(MoneyErrorCode.Overflow, 'Amount is beyond the range VAT is computed for');
|
|
35
|
+
}
|
|
36
|
+
const share = mulDivHalfEven(magnitude, rate, 100 * 10 ** scale + rate);
|
|
37
|
+
return { amount: total.amount < 0 && share !== 0 ? -share : share, currency: total.currency };
|
|
38
|
+
}
|
|
@@ -56,6 +56,9 @@ export interface ExclusiveRuntimeOptions {
|
|
|
56
56
|
/** The key on when there is no server state (the builder canvas: a Segmented control's On by default), else the
|
|
57
57
|
* root's `data-iox-default-on`, else the first item. */
|
|
58
58
|
defaultKey?: () => string | null;
|
|
59
|
+
/** The triggers' selector, when they are not one of the kinds' role classes (commerce E1c: a variant picker's values
|
|
60
|
+
* run as a Segmented control's options — `.iox-role-shop-variant-value`). */
|
|
61
|
+
triggerSelector?: string;
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
const KIND_ROOT = (kind: ExclusiveCompositeKind) => `[${COMPOSITE_ATTRIBUTE}="${kind}"]`;
|
|
@@ -79,6 +82,12 @@ export function closestExclusiveItemPart(el: Element | null | undefined): HTMLEl
|
|
|
79
82
|
return (el?.closest?.(ITEM_PART_SELECTOR) as HTMLElement | null) ?? null;
|
|
80
83
|
}
|
|
81
84
|
|
|
85
|
+
/** A trigger that cannot be turned on: `aria-disabled="true"` or a disabled button (a sold-out variant value). It stays
|
|
86
|
+
* visible and announced, is skipped by the arrow keys and refuses a click (APG radio group). */
|
|
87
|
+
export function isExclusiveTriggerDisabled(el: Element | null | undefined): boolean {
|
|
88
|
+
return !!el && (el.getAttribute('aria-disabled') === 'true' || (el as HTMLButtonElement).disabled === true);
|
|
89
|
+
}
|
|
90
|
+
|
|
82
91
|
const kindOf = (root: Element): ExclusiveCompositeKind => {
|
|
83
92
|
const raw = root.getAttribute(COMPOSITE_ATTRIBUTE) as CompositeKind | null;
|
|
84
93
|
return isExclusiveKind(raw) ? raw : CompositeKind.Tabs;
|
|
@@ -154,7 +163,7 @@ export class ExclusiveRuntime {
|
|
|
154
163
|
|
|
155
164
|
/** THIS composite's triggers in document order (a nested composite's are its own). */
|
|
156
165
|
triggers(): HTMLElement[] {
|
|
157
|
-
return this.own(TRIGGER_SELECTOR);
|
|
166
|
+
return this.own(this.options.triggerSelector ?? TRIGGER_SELECTOR);
|
|
158
167
|
}
|
|
159
168
|
|
|
160
169
|
/** THIS composite's panels in document order. */
|
|
@@ -188,7 +197,8 @@ export class ExclusiveRuntime {
|
|
|
188
197
|
|
|
189
198
|
/** Turn `key` on (a click, a key, a hover, the `openItem` action, "Showing on canvas"). */
|
|
190
199
|
activate(key: string): void {
|
|
191
|
-
|
|
200
|
+
const trigger = this.triggers().find(t => this.keyOf(t) === key);
|
|
201
|
+
if (!trigger || isExclusiveTriggerDisabled(trigger)) return;
|
|
192
202
|
const changed = key !== this.live;
|
|
193
203
|
this.live = key;
|
|
194
204
|
this.stamp();
|
|
@@ -222,9 +232,12 @@ export class ExclusiveRuntime {
|
|
|
222
232
|
|
|
223
233
|
/** The key on with no server state: the asked default when it names an item, else the first item. */
|
|
224
234
|
private defaultKey(): string | null {
|
|
225
|
-
const
|
|
235
|
+
const triggers = this.triggers();
|
|
236
|
+
const keys = triggers.map(t => this.keyOf(t));
|
|
237
|
+
const enabled = triggers.filter(t => !isExclusiveTriggerDisabled(t)).map(t => this.keyOf(t));
|
|
226
238
|
const asked = this.options.defaultKey?.() ?? this.root.getAttribute(DEFAULT_ON_ATTRIBUTE);
|
|
227
|
-
|
|
239
|
+
if (asked && keys.includes(asked)) return asked;
|
|
240
|
+
return enabled[0] ?? keys[0] ?? null;
|
|
228
241
|
}
|
|
229
242
|
|
|
230
243
|
/** True for a Segmented control: the root IS its radio group and holds its options. */
|
|
@@ -338,7 +351,7 @@ export class ExclusiveRuntime {
|
|
|
338
351
|
}
|
|
339
352
|
|
|
340
353
|
private ownTrigger(target: EventTarget | null): HTMLElement | null {
|
|
341
|
-
const trigger = (target as Element | null)?.closest?.(TRIGGER_SELECTOR) as HTMLElement | null;
|
|
354
|
+
const trigger = (target as Element | null)?.closest?.(this.options.triggerSelector ?? TRIGGER_SELECTOR) as HTMLElement | null;
|
|
342
355
|
return trigger && closestExclusive(trigger.parentElement) === this.root ? trigger : null;
|
|
343
356
|
}
|
|
344
357
|
|
|
@@ -362,9 +375,9 @@ export class ExclusiveRuntime {
|
|
|
362
375
|
const rtl = isRtl(trigger);
|
|
363
376
|
const action = this.holdsTriggers ? segmentedKeyAction(e.key, rtl) : tabsKeyAction(e.key, this.behaviourValue.orientation, rtl);
|
|
364
377
|
if (!action) return;
|
|
365
|
-
const triggers = this.triggers();
|
|
378
|
+
const triggers = this.triggers().filter(t => t === trigger || !isExclusiveTriggerDisabled(t));
|
|
366
379
|
const target = triggers[accordionFocusIndex(action, triggers.indexOf(trigger), triggers.length)];
|
|
367
|
-
if (!target) return;
|
|
380
|
+
if (!target || isExclusiveTriggerDisabled(target)) return;
|
|
368
381
|
e.preventDefault();
|
|
369
382
|
target.focus();
|
|
370
383
|
this.activate(this.keyOf(target));
|