shirkasoft-ui-components 1.3.15 → 1.4.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/README.md +447 -71
- package/fesm2022/shirkasoft-ui-components-breadcrumb.mjs +7 -7
- package/fesm2022/shirkasoft-ui-components-breadcrumb.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-button.mjs +11 -11
- package/fesm2022/shirkasoft-ui-components-button.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-chart.mjs +6 -6
- package/fesm2022/shirkasoft-ui-components-chart.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-config.mjs +914 -0
- package/fesm2022/shirkasoft-ui-components-config.mjs.map +1 -0
- package/fesm2022/shirkasoft-ui-components-confirm-dialog.mjs +8 -8
- package/fesm2022/shirkasoft-ui-components-confirm-dialog.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-date-picker.mjs +901 -753
- package/fesm2022/shirkasoft-ui-components-date-picker.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-fieldset.mjs +5 -5
- package/fesm2022/shirkasoft-ui-components-fieldset.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-file-upload.mjs +16 -9
- package/fesm2022/shirkasoft-ui-components-file-upload.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-icons.mjs +646 -0
- package/fesm2022/shirkasoft-ui-components-icons.mjs.map +1 -0
- package/fesm2022/shirkasoft-ui-components-knob.mjs +44 -17
- package/fesm2022/shirkasoft-ui-components-knob.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-loading.mjs +16 -16
- package/fesm2022/shirkasoft-ui-components-loading.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-modal.mjs +42 -31
- package/fesm2022/shirkasoft-ui-components-modal.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-notification.mjs +8 -8
- package/fesm2022/shirkasoft-ui-components-notification.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-price-input.mjs +79 -29
- package/fesm2022/shirkasoft-ui-components-price-input.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-rail-sidebar.mjs +7 -7
- package/fesm2022/shirkasoft-ui-components-rail-sidebar.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-select.mjs +50 -23
- package/fesm2022/shirkasoft-ui-components-select.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-sidebar.mjs +6 -6
- package/fesm2022/shirkasoft-ui-components-sidebar.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-skeleton.mjs +4 -4
- package/fesm2022/shirkasoft-ui-components-skeleton.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-table.mjs +163 -102
- package/fesm2022/shirkasoft-ui-components-table.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-tabs.mjs +21 -23
- package/fesm2022/shirkasoft-ui-components-tabs.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-tag.mjs +9 -9
- package/fesm2022/shirkasoft-ui-components-tag.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-text-field.mjs +63 -30
- package/fesm2022/shirkasoft-ui-components-text-field.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-theme-picker.mjs +280 -0
- package/fesm2022/shirkasoft-ui-components-theme-picker.mjs.map +1 -0
- package/fesm2022/shirkasoft-ui-components-theme.mjs +180 -0
- package/fesm2022/shirkasoft-ui-components-theme.mjs.map +1 -0
- package/fesm2022/shirkasoft-ui-components-timeline.mjs +10 -22
- package/fesm2022/shirkasoft-ui-components-timeline.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-toggle.mjs +60 -53
- package/fesm2022/shirkasoft-ui-components-toggle.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-tooltip.mjs +29 -11
- package/fesm2022/shirkasoft-ui-components-tooltip.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-utils.mjs +22 -11
- package/fesm2022/shirkasoft-ui-components-utils.mjs.map +1 -1
- package/fesm2022/shirkasoft-ui-components-wizard.mjs +9 -10
- package/fesm2022/shirkasoft-ui-components-wizard.mjs.map +1 -1
- package/package.json +33 -3
- package/src/baseline.css +2472 -0
- package/src/theme.css +15 -543
- package/src/themes/ocean.css +56 -0
- package/src/themes/sunset.css +55 -0
- package/src/themes/violet.css +25 -0
- package/src/tokens.css +612 -0
- package/types/shirkasoft-ui-components-button.d.ts +1 -1
- package/types/shirkasoft-ui-components-config.d.ts +368 -0
- package/types/shirkasoft-ui-components-confirm-dialog.d.ts +2 -1
- package/types/shirkasoft-ui-components-date-picker.d.ts +276 -224
- package/types/shirkasoft-ui-components-fieldset.d.ts +2 -1
- package/types/shirkasoft-ui-components-file-upload.d.ts +11 -5
- package/types/shirkasoft-ui-components-icons.d.ts +1422 -0
- package/types/shirkasoft-ui-components-knob.d.ts +28 -7
- package/types/shirkasoft-ui-components-modal.d.ts +11 -7
- package/types/shirkasoft-ui-components-notification.d.ts +2 -1
- package/types/shirkasoft-ui-components-price-input.d.ts +37 -9
- package/types/shirkasoft-ui-components-rail-sidebar.d.ts +3 -3
- package/types/shirkasoft-ui-components-select.d.ts +24 -8
- package/types/shirkasoft-ui-components-sidebar.d.ts +2 -1
- package/types/shirkasoft-ui-components-table.d.ts +91 -80
- package/types/shirkasoft-ui-components-tag.d.ts +1 -1
- package/types/shirkasoft-ui-components-text-field.d.ts +29 -13
- package/types/shirkasoft-ui-components-theme-picker.d.ts +83 -0
- package/types/shirkasoft-ui-components-theme.d.ts +88 -0
- package/types/shirkasoft-ui-components-timeline.d.ts +5 -4
- package/types/shirkasoft-ui-components-toggle.d.ts +20 -13
- package/types/shirkasoft-ui-components-tooltip.d.ts +6 -0
- package/types/shirkasoft-ui-components-utils.d.ts +16 -6
- package/types/shirkasoft-ui-components-wizard.d.ts +4 -3
|
@@ -0,0 +1,914 @@
|
|
|
1
|
+
import { DOCUMENT } from '@angular/common';
|
|
2
|
+
import * as i0 from '@angular/core';
|
|
3
|
+
import { InjectionToken, inject, signal, computed, effect, untracked, Injectable, makeEnvironmentProviders, provideAppInitializer, CSP_NONCE } from '@angular/core';
|
|
4
|
+
import { DEFAULT_SHK_THEME_CONFIG, SHK_THEME_CONFIG, ShkThemeService } from 'shirkasoft-ui-components/theme';
|
|
5
|
+
|
|
6
|
+
/** Locales offered by the built-in `select` options (any BCP 47 tag is still accepted via `set`). */
|
|
7
|
+
const LOCALE_CHOICES = [
|
|
8
|
+
{ value: 'es', label: 'Español' },
|
|
9
|
+
{ value: 'es-ES', label: 'Español (España)' },
|
|
10
|
+
{ value: 'es-MX', label: 'Español (México)' },
|
|
11
|
+
{ value: 'en-US', label: 'English (US)' },
|
|
12
|
+
{ value: 'en-GB', label: 'English (UK)' },
|
|
13
|
+
{ value: 'pt-BR', label: 'Português (Brasil)' },
|
|
14
|
+
{ value: 'fr-FR', label: 'Français' },
|
|
15
|
+
];
|
|
16
|
+
/** Date options (`dates.*`), always registered. */
|
|
17
|
+
const DATE_OPTIONS = [
|
|
18
|
+
{
|
|
19
|
+
key: 'dates.locale',
|
|
20
|
+
module: 'dates',
|
|
21
|
+
type: 'select',
|
|
22
|
+
default: 'es',
|
|
23
|
+
label: 'Idioma de las fechas',
|
|
24
|
+
description: 'Nombres de meses y días del calendario.',
|
|
25
|
+
choices: LOCALE_CHOICES,
|
|
26
|
+
allowCustom: true,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
key: 'dates.format',
|
|
30
|
+
module: 'dates',
|
|
31
|
+
type: 'select',
|
|
32
|
+
default: '',
|
|
33
|
+
label: 'Formato de fecha',
|
|
34
|
+
description: 'Cómo se escriben y muestran las fechas en campos y tablas.',
|
|
35
|
+
allowCustom: true,
|
|
36
|
+
choices: [
|
|
37
|
+
{ value: '', label: 'Automático según el idioma' },
|
|
38
|
+
{ value: 'dd/MM/yyyy', label: '31/12/2026' },
|
|
39
|
+
{ value: 'MM/dd/yyyy', label: '12/31/2026' },
|
|
40
|
+
{ value: 'yyyy-MM-dd', label: '2026-12-31' },
|
|
41
|
+
{ value: 'dd-MM-yyyy', label: '31-12-2026' },
|
|
42
|
+
{ value: 'dd.MM.yyyy', label: '31.12.2026' },
|
|
43
|
+
{ value: 'd MMM yyyy', label: '31 dic 2026' },
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
key: 'dates.displayStyle',
|
|
48
|
+
module: 'dates',
|
|
49
|
+
type: 'select',
|
|
50
|
+
default: 'long',
|
|
51
|
+
label: 'Fecha seleccionada',
|
|
52
|
+
description: 'Cómo muestra el selector de fecha el valor elegido.',
|
|
53
|
+
choices: [
|
|
54
|
+
{ value: 'long', label: 'Larga (31 de diciembre de 2026)' },
|
|
55
|
+
{ value: 'medium', label: 'Media (31 dic 2026)' },
|
|
56
|
+
{ value: 'short', label: 'Corta (31/12/26)' },
|
|
57
|
+
{ value: 'format', label: 'Con el formato de fecha' },
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
key: 'dates.hourFormat',
|
|
62
|
+
module: 'dates',
|
|
63
|
+
type: 'select',
|
|
64
|
+
default: '24',
|
|
65
|
+
label: 'Formato de hora',
|
|
66
|
+
choices: [
|
|
67
|
+
{ value: '24', label: '24 horas (14:30)' },
|
|
68
|
+
{ value: '12', label: '12 horas (02:30 PM)' },
|
|
69
|
+
],
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
key: 'dates.weekStart',
|
|
73
|
+
module: 'dates',
|
|
74
|
+
type: 'select',
|
|
75
|
+
default: null,
|
|
76
|
+
label: 'Primer día de la semana',
|
|
77
|
+
choices: [
|
|
78
|
+
{ value: null, label: 'Según el idioma' },
|
|
79
|
+
{ value: 1, label: 'Lunes' },
|
|
80
|
+
{ value: 0, label: 'Domingo' },
|
|
81
|
+
{ value: 6, label: 'Sábado' },
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
/** Number and currency options (`numbers.*`), always registered. */
|
|
86
|
+
const NUMBER_OPTIONS = [
|
|
87
|
+
{
|
|
88
|
+
key: 'numbers.locale',
|
|
89
|
+
module: 'numbers',
|
|
90
|
+
type: 'select',
|
|
91
|
+
default: 'en-US',
|
|
92
|
+
label: 'Formato de números',
|
|
93
|
+
description: 'Separadores de miles y decimales (1,234.56 o 1.234,56).',
|
|
94
|
+
allowCustom: true,
|
|
95
|
+
choices: [
|
|
96
|
+
{ value: 'en-US', label: '1,234.56' },
|
|
97
|
+
{ value: 'es-ES', label: '1.234,56' },
|
|
98
|
+
{ value: 'fr-FR', label: '1 234,56' },
|
|
99
|
+
{ value: 'de-CH', label: "1'234.56" },
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
key: 'numbers.currency',
|
|
104
|
+
module: 'numbers',
|
|
105
|
+
type: 'select',
|
|
106
|
+
default: 'USD',
|
|
107
|
+
label: 'Moneda',
|
|
108
|
+
description: 'Moneda por defecto de importes y columnas de moneda.',
|
|
109
|
+
allowCustom: true,
|
|
110
|
+
choices: [
|
|
111
|
+
{ value: 'USD', label: 'Dólar estadounidense (USD)' },
|
|
112
|
+
{ value: 'EUR', label: 'Euro (EUR)' },
|
|
113
|
+
{ value: 'CUP', label: 'Peso cubano (CUP)' },
|
|
114
|
+
{ value: 'MXN', label: 'Peso mexicano (MXN)' },
|
|
115
|
+
{ value: 'GBP', label: 'Libra esterlina (GBP)' },
|
|
116
|
+
{ value: 'BRL', label: 'Real brasileño (BRL)' },
|
|
117
|
+
],
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
key: 'numbers.currencySymbol',
|
|
121
|
+
module: 'numbers',
|
|
122
|
+
type: 'string',
|
|
123
|
+
default: '',
|
|
124
|
+
label: 'Símbolo de moneda',
|
|
125
|
+
description: 'Vacío = el de la moneda ($, €…).',
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
key: 'numbers.decimals',
|
|
129
|
+
module: 'numbers',
|
|
130
|
+
type: 'select',
|
|
131
|
+
default: null,
|
|
132
|
+
label: 'Decimales',
|
|
133
|
+
description: 'Decimales de los importes.',
|
|
134
|
+
choices: [
|
|
135
|
+
{ value: null, label: 'Según la moneda (USD 2, JPY 0)' },
|
|
136
|
+
{ value: 0, label: '0' },
|
|
137
|
+
{ value: 1, label: '1' },
|
|
138
|
+
{ value: 2, label: '2' },
|
|
139
|
+
{ value: 3, label: '3' },
|
|
140
|
+
{ value: 4, label: '4' },
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Shirkasoft UI Components.
|
|
147
|
+
* Authors: Roberto Valladares Martin (Ansem13), Julio César García García (Sanjuli14).
|
|
148
|
+
* Belongs to Shirkasoft.
|
|
149
|
+
*/
|
|
150
|
+
/** Features collected from `provideShk(...)`. */
|
|
151
|
+
const SHK_CONFIG_FEATURES = new InjectionToken('SHK_CONFIG_FEATURES');
|
|
152
|
+
const SHK_CONFIG_PERSISTENCE = new InjectionToken('SHK_CONFIG_PERSISTENCE');
|
|
153
|
+
/**
|
|
154
|
+
* Registry of the library's configurable options ("nomenclador" de opciones).
|
|
155
|
+
*
|
|
156
|
+
* Every option has a key (`dates.format`), a module, a type and a default.
|
|
157
|
+
* Components read options as signals, so changing one at runtime updates every
|
|
158
|
+
* component that uses it. A settings screen can list them with `options()`.
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* const config = inject(ShkConfigService);
|
|
162
|
+
* config.get('dates.format')(); // 'dd/MM/yyyy'
|
|
163
|
+
* config.set('numbers.currency', 'EUR'); // every price input/table updates
|
|
164
|
+
* config.options(); // definitions + current values
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
class ShkConfigService {
|
|
168
|
+
constructor() {
|
|
169
|
+
this.win = inject(DOCUMENT).defaultView;
|
|
170
|
+
this.persistence = inject(SHK_CONFIG_PERSISTENCE, { optional: true });
|
|
171
|
+
/** Registered definitions, by key. */
|
|
172
|
+
this.defs = signal(new Map(), ...(ngDevMode ? [{ debugName: "defs" }] : /* istanbul ignore next */ []));
|
|
173
|
+
/** Values set explicitly (anything else falls back to the definition's default). */
|
|
174
|
+
this.values = signal({}, ...(ngDevMode ? [{ debugName: "values" }] : /* istanbul ignore next */ []));
|
|
175
|
+
/** Options whose value lives elsewhere. */
|
|
176
|
+
this.sources = signal(new Map(), ...(ngDevMode ? [{ debugName: "sources" }] : /* istanbul ignore next */ []));
|
|
177
|
+
/** Stored values whose option is not registered yet; applied on `register`. */
|
|
178
|
+
this.pending = {};
|
|
179
|
+
/** Cached per-key signals returned by `get`. */
|
|
180
|
+
this.cache = new Map();
|
|
181
|
+
/** Every registered option with its current value, in registration order. */
|
|
182
|
+
this.options = computed(() => [...this.defs().values()].map((def) => {
|
|
183
|
+
const value = this.read(def.key);
|
|
184
|
+
return { ...def, value, isDefault: sameValue(value, def.default) };
|
|
185
|
+
}), ...(ngDevMode ? [{ debugName: "options" }] : /* istanbul ignore next */ []));
|
|
186
|
+
/** Module names in registration order (`dates`, `numbers`, `theme`, …). */
|
|
187
|
+
this.modules = computed(() => [
|
|
188
|
+
...new Set([...this.defs().values()].map((def) => def.module)),
|
|
189
|
+
], ...(ngDevMode ? [{ debugName: "modules" }] : /* istanbul ignore next */ []));
|
|
190
|
+
this.pending = this.loadStored();
|
|
191
|
+
this.register([...DATE_OPTIONS, ...NUMBER_OPTIONS]);
|
|
192
|
+
for (const feature of inject(SHK_CONFIG_FEATURES, { optional: true }) ?? []) {
|
|
193
|
+
if (feature.options)
|
|
194
|
+
this.register(feature.options);
|
|
195
|
+
if (feature.defaults)
|
|
196
|
+
this.setDefaults(feature.defaults);
|
|
197
|
+
}
|
|
198
|
+
if (this.persistence) {
|
|
199
|
+
effect(() => this.save(this.values()));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
get(key) {
|
|
203
|
+
this.requireDef(key);
|
|
204
|
+
let sig = this.cache.get(key);
|
|
205
|
+
if (!sig) {
|
|
206
|
+
sig = computed(() => this.read(key));
|
|
207
|
+
this.cache.set(key, sig);
|
|
208
|
+
}
|
|
209
|
+
return sig;
|
|
210
|
+
}
|
|
211
|
+
/** Whether an option is registered (e.g. `theme.*` only exists with `withTheme()`). */
|
|
212
|
+
has(key) {
|
|
213
|
+
return untracked(this.defs).has(key);
|
|
214
|
+
}
|
|
215
|
+
value(key) {
|
|
216
|
+
return untracked(this.get(key));
|
|
217
|
+
}
|
|
218
|
+
set(key, value) {
|
|
219
|
+
const def = this.requireDef(key);
|
|
220
|
+
const valid = this.validate(def, value);
|
|
221
|
+
const source = untracked(this.sources).get(key);
|
|
222
|
+
if (source) {
|
|
223
|
+
source.set(valid);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
this.values.update((current) => ({ ...current, [key]: valid }));
|
|
227
|
+
}
|
|
228
|
+
/** Restores one option (or all of them) to its default. */
|
|
229
|
+
reset(key) {
|
|
230
|
+
const keys = key ? [key] : [...untracked(this.defs).keys()];
|
|
231
|
+
for (const k of keys) {
|
|
232
|
+
const def = this.requireDef(k);
|
|
233
|
+
const source = untracked(this.sources).get(k);
|
|
234
|
+
if (source)
|
|
235
|
+
source.set(def.default);
|
|
236
|
+
}
|
|
237
|
+
this.values.update((current) => {
|
|
238
|
+
if (!key)
|
|
239
|
+
return {};
|
|
240
|
+
const { [key]: _removed, ...rest } = current;
|
|
241
|
+
return rest;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Adds option definitions (or replaces existing ones with the same key).
|
|
246
|
+
* Stored values for them are applied if they are still valid.
|
|
247
|
+
*/
|
|
248
|
+
register(defs) {
|
|
249
|
+
this.defs.update((current) => {
|
|
250
|
+
const next = new Map(current);
|
|
251
|
+
for (const def of defs)
|
|
252
|
+
next.set(def.key, def);
|
|
253
|
+
return next;
|
|
254
|
+
});
|
|
255
|
+
for (const def of defs) {
|
|
256
|
+
if (!(def.key in this.pending))
|
|
257
|
+
continue;
|
|
258
|
+
const stored = this.pending[def.key];
|
|
259
|
+
delete this.pending[def.key];
|
|
260
|
+
if (def.persist === false)
|
|
261
|
+
continue;
|
|
262
|
+
try {
|
|
263
|
+
const valid = this.validate(def, stored);
|
|
264
|
+
this.values.update((current) => ({ ...current, [def.key]: valid }));
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
// Stale or hand-edited storage: ignore the value, keep the default.
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Binds an option to a value held elsewhere (used by `withTheme()` to share
|
|
273
|
+
* `theme.name`/`theme.scheme` with `ShkThemeService`).
|
|
274
|
+
*/
|
|
275
|
+
bind(key, source) {
|
|
276
|
+
this.requireDef(key);
|
|
277
|
+
this.sources.update((current) => new Map(current).set(key, source));
|
|
278
|
+
}
|
|
279
|
+
/** Changes the defaults of registered options (`reset` returns to these). */
|
|
280
|
+
setDefaults(defaults) {
|
|
281
|
+
const updated = [];
|
|
282
|
+
for (const [key, value] of Object.entries(defaults)) {
|
|
283
|
+
if (value === undefined)
|
|
284
|
+
continue;
|
|
285
|
+
const def = this.requireDef(key);
|
|
286
|
+
updated.push({ ...def, default: this.validate(def, value) });
|
|
287
|
+
}
|
|
288
|
+
this.defs.update((current) => {
|
|
289
|
+
const next = new Map(current);
|
|
290
|
+
for (const def of updated)
|
|
291
|
+
next.set(def.key, def);
|
|
292
|
+
return next;
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
read(key) {
|
|
296
|
+
const source = this.sources().get(key);
|
|
297
|
+
if (source)
|
|
298
|
+
return source.value();
|
|
299
|
+
const values = this.values();
|
|
300
|
+
return key in values ? values[key] : this.defs().get(key)?.default;
|
|
301
|
+
}
|
|
302
|
+
requireDef(key) {
|
|
303
|
+
const def = untracked(this.defs).get(key);
|
|
304
|
+
if (!def) {
|
|
305
|
+
throw new Error(`[shk-config] Unknown option "${key}". Register it with withOptions() or ShkConfigService.register().`);
|
|
306
|
+
}
|
|
307
|
+
return def;
|
|
308
|
+
}
|
|
309
|
+
/** Returns the value coerced to the option's type, or throws if it is not acceptable. */
|
|
310
|
+
validate(def, value) {
|
|
311
|
+
const fail = (why) => {
|
|
312
|
+
throw new Error(`[shk-config] Invalid value for "${def.key}": ${why}.`);
|
|
313
|
+
};
|
|
314
|
+
switch (def.type) {
|
|
315
|
+
case 'boolean':
|
|
316
|
+
if (typeof value !== 'boolean')
|
|
317
|
+
fail('expected a boolean');
|
|
318
|
+
return value;
|
|
319
|
+
case 'number': {
|
|
320
|
+
const n = typeof value === 'string' && value.trim() !== '' ? Number(value) : value;
|
|
321
|
+
if (typeof n !== 'number' || !isFinite(n))
|
|
322
|
+
fail('expected a number');
|
|
323
|
+
if (def.min !== undefined && n < def.min)
|
|
324
|
+
fail(`minimum is ${def.min}`);
|
|
325
|
+
if (def.max !== undefined && n > def.max)
|
|
326
|
+
fail(`maximum is ${def.max}`);
|
|
327
|
+
return n;
|
|
328
|
+
}
|
|
329
|
+
case 'color':
|
|
330
|
+
if (typeof value !== 'string')
|
|
331
|
+
fail('expected a color string');
|
|
332
|
+
if (value !== '' && !/^#([0-9a-f]{3}|[0-9a-f]{6})$/i.test(value)) {
|
|
333
|
+
fail('expected a hex color such as #2563eb (or empty)');
|
|
334
|
+
}
|
|
335
|
+
return value;
|
|
336
|
+
case 'select': {
|
|
337
|
+
const match = def.choices?.find((choice) => sameValue(choice.value, value));
|
|
338
|
+
if (match)
|
|
339
|
+
return match.value;
|
|
340
|
+
if (def.allowCustom && typeof value === 'string' && value.trim() !== '') {
|
|
341
|
+
return value.trim();
|
|
342
|
+
}
|
|
343
|
+
return fail('not one of the allowed choices');
|
|
344
|
+
}
|
|
345
|
+
default:
|
|
346
|
+
if (typeof value !== 'string')
|
|
347
|
+
fail('expected a string');
|
|
348
|
+
return value;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
loadStored() {
|
|
352
|
+
if (!this.persistence)
|
|
353
|
+
return {};
|
|
354
|
+
try {
|
|
355
|
+
const raw = this.win?.localStorage?.getItem(this.persistence.storageKey);
|
|
356
|
+
const parsed = raw ? JSON.parse(raw) : null;
|
|
357
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {};
|
|
358
|
+
}
|
|
359
|
+
catch {
|
|
360
|
+
return {};
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
save(values) {
|
|
364
|
+
if (!this.persistence)
|
|
365
|
+
return;
|
|
366
|
+
const defs = untracked(this.defs);
|
|
367
|
+
// Keep stored values of options that are not registered yet (registered later).
|
|
368
|
+
const stored = { ...this.pending };
|
|
369
|
+
for (const [key, value] of Object.entries(values)) {
|
|
370
|
+
if (defs.get(key)?.persist !== false)
|
|
371
|
+
stored[key] = value;
|
|
372
|
+
}
|
|
373
|
+
try {
|
|
374
|
+
this.win?.localStorage?.setItem(this.persistence.storageKey, JSON.stringify(stored));
|
|
375
|
+
}
|
|
376
|
+
catch {
|
|
377
|
+
// Storage can be blocked (private mode, quota); the option still applies.
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.27", ngImport: i0, type: ShkConfigService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
381
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.27", ngImport: i0, type: ShkConfigService, providedIn: 'root' }); }
|
|
382
|
+
}
|
|
383
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.27", ngImport: i0, type: ShkConfigService, decorators: [{
|
|
384
|
+
type: Injectable,
|
|
385
|
+
args: [{ providedIn: 'root' }]
|
|
386
|
+
}], ctorParameters: () => [] });
|
|
387
|
+
/** Value equality for option values (primitives and `null`). */
|
|
388
|
+
function sameValue(a, b) {
|
|
389
|
+
return a === b || (Number.isNaN(a) && Number.isNaN(b));
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Shirkasoft UI Components.
|
|
394
|
+
* Authors: Roberto Valladares Martin (Ansem13), Julio César García García (Sanjuli14).
|
|
395
|
+
* Belongs to Shirkasoft.
|
|
396
|
+
*
|
|
397
|
+
* Builds the `--shk-primary-*` scale from a single color with CSS `color-mix()`,
|
|
398
|
+
* so the browser derives every shade and no color math ships in JavaScript.
|
|
399
|
+
*/
|
|
400
|
+
const HEX = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
401
|
+
const THEME_NAME = /^[a-z][a-z0-9-]*$/i;
|
|
402
|
+
const CSS_VAR_NAME = /^--shk-[a-z0-9-]+$/i;
|
|
403
|
+
/** How much of the base color each shade keeps, mixed with white (light) or black (dark). */
|
|
404
|
+
const SHADES = [
|
|
405
|
+
[50, 6, 'white'],
|
|
406
|
+
[100, 12, 'white'],
|
|
407
|
+
[200, 24, 'white'],
|
|
408
|
+
[300, 42, 'white'],
|
|
409
|
+
[400, 66, 'white'],
|
|
410
|
+
[500, 85, 'white'],
|
|
411
|
+
[600, 100, null],
|
|
412
|
+
[700, 84, 'black'],
|
|
413
|
+
[800, 68, 'black'],
|
|
414
|
+
[900, 54, 'black'],
|
|
415
|
+
[950, 36, 'black'],
|
|
416
|
+
];
|
|
417
|
+
/** Custom properties a primary color controls (the scale + the decorative accent). */
|
|
418
|
+
const PRIMARY_VARS = [
|
|
419
|
+
...SHADES.map(([shade]) => `--shk-primary-${shade}`),
|
|
420
|
+
'--shk-emerald-400',
|
|
421
|
+
'--shk-emerald-600',
|
|
422
|
+
'--shk-emerald-700',
|
|
423
|
+
];
|
|
424
|
+
/** Throws when `color` is not a hex color. */
|
|
425
|
+
function assertHexColor(color, where) {
|
|
426
|
+
if (!HEX.test(color)) {
|
|
427
|
+
throw new Error(`[shk-config] ${where}: "${color}" is not a hex color such as #2563eb.`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/** `--shk-primary-50` … `--shk-primary-950` (and the accent vars) derived from one color. */
|
|
431
|
+
function primaryScale(color) {
|
|
432
|
+
const vars = {};
|
|
433
|
+
for (const [shade, base, mixWith] of SHADES) {
|
|
434
|
+
vars[`--shk-primary-${shade}`] = mixWith
|
|
435
|
+
? `color-mix(in oklab, ${color} ${base}%, ${mixWith})`
|
|
436
|
+
: color;
|
|
437
|
+
}
|
|
438
|
+
// Themes keep the decorative gradient in the accent color.
|
|
439
|
+
vars['--shk-emerald-400'] = vars['--shk-primary-400'];
|
|
440
|
+
vars['--shk-emerald-600'] = vars['--shk-primary-600'];
|
|
441
|
+
vars['--shk-emerald-700'] = vars['--shk-primary-700'];
|
|
442
|
+
return vars;
|
|
443
|
+
}
|
|
444
|
+
/** Normalizes `'#2563eb' | { primary }` and validates name and colors. */
|
|
445
|
+
function normalizeCustomTheme(name, theme) {
|
|
446
|
+
if (!THEME_NAME.test(name)) {
|
|
447
|
+
throw new Error(`[shk-config] Theme name "${name}" must start with a letter and contain only letters, digits and dashes.`);
|
|
448
|
+
}
|
|
449
|
+
const normalized = typeof theme === 'string' ? { primary: theme } : theme;
|
|
450
|
+
assertHexColor(normalized.primary, `theme "${name}"`);
|
|
451
|
+
for (const [prop, value] of Object.entries(normalized.vars ?? {})) {
|
|
452
|
+
// The values are written into a CSS rule: reject anything that could end it.
|
|
453
|
+
if (!CSS_VAR_NAME.test(prop) || typeof value !== 'string' || /[;{}<>]/.test(value)) {
|
|
454
|
+
throw new Error(`[shk-config] theme "${name}": invalid variable ${prop}: ${String(value)} (names are --shk-…; values cannot contain ; { } < >).`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return normalized;
|
|
458
|
+
}
|
|
459
|
+
/** CSS rule that activates a custom theme through `<html data-shk-theme="name">`. */
|
|
460
|
+
function customThemeCss(name, theme, attribute) {
|
|
461
|
+
const vars = { ...primaryScale(theme.primary), ...(theme.vars ?? {}) };
|
|
462
|
+
const body = Object.entries(vars)
|
|
463
|
+
.map(([prop, value]) => ` ${prop}: ${value};`)
|
|
464
|
+
.join('\n');
|
|
465
|
+
return `[${attribute}='${name}'] {\n${body}\n}`;
|
|
466
|
+
}
|
|
467
|
+
/** Neutral palettes for `theme.surface` (Tailwind scales, 50 … 900). */
|
|
468
|
+
const SURFACE_PALETTES = {
|
|
469
|
+
slate: {
|
|
470
|
+
label: 'Slate',
|
|
471
|
+
scale: ['#f8fafc', '#f1f5f9', '#e2e8f0', '#cbd5e1', '#94a3b8', '#64748b', '#475569', '#334155', '#1e293b', '#0f172a'],
|
|
472
|
+
},
|
|
473
|
+
gray: {
|
|
474
|
+
label: 'Gray',
|
|
475
|
+
scale: ['#f9fafb', '#f3f4f6', '#e5e7eb', '#d1d5db', '#9ca3af', '#6b7280', '#4b5563', '#374151', '#1f2937', '#111827'],
|
|
476
|
+
},
|
|
477
|
+
zinc: {
|
|
478
|
+
label: 'Zinc',
|
|
479
|
+
scale: ['#fafafa', '#f4f4f5', '#e4e4e7', '#d4d4d8', '#a1a1aa', '#71717a', '#52525b', '#3f3f46', '#27272a', '#18181b'],
|
|
480
|
+
},
|
|
481
|
+
neutral: {
|
|
482
|
+
label: 'Neutral',
|
|
483
|
+
scale: ['#fafafa', '#f5f5f5', '#e5e5e5', '#d4d4d4', '#a3a3a3', '#737373', '#525252', '#404040', '#262626', '#171717'],
|
|
484
|
+
},
|
|
485
|
+
stone: {
|
|
486
|
+
label: 'Stone',
|
|
487
|
+
scale: ['#fafaf9', '#f5f5f4', '#e7e5e4', '#d6d3d1', '#a8a29e', '#78716c', '#57534e', '#44403c', '#292524', '#1c1917'],
|
|
488
|
+
},
|
|
489
|
+
};
|
|
490
|
+
const SURFACE_STEPS = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900];
|
|
491
|
+
/**
|
|
492
|
+
* CSS for every surface palette, activated with `<html data-shk-surface="slate">`.
|
|
493
|
+
* Dark mode uses the reversed scale (50 ↔ 900), like the built-in themes. The
|
|
494
|
+
* doubled attribute selector out-ranks the theme packs (`html.dark[data-shk-theme]`).
|
|
495
|
+
*/
|
|
496
|
+
function surfaceCss(attribute = 'data-shk-surface') {
|
|
497
|
+
return Object.entries(SURFACE_PALETTES)
|
|
498
|
+
.map(([name, { scale }]) => {
|
|
499
|
+
const sel = `html[${attribute}='${name}'][${attribute}]`;
|
|
500
|
+
const light = SURFACE_STEPS.map((step, i) => `--shk-surface-${step}: ${scale[i]};`).join(' ');
|
|
501
|
+
const dark = SURFACE_STEPS.map((step, i) => `--shk-surface-${step}: ${scale[9 - i]};`).join(' ');
|
|
502
|
+
return (`${sel} { ${light} --shk-scrollbar-thumb: ${scale[3]}; --shk-scrollbar-thumb-hover: ${scale[4]}; }\n` +
|
|
503
|
+
`html.dark[${attribute}='${name}'][${attribute}] { ${dark} --shk-scrollbar-thumb: ${scale[6]}; --shk-scrollbar-thumb-hover: ${scale[5]}; }`);
|
|
504
|
+
})
|
|
505
|
+
.join('\n');
|
|
506
|
+
}
|
|
507
|
+
/** The built-in theme packs as presets. */
|
|
508
|
+
const BUILT_IN_PRESETS = [
|
|
509
|
+
{ name: 'default', label: 'Esmeralda', primary: '#059669', surface: 'zinc' },
|
|
510
|
+
{ name: 'ocean', label: 'Océano', primary: '#0284c7', surface: 'slate' },
|
|
511
|
+
{ name: 'violet', label: 'Violeta', primary: '#7c3aed', surface: 'zinc' },
|
|
512
|
+
{ name: 'sunset', label: 'Atardecer', primary: '#ea580c', surface: 'stone' },
|
|
513
|
+
];
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Shirkasoft UI Components.
|
|
517
|
+
* Authors: Roberto Valladares Martin (Ansem13), Julio César García García (Sanjuli14).
|
|
518
|
+
* Belongs to Shirkasoft.
|
|
519
|
+
*/
|
|
520
|
+
/**
|
|
521
|
+
* Theme packs offered as presets (built-in + `withTheme({ themes })`), with the
|
|
522
|
+
* primary color and surface each one uses. Read by `shk-theme-picker`.
|
|
523
|
+
*/
|
|
524
|
+
const SHK_THEME_PRESETS = new InjectionToken('SHK_THEME_PRESETS', {
|
|
525
|
+
providedIn: 'root',
|
|
526
|
+
factory: () => BUILT_IN_PRESETS,
|
|
527
|
+
});
|
|
528
|
+
/** Attribute on `<html>` that activates a `theme.surface` palette. */
|
|
529
|
+
const SURFACE_ATTRIBUTE = 'data-shk-surface';
|
|
530
|
+
function feature(value, extra = []) {
|
|
531
|
+
return {
|
|
532
|
+
ɵproviders: [{ provide: SHK_CONFIG_FEATURES, multi: true, useValue: value }, ...extra],
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
/** Drops `undefined` entries and prefixes the keys with the module name. */
|
|
536
|
+
function defaultsOf(module, values) {
|
|
537
|
+
const out = {};
|
|
538
|
+
for (const [name, value] of Object.entries(values)) {
|
|
539
|
+
if (value !== undefined)
|
|
540
|
+
out[`${module}.${name}`] = value;
|
|
541
|
+
}
|
|
542
|
+
return out;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Configures the library. Every module is optional; component inputs still
|
|
546
|
+
* override the configured values.
|
|
547
|
+
*
|
|
548
|
+
* ```ts
|
|
549
|
+
* bootstrapApplication(App, {
|
|
550
|
+
* providers: [
|
|
551
|
+
* provideShk(
|
|
552
|
+
* withDates({ locale: 'es', format: 'dd/MM/yyyy' }),
|
|
553
|
+
* withNumbers({ currency: 'EUR', locale: 'es-ES' }),
|
|
554
|
+
* withTheme({ defaultTheme: 'marca', themes: { marca: '#2563eb' } }),
|
|
555
|
+
* withPersistence(),
|
|
556
|
+
* ),
|
|
557
|
+
* ],
|
|
558
|
+
* });
|
|
559
|
+
* ```
|
|
560
|
+
*/
|
|
561
|
+
function provideShk(...features) {
|
|
562
|
+
return makeEnvironmentProviders(features.flatMap((f) => f.ɵproviders));
|
|
563
|
+
}
|
|
564
|
+
/** Date module: locale, pattern, display style, 12/24 h and first day of the week. */
|
|
565
|
+
function withDates(config) {
|
|
566
|
+
return feature({ defaults: defaultsOf('dates', config) });
|
|
567
|
+
}
|
|
568
|
+
/** Number module: separators locale, currency, currency symbol and decimals. */
|
|
569
|
+
function withNumbers(config) {
|
|
570
|
+
return feature({ defaults: defaultsOf('numbers', config) });
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Theme module: built-in and custom themes, light/dark/system scheme and a
|
|
574
|
+
* runtime primary color. Replaces `provideShkTheme()` (do not use both).
|
|
575
|
+
*
|
|
576
|
+
* `theme.name` and `theme.scheme` are shared with `ShkThemeService` (which
|
|
577
|
+
* persists them); `theme.primary` is stored by `withPersistence()`.
|
|
578
|
+
*/
|
|
579
|
+
function withTheme(config = {}) {
|
|
580
|
+
const { themes = {}, primary = '', surface = '', ...themeConfig } = config;
|
|
581
|
+
const resolved = { ...DEFAULT_SHK_THEME_CONFIG, ...themeConfig };
|
|
582
|
+
const custom = Object.entries(themes).map(([name, theme]) => [name, normalizeCustomTheme(name, theme)]);
|
|
583
|
+
if (primary)
|
|
584
|
+
assertHexColor(primary, 'withTheme({ primary })');
|
|
585
|
+
const presets = [
|
|
586
|
+
...BUILT_IN_PRESETS,
|
|
587
|
+
...custom.map(([name, theme]) => ({
|
|
588
|
+
name,
|
|
589
|
+
label: theme.label ?? name,
|
|
590
|
+
primary: theme.primary,
|
|
591
|
+
surface: '',
|
|
592
|
+
})),
|
|
593
|
+
];
|
|
594
|
+
const options = [
|
|
595
|
+
{
|
|
596
|
+
key: 'theme.name',
|
|
597
|
+
module: 'theme',
|
|
598
|
+
type: 'select',
|
|
599
|
+
default: resolved.defaultTheme,
|
|
600
|
+
label: 'Tema',
|
|
601
|
+
description: 'Paleta de colores de la interfaz.',
|
|
602
|
+
choices: presets.map((preset) => ({ value: preset.name, label: preset.label })),
|
|
603
|
+
persist: false,
|
|
604
|
+
},
|
|
605
|
+
{
|
|
606
|
+
key: 'theme.scheme',
|
|
607
|
+
module: 'theme',
|
|
608
|
+
type: 'select',
|
|
609
|
+
default: resolved.defaultScheme,
|
|
610
|
+
label: 'Modo',
|
|
611
|
+
choices: [
|
|
612
|
+
{ value: 'system', label: 'Según el sistema' },
|
|
613
|
+
{ value: 'light', label: 'Claro' },
|
|
614
|
+
{ value: 'dark', label: 'Oscuro' },
|
|
615
|
+
],
|
|
616
|
+
persist: false,
|
|
617
|
+
},
|
|
618
|
+
{
|
|
619
|
+
key: 'theme.primary',
|
|
620
|
+
module: 'theme',
|
|
621
|
+
type: 'color',
|
|
622
|
+
default: primary,
|
|
623
|
+
label: 'Color principal',
|
|
624
|
+
description: 'Sustituye el color principal del tema. Vacío = el del tema.',
|
|
625
|
+
},
|
|
626
|
+
{
|
|
627
|
+
key: 'theme.surface',
|
|
628
|
+
module: 'theme',
|
|
629
|
+
type: 'select',
|
|
630
|
+
default: surface,
|
|
631
|
+
label: 'Superficie',
|
|
632
|
+
description: 'Gama de grises de fondos, bordes y textos. Vacío = la del tema.',
|
|
633
|
+
choices: [
|
|
634
|
+
{ value: '', label: 'La del tema' },
|
|
635
|
+
...Object.entries(SURFACE_PALETTES).map(([value, { label }]) => ({ value, label })),
|
|
636
|
+
],
|
|
637
|
+
},
|
|
638
|
+
];
|
|
639
|
+
return feature({ options }, [
|
|
640
|
+
{ provide: SHK_THEME_CONFIG, useValue: resolved },
|
|
641
|
+
{ provide: SHK_THEME_PRESETS, useValue: presets },
|
|
642
|
+
provideAppInitializer(() => {
|
|
643
|
+
const doc = inject(DOCUMENT);
|
|
644
|
+
const themeService = inject(ShkThemeService);
|
|
645
|
+
const configService = inject(ShkConfigService);
|
|
646
|
+
const nonce = inject(CSP_NONCE, { optional: true });
|
|
647
|
+
// Custom themes + surface palettes, as one stylesheet.
|
|
648
|
+
const css = [
|
|
649
|
+
surfaceCss(SURFACE_ATTRIBUTE),
|
|
650
|
+
...custom.map(([name, theme]) => customThemeCss(name, theme, resolved.attribute)),
|
|
651
|
+
].join('\n');
|
|
652
|
+
let style = doc.head?.querySelector('style[data-shk-themes]');
|
|
653
|
+
if (!style && doc.head) {
|
|
654
|
+
style = doc.createElement('style');
|
|
655
|
+
style.setAttribute('data-shk-themes', '');
|
|
656
|
+
// Lets the rules through a strict Content-Security-Policy (style-src 'nonce-…').
|
|
657
|
+
if (nonce)
|
|
658
|
+
style.setAttribute('nonce', nonce);
|
|
659
|
+
doc.head.appendChild(style);
|
|
660
|
+
}
|
|
661
|
+
if (style)
|
|
662
|
+
style.textContent = css;
|
|
663
|
+
configService.bind('theme.name', {
|
|
664
|
+
value: themeService.theme,
|
|
665
|
+
set: (name) => themeService.setTheme(name),
|
|
666
|
+
});
|
|
667
|
+
configService.bind('theme.scheme', {
|
|
668
|
+
value: themeService.scheme,
|
|
669
|
+
set: (scheme) => themeService.setScheme(scheme),
|
|
670
|
+
});
|
|
671
|
+
// Runtime primary color: inline custom properties on <html> win over any theme.
|
|
672
|
+
const root = doc.documentElement;
|
|
673
|
+
effect(() => {
|
|
674
|
+
const color = configService.get('theme.primary')();
|
|
675
|
+
if (color) {
|
|
676
|
+
for (const [prop, value] of Object.entries(primaryScale(color))) {
|
|
677
|
+
root.style.setProperty(prop, value);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
else {
|
|
681
|
+
for (const prop of PRIMARY_VARS)
|
|
682
|
+
root.style.removeProperty(prop);
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
// Runtime surface palette: an attribute selector, so light and dark both apply.
|
|
686
|
+
effect(() => {
|
|
687
|
+
const palette = configService.get('theme.surface')();
|
|
688
|
+
if (palette)
|
|
689
|
+
root.setAttribute(SURFACE_ATTRIBUTE, palette);
|
|
690
|
+
else
|
|
691
|
+
root.removeAttribute(SURFACE_ATTRIBUTE);
|
|
692
|
+
});
|
|
693
|
+
}),
|
|
694
|
+
]);
|
|
695
|
+
}
|
|
696
|
+
/** Registers app-defined options in the same registry (`app.pageSize`, …). */
|
|
697
|
+
function withOptions(options) {
|
|
698
|
+
return feature({ options });
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Persists changed options in localStorage so they survive reloads.
|
|
702
|
+
* Options with `persist: false` are skipped (the theme service stores its own).
|
|
703
|
+
*/
|
|
704
|
+
function withPersistence(config = {}) {
|
|
705
|
+
return {
|
|
706
|
+
ɵproviders: [
|
|
707
|
+
{
|
|
708
|
+
provide: SHK_CONFIG_PERSISTENCE,
|
|
709
|
+
useValue: { storageKey: config.storageKey ?? 'shk-config' },
|
|
710
|
+
},
|
|
711
|
+
],
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
* Shirkasoft UI Components.
|
|
717
|
+
* Authors: Roberto Valladares Martin (Ansem13), Julio César García García (Sanjuli14).
|
|
718
|
+
* Belongs to Shirkasoft.
|
|
719
|
+
*
|
|
720
|
+
* Locale-aware formatting helpers used by the components and available to apps.
|
|
721
|
+
* Pure functions on top of `Intl`: no Angular locale data needs registering.
|
|
722
|
+
*/
|
|
723
|
+
/** `Intl` formatters are expensive to build; reuse them per locale/options. */
|
|
724
|
+
const formatterCache = new Map();
|
|
725
|
+
function dateFormatter(locale, options) {
|
|
726
|
+
const key = `d|${locale}|${JSON.stringify(options)}`;
|
|
727
|
+
let fmt = formatterCache.get(key);
|
|
728
|
+
if (!fmt) {
|
|
729
|
+
fmt = new Intl.DateTimeFormat(safeLocale(locale), options);
|
|
730
|
+
formatterCache.set(key, fmt);
|
|
731
|
+
}
|
|
732
|
+
return fmt;
|
|
733
|
+
}
|
|
734
|
+
function numberFormatter(locale, options) {
|
|
735
|
+
const key = `n|${locale}|${JSON.stringify(options)}`;
|
|
736
|
+
let fmt = formatterCache.get(key);
|
|
737
|
+
if (!fmt) {
|
|
738
|
+
fmt = new Intl.NumberFormat(safeLocale(locale), options);
|
|
739
|
+
formatterCache.set(key, fmt);
|
|
740
|
+
}
|
|
741
|
+
return fmt;
|
|
742
|
+
}
|
|
743
|
+
/** Falls back to the runtime default when the tag is not a valid BCP 47 locale. */
|
|
744
|
+
function safeLocale(locale) {
|
|
745
|
+
try {
|
|
746
|
+
return Intl.getCanonicalLocales(locale)[0];
|
|
747
|
+
}
|
|
748
|
+
catch {
|
|
749
|
+
return undefined;
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
/** Default numeric pattern for a locale: `MM/dd/yyyy` for English, `dd/MM/yyyy` otherwise. */
|
|
753
|
+
function resolveDateFormat(format, locale) {
|
|
754
|
+
if (format)
|
|
755
|
+
return format;
|
|
756
|
+
return locale.toLowerCase().startsWith('en') ? 'MM/dd/yyyy' : 'dd/MM/yyyy';
|
|
757
|
+
}
|
|
758
|
+
/** Converts a value to a `Date`. ISO date-only strings (`2026-12-31`) are read as local dates. */
|
|
759
|
+
function toDate(value) {
|
|
760
|
+
if (value === null || value === undefined || value === '')
|
|
761
|
+
return null;
|
|
762
|
+
if (value instanceof Date)
|
|
763
|
+
return isNaN(value.getTime()) ? null : value;
|
|
764
|
+
if (typeof value === 'string') {
|
|
765
|
+
const dateOnly = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(value.trim());
|
|
766
|
+
if (dateOnly) {
|
|
767
|
+
return new Date(Number(dateOnly[1]), Number(dateOnly[2]) - 1, Number(dateOnly[3]));
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
const date = new Date(value);
|
|
771
|
+
return isNaN(date.getTime()) ? null : date;
|
|
772
|
+
}
|
|
773
|
+
const pad = (n, width = 2) => String(n).padStart(width, '0');
|
|
774
|
+
/**
|
|
775
|
+
* Pattern tokens, longest first. Text between single quotes is copied verbatim.
|
|
776
|
+
* Compatible with the common Angular `DatePipe` tokens (`y` = full year).
|
|
777
|
+
*/
|
|
778
|
+
const DATE_TOKENS = /'[^']*'|yyyy|yy|y|MMMM|MMM|MM|M|dd|d|EEEE|EEE|HH|H|hh|h|mm|ss|a/g;
|
|
779
|
+
/**
|
|
780
|
+
* Formats a date with a pattern (`dd/MM/yyyy`, `d MMM yyyy`, `yyyy-MM-dd HH:mm`…).
|
|
781
|
+
*
|
|
782
|
+
* Tokens: `yyyy y yy` year · `MMMM MMM MM M` month · `dd d` day · `EEEE EEE` weekday ·
|
|
783
|
+
* `HH H` 0-23 · `hh h` 1-12 · `mm` minutes · `ss` seconds · `a` AM/PM ·
|
|
784
|
+
* `'text'` literal. Month and weekday names follow `locale`, with its own casing
|
|
785
|
+
* ("31 dic 2026" in Spanish, "Dec 31, 2026" in English).
|
|
786
|
+
* Returns an empty string for empty or invalid values.
|
|
787
|
+
*/
|
|
788
|
+
function formatDate(value, pattern, locale) {
|
|
789
|
+
const date = toDate(value);
|
|
790
|
+
if (!date)
|
|
791
|
+
return '';
|
|
792
|
+
const name = (options) => dateFormatter(locale, options).format(date).replace('.', '');
|
|
793
|
+
const h = date.getHours();
|
|
794
|
+
return pattern.replace(DATE_TOKENS, (token) => {
|
|
795
|
+
switch (token) {
|
|
796
|
+
case 'yyyy':
|
|
797
|
+
case 'y': return String(date.getFullYear());
|
|
798
|
+
case 'yy': return pad(date.getFullYear() % 100);
|
|
799
|
+
case 'MMMM': return name({ month: 'long' });
|
|
800
|
+
case 'MMM': return name({ month: 'short' });
|
|
801
|
+
case 'MM': return pad(date.getMonth() + 1);
|
|
802
|
+
case 'M': return String(date.getMonth() + 1);
|
|
803
|
+
case 'dd': return pad(date.getDate());
|
|
804
|
+
case 'd': return String(date.getDate());
|
|
805
|
+
case 'EEEE': return name({ weekday: 'long' });
|
|
806
|
+
case 'EEE': return name({ weekday: 'short' });
|
|
807
|
+
case 'HH': return pad(h);
|
|
808
|
+
case 'H': return String(h);
|
|
809
|
+
case 'hh': return pad(h % 12 || 12);
|
|
810
|
+
case 'h': return String(h % 12 || 12);
|
|
811
|
+
case 'mm': return pad(date.getMinutes());
|
|
812
|
+
case 'ss': return pad(date.getSeconds());
|
|
813
|
+
case 'a': return h < 12 ? 'AM' : 'PM';
|
|
814
|
+
default: return token.slice(1, -1); // 'literal'
|
|
815
|
+
}
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Date formatted with an `Intl` style (`long`: "31 de diciembre de 2026"),
|
|
820
|
+
* optionally followed by the time in `timeStyle`.
|
|
821
|
+
*/
|
|
822
|
+
function formatDateStyle(value, style, locale, timeStyle) {
|
|
823
|
+
const date = toDate(value);
|
|
824
|
+
if (!date)
|
|
825
|
+
return '';
|
|
826
|
+
const options = timeStyle
|
|
827
|
+
? { dateStyle: style, timeStyle }
|
|
828
|
+
: { dateStyle: style };
|
|
829
|
+
return dateFormatter(locale, options).format(date);
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Field order and separator of a pattern, for typing dates in an input.
|
|
833
|
+
* Named months and weekdays are typed as numbers (`d MMM yyyy` → `dd/MM/yyyy`).
|
|
834
|
+
*/
|
|
835
|
+
function typedDateLayout(pattern) {
|
|
836
|
+
const order = [];
|
|
837
|
+
for (const token of pattern.replace(/'[^']*'/g, '').match(/y+|M+|d+/g) ?? []) {
|
|
838
|
+
const field = token[0] === 'y' ? 'y' : token[0] === 'M' ? 'm' : 'd';
|
|
839
|
+
if (!order.includes(field))
|
|
840
|
+
order.push(field);
|
|
841
|
+
}
|
|
842
|
+
for (const field of ['d', 'm', 'y']) {
|
|
843
|
+
if (!order.includes(field))
|
|
844
|
+
order.push(field);
|
|
845
|
+
}
|
|
846
|
+
const separator = /[\/\-.]/.exec(pattern)?.[0] ?? '/';
|
|
847
|
+
return { order, separator };
|
|
848
|
+
}
|
|
849
|
+
/** Narrow currency symbol (`USD` → `$`, `EUR` → `€`). Unknown codes are returned as-is. */
|
|
850
|
+
function currencySymbol(currency, locale) {
|
|
851
|
+
try {
|
|
852
|
+
const parts = numberFormatter(locale, {
|
|
853
|
+
style: 'currency',
|
|
854
|
+
currency,
|
|
855
|
+
currencyDisplay: 'narrowSymbol',
|
|
856
|
+
}).formatToParts(0);
|
|
857
|
+
return parts.find((p) => p.type === 'currency')?.value ?? currency;
|
|
858
|
+
}
|
|
859
|
+
catch {
|
|
860
|
+
return currency;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* Formats an amount as money (`$1,234.50`, `1.234,50 €`, `¥1,000`).
|
|
865
|
+
* `decimals` `null`/omitted = the currency's own digits (2 for USD, 0 for JPY).
|
|
866
|
+
*/
|
|
867
|
+
function formatCurrency(value, currency, locale, decimals = null) {
|
|
868
|
+
const n = typeof value === 'string' ? Number(value) : value;
|
|
869
|
+
if (n === null || n === undefined || !isFinite(n))
|
|
870
|
+
return '';
|
|
871
|
+
const digits = decimals === null ? {} : { minimumFractionDigits: decimals, maximumFractionDigits: decimals };
|
|
872
|
+
try {
|
|
873
|
+
return numberFormatter(locale, { style: 'currency', currency, ...digits }).format(n);
|
|
874
|
+
}
|
|
875
|
+
catch {
|
|
876
|
+
// Unknown currency code: plain number followed by the code.
|
|
877
|
+
const d = decimals ?? 2;
|
|
878
|
+
return `${formatNumber(n, locale, d, d)} ${currency}`;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
/** Decimal digits a currency uses by default (USD → 2, JPY → 0, BHD → 3). Unknown codes → 2. */
|
|
882
|
+
function currencyDecimals(currency) {
|
|
883
|
+
try {
|
|
884
|
+
return numberFormatter('en-US', { style: 'currency', currency }).resolvedOptions()
|
|
885
|
+
.maximumFractionDigits ?? 2;
|
|
886
|
+
}
|
|
887
|
+
catch {
|
|
888
|
+
return 2;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
/** Formats a number with the locale's grouping and decimal separators. */
|
|
892
|
+
function formatNumber(value, locale, maxDecimals = 2, minDecimals = 0) {
|
|
893
|
+
if (!isFinite(value))
|
|
894
|
+
return '';
|
|
895
|
+
return numberFormatter(locale, {
|
|
896
|
+
minimumFractionDigits: minDecimals,
|
|
897
|
+
maximumFractionDigits: maxDecimals,
|
|
898
|
+
}).format(value);
|
|
899
|
+
}
|
|
900
|
+
/** Decimal and grouping separators of a locale (`es-ES` → `,` and `.`). */
|
|
901
|
+
function numberSeparators(locale) {
|
|
902
|
+
const parts = numberFormatter(locale, {}).formatToParts(12345.6);
|
|
903
|
+
return {
|
|
904
|
+
decimal: parts.find((p) => p.type === 'decimal')?.value ?? '.',
|
|
905
|
+
group: parts.find((p) => p.type === 'group')?.value ?? ',',
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/**
|
|
910
|
+
* Generated bundle index. Do not edit.
|
|
911
|
+
*/
|
|
912
|
+
|
|
913
|
+
export { BUILT_IN_PRESETS, DATE_OPTIONS, NUMBER_OPTIONS, SHK_CONFIG_FEATURES, SHK_CONFIG_PERSISTENCE, SHK_THEME_PRESETS, SURFACE_PALETTES, ShkConfigService, currencyDecimals, currencySymbol, formatCurrency, formatDate, formatDateStyle, formatNumber, numberSeparators, primaryScale, provideShk, resolveDateFormat, toDate, typedDateLayout, withDates, withNumbers, withOptions, withPersistence, withTheme };
|
|
914
|
+
//# sourceMappingURL=shirkasoft-ui-components-config.mjs.map
|