@acorex/core 20.7.36 → 20.7.38

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,4 @@
1
- import * as rxjs from 'rxjs';
2
- import { Subject } from 'rxjs';
1
+ import { Observable, Subject } from 'rxjs';
3
2
  import * as i1 from '@acorex/core/format';
4
3
  import { AXFormatOptions, AXFormatter } from '@acorex/core/format';
5
4
  import * as i0 from '@angular/core';
@@ -60,6 +59,10 @@ declare abstract class AXCalendar {
60
59
  constructor(config: AXCalendarConfig);
61
60
  get weekend(): number[];
62
61
  get weekdays(): number[];
62
+ /**
63
+ * Updates week layout settings (column order and weekend days).
64
+ */
65
+ applyWeekConfig(config: Partial<AXCalendarConfig>): void;
63
66
  /**
64
67
  * Returns the name of the calendar system.
65
68
  * @returns The calendar system name (e.g., 'gregorian', 'persian', 'islamic').
@@ -83,6 +86,10 @@ declare abstract class AXCalendar {
83
86
  * @returns The day of the week (1-7).
84
87
  */
85
88
  abstract dayOfWeek(date: Date): number;
89
+ /**
90
+ * Returns the 0-based weekday index used for week layout (0 = Sunday … 6 = Saturday).
91
+ */
92
+ weekdayIndex(date: Date): number;
86
93
  /**
87
94
  * Returns the month of the year for the given date.
88
95
  * @param date - The date to get the month from.
@@ -154,6 +161,7 @@ declare class AXDateTime {
154
161
  get dayOfMonth(): number;
155
162
  get dayOfYear(): number;
156
163
  get dayOfWeek(): number;
164
+ get weekdayIndex(): number;
157
165
  get hour(): number;
158
166
  get minute(): number;
159
167
  get second(): number;
@@ -278,17 +286,21 @@ declare class AXHolidaysLoaderDefault implements AXHolidaysLoader {
278
286
  declare const AX_DATETIME_HOLIDAYS_LOADER: InjectionToken<AXHolidaysLoader>;
279
287
 
280
288
  declare class AXCalendarService {
281
- private config;
282
289
  private _holidaysLoader;
283
290
  private eventService;
284
291
  private formatService;
285
292
  private localeService;
293
+ private readonly calendars;
286
294
  get calendar(): AXCalendar;
287
295
  private activeCalendar;
288
- calendarChanges$: rxjs.Observable<AXCalendar>;
296
+ calendarChanges$: Observable<AXCalendar>;
289
297
  getDefaultCalendar(): AXCalendar;
290
298
  getActiveCalendar(): AXCalendar;
291
299
  setActiveCalendar(name: string): void;
300
+ private notifyCalendarChanged;
301
+ private buildWeekConfig;
302
+ private registerCalendars;
303
+ private applyLocaleWeekSettings;
292
304
  onHolidaysChanged: Subject<AXHolidayDate[]>;
293
305
  private _holidays;
294
306
  get holidays(): AXHolidayDate[];
@@ -307,17 +319,6 @@ declare class AXCalendarService {
307
319
  static ɵprov: i0.ɵɵInjectableDeclaration<AXCalendarService>;
308
320
  }
309
321
 
310
- interface AXDateTimeConfig {
311
- calendar: string;
312
- calendars: {
313
- [key: string]: AXCalendar;
314
- };
315
- }
316
- declare const AX_DATETIME_CONFIG: InjectionToken<AXDateTimeConfig>;
317
- declare const AXDateTimeDefaultConfig: AXDateTimeConfig;
318
- type PartialDateTimeConfig = Partial<AXDateTimeConfig>;
319
- declare function dateTimeConfig(config?: PartialDateTimeConfig): AXDateTimeConfig;
320
-
321
322
  /**
322
323
  * @deprecated The pipe should not be used, use format instead
323
324
  */
@@ -402,6 +403,7 @@ declare class AXSolarHijriCalendar extends AXCalendar {
402
403
  dayOfMonth(date: Date): number;
403
404
  dayOfYear(date: Date): number;
404
405
  dayOfWeek(date: Date): number;
406
+ weekdayIndex(date: Date): number;
405
407
  weekOfYear(date: Date): number;
406
408
  year(date: Date): number;
407
409
  monthOfYear(date: Date): number;
@@ -431,5 +433,5 @@ declare class AXSolarHijriCalendar extends AXCalendar {
431
433
  private mod;
432
434
  }
433
435
 
434
- export { AXCalendar, AXCalendarMonth, AXCalendarService, AXDateTime, AXDateTimeDefaultConfig, AXDateTimeFormatter, AXDateTimeModule, AXDateTimePipe, AXDateTimeRange, AXGregorianCalendar, AXHolidaysLoaderDefault, AXSolarHijriCalendar, AXTimeDurationFormatter, AXTimeLeftFormatter, AX_DATETIME_CONFIG, AX_DATETIME_HOLIDAYS_LOADER, dateTimeConfig };
435
- export type { AXCalendarConfig, AXDateTimeConfig, AXDateTimeDisplayFormats, AXDateTimeFormatterOptions, AXDateTimePart, AXDateTimeParts, AXDateTimeUnitNames, AXDateValue, AXHolidayDate, AXHolidaysLoader, AXHolidaysLoaderOptions, AXTimeDurationFormatterOptions, AXTimeLeftFormatterOptions, AXTimeSpan, AXTimeSpanTotal, AXValidDateTimeValue, PartialDateTimeConfig, TimeUnit };
436
+ export { AXCalendar, AXCalendarMonth, AXCalendarService, AXDateTime, AXDateTimeFormatter, AXDateTimeModule, AXDateTimePipe, AXDateTimeRange, AXGregorianCalendar, AXHolidaysLoaderDefault, AXSolarHijriCalendar, AXTimeDurationFormatter, AXTimeLeftFormatter, AX_DATETIME_HOLIDAYS_LOADER };
437
+ export type { AXCalendarConfig, AXDateTimeDisplayFormats, AXDateTimeFormatterOptions, AXDateTimePart, AXDateTimeParts, AXDateTimeUnitNames, AXDateValue, AXHolidayDate, AXHolidaysLoader, AXHolidaysLoaderOptions, AXTimeDurationFormatterOptions, AXTimeLeftFormatterOptions, AXTimeSpan, AXTimeSpanTotal, AXValidDateTimeValue, TimeUnit };
@@ -4,11 +4,10 @@ import { AXFormatService, AXFormatModule } from '@acorex/core/format';
4
4
  import { AXLocaleService } from '@acorex/core/locale';
5
5
  import * as i0 from '@angular/core';
6
6
  import { InjectionToken, inject, Injectable, Pipe, NgModule } from '@angular/core';
7
- import { BehaviorSubject, Subject } from 'rxjs';
8
- import { AX_GLOBAL_CONFIG } from '@acorex/core/config';
9
- import { defaults, orderBy, set, isNil, isDate } from 'lodash-es';
7
+ import { Subject, BehaviorSubject } from 'rxjs';
10
8
  import { isBrowser } from '@acorex/core/platform';
11
9
  import { translateSync, AXTranslationService } from '@acorex/core/translation';
10
+ import { defaults, orderBy, isNil, isDate } from 'lodash-es';
12
11
 
13
12
  class AXCalendar {
14
13
  /**
@@ -26,6 +25,18 @@ class AXCalendar {
26
25
  get weekdays() {
27
26
  return this._config.weekdays;
28
27
  }
28
+ /**
29
+ * Updates week layout settings (column order and weekend days).
30
+ */
31
+ applyWeekConfig(config) {
32
+ this._config = { ...this._config, ...config };
33
+ }
34
+ /**
35
+ * Returns the 0-based weekday index used for week layout (0 = Sunday … 6 = Saturday).
36
+ */
37
+ weekdayIndex(date) {
38
+ return this.dayOfWeek(date) - 1;
39
+ }
29
40
  }
30
41
  class AXDateTime {
31
42
  get date() {
@@ -44,10 +55,10 @@ class AXDateTime {
44
55
  // 0 1 ... 5 6 day of week ***
45
56
  d: () => (this.calendar.dayOfWeek(this.date) - 1).toString(),
46
57
  // Su Mo ... Fr Sa ***
47
- dd: () => translateSync(`@acorex:dateTime.weekdays.short.${this.calendar.weekdays[this.calendar.dayOfWeek(this.date) - 1]}`),
48
- ddd: () => translateSync(`@acorex:dateTime.weekdays.short.${this.calendar.weekdays[this.calendar.dayOfWeek(this.date) - 1]}`),
58
+ dd: () => translateSync(`@acorex:dateTime.weekdays.short.${this.weekdayIndex}`),
59
+ ddd: () => translateSync(`@acorex:dateTime.weekdays.short.${this.weekdayIndex}`),
49
60
  // Sunday Monday ... Friday Saturday
50
- dddd: () => translateSync(`@acorex:dateTime.weekdays.long.${this.calendar.weekdays[this.calendar.dayOfWeek(this.date) - 1]}`),
61
+ dddd: () => translateSync(`@acorex:dateTime.weekdays.long.${this.weekdayIndex}`),
51
62
  // 1 2 ... 30 31
52
63
  D: () => this.calendar.dayOfMonth(this.date).toString(),
53
64
  // 01 02 ... 30 31 day of month
@@ -73,9 +84,9 @@ class AXDateTime {
73
84
  // 01 02 ... 07
74
85
  EE: () => this.pad(this.calendar.dayOfWeek(this.date), 2),
75
86
  // Sun Mon ... Sat
76
- EEE: () => translateSync(`@acorex:dateTime.weekdays.short.${this.calendar.weekdays[this.calendar.dayOfWeek(this.date) - 1]}`),
87
+ EEE: () => translateSync(`@acorex:dateTime.weekdays.short.${this.weekdayIndex}`),
77
88
  // Sunday Monday ... Saturday
78
- EEEE: () => translateSync(`@acorex:dateTime.weekdays.long.${this.calendar.weekdays[this.calendar.dayOfWeek(this.date) - 1]}`),
89
+ EEEE: () => translateSync(`@acorex:dateTime.weekdays.long.${this.weekdayIndex}`),
79
90
  // 1 2 ... 2020 ...
80
91
  y: () => this.calendar.year(this.date).toString(),
81
92
  // 70 71 ... 29 30
@@ -131,6 +142,9 @@ class AXDateTime {
131
142
  get dayOfWeek() {
132
143
  return this._calendar.dayOfWeek(this.date);
133
144
  }
145
+ get weekdayIndex() {
146
+ return this._calendar.weekdayIndex(this.date);
147
+ }
134
148
  get hour() {
135
149
  return this._date.getHours();
136
150
  }
@@ -619,6 +633,18 @@ class AXGregorianCalendar extends AXCalendar {
619
633
  }
620
634
  }
621
635
 
636
+ class AXHolidaysLoaderDefault {
637
+ getHolidays(options) {
638
+ return Promise.resolve([]);
639
+ }
640
+ }
641
+ const AX_DATETIME_HOLIDAYS_LOADER = new InjectionToken('AX_DATETIME_HOLIDAYS_LOADER', {
642
+ providedIn: 'root',
643
+ factory: () => {
644
+ return new AXHolidaysLoaderDefault();
645
+ },
646
+ });
647
+
622
648
  class AXSolarHijriCalendar extends AXCalendar {
623
649
  /**
624
650
  * @ignore
@@ -649,6 +675,10 @@ class AXSolarHijriCalendar extends AXCalendar {
649
675
  dayOfWeek(date) {
650
676
  return date.getDay() == 6 ? 1 : date.getDay() + 2;
651
677
  }
678
+ weekdayIndex(date) {
679
+ const dow = this.dayOfWeek(date);
680
+ return dow === 1 ? 6 : dow - 2;
681
+ }
652
682
  weekOfYear(date) {
653
683
  //TODO : apply jalali
654
684
  const firstDay = new AXDateTime(date, this).startOf('year');
@@ -782,8 +812,12 @@ class AXSolarHijriCalendar extends AXCalendar {
782
812
  case 'day':
783
813
  clone.setHours(0, 0, 0, 0);
784
814
  return new AXDateTime(clone, this);
785
- case 'week':
786
- return new AXDateTime(clone, this).add('day', -this.dayOfWeek(clone) + 1).startOf('day');
815
+ case 'week': {
816
+ const currentDayOfWeek_0based = this.weekdayIndex(clone);
817
+ const firstDayOfWeek_0based = this.weekdays[0];
818
+ const daysToSubtract = (currentDayOfWeek_0based - firstDayOfWeek_0based + 7) % 7;
819
+ return new AXDateTime(clone, this).add('day', -daysToSubtract).startOf('day');
820
+ }
787
821
  case 'month': {
788
822
  const jy = this.year(date);
789
823
  const jm = this.monthOfYear(date);
@@ -1058,53 +1092,12 @@ class AXSolarHijriCalendar extends AXCalendar {
1058
1092
  }
1059
1093
  }
1060
1094
 
1061
- const AX_DATETIME_CONFIG = new InjectionToken('AX_DATETIME_CONFIG', {
1062
- providedIn: 'root',
1063
- factory: () => {
1064
- const global = inject(AX_GLOBAL_CONFIG);
1065
- set(global, 'dateTime', AX_DATETIME_CONFIG);
1066
- return AXDateTimeDefaultConfig;
1067
- },
1068
- });
1069
- const AXDateTimeDefaultConfig = {
1070
- calendar: 'gregorian',
1071
- calendars: {
1072
- gregorian: new AXGregorianCalendar({
1073
- weekdays: [0, 1, 2, 3, 4, 5, 6],
1074
- weekend: [5, 6],
1075
- }),
1076
- 'solar-hijri': new AXSolarHijriCalendar({
1077
- weekdays: [6, 0, 1, 2, 3, 4, 5],
1078
- weekend: [6],
1079
- }),
1080
- },
1081
- };
1082
- function dateTimeConfig(config = {}) {
1083
- const result = {
1084
- ...AXDateTimeDefaultConfig,
1085
- ...config,
1086
- };
1087
- return result;
1088
- }
1089
-
1090
- class AXHolidaysLoaderDefault {
1091
- getHolidays(options) {
1092
- return Promise.resolve([]);
1093
- }
1094
- }
1095
- const AX_DATETIME_HOLIDAYS_LOADER = new InjectionToken('AX_DATETIME_HOLIDAYS_LOADER', {
1096
- providedIn: 'root',
1097
- factory: () => {
1098
- return new AXHolidaysLoaderDefault();
1099
- },
1100
- });
1101
-
1102
1095
  class AXCalendarService {
1103
1096
  get calendar() {
1104
1097
  return this.getActiveCalendar();
1105
1098
  }
1106
1099
  getDefaultCalendar() {
1107
- return this.config.calendars[this.config.calendar];
1100
+ return this.resolveCalendar(this.localeService.activeProfile().calendar.system);
1108
1101
  }
1109
1102
  getActiveCalendar() {
1110
1103
  return this.activeCalendar.getValue();
@@ -1112,31 +1105,52 @@ class AXCalendarService {
1112
1105
  setActiveCalendar(name) {
1113
1106
  if (name != this.calendar?.name()) {
1114
1107
  const calendar = this.resolveCalendar(name);
1115
- this.activeCalendar.next(calendar);
1116
- this.eventService.emitEvent({
1117
- type: AXSystemEvents.AXCalendarChanged,
1118
- payload: this.getActiveCalendar(),
1119
- });
1108
+ this.notifyCalendarChanged(calendar);
1120
1109
  }
1121
1110
  }
1111
+ notifyCalendarChanged(calendar) {
1112
+ this.activeCalendar.next(calendar);
1113
+ this.eventService.emitEvent({
1114
+ type: AXSystemEvents.AXCalendarChanged,
1115
+ payload: calendar,
1116
+ });
1117
+ }
1118
+ buildWeekConfig(profile) {
1119
+ const weekdays = Array.from({ length: 7 }, (_, i) => (profile.calendar.week.startsOn + i) % 7);
1120
+ return {
1121
+ weekdays,
1122
+ weekend: profile.calendar.week.weekends,
1123
+ };
1124
+ }
1125
+ registerCalendars(profile) {
1126
+ const weekConfig = this.buildWeekConfig(profile);
1127
+ this.calendars.set('gregorian', new AXGregorianCalendar(weekConfig));
1128
+ this.calendars.set('solar-hijri', new AXSolarHijriCalendar(weekConfig));
1129
+ }
1130
+ applyLocaleWeekSettings(locale) {
1131
+ const weekConfig = this.buildWeekConfig(locale);
1132
+ this.calendars.forEach((calendar) => calendar.applyWeekConfig(weekConfig));
1133
+ this.notifyCalendarChanged(this.resolveCalendar(locale.calendar.system));
1134
+ }
1122
1135
  get holidays() {
1123
1136
  return this._holidays;
1124
1137
  }
1125
1138
  constructor() {
1126
- this.config = inject(AX_DATETIME_CONFIG);
1127
1139
  this._holidaysLoader = inject(AX_DATETIME_HOLIDAYS_LOADER);
1128
1140
  this.eventService = inject(AXEventService);
1129
1141
  this.formatService = inject(AXFormatService);
1130
1142
  this.localeService = inject(AXLocaleService);
1131
- this.activeCalendar = new BehaviorSubject(this.getDefaultCalendar());
1132
- this.calendarChanges$ = this.activeCalendar.asObservable();
1143
+ this.calendars = new Map();
1133
1144
  this.onHolidaysChanged = new Subject();
1134
1145
  this._holidays = [];
1146
+ const profile = this.localeService.activeProfile();
1147
+ this.registerCalendars(profile);
1148
+ this.activeCalendar = new BehaviorSubject(this.resolveCalendar(profile.calendar.system));
1149
+ this.calendarChanges$ = this.activeCalendar.asObservable();
1135
1150
  this.loadHolidays();
1136
- //
1137
1151
  this.localeService.profileChanged$.subscribe((locale) => {
1138
1152
  if (locale?.calendar?.system) {
1139
- this.setActiveCalendar(locale.calendar.system);
1153
+ this.applyLocaleWeekSettings(locale);
1140
1154
  }
1141
1155
  });
1142
1156
  }
@@ -1208,11 +1222,11 @@ class AXCalendarService {
1208
1222
  return new AXDateTime(new Date(), calendar);
1209
1223
  }
1210
1224
  resolveCalendar(name) {
1211
- const c = this.config.calendars[name];
1212
- if (!c) {
1225
+ const calendar = this.calendars.get(name);
1226
+ if (!calendar) {
1213
1227
  return this.calendar;
1214
1228
  }
1215
- return c;
1229
+ return calendar;
1216
1230
  }
1217
1231
  convert(value, calendarName) {
1218
1232
  const isISOString = (str) => {
@@ -1700,5 +1714,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.3", ngImpor
1700
1714
  * Generated bundle index. Do not edit.
1701
1715
  */
1702
1716
 
1703
- export { AXCalendar, AXCalendarMonth, AXCalendarService, AXDateTime, AXDateTimeDefaultConfig, AXDateTimeFormatter, AXDateTimeModule, AXDateTimePipe, AXDateTimeRange, AXGregorianCalendar, AXHolidaysLoaderDefault, AXSolarHijriCalendar, AXTimeDurationFormatter, AXTimeLeftFormatter, AX_DATETIME_CONFIG, AX_DATETIME_HOLIDAYS_LOADER, dateTimeConfig };
1717
+ export { AXCalendar, AXCalendarMonth, AXCalendarService, AXDateTime, AXDateTimeFormatter, AXDateTimeModule, AXDateTimePipe, AXDateTimeRange, AXGregorianCalendar, AXHolidaysLoaderDefault, AXSolarHijriCalendar, AXTimeDurationFormatter, AXTimeLeftFormatter, AX_DATETIME_HOLIDAYS_LOADER };
1704
1718
  //# sourceMappingURL=acorex-core-date-time.mjs.map