@acorex/core 7.3.1 → 7.4.1

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.
Files changed (58) hide show
  1. package/config/lib/configs.d.ts +6 -9
  2. package/config/lib/configs.service.d.ts +6 -1
  3. package/dateTime/index.d.ts +2 -0
  4. package/dateTime/lib/calendar.service.d.ts +13 -0
  5. package/dateTime/lib/dateTime.config.d.ts +12 -0
  6. package/dateTime/lib/datetime.class.d.ts +61 -26
  7. package/dateTime/lib/datetime.module.d.ts +0 -1
  8. package/dateTime/lib/datetime.pipe.d.ts +3 -2
  9. package/dateTime/lib/georgian.calendar.d.ts +6 -6
  10. package/dateTime/lib/jalali.calendar.d.ts +7 -7
  11. package/esm2022/config/lib/configs.mjs +7 -24
  12. package/esm2022/config/lib/configs.service.mjs +20 -16
  13. package/esm2022/dateTime/index.mjs +3 -1
  14. package/esm2022/dateTime/lib/calendar.service.mjs +41 -0
  15. package/esm2022/dateTime/lib/dateTime.config.mjs +65 -0
  16. package/esm2022/dateTime/lib/datetime.class.mjs +43 -64
  17. package/esm2022/dateTime/lib/datetime.module.mjs +8 -28
  18. package/esm2022/dateTime/lib/datetime.pipe.mjs +18 -12
  19. package/esm2022/dateTime/lib/georgian.calendar.mjs +69 -72
  20. package/esm2022/dateTime/lib/jalali.calendar.mjs +50 -49
  21. package/esm2022/events/lib/event.service.mjs +3 -3
  22. package/esm2022/file/lib/file.service.mjs +3 -3
  23. package/esm2022/http/lib/http.module.mjs +4 -4
  24. package/esm2022/http/lib/http.service.mjs +3 -3
  25. package/esm2022/image/lib/image.service.mjs +3 -3
  26. package/esm2022/pipes/lib/pipes.module.mjs +4 -4
  27. package/esm2022/pipes/lib/safe.pipe.mjs +3 -3
  28. package/esm2022/platform/lib/platform.service.mjs +24 -13
  29. package/esm2022/translation/lib/translation.module.mjs +4 -4
  30. package/esm2022/translation/lib/translator.pipe.mjs +3 -3
  31. package/esm2022/types/acorex-core-types.mjs +5 -0
  32. package/esm2022/types/index.mjs +2 -0
  33. package/esm2022/utils/index.mjs +1 -2
  34. package/esm2022/utils/lib/auto-unsubscribe.mjs +3 -3
  35. package/esm2022/utils/lib/string-util.mjs +1 -1
  36. package/fesm2022/acorex-core-config.mjs +24 -39
  37. package/fesm2022/acorex-core-config.mjs.map +1 -1
  38. package/fesm2022/acorex-core-dateTime.mjs +408 -345
  39. package/fesm2022/acorex-core-dateTime.mjs.map +1 -1
  40. package/fesm2022/acorex-core-events.mjs +3 -3
  41. package/fesm2022/acorex-core-file.mjs +3 -3
  42. package/fesm2022/acorex-core-http.mjs +7 -7
  43. package/fesm2022/acorex-core-image.mjs +3 -3
  44. package/fesm2022/acorex-core-pipes.mjs +7 -7
  45. package/fesm2022/acorex-core-platform.mjs +23 -12
  46. package/fesm2022/acorex-core-platform.mjs.map +1 -1
  47. package/fesm2022/acorex-core-translation.mjs +7 -7
  48. package/fesm2022/acorex-core-types.mjs +6 -0
  49. package/fesm2022/acorex-core-types.mjs.map +1 -0
  50. package/fesm2022/acorex-core-utils.mjs +4 -43
  51. package/fesm2022/acorex-core-utils.mjs.map +1 -1
  52. package/package.json +21 -17
  53. package/platform/lib/platform.service.d.ts +2 -1
  54. package/types/README.md +3 -0
  55. package/types/index.d.ts +2 -0
  56. package/utils/index.d.ts +0 -1
  57. package/esm2022/utils/lib/object-util.mjs +0 -39
  58. package/utils/lib/object-util.d.ts +0 -4
@@ -1,47 +1,36 @@
1
- import { AXConfig } from '@acorex/core/config';
2
- import { orderBy } from 'lodash-es';
3
1
  import * as i0 from '@angular/core';
4
- import { Pipe, NgModule } from '@angular/core';
2
+ import { InjectionToken, inject, Injectable, Pipe, NgModule } from '@angular/core';
3
+ import { orderBy, set } from 'lodash-es';
4
+ import { AX_GLOBAL_CONFIG } from '@acorex/core/config';
5
5
 
6
- // @dynamic
7
- class AXDateTime {
8
- static convert(value, calendar = AXConfig.get('dateTime.calendar')) {
9
- let date;
10
- if (typeof value === 'string' || value instanceof String) {
11
- date = new AXDateTime(value, calendar);
12
- }
13
- else if (value instanceof Date) {
14
- date = new AXDateTime(value, calendar);
15
- }
16
- else if (value instanceof AXDateTime) {
17
- date = new AXDateTime(value.date, calendar);
18
- }
19
- return date;
20
- }
21
- static from(value = {
22
- calendar: AXConfig.get('dateTime.calendar'),
23
- year: 1,
24
- month: 1,
25
- date: 1,
26
- hours: 0,
27
- minutes: 0,
28
- seconds: 0,
29
- ms: 0,
30
- }) {
31
- return AXDateTime.resolveCalendar(value.calendar).create(value);
32
- }
33
- static resolveCalendar(calendar) {
34
- return typeof calendar == 'string'
35
- ? AXConfig.get(`dateTime.calendars.${calendar}.class`)
36
- : calendar;
6
+ class AXCalendar {
7
+ /**
8
+ * @ignore
9
+ */
10
+ constructor(config) {
11
+ this._config = config;
12
+ }
13
+ get weekend() {
14
+ return this._config.weekend;
37
15
  }
16
+ get formats() {
17
+ return this._config.formats;
18
+ }
19
+ get dayNames() {
20
+ return this._config.dayNames;
21
+ }
22
+ get monthNames() {
23
+ return this._config.monthNames;
24
+ }
25
+ }
26
+ class AXDateTime {
38
27
  get date() {
39
28
  return this._date;
40
29
  }
41
30
  get calendar() {
42
31
  return this._calendar;
43
32
  }
44
- constructor(value = new Date(), calendar = AXConfig.get(`dateTime.calendar`)) {
33
+ constructor(value, calendar) {
45
34
  this._formatKeys = {
46
35
  ss: () => this.pad(this.date.getSeconds(), 2),
47
36
  s: () => this.date.getSeconds().toString(),
@@ -52,17 +41,18 @@ class AXDateTime {
52
41
  mm: () => this.pad(this.date.getMinutes(), 2),
53
42
  m: () => this.date.getMinutes().toString(),
54
43
  //
55
- MMMM: () => this.calendar.monthNames[this.calendar.monthOfYear(this.date) - 1],
56
- MMM: () => this.calendar.monthShortNames[this.calendar.monthOfYear(this.date) - 1],
44
+ MMMM: () => this.calendar.monthNames[this.calendar.monthOfYear(this.date) - 1].long,
45
+ MMM: () => this.calendar.monthNames[this.calendar.monthOfYear(this.date) - 1].short,
57
46
  MM: () => this.pad(this.calendar.monthOfYear(this.date), 2),
47
+ M: () => this.calendar.monthOfYear(this.date),
58
48
  //
59
- DDDD: () => this.calendar.dayNames[this.calendar.dayOfWeek(this.date) - 1],
60
- DDD: () => this.calendar.dayShortNames[this.calendar.dayOfWeek(this.date) - 1],
49
+ DDDD: () => this.calendar.dayNames[this.calendar.dayOfWeek(this.date) - 1].long,
50
+ DDD: () => this.calendar.dayNames[this.calendar.dayOfWeek(this.date) - 1].short,
61
51
  //
62
- yyyy: () => this.calendar.year(this.date).toString(),
63
- YYYY: () => this.calendar.year(this.date).toString(),
64
- yy: () => this.calendar.year(this.date).toString().substring(2),
65
- YY: () => this.calendar.year(this.date).toString().substring(2),
52
+ yyyy: () => this.pad(this.calendar.year(this.date), 4),
53
+ YYYY: () => this.pad(this.calendar.year(this.date), 4),
54
+ yy: () => this.pad(this.calendar.year(this.date), 4).substring(2),
55
+ YY: () => this.pad(this.calendar.year(this.date), 4).substring(2),
66
56
  //
67
57
  HH: () => this.pad(this.date.getHours(), 2),
68
58
  H: () => this.date.getHours().toString(),
@@ -73,14 +63,8 @@ class AXDateTime {
73
63
  A: () => (this.date.getHours() < 12 ? 'am' : 'pm'),
74
64
  a: () => (this.date.getHours() < 12 ? 'am' : 'pm'),
75
65
  };
76
- this._calendar = AXDateTime.resolveCalendar(calendar);
77
- if (value instanceof Date) {
78
- this._date = value;
79
- }
80
- else {
81
- //TODO: parse jalali
82
- this._date = new Date(value);
83
- }
66
+ this._calendar = calendar;
67
+ this._date = value;
84
68
  }
85
69
  clone() {
86
70
  return new AXDateTime(this.date, this.calendar);
@@ -121,8 +105,10 @@ class AXDateTime {
121
105
  set(unit = 'day', value) {
122
106
  return this._calendar.set(this.date, unit, value);
123
107
  }
124
- duration(end, unit = 'day') {
125
- const range = new AXDateTimeRange(this, AXDateTime.convert(end, this.calendar.name()));
108
+ duration(end) {
109
+ const date = end instanceof AXDateTime ? end.date : end;
110
+ const val = new AXDateTime(date, this.calendar);
111
+ const range = new AXDateTimeRange(this, val);
126
112
  return range.duration();
127
113
  }
128
114
  startOf(unit = 'day') {
@@ -131,7 +117,7 @@ class AXDateTime {
131
117
  endOf(unit = 'day') {
132
118
  return this._calendar.endOf(this.date, unit);
133
119
  }
134
- format(format = AXConfig.get('dateTime.shortDateFormat')) {
120
+ format(format = this.calendar.formats.dateTimeDisplay) {
135
121
  const re = new RegExp(orderBy(Object.keys(this._formatKeys), (c) => c.length, ['desc']).join('|'), 'gi');
136
122
  return format.replace(re, (matched) => {
137
123
  return this._formatKeys[matched]();
@@ -141,14 +127,12 @@ class AXDateTime {
141
127
  n = n + '';
142
128
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
143
129
  }
144
- toString(format = AXConfig.get('dateTime.shortDateFormat')) {
145
- return this.format(format);
146
- }
147
130
  equal(value, unit = 'day') {
148
131
  return this.compare(value, unit) == 0;
149
132
  }
150
133
  compare(value, unit = 'day') {
151
- const val = AXDateTime.convert(value, this.calendar.name());
134
+ const date = value instanceof AXDateTime ? value.date : value;
135
+ const val = new AXDateTime(date, this.calendar);
152
136
  const func = (v1, v2) => {
153
137
  if (v1 == v2) {
154
138
  return 0;
@@ -186,9 +170,6 @@ class AXDateTime {
186
170
  return func(this.date.getTime(), val.date.getTime());
187
171
  }
188
172
  }
189
- convert(calendar) {
190
- return AXDateTime.convert(this, calendar);
191
- }
192
173
  }
193
174
  class AXCalendarMonth {
194
175
  get range() {
@@ -197,6 +178,9 @@ class AXCalendarMonth {
197
178
  set range(v) {
198
179
  this._range = v;
199
180
  }
181
+ get totalDays() {
182
+ return this.range.endTime.dayOfMonth;
183
+ }
200
184
  constructor(date) {
201
185
  this.index = date.date.getMonth();
202
186
  this.name = date.format('MMMM');
@@ -283,239 +267,25 @@ class AXDateTimeRange {
283
267
  }
284
268
  return result;
285
269
  }
286
- includes(value, unit = 'day') {
287
- // TODO: ??
288
- return true;
289
- }
290
- }
291
-
292
- class AXDateTimePipe {
293
- constructor() { }
294
- transform(value, format, calendar) {
295
- if (value == null) {
296
- return '';
297
- }
298
- const date = value instanceof AXDateTime ? value.clone() : AXDateTime.convert(value, calendar);
299
- if (!format) {
300
- return date.format(AXConfig.get('dateTime.shortDateFormat'));
301
- }
302
- else {
303
- return date.format(format);
304
- }
305
- }
306
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: AXDateTimePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
307
- static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "16.1.7", ngImport: i0, type: AXDateTimePipe, name: "axDate" }); }
308
270
  }
309
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: AXDateTimePipe, decorators: [{
310
- type: Pipe,
311
- args: [{ name: 'axDate' }]
312
- }], ctorParameters: function () { return []; } });
313
271
 
314
- class GeorgianCalendar {
315
- constructor() {
316
- this.monthNames = [
317
- "January", "February", "March",
318
- "April", "May", "June", "July",
319
- "August", "September", "October",
320
- "November", "December"
321
- ];
322
- this.monthShortNames = [
323
- "Jan", "Feb", "Mar",
324
- "Apr", "May", "Jun", "Jul",
325
- "Aug", "Sep", "Oct",
326
- "Nov", "Dec"
327
- ];
328
- this.dayNames = [
329
- "Sunday", "Monday", "Tuesday", "Wednesday",
330
- "Thursday", "Friday", "Saturday"
331
- ];
332
- this.dayShortNames = [
333
- "Sun", "Mon", "Tue", "Wed",
334
- "Thu", "Fri", "Sat"
335
- ];
336
- }
337
- create(value) {
338
- return new AXDateTime(new Date(value.year, value.month - 1, value.date, value.hours || 0, value.minutes || 0, value.seconds || 0, value.ms || 0), this.name());
339
- }
340
- name() {
341
- return 'gregorian';
342
- }
343
- dayOfMonth(date) {
344
- return date.getDate();
345
- }
346
- dayOfYear(date) {
347
- let result = 0;
348
- let m = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
349
- if (this.isLeap(date)) {
350
- m[1] = 29;
351
- }
352
- for (let i = 0; i < date.getMonth(); i++) {
353
- result = result + m[i];
354
- }
355
- result += date.getDate();
356
- return result;
357
- }
358
- dayOfWeek(date) {
359
- return date.getDay() + 1;
360
- }
361
- weekOfYear(date) {
362
- const firstDay = new AXDateTime(date).startOf('year');
363
- return Math.ceil((((date.getTime() - firstDay.date.getTime()) / 86400000) + firstDay.date.getDay() + 1) / 7);
364
- }
365
- year(date) {
366
- return date.getFullYear();
367
- }
368
- monthOfYear(date) {
369
- return date.getMonth() + 1;
370
- }
371
- add(date, unit, amount) {
372
- let value = date.valueOf();
373
- switch (unit) {
374
- case 'second':
375
- value += 1000 * amount;
376
- break;
377
- case 'minute':
378
- value += 60000 * amount;
379
- break;
380
- case 'hour':
381
- value += 3600000 * amount;
382
- break;
383
- case 'month':
384
- const v = new Date(value);
385
- var mo = date.getMonth();
386
- var yr = date.getFullYear();
387
- mo = (mo + amount) % 12;
388
- if (0 > mo) {
389
- yr += (date.getMonth() + amount - mo - 12) / 12;
390
- mo += 12;
391
- }
392
- else
393
- yr += ((date.getMonth() + amount - mo) / 12);
394
- v.setMonth(mo);
395
- v.setFullYear(yr);
396
- value = v.valueOf();
397
- break;
398
- case 'week':
399
- value += 7 * 86400000 * amount;
400
- break;
401
- case 'year':
402
- const yv = new Date(value);
403
- yv.setFullYear(yv.getFullYear() + amount);
404
- value = yv.valueOf();
405
- break;
406
- case 'day':
407
- default:
408
- value += 86400000 * amount;
409
- }
410
- return new AXDateTime(new Date(value), this.name());
411
- }
412
- set(date, unit, value) {
413
- const clone = new Date(date.valueOf());
414
- switch (unit) {
415
- case 'second':
416
- clone.setHours(clone.getHours(), clone.getMinutes(), value, clone.getMilliseconds());
417
- break;
418
- case 'minute':
419
- clone.setHours(clone.getHours(), value, clone.getSeconds(), clone.getMilliseconds());
420
- break;
421
- case 'hour':
422
- clone.setHours(value, clone.getMinutes(), clone.getSeconds(), clone.getMilliseconds());
423
- break;
424
- default:
425
- case 'day':
426
- clone.setDate(value);
427
- break;
428
- case "week":
429
- break;
430
- case "month":
431
- clone.setMonth(Math.max(0, value - 1));
432
- break;
433
- case "year":
434
- clone.setFullYear(value);
435
- break;
436
- }
437
- return new AXDateTime(clone, this.name());
438
- }
439
- startOf(date, unit) {
440
- const clone = new Date(date.valueOf());
441
- switch (unit) {
442
- case 'second':
443
- clone.setHours(clone.getHours(), clone.getMinutes(), clone.getSeconds(), 0);
444
- return new AXDateTime(clone, this.name());
445
- case 'minute':
446
- clone.setHours(clone.getHours(), clone.getMinutes(), 0, 0);
447
- return new AXDateTime(clone, this.name());
448
- case 'hour':
449
- clone.setHours(clone.getHours(), 0, 0, 0);
450
- return new AXDateTime(clone, this.name());
451
- default:
452
- case 'day':
453
- clone.setHours(0, 0, 0, 0);
454
- return new AXDateTime(clone, this.name());
455
- case "week":
456
- const index = 0;
457
- const start = index >= 0 ? index : 0;
458
- const day = clone.getDay();
459
- const diff = clone.getDate() - day + (start > day ? start - 7 : start);
460
- clone.setDate(diff);
461
- return new AXDateTime(clone, this.name()).startOf('day');
462
- case "month":
463
- clone.setDate(1);
464
- return new AXDateTime(clone, this.name()).startOf('day');
465
- case "year":
466
- clone.setMonth(0);
467
- clone.setDate(1);
468
- return new AXDateTime(clone, this.name()).startOf('day');
469
- }
470
- }
471
- endOf(date, unit) {
472
- const clone = new Date(date.valueOf());
473
- switch (unit) {
474
- case 'second':
475
- clone.setHours(clone.getHours(), clone.getMinutes(), clone.getSeconds(), 999);
476
- return new AXDateTime(clone, this.name());
477
- case 'minute':
478
- clone.setHours(clone.getHours(), clone.getMinutes(), 59, 999);
479
- return new AXDateTime(clone, this.name());
480
- case 'hour':
481
- clone.setHours(clone.getHours(), 59, 59, 999);
482
- return new AXDateTime(clone, this.name());
483
- default:
484
- case 'day':
485
- clone.setHours(23, 59, 59, 999);
486
- return new AXDateTime(clone, this.name());
487
- case 'week':
488
- return this.startOf(date, 'week').add('day', 6).endOf('day');
489
- case 'month':
490
- return new AXDateTime(new Date(date.getFullYear(), date.getMonth() + 1, 0), this.name()).endOf('day');
491
- case "year":
492
- clone.setMonth(11);
493
- return new AXDateTime(clone, this.name()).endOf('month');
494
- }
495
- }
496
- isLeap(date) {
497
- let leapYear = new Date(date.getFullYear(), 1, 29);
498
- return leapYear.getDate() == 29;
499
- }
500
- }
501
-
502
- class JalaliCalendar {
503
- constructor() {
504
- this.monthNames = 'فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند'.split('_');
505
- this.monthShortNames = 'فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند'.split('_');
506
- this.dayNames = 'شنبه_یکشنبه_دوشنبه_سه شنبه_چهارشنبه_پنج شنبه_جمعه'.split('_');
507
- this.dayShortNames = 'ش_ی_د_س_چ_پ_ج'.split('_');
272
+ class JalaliCalendar extends AXCalendar {
273
+ /**
274
+ * @ignore
275
+ */
276
+ constructor(config) {
277
+ super(config);
508
278
  /*
509
279
  Jalaali years starting the 33-year rule.
510
280
  */
511
281
  this.breaks = [
512
- -61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, 1635, 2060, 2097, 2192, 2262, 2324, 2394,
513
- 2456, 3178,
282
+ -61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456,
283
+ 3178,
514
284
  ];
515
285
  }
516
286
  create(value) {
517
287
  const a = this.toGregorian(value.year, value.month, value.date);
518
- return new AXDateTime(new Date(a.getFullYear(), a.getMonth(), a.getDate(), value.hours || 0, value.minutes || 0, value.seconds || 0, value.ms || 0), this.name());
288
+ return new AXDateTime(new Date(a.getFullYear(), a.getMonth(), a.getDate(), value.hours || 0, value.minutes || 0, value.seconds || 0, value.ms || 0), this);
519
289
  }
520
290
  name() {
521
291
  return 'jalali';
@@ -532,7 +302,7 @@ class JalaliCalendar {
532
302
  }
533
303
  weekOfYear(date) {
534
304
  //TODO : apply jalali
535
- const firstDay = new AXDateTime(date).startOf('year');
305
+ const firstDay = new AXDateTime(date, this).startOf('year');
536
306
  return Math.ceil(((date.getTime() - firstDay.date.getTime()) / 86400000 + firstDay.date.getDay() + 1) / 7);
537
307
  }
538
308
  year(date) {
@@ -592,7 +362,7 @@ class JalaliCalendar {
592
362
  default:
593
363
  value += 86400000 * amount;
594
364
  }
595
- return new AXDateTime(new Date(value), this.name());
365
+ return new AXDateTime(new Date(value), this);
596
366
  }
597
367
  set(date, unit, value) {
598
368
  const clone = new Date(date.valueOf());
@@ -609,52 +379,56 @@ class JalaliCalendar {
609
379
  break;
610
380
  default:
611
381
  case 'day':
612
- const gDate = this.toGregorian(jDate.year, jDate.month, value);
613
- clone.setDate(gDate.getDate());
614
- break;
382
+ {
383
+ const gDate = this.toGregorian(jDate.year, jDate.month, value);
384
+ clone.setDate(gDate.getDate());
385
+ break;
386
+ }
615
387
  case 'week':
616
388
  break;
617
389
  case 'month':
618
- const gDate2 = this.toGregorian(jDate.year, value, jDate.day);
619
- clone.setMonth(gDate2.getMonth());
620
- break;
390
+ {
391
+ const gDate2 = this.toGregorian(jDate.year, value, jDate.day);
392
+ clone.setMonth(gDate2.getMonth());
393
+ break;
394
+ }
621
395
  case 'year':
622
- const gDate3 = this.toGregorian(value, jDate.month, jDate.day);
623
- clone.setFullYear(gDate3.getFullYear());
624
- break;
396
+ {
397
+ const gDate3 = this.toGregorian(value, jDate.month, jDate.day);
398
+ clone.setFullYear(gDate3.getFullYear());
399
+ break;
400
+ }
625
401
  }
626
- return new AXDateTime(clone, this.name());
402
+ return new AXDateTime(clone, this);
627
403
  }
628
404
  startOf(date, unit) {
629
405
  const clone = new Date(date.valueOf());
630
406
  switch (unit) {
631
407
  case 'second':
632
408
  clone.setHours(clone.getHours(), clone.getMinutes(), clone.getSeconds(), 0);
633
- return new AXDateTime(clone, this.name());
409
+ return new AXDateTime(clone, this);
634
410
  case 'minute':
635
411
  clone.setHours(clone.getHours(), clone.getMinutes(), 0, 0);
636
- return new AXDateTime(clone, this.name());
412
+ return new AXDateTime(clone, this);
637
413
  case 'hour':
638
414
  clone.setHours(clone.getHours(), 0, 0, 0);
639
- return new AXDateTime(clone, this.name());
415
+ return new AXDateTime(clone, this);
640
416
  default:
641
417
  case 'day':
642
418
  clone.setHours(0, 0, 0, 0);
643
- return new AXDateTime(clone, this.name());
419
+ return new AXDateTime(clone, this);
644
420
  case 'week':
645
- return new AXDateTime(clone, this.name())
646
- .add('day', -this.dayOfWeek(clone) + 1)
647
- .startOf('day');
421
+ return new AXDateTime(clone, this).add('day', -this.dayOfWeek(clone) + 1).startOf('day');
648
422
  case 'month': {
649
423
  const jy = this.year(date);
650
424
  const jm = this.monthOfYear(date);
651
425
  const gDate = this.toGregorian(jy, jm, 1);
652
- return new AXDateTime(gDate, this.name()).startOf('day');
426
+ return new AXDateTime(gDate, this).startOf('day');
653
427
  }
654
428
  case 'year': {
655
429
  const jy = this.year(date);
656
430
  const gDate = this.toGregorian(jy, 1, 1);
657
- return new AXDateTime(gDate, this.name()).startOf('day');
431
+ return new AXDateTime(gDate, this).startOf('day');
658
432
  }
659
433
  }
660
434
  }
@@ -663,17 +437,17 @@ class JalaliCalendar {
663
437
  switch (unit) {
664
438
  case 'second':
665
439
  clone.setHours(clone.getHours(), clone.getMinutes(), clone.getSeconds(), 999);
666
- return new AXDateTime(clone, this.name());
440
+ return new AXDateTime(clone, this);
667
441
  case 'minute':
668
442
  clone.setHours(clone.getHours(), clone.getMinutes(), 59, 999);
669
- return new AXDateTime(clone, this.name());
443
+ return new AXDateTime(clone, this);
670
444
  case 'hour':
671
445
  clone.setHours(clone.getHours(), 59, 59, 999);
672
- return new AXDateTime(clone, this.name());
446
+ return new AXDateTime(clone, this);
673
447
  default:
674
448
  case 'day':
675
449
  clone.setHours(23, 59, 59, 999);
676
- return new AXDateTime(clone, this.name());
450
+ return new AXDateTime(clone, this);
677
451
  case 'week': {
678
452
  return this.startOf(date, 'week').add('day', 6).endOf('day');
679
453
  }
@@ -682,21 +456,23 @@ class JalaliCalendar {
682
456
  const jm = this.monthOfYear(date);
683
457
  const jd = this.jalaaliMonthLength(jy, jm);
684
458
  const gDate = this.toGregorian(jy, jm, jd);
685
- return new AXDateTime(gDate, this.name()).endOf('day');
459
+ return new AXDateTime(gDate, this).endOf('day');
686
460
  }
687
461
  case 'year':
688
- let jy = this.year(date);
689
- const gDate = this.toGregorian(jy, 12, this.jalaaliMonthLength(jy, 12));
690
- return new AXDateTime(gDate, this.name()).endOf('day');
462
+ {
463
+ const jy = this.year(date);
464
+ const gDate = this.toGregorian(jy, 12, this.jalaaliMonthLength(jy, 12));
465
+ return new AXDateTime(gDate, this).endOf('day');
466
+ }
691
467
  }
692
468
  }
693
469
  /*
694
470
  Converts a Gregorian date to Jalaali.
695
471
  */
696
472
  toJalaali(date) {
697
- let gd = date.getDate();
698
- let gm = date.getMonth() + 1;
699
- let gy = date.getFullYear();
473
+ const gd = date.getDate();
474
+ const gm = date.getMonth() + 1;
475
+ const gy = date.getFullYear();
700
476
  const r = this.d2j(this.g2d(gy, gm, gd));
701
477
  return {
702
478
  year: r.jy,
@@ -715,12 +491,7 @@ class JalaliCalendar {
715
491
  Checks whether a Jalaali date is valid or not.
716
492
  */
717
493
  isValidJalaaliDate(jy, jm, jd) {
718
- return (jy >= -61 &&
719
- jy <= 3177 &&
720
- jm >= 1 &&
721
- jm <= 12 &&
722
- jd >= 1 &&
723
- jd <= this.jalaaliMonthLength(jy, jm));
494
+ return jy >= -61 && jy <= 3177 && jm >= 1 && jm <= 12 && jd >= 1 && jd <= this.jalaaliMonthLength(jy, jm);
724
495
  }
725
496
  /*
726
497
  Is this a leap year or not?
@@ -892,42 +663,334 @@ class JalaliCalendar {
892
663
  }
893
664
  }
894
665
 
895
- class AXDateTimeModule {
896
- constructor() {
897
- AXConfig.set({
898
- dateTime: {
899
- calendars: {
900
- jalali: {
901
- "class": new JalaliCalendar(),
902
- weekend: [6, 7]
903
- },
904
- gregorian: {
905
- "class": new GeorgianCalendar(),
906
- weekend: [7, 1]
666
+ class GeorgianCalendar extends AXCalendar {
667
+ /**
668
+ * @ignore
669
+ */
670
+ constructor(config) {
671
+ super(config);
672
+ }
673
+ create(value) {
674
+ return new AXDateTime(new Date(value.year, value.month - 1, value.date, value.hours || 0, value.minutes || 0, value.seconds || 0, value.ms || 0), this);
675
+ }
676
+ name() {
677
+ return 'georgian';
678
+ }
679
+ dayOfMonth(date) {
680
+ return date.getDate();
681
+ }
682
+ dayOfYear(date) {
683
+ let result = 0;
684
+ let m = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
685
+ if (this.isLeap(date)) {
686
+ m[1] = 29;
687
+ }
688
+ for (let i = 0; i < date.getMonth(); i++) {
689
+ result = result + m[i];
690
+ }
691
+ result += date.getDate();
692
+ return result;
693
+ }
694
+ dayOfWeek(date) {
695
+ return date.getDay() + 1;
696
+ }
697
+ weekOfYear(date) {
698
+ const firstDay = new AXDateTime(date, this).startOf('year');
699
+ return Math.ceil(((date.getTime() - firstDay.date.getTime()) / 86400000 + firstDay.date.getDay() + 1) / 7);
700
+ }
701
+ year(date) {
702
+ return date.getFullYear();
703
+ }
704
+ monthOfYear(date) {
705
+ return date.getMonth() + 1;
706
+ }
707
+ add(date, unit, amount) {
708
+ let value = date.valueOf();
709
+ switch (unit) {
710
+ case 'second':
711
+ value += 1000 * amount;
712
+ break;
713
+ case 'minute':
714
+ value += 60000 * amount;
715
+ break;
716
+ case 'hour':
717
+ value += 3600000 * amount;
718
+ break;
719
+ case 'month':
720
+ {
721
+ const v = new Date(value);
722
+ let mo = date.getMonth();
723
+ let yr = date.getFullYear();
724
+ mo = (mo + amount) % 12;
725
+ if (0 > mo) {
726
+ yr += (date.getMonth() + amount - mo - 12) / 12;
727
+ mo += 12;
907
728
  }
908
- },
909
- calendar: 'gregorian',
910
- shortDateFormat: 'DDDD, d MMM yyyy'
729
+ else
730
+ yr += (date.getMonth() + amount - mo) / 12;
731
+ v.setMonth(mo);
732
+ v.setFullYear(yr);
733
+ value = v.valueOf();
734
+ break;
735
+ }
736
+ case 'week':
737
+ value += 7 * 86400000 * amount;
738
+ break;
739
+ case 'year':
740
+ {
741
+ const yv = new Date(value);
742
+ yv.setFullYear(yv.getFullYear() + amount);
743
+ value = yv.valueOf();
744
+ break;
745
+ }
746
+ case 'day':
747
+ default:
748
+ value += 86400000 * amount;
749
+ }
750
+ return new AXDateTime(new Date(value), this);
751
+ }
752
+ set(date, unit, value) {
753
+ const clone = new Date(date.valueOf());
754
+ switch (unit) {
755
+ case 'second':
756
+ clone.setHours(clone.getHours(), clone.getMinutes(), value, clone.getMilliseconds());
757
+ break;
758
+ case 'minute':
759
+ clone.setHours(clone.getHours(), value, clone.getSeconds(), clone.getMilliseconds());
760
+ break;
761
+ case 'hour':
762
+ clone.setHours(value, clone.getMinutes(), clone.getSeconds(), clone.getMilliseconds());
763
+ break;
764
+ default:
765
+ case 'day':
766
+ clone.setDate(value);
767
+ break;
768
+ case 'week':
769
+ break;
770
+ case 'month':
771
+ clone.setMonth(Math.max(0, value - 1));
772
+ break;
773
+ case 'year':
774
+ clone.setFullYear(value);
775
+ break;
776
+ }
777
+ return new AXDateTime(clone, this);
778
+ }
779
+ startOf(date, unit) {
780
+ const clone = new Date(date.valueOf());
781
+ switch (unit) {
782
+ case 'second':
783
+ clone.setHours(clone.getHours(), clone.getMinutes(), clone.getSeconds(), 0);
784
+ return new AXDateTime(clone, this);
785
+ case 'minute':
786
+ clone.setHours(clone.getHours(), clone.getMinutes(), 0, 0);
787
+ return new AXDateTime(clone, this);
788
+ case 'hour':
789
+ clone.setHours(clone.getHours(), 0, 0, 0);
790
+ return new AXDateTime(clone, this);
791
+ default:
792
+ case 'day':
793
+ clone.setHours(0, 0, 0, 0);
794
+ return new AXDateTime(clone, this);
795
+ case 'week':
796
+ {
797
+ const index = 0;
798
+ const start = index >= 0 ? index : 0;
799
+ const day = clone.getDay();
800
+ const diff = clone.getDate() - day + (start > day ? start - 7 : start);
801
+ clone.setDate(diff);
802
+ return new AXDateTime(clone, this).startOf('day');
803
+ }
804
+ case 'month':
805
+ clone.setDate(1);
806
+ return new AXDateTime(clone, this).startOf('day');
807
+ case 'year':
808
+ clone.setMonth(0);
809
+ clone.setDate(1);
810
+ return new AXDateTime(clone, this).startOf('day');
811
+ }
812
+ }
813
+ endOf(date, unit) {
814
+ const clone = new Date(date.valueOf());
815
+ switch (unit) {
816
+ case 'second':
817
+ clone.setHours(clone.getHours(), clone.getMinutes(), clone.getSeconds(), 999);
818
+ return new AXDateTime(clone, this);
819
+ case 'minute':
820
+ clone.setHours(clone.getHours(), clone.getMinutes(), 59, 999);
821
+ return new AXDateTime(clone, this);
822
+ case 'hour':
823
+ clone.setHours(clone.getHours(), 59, 59, 999);
824
+ return new AXDateTime(clone, this);
825
+ default:
826
+ case 'day':
827
+ clone.setHours(23, 59, 59, 999);
828
+ return new AXDateTime(clone, this);
829
+ case 'week':
830
+ return this.startOf(date, 'week').add('day', 6).endOf('day');
831
+ case 'month':
832
+ {
833
+ const dateObj = new Date();
834
+ dateObj.setFullYear(date.getFullYear());
835
+ dateObj.setMonth(date.getMonth() + 1);
836
+ dateObj.setDate(0);
837
+ return new AXDateTime(dateObj, this).endOf('day');
838
+ //return new AXDateTime(new Date(date.getFullYear(), date.getMonth() + 1, 0), this).endOf('day');
839
+ }
840
+ case 'year':
841
+ clone.setMonth(11);
842
+ return new AXDateTime(clone, this).endOf('month');
843
+ }
844
+ }
845
+ isLeap(date) {
846
+ let leapYear = new Date(date.getFullYear(), 1, 29);
847
+ return leapYear.getDate() == 29;
848
+ }
849
+ }
850
+
851
+ const AX_DATETIME_CONFIG = new InjectionToken('AX_DATETIME_CONFIG', {
852
+ providedIn: 'root',
853
+ factory: () => {
854
+ const global = inject(AX_GLOBAL_CONFIG);
855
+ set(global, "dateTime", AX_DATETIME_CONFIG);
856
+ return AXDateTimeDefaultConfig;
857
+ },
858
+ });
859
+ const AXDateTimeDefaultConfig = {
860
+ calendar: 'georgian',
861
+ calendars: {
862
+ "georgian": new GeorgianCalendar({
863
+ dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'].map(d => ({ long: d, short: d.substring(0, 3), letter: d[0] })),
864
+ monthNames: [
865
+ 'January',
866
+ 'February',
867
+ 'March',
868
+ 'April',
869
+ 'May',
870
+ 'June',
871
+ 'July',
872
+ 'August',
873
+ 'September',
874
+ 'October',
875
+ 'November',
876
+ 'December',
877
+ ].map(d => ({ long: d, short: d.substring(0, 3), letter: d[0] })),
878
+ weekend: [6, 7],
879
+ formats: {
880
+ dateInput: "yyyy/MM/dd",
881
+ timeInput: "HH:mm",
882
+ dateTimeInput: "yyyy/MM/dd HH:mm",
883
+ dateDisplay: "DDDD, d MMM yyyy",
884
+ timeDisplay: "HH:mm",
885
+ dateTimeDisplay: "DDDD, d MMM yyyy HH:mm"
911
886
  }
912
- });
887
+ }),
888
+ "jalali": new JalaliCalendar({
889
+ monthNames: 'فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند'.split('_').map(d => ({ long: d, short: d, letter: d[0] })),
890
+ dayNames: 'شنبه_یکشنبه_دوشنبه_سه شنبه_چهارشنبه_پنج شنبه_جمعه'.split('_').map(d => ({ long: d, short: d[0], letter: d[0] })),
891
+ weekend: [7, 1],
892
+ formats: {
893
+ dateInput: "yyyy/MM/dd",
894
+ timeInput: "HH:mm",
895
+ dateTimeInput: "yyyy/MM/dd HH:mm",
896
+ dateDisplay: "DDDD, d MMM yyyy",
897
+ timeDisplay: "HH:mm",
898
+ dateTimeDisplay: "DDDD, d MMM yyyy HH:mm"
899
+ }
900
+ })
901
+ }
902
+ };
903
+ function dateTimeConfig(config = {}) {
904
+ const result = {
905
+ ...AXDateTimeDefaultConfig,
906
+ ...config,
907
+ };
908
+ return result;
909
+ }
910
+
911
+ class AXCalendarService {
912
+ constructor() {
913
+ this._config = inject(AX_DATETIME_CONFIG);
914
+ }
915
+ get calendar() {
916
+ return this._config.calendars[this._config.calendar];
917
+ }
918
+ create(value, calendarName) {
919
+ const calendar = calendarName ? this.resolveCalendar(calendarName) : this.calendar;
920
+ if (value instanceof Date) {
921
+ return new AXDateTime(value, calendar);
922
+ }
923
+ else if (value?.year && value?.month) {
924
+ return calendar.create(value);
925
+ }
926
+ return this.now();
927
+ }
928
+ now(calendarName) {
929
+ const calendar = calendarName ? this.resolveCalendar(calendarName) : this.calendar;
930
+ return new AXDateTime(new Date(), calendar);
931
+ }
932
+ resolveCalendar(name) {
933
+ return this._config.calendars[name];
913
934
  }
914
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: AXDateTimeModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
915
- static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.1.7", ngImport: i0, type: AXDateTimeModule, declarations: [AXDateTimePipe], exports: [AXDateTimePipe] }); }
916
- static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: AXDateTimeModule }); }
935
+ convert(value, calendarName) {
936
+ const calendar = calendarName ? this.resolveCalendar(calendarName) : this.calendar;
937
+ const date = value instanceof AXDateTime ? value.date : value;
938
+ return new AXDateTime(date, calendar);
939
+ }
940
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.9", ngImport: i0, type: AXCalendarService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
941
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.1.9", ngImport: i0, type: AXCalendarService, providedIn: 'root' }); }
942
+ }
943
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.9", ngImport: i0, type: AXCalendarService, decorators: [{
944
+ type: Injectable,
945
+ args: [{ providedIn: 'root' }]
946
+ }] });
947
+
948
+ class AXDateTimePipe {
949
+ constructor() {
950
+ this._calendarService = inject(AXCalendarService);
951
+ }
952
+ transform(value, format, calendarName) {
953
+ if (value == null) {
954
+ return '';
955
+ }
956
+ let calendar = this._calendarService.calendar;
957
+ if (calendarName) {
958
+ calendar = this._calendarService.resolveCalendar(calendarName);
959
+ }
960
+ const val = value instanceof AXDateTime ? value.clone() : new AXDateTime(value, calendar);
961
+ if (!format) {
962
+ return val.format(calendar.formats.dateDisplay);
963
+ }
964
+ else {
965
+ return val.format(format);
966
+ }
967
+ }
968
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.9", ngImport: i0, type: AXDateTimePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
969
+ static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "16.1.9", ngImport: i0, type: AXDateTimePipe, name: "axDate" }); }
970
+ }
971
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.9", ngImport: i0, type: AXDateTimePipe, decorators: [{
972
+ type: Pipe,
973
+ args: [{ name: 'axDate' }]
974
+ }] });
975
+
976
+ class AXDateTimeModule {
977
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.9", ngImport: i0, type: AXDateTimeModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
978
+ static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.1.9", ngImport: i0, type: AXDateTimeModule, declarations: [AXDateTimePipe], exports: [AXDateTimePipe] }); }
979
+ static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.1.9", ngImport: i0, type: AXDateTimeModule, providers: [AXCalendarService] }); }
917
980
  }
918
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: AXDateTimeModule, decorators: [{
981
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.9", ngImport: i0, type: AXDateTimeModule, decorators: [{
919
982
  type: NgModule,
920
983
  args: [{
921
984
  imports: [],
922
985
  exports: [AXDateTimePipe],
923
986
  declarations: [AXDateTimePipe],
924
- providers: [],
987
+ providers: [AXCalendarService],
925
988
  }]
926
- }], ctorParameters: function () { return []; } });
989
+ }] });
927
990
 
928
991
  /**
929
992
  * Generated bundle index. Do not edit.
930
993
  */
931
994
 
932
- export { AXCalendarMonth, AXDateTime, AXDateTimeModule, AXDateTimePipe, AXDateTimeRange, GeorgianCalendar, JalaliCalendar };
995
+ export { AXCalendar, AXCalendarMonth, AXCalendarService, AXDateTime, AXDateTimeDefaultConfig, AXDateTimeModule, AXDateTimePipe, AXDateTimeRange, AX_DATETIME_CONFIG, GeorgianCalendar, JalaliCalendar, dateTimeConfig };
933
996
  //# sourceMappingURL=acorex-core-dateTime.mjs.map