cerxos-ui 0.0.15 → 0.0.16

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,5 +1,5 @@
1
1
  import * as i0 from '@angular/core';
2
- import { Component, EventEmitter, ElementRef, HostListener, ViewChildren, ViewChild, Output, Input, ChangeDetectionStrategy, Attribute, forwardRef, Directive, ContentChildren, ContentChild, HostBinding } from '@angular/core';
2
+ import { Component, EventEmitter, ElementRef, HostListener, ViewChildren, ViewChild, Output, Input, ChangeDetectionStrategy, Attribute, forwardRef, Directive, ContentChildren, ContentChild, HostBinding, signal, Injectable, inject, Pipe } from '@angular/core';
3
3
  import { NG_VALUE_ACCESSOR } from '@angular/forms';
4
4
  import * as i1 from '@angular/common';
5
5
  import { NgTemplateOutlet, CommonModule } from '@angular/common';
@@ -4384,6 +4384,180 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.10", ngImpo
4384
4384
  type: Input
4385
4385
  }] } });
4386
4386
 
4387
+ const CXS_USER_SESSION_STORAGE_KEY = 'user_session';
4388
+ const CXS_USER_PREFERENCE_CHANGED_EVENT = 'cxs:user-preference-changed';
4389
+ const DATE_FORMAT_PREFERENCE_KEY = 'ui.dateFormat';
4390
+ const CURRENCY_FORMAT_PREFERENCE_KEY = 'ui.currencyFormat';
4391
+ const DATE_FORMAT_ALIASES = ['dateFormat', 'date_format'];
4392
+ const CURRENCY_FORMAT_ALIASES = ['currencyFormat', 'currency_format', 'currency'];
4393
+ const DEFAULT_DATE_FORMAT = 'mdy';
4394
+ const DEFAULT_CURRENCY_FORMAT = 'usd';
4395
+ const CURRENCY_FORMATS = {
4396
+ usd: { currency: 'USD', locale: 'en-US' },
4397
+ myr: { currency: 'MYR', locale: 'en-MY' },
4398
+ eur: { currency: 'EUR', locale: 'en-IE' },
4399
+ gbp: { currency: 'GBP', locale: 'en-GB' },
4400
+ jpy: { currency: 'JPY', locale: 'ja-JP' }
4401
+ };
4402
+ class CxsPreferenceFormattingService {
4403
+ preferenceVersionSignal = signal(0, ...(ngDevMode ? [{ debugName: "preferenceVersionSignal" }] : /* istanbul ignore next */ []));
4404
+ preferenceVersion = this.preferenceVersionSignal.asReadonly();
4405
+ constructor() {
4406
+ this.addPreferenceListeners();
4407
+ }
4408
+ formatDate(value) {
4409
+ if (value === null || value === undefined || value === '') {
4410
+ return '';
4411
+ }
4412
+ const date = value instanceof Date ? value : this.parseDateString(value);
4413
+ if (!date) {
4414
+ return String(value);
4415
+ }
4416
+ const format = this.dateFormatPreference();
4417
+ const year = date.getFullYear();
4418
+ const month = this.padDatePart(date.getMonth() + 1);
4419
+ const day = this.padDatePart(date.getDate());
4420
+ if (format === 'dmy') {
4421
+ return `${day}/${month}/${year}`;
4422
+ }
4423
+ if (format === 'ymd') {
4424
+ return `${year}-${month}-${day}`;
4425
+ }
4426
+ return `${month}/${day}/${year}`;
4427
+ }
4428
+ formatCurrency(value) {
4429
+ if (value === null || value === undefined || value === '') {
4430
+ return '';
4431
+ }
4432
+ const amount = typeof value === 'number' ? value : Number(value);
4433
+ if (!Number.isFinite(amount)) {
4434
+ return String(value);
4435
+ }
4436
+ const config = CURRENCY_FORMATS[this.currencyFormatPreference()];
4437
+ return new Intl.NumberFormat(config.locale, {
4438
+ currency: config.currency,
4439
+ style: 'currency'
4440
+ }).format(amount);
4441
+ }
4442
+ dateFormatPreference() {
4443
+ const value = this.getPreference(DATE_FORMAT_PREFERENCE_KEY, DATE_FORMAT_ALIASES);
4444
+ return this.isDateFormatPreference(value) ? value : DEFAULT_DATE_FORMAT;
4445
+ }
4446
+ currencyFormatPreference() {
4447
+ const value = this.getPreference(CURRENCY_FORMAT_PREFERENCE_KEY, CURRENCY_FORMAT_ALIASES);
4448
+ return this.isCurrencyFormatPreference(value) ? value : DEFAULT_CURRENCY_FORMAT;
4449
+ }
4450
+ getPreference(key, aliases) {
4451
+ const preferences = this.readSession()?.preferences ?? [];
4452
+ const keys = [key, ...aliases];
4453
+ const preference = keys
4454
+ .map((preferenceKey) => preferences.find((item) => item.key === preferenceKey))
4455
+ .find(Boolean);
4456
+ return preference?.value?.trim().toLowerCase();
4457
+ }
4458
+ readSession() {
4459
+ const storage = this.getLocalStorage();
4460
+ if (!storage) {
4461
+ return null;
4462
+ }
4463
+ const raw = storage.getItem(CXS_USER_SESSION_STORAGE_KEY);
4464
+ if (!raw) {
4465
+ return null;
4466
+ }
4467
+ try {
4468
+ return JSON.parse(raw);
4469
+ }
4470
+ catch {
4471
+ return null;
4472
+ }
4473
+ }
4474
+ parseDateString(value) {
4475
+ const apiDateMatch = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
4476
+ if (apiDateMatch) {
4477
+ const [, year, month, day] = apiDateMatch;
4478
+ return new Date(Number(year), Number(month) - 1, Number(day));
4479
+ }
4480
+ const parsed = new Date(value);
4481
+ return Number.isNaN(parsed.getTime()) ? null : parsed;
4482
+ }
4483
+ padDatePart(value) {
4484
+ return String(value).padStart(2, '0');
4485
+ }
4486
+ isDateFormatPreference(value) {
4487
+ return value === 'mdy' || value === 'dmy' || value === 'ymd';
4488
+ }
4489
+ isCurrencyFormatPreference(value) {
4490
+ return (value === 'usd' ||
4491
+ value === 'myr' ||
4492
+ value === 'eur' ||
4493
+ value === 'gbp' ||
4494
+ value === 'jpy');
4495
+ }
4496
+ addPreferenceListeners() {
4497
+ if (typeof window === 'undefined') {
4498
+ return;
4499
+ }
4500
+ window.addEventListener('storage', (event) => {
4501
+ if (event.key === CXS_USER_SESSION_STORAGE_KEY) {
4502
+ this.bumpPreferenceVersion();
4503
+ }
4504
+ });
4505
+ window.addEventListener(CXS_USER_PREFERENCE_CHANGED_EVENT, () => {
4506
+ this.bumpPreferenceVersion();
4507
+ });
4508
+ }
4509
+ bumpPreferenceVersion() {
4510
+ this.preferenceVersionSignal.update((version) => version + 1);
4511
+ }
4512
+ getLocalStorage() {
4513
+ return typeof localStorage === 'undefined' ? null : localStorage;
4514
+ }
4515
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.10", ngImport: i0, type: CxsPreferenceFormattingService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
4516
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.10", ngImport: i0, type: CxsPreferenceFormattingService, providedIn: 'root' });
4517
+ }
4518
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.10", ngImport: i0, type: CxsPreferenceFormattingService, decorators: [{
4519
+ type: Injectable,
4520
+ args: [{
4521
+ providedIn: 'root'
4522
+ }]
4523
+ }], ctorParameters: () => [] });
4524
+
4525
+ class CxsCurrencyPipe {
4526
+ formatter = inject(CxsPreferenceFormattingService);
4527
+ transform(value) {
4528
+ this.formatter.preferenceVersion();
4529
+ return this.formatter.formatCurrency(value);
4530
+ }
4531
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.10", ngImport: i0, type: CxsCurrencyPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
4532
+ static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "21.2.10", ngImport: i0, type: CxsCurrencyPipe, isStandalone: true, name: "cxsCurrency", pure: false });
4533
+ }
4534
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.10", ngImport: i0, type: CxsCurrencyPipe, decorators: [{
4535
+ type: Pipe,
4536
+ args: [{
4537
+ name: 'cxsCurrency',
4538
+ standalone: true,
4539
+ pure: false
4540
+ }]
4541
+ }] });
4542
+
4543
+ class CxsDatePipe {
4544
+ formatter = inject(CxsPreferenceFormattingService);
4545
+ transform(value) {
4546
+ this.formatter.preferenceVersion();
4547
+ return this.formatter.formatDate(value);
4548
+ }
4549
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.10", ngImport: i0, type: CxsDatePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
4550
+ static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "21.2.10", ngImport: i0, type: CxsDatePipe, isStandalone: true, name: "cxsDate", pure: false });
4551
+ }
4552
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.10", ngImport: i0, type: CxsDatePipe, decorators: [{
4553
+ type: Pipe,
4554
+ args: [{
4555
+ name: 'cxsDate',
4556
+ standalone: true,
4557
+ pure: false
4558
+ }]
4559
+ }] });
4560
+
4387
4561
  /*
4388
4562
  * Public API Surface of cerxos-ui
4389
4563
  */
@@ -4392,5 +4566,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.10", ngImpo
4392
4566
  * Generated bundle index. Do not edit.
4393
4567
  */
4394
4568
 
4395
- export { CerxosUi, CxsAccordionComponent, CxsAccordionItemComponent, CxsActionMenuComponent, CxsAlertComponent, CxsAvatarComponent, CxsBadgeComponent, CxsBreadcrumbComponent, CxsButtonComponent, CxsCardComponent, CxsCarouselComponent, CxsCheckboxComponent, CxsDataTableCellDirective, CxsDataTableComponent, CxsDatepickerComponent, CxsDialogComponent, CxsIconComponent, CxsInputComponent, CxsMenubarComponent, CxsMultiSelectComponent, CxsProgressBarComponent, CxsRadioComponent, CxsSelectComponent, CxsSkeletonComponent, CxsSliderComponent, CxsTabLabelDirective, CxsTabPanelComponent, CxsTableComponent, CxsTabsComponent, CxsThemeDirective, CxsToastComponent, CxsToggleComponent, CxsTooltipComponent };
4569
+ export { CXS_USER_PREFERENCE_CHANGED_EVENT, CXS_USER_SESSION_STORAGE_KEY, CerxosUi, CxsAccordionComponent, CxsAccordionItemComponent, CxsActionMenuComponent, CxsAlertComponent, CxsAvatarComponent, CxsBadgeComponent, CxsBreadcrumbComponent, CxsButtonComponent, CxsCardComponent, CxsCarouselComponent, CxsCheckboxComponent, CxsCurrencyPipe, CxsDataTableCellDirective, CxsDataTableComponent, CxsDatePipe, CxsDatepickerComponent, CxsDialogComponent, CxsIconComponent, CxsInputComponent, CxsMenubarComponent, CxsMultiSelectComponent, CxsPreferenceFormattingService, CxsProgressBarComponent, CxsRadioComponent, CxsSelectComponent, CxsSkeletonComponent, CxsSliderComponent, CxsTabLabelDirective, CxsTabPanelComponent, CxsTableComponent, CxsTabsComponent, CxsThemeDirective, CxsToastComponent, CxsToggleComponent, CxsTooltipComponent };
4396
4570
  //# sourceMappingURL=cerxos-ui.mjs.map