@telperion/elements 7.1.0 → 8.0.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/.build-manifest.json +12 -1
- package/README.md +31 -0
- package/custom-elements.json +1672 -0
- package/date/date-element.cjs +231 -0
- package/date/date-element.cjs.map +1 -0
- package/date/date-element.js +229 -0
- package/date/date-element.js.map +1 -0
- package/date/format-pattern.cjs +49 -0
- package/date/format-pattern.cjs.map +1 -0
- package/date/format-pattern.js +47 -0
- package/date/format-pattern.js.map +1 -0
- package/date/format.cjs +57 -0
- package/date/format.cjs.map +1 -0
- package/date/format.js +51 -0
- package/date/format.js.map +1 -0
- package/date/index.cjs +17 -0
- package/date/index.cjs.map +1 -0
- package/date/index.d.cts +171 -0
- package/date/index.d.ts +171 -0
- package/date/index.js +6 -0
- package/date/index.js.map +1 -0
- package/date/intl.cjs +36 -0
- package/date/intl.cjs.map +1 -0
- package/date/intl.js +32 -0
- package/date/intl.js.map +1 -0
- package/date/locale.cjs +36 -0
- package/date/locale.cjs.map +1 -0
- package/date/locale.js +34 -0
- package/date/locale.js.map +1 -0
- package/date/names.cjs +41 -0
- package/date/names.cjs.map +1 -0
- package/date/names.js +37 -0
- package/date/names.js.map +1 -0
- package/date/parse-fields.cjs +80 -0
- package/date/parse-fields.cjs.map +1 -0
- package/date/parse-fields.js +75 -0
- package/date/parse-fields.js.map +1 -0
- package/date/parse-iso.cjs +41 -0
- package/date/parse-iso.cjs.map +1 -0
- package/date/parse-iso.js +39 -0
- package/date/parse-iso.js.map +1 -0
- package/date/parse-numeric.cjs +53 -0
- package/date/parse-numeric.cjs.map +1 -0
- package/date/parse-numeric.js +50 -0
- package/date/parse-numeric.js.map +1 -0
- package/date/parse-pattern.cjs +123 -0
- package/date/parse-pattern.cjs.map +1 -0
- package/date/parse-pattern.js +121 -0
- package/date/parse-pattern.js.map +1 -0
- package/date/parse.cjs +35 -0
- package/date/parse.cjs.map +1 -0
- package/date/parse.js +33 -0
- package/date/parse.js.map +1 -0
- package/date/relative.cjs +47 -0
- package/date/relative.cjs.map +1 -0
- package/date/relative.js +44 -0
- package/date/relative.js.map +1 -0
- package/date/resolution.cjs +62 -0
- package/date/resolution.cjs.map +1 -0
- package/date/resolution.js +59 -0
- package/date/resolution.js.map +1 -0
- package/date/styles.cjs +21 -0
- package/date/styles.cjs.map +1 -0
- package/date/styles.js +19 -0
- package/date/styles.js.map +1 -0
- package/date/ticker.cjs +71 -0
- package/date/ticker.cjs.map +1 -0
- package/date/ticker.js +69 -0
- package/date/ticker.js.map +1 -0
- package/date/tokens.cjs +94 -0
- package/date/tokens.cjs.map +1 -0
- package/date/tokens.js +91 -0
- package/date/tokens.js.map +1 -0
- package/date/zone.cjs +110 -0
- package/date/zone.cjs.map +1 -0
- package/date/zone.js +101 -0
- package/date/zone.js.map +1 -0
- package/fesm/elements.mjs +1053 -2
- package/fesm/elements.mjs.map +1 -1
- package/iife/date/index.js +53 -0
- package/iife/date/index.js.map +1 -0
- package/iife/elements.js +52 -34
- package/iife/elements.js.map +1 -1
- package/index.cjs +11 -0
- package/index.cjs.map +1 -1
- package/index.d.cts +1 -0
- package/index.d.ts +1 -0
- package/index.js +5 -0
- package/index.js.map +1 -1
- package/package.json +31 -3
package/fesm/elements.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import { LitElement, css, html as html$1 } from 'lit';
|
|
2
|
+
import { LitElement, css, html as html$1, nothing } from 'lit';
|
|
3
3
|
import { property, customElement, state } from 'lit/decorators.js';
|
|
4
4
|
import { signal, computed, SignalWatcher, html } from '@lit-labs/signals';
|
|
5
5
|
import { noop } from '@telperion/js-utils/function/noop';
|
|
@@ -4538,5 +4538,1056 @@ class PortalOutletFallbackElement extends HTMLElement {
|
|
|
4538
4538
|
if (!customElements.get("tp-portal-outlet-fallback"))
|
|
4539
4539
|
customElements.define("tp-portal-outlet-fallback", PortalOutletFallbackElement);
|
|
4540
4540
|
|
|
4541
|
-
|
|
4541
|
+
const dateTimeFormats = new Map();
|
|
4542
|
+
const relativeTimeFormats = new Map();
|
|
4543
|
+
/**
|
|
4544
|
+
* Cached `Intl.DateTimeFormat`. Constructing formatters is the expensive part
|
|
4545
|
+
* of `Intl`, and live dates re-format on every tick.
|
|
4546
|
+
*/
|
|
4547
|
+
function dateTimeFormat(locale, options) {
|
|
4548
|
+
const key = `${locale ?? ""}|${JSON.stringify(options)}`;
|
|
4549
|
+
let format = dateTimeFormats.get(key);
|
|
4550
|
+
if (!format) {
|
|
4551
|
+
format = new Intl.DateTimeFormat(locale, options);
|
|
4552
|
+
dateTimeFormats.set(key, format);
|
|
4553
|
+
}
|
|
4554
|
+
return format;
|
|
4555
|
+
}
|
|
4556
|
+
/** Cached `Intl.RelativeTimeFormat` with `numeric: "auto"` ("yesterday", "now"). */
|
|
4557
|
+
function relativeTimeFormat(locale) {
|
|
4558
|
+
const key = locale ?? "";
|
|
4559
|
+
let format = relativeTimeFormats.get(key);
|
|
4560
|
+
if (!format) {
|
|
4561
|
+
format = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
|
4562
|
+
relativeTimeFormats.set(key, format);
|
|
4563
|
+
}
|
|
4564
|
+
return format;
|
|
4565
|
+
}
|
|
4566
|
+
/** Returns the value of the first part of `type`, if any. */
|
|
4567
|
+
function partValue(parts, type) {
|
|
4568
|
+
return parts.find(part => part.type === type)?.value;
|
|
4569
|
+
}
|
|
4570
|
+
|
|
4571
|
+
const cache$2 = new Map();
|
|
4572
|
+
function cached(key, build) {
|
|
4573
|
+
let names = cache$2.get(key);
|
|
4574
|
+
if (!names) {
|
|
4575
|
+
names = build();
|
|
4576
|
+
cache$2.set(key, names);
|
|
4577
|
+
}
|
|
4578
|
+
return names;
|
|
4579
|
+
}
|
|
4580
|
+
/** Localized month names, January first. Uses the "format" form (as in "22 January"). */
|
|
4581
|
+
function monthNames(locale, width) {
|
|
4582
|
+
return cached(`month|${locale}|${width}`, () => {
|
|
4583
|
+
const format = dateTimeFormat(locale, { timeZone: "UTC", day: "numeric", month: width });
|
|
4584
|
+
return Array.from({ length: 12 }, (_, month) => partValue(format.formatToParts(Date.UTC(2000, month, 15)), "month") ?? "");
|
|
4585
|
+
});
|
|
4586
|
+
}
|
|
4587
|
+
/** Localized weekday names, Sunday first. */
|
|
4588
|
+
function weekdayNames(locale, width) {
|
|
4589
|
+
return cached(`weekday|${locale}|${width}`, () => {
|
|
4590
|
+
const format = dateTimeFormat(locale, { timeZone: "UTC", weekday: width });
|
|
4591
|
+
// 2000-01-02 was a Sunday.
|
|
4592
|
+
return Array.from({ length: 7 }, (_, day) => format.format(Date.UTC(2000, 0, 2 + day)));
|
|
4593
|
+
});
|
|
4594
|
+
}
|
|
4595
|
+
/** Localized day periods as `[am, pm]`. Falls back to `AM` / `PM`. */
|
|
4596
|
+
function dayPeriods(locale) {
|
|
4597
|
+
return cached(`period|${locale}`, () => {
|
|
4598
|
+
const format = dateTimeFormat(locale, { timeZone: "UTC", hour: "numeric", hourCycle: "h12" });
|
|
4599
|
+
const read = (hour, fallback) => partValue(format.formatToParts(Date.UTC(2000, 0, 1, hour)), "dayPeriod") ?? fallback;
|
|
4600
|
+
return [read(9, "AM"), read(21, "PM")];
|
|
4601
|
+
});
|
|
4602
|
+
}
|
|
4603
|
+
|
|
4604
|
+
/**
|
|
4605
|
+
* Moment-style tokens shared by formatting (`format`) and parsing
|
|
4606
|
+
* (`input-format`). Ordered longest-first so the tokenizer is greedy.
|
|
4607
|
+
*
|
|
4608
|
+
* | Token | Meaning |
|
|
4609
|
+
* | ------------ | ------------------------------------ |
|
|
4610
|
+
* | `YYYY` `YY` | Year (4 digits / 2 digits) |
|
|
4611
|
+
* | `MMMM` `MMM` | Month name (long / short, localized) |
|
|
4612
|
+
* | `MM` `M` | Month number (padded / unpadded) |
|
|
4613
|
+
* | `DD` `D` | Day of month |
|
|
4614
|
+
* | `dddd` `ddd` | Weekday name (long / short) |
|
|
4615
|
+
* | `d` | Weekday number (0 = Sunday) |
|
|
4616
|
+
* | `HH` `H` | Hour 0-23 |
|
|
4617
|
+
* | `hh` `h` | Hour 1-12 |
|
|
4618
|
+
* | `mm` `m` | Minute |
|
|
4619
|
+
* | `ss` `s` | Second |
|
|
4620
|
+
* | `SSS` | Millisecond |
|
|
4621
|
+
* | `A` `a` | Day period (AM/PM, localized) |
|
|
4622
|
+
* | `ZZ` `Z` | UTC offset (`+0300` / `+03:00`) |
|
|
4623
|
+
* | `X` `x` | Unix seconds / milliseconds |
|
|
4624
|
+
*
|
|
4625
|
+
* Text in `[brackets]` is literal. Any other character is literal too.
|
|
4626
|
+
*/
|
|
4627
|
+
const TOKENS = [
|
|
4628
|
+
"YYYY",
|
|
4629
|
+
"MMMM",
|
|
4630
|
+
"dddd",
|
|
4631
|
+
"MMM",
|
|
4632
|
+
"ddd",
|
|
4633
|
+
"SSS",
|
|
4634
|
+
"YY",
|
|
4635
|
+
"MM",
|
|
4636
|
+
"DD",
|
|
4637
|
+
"HH",
|
|
4638
|
+
"hh",
|
|
4639
|
+
"mm",
|
|
4640
|
+
"ss",
|
|
4641
|
+
"ZZ",
|
|
4642
|
+
"M",
|
|
4643
|
+
"D",
|
|
4644
|
+
"d",
|
|
4645
|
+
"H",
|
|
4646
|
+
"h",
|
|
4647
|
+
"m",
|
|
4648
|
+
"s",
|
|
4649
|
+
"A",
|
|
4650
|
+
"a",
|
|
4651
|
+
"Z",
|
|
4652
|
+
"X",
|
|
4653
|
+
"x",
|
|
4654
|
+
];
|
|
4655
|
+
const cache$1 = new Map();
|
|
4656
|
+
/** Splits a pattern into tokens and literals. Results are cached per pattern. */
|
|
4657
|
+
function tokenize(pattern) {
|
|
4658
|
+
let parts = cache$1.get(pattern);
|
|
4659
|
+
if (parts)
|
|
4660
|
+
return parts;
|
|
4661
|
+
parts = [];
|
|
4662
|
+
let literal = "";
|
|
4663
|
+
let i = 0;
|
|
4664
|
+
const flush = () => {
|
|
4665
|
+
if (literal)
|
|
4666
|
+
parts.push({ literal });
|
|
4667
|
+
literal = "";
|
|
4668
|
+
};
|
|
4669
|
+
while (i < pattern.length) {
|
|
4670
|
+
if (pattern[i] === "[") {
|
|
4671
|
+
const end = pattern.indexOf("]", i + 1);
|
|
4672
|
+
if (end !== -1) {
|
|
4673
|
+
literal += pattern.slice(i + 1, end);
|
|
4674
|
+
i = end + 1;
|
|
4675
|
+
continue;
|
|
4676
|
+
}
|
|
4677
|
+
}
|
|
4678
|
+
const token = TOKENS.find(candidate => pattern.startsWith(candidate, i));
|
|
4679
|
+
if (token) {
|
|
4680
|
+
flush();
|
|
4681
|
+
parts.push({ token });
|
|
4682
|
+
i += token.length;
|
|
4683
|
+
}
|
|
4684
|
+
else {
|
|
4685
|
+
literal += pattern[i++];
|
|
4686
|
+
}
|
|
4687
|
+
}
|
|
4688
|
+
flush();
|
|
4689
|
+
cache$1.set(pattern, parts);
|
|
4690
|
+
return parts;
|
|
4691
|
+
}
|
|
4692
|
+
|
|
4693
|
+
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
4694
|
+
/**
|
|
4695
|
+
* Wall-clock fields of `date` in `timeZone` (an IANA name such as
|
|
4696
|
+
* `Europe/Istanbul`). Without a time zone the runtime's local zone is used.
|
|
4697
|
+
*/
|
|
4698
|
+
function getFields(date, timeZone) {
|
|
4699
|
+
if (!timeZone) {
|
|
4700
|
+
return {
|
|
4701
|
+
year: date.getFullYear(),
|
|
4702
|
+
month: date.getMonth() + 1,
|
|
4703
|
+
day: date.getDate(),
|
|
4704
|
+
hour: date.getHours(),
|
|
4705
|
+
minute: date.getMinutes(),
|
|
4706
|
+
second: date.getSeconds(),
|
|
4707
|
+
millisecond: date.getMilliseconds(),
|
|
4708
|
+
weekday: date.getDay(),
|
|
4709
|
+
};
|
|
4710
|
+
}
|
|
4711
|
+
const parts = dateTimeFormat("en-US", {
|
|
4712
|
+
timeZone,
|
|
4713
|
+
hourCycle: "h23",
|
|
4714
|
+
era: "short",
|
|
4715
|
+
year: "numeric",
|
|
4716
|
+
month: "numeric",
|
|
4717
|
+
day: "numeric",
|
|
4718
|
+
hour: "numeric",
|
|
4719
|
+
minute: "numeric",
|
|
4720
|
+
second: "numeric",
|
|
4721
|
+
weekday: "short",
|
|
4722
|
+
}).formatToParts(date);
|
|
4723
|
+
const read = (type) => Number(partValue(parts, type));
|
|
4724
|
+
const year = read("year");
|
|
4725
|
+
return {
|
|
4726
|
+
year: partValue(parts, "era") === "BC" ? 1 - year : year,
|
|
4727
|
+
month: read("month"),
|
|
4728
|
+
day: read("day"),
|
|
4729
|
+
hour: read("hour"),
|
|
4730
|
+
minute: read("minute"),
|
|
4731
|
+
second: read("second"),
|
|
4732
|
+
millisecond: date.getUTCMilliseconds(),
|
|
4733
|
+
weekday: WEEKDAYS.indexOf(partValue(parts, "weekday") ?? ""),
|
|
4734
|
+
};
|
|
4735
|
+
}
|
|
4736
|
+
/** UTC timestamp of the given wall-clock fields, as if they were in UTC. Handles years 0-99. */
|
|
4737
|
+
function utcFromFields(fields) {
|
|
4738
|
+
const date = new Date(0);
|
|
4739
|
+
date.setUTCFullYear(fields.year, fields.month - 1, fields.day);
|
|
4740
|
+
date.setUTCHours(fields.hour, fields.minute, fields.second, fields.millisecond);
|
|
4741
|
+
return date.getTime();
|
|
4742
|
+
}
|
|
4743
|
+
/** Offset of `timeZone` from UTC at `date`, in minutes (east positive, e.g. `+180` for Istanbul). */
|
|
4744
|
+
function offsetMinutes(date, timeZone) {
|
|
4745
|
+
if (!timeZone)
|
|
4746
|
+
return -date.getTimezoneOffset();
|
|
4747
|
+
return Math.round((utcFromFields(getFields(date, timeZone)) - date.getTime()) / 60_000);
|
|
4748
|
+
}
|
|
4749
|
+
/**
|
|
4750
|
+
* The instant at which the wall clock in `timeZone` shows `fields`. In a DST
|
|
4751
|
+
* gap the time is shifted forward, matching how `Date` handles local times.
|
|
4752
|
+
*/
|
|
4753
|
+
function fromFields(fields, timeZone) {
|
|
4754
|
+
if (!timeZone) {
|
|
4755
|
+
const date = new Date(0);
|
|
4756
|
+
date.setFullYear(fields.year, fields.month - 1, fields.day);
|
|
4757
|
+
date.setHours(fields.hour, fields.minute, fields.second, fields.millisecond);
|
|
4758
|
+
return date;
|
|
4759
|
+
}
|
|
4760
|
+
const guess = utcFromFields(fields);
|
|
4761
|
+
const first = offsetMinutes(new Date(guess), timeZone);
|
|
4762
|
+
const time = guess - first * 60_000;
|
|
4763
|
+
const second = offsetMinutes(new Date(time), timeZone);
|
|
4764
|
+
return new Date(second === first ? time : guess - second * 60_000);
|
|
4765
|
+
}
|
|
4766
|
+
/** Number of days in a month (`month` is 1-12). */
|
|
4767
|
+
function daysInMonth(year, month) {
|
|
4768
|
+
return new Date(utcFromFields({ year, month: month + 1, day: 0, hour: 0, minute: 0, second: 0, millisecond: 0 })).getUTCDate();
|
|
4769
|
+
}
|
|
4770
|
+
/** Formats an offset in minutes as `+03:00` (or `+0300` without a separator). */
|
|
4771
|
+
function formatOffset(minutes, separator = ":") {
|
|
4772
|
+
const sign = minutes < 0 ? "-" : "+";
|
|
4773
|
+
const abs = Math.abs(minutes);
|
|
4774
|
+
return `${sign}${pad(Math.floor(abs / 60))}${separator}${pad(abs % 60)}`;
|
|
4775
|
+
}
|
|
4776
|
+
/** `true` when `timeZone` is a zone the runtime's `Intl` understands. */
|
|
4777
|
+
function isValidTimeZone(timeZone) {
|
|
4778
|
+
try {
|
|
4779
|
+
dateTimeFormat(undefined, { timeZone });
|
|
4780
|
+
return true;
|
|
4781
|
+
}
|
|
4782
|
+
catch {
|
|
4783
|
+
return false;
|
|
4784
|
+
}
|
|
4785
|
+
}
|
|
4786
|
+
function pad(value, length = 2) {
|
|
4787
|
+
return String(Math.abs(value)).padStart(length, "0");
|
|
4788
|
+
}
|
|
4789
|
+
|
|
4790
|
+
const hour12 = (hour) => hour % 12 || 12;
|
|
4791
|
+
const FORMATTERS = {
|
|
4792
|
+
YYYY: ({ fields }) => (fields.year < 0 ? "-" : "") + pad(fields.year, 4),
|
|
4793
|
+
YY: ({ fields }) => pad(fields.year % 100),
|
|
4794
|
+
MMMM: ({ fields, locale }) => monthNames(locale, "long")[fields.month - 1],
|
|
4795
|
+
MMM: ({ fields, locale }) => monthNames(locale, "short")[fields.month - 1],
|
|
4796
|
+
MM: ({ fields }) => pad(fields.month),
|
|
4797
|
+
M: ({ fields }) => String(fields.month),
|
|
4798
|
+
DD: ({ fields }) => pad(fields.day),
|
|
4799
|
+
D: ({ fields }) => String(fields.day),
|
|
4800
|
+
dddd: ({ fields, locale }) => weekdayNames(locale, "long")[fields.weekday],
|
|
4801
|
+
ddd: ({ fields, locale }) => weekdayNames(locale, "short")[fields.weekday],
|
|
4802
|
+
d: ({ fields }) => String(fields.weekday),
|
|
4803
|
+
HH: ({ fields }) => pad(fields.hour),
|
|
4804
|
+
H: ({ fields }) => String(fields.hour),
|
|
4805
|
+
hh: ({ fields }) => pad(hour12(fields.hour)),
|
|
4806
|
+
h: ({ fields }) => String(hour12(fields.hour)),
|
|
4807
|
+
mm: ({ fields }) => pad(fields.minute),
|
|
4808
|
+
m: ({ fields }) => String(fields.minute),
|
|
4809
|
+
ss: ({ fields }) => pad(fields.second),
|
|
4810
|
+
s: ({ fields }) => String(fields.second),
|
|
4811
|
+
SSS: ({ fields }) => pad(fields.millisecond, 3),
|
|
4812
|
+
A: ({ fields, locale }) => dayPeriods(locale)[fields.hour < 12 ? 0 : 1],
|
|
4813
|
+
a: ({ fields, locale }) => dayPeriods(locale)[fields.hour < 12 ? 0 : 1].toLocaleLowerCase(locale),
|
|
4814
|
+
ZZ: ({ date, timeZone }) => formatOffset(offsetMinutes(date, timeZone), ""),
|
|
4815
|
+
Z: ({ date, timeZone }) => formatOffset(offsetMinutes(date, timeZone)),
|
|
4816
|
+
X: ({ date }) => String(Math.floor(date.getTime() / 1000)),
|
|
4817
|
+
x: ({ date }) => String(date.getTime()),
|
|
4818
|
+
};
|
|
4819
|
+
/**
|
|
4820
|
+
* Formats `date` with a moment-style pattern (see `tokens.ts`). Numbers are
|
|
4821
|
+
* always Latin digits; month, weekday and day-period names come from `Intl`
|
|
4822
|
+
* in the given locale.
|
|
4823
|
+
*/
|
|
4824
|
+
function formatPattern(date, pattern, locale, timeZone) {
|
|
4825
|
+
const context = { date, fields: getFields(date, timeZone), locale, timeZone };
|
|
4826
|
+
return tokenize(pattern)
|
|
4827
|
+
.map(part => ("token" in part ? FORMATTERS[part.token](context) : part.literal))
|
|
4828
|
+
.join("");
|
|
4829
|
+
}
|
|
4830
|
+
|
|
4831
|
+
const SECOND$1 = 1000;
|
|
4832
|
+
const MINUTE$1 = 60 * SECOND$1;
|
|
4833
|
+
const HOUR = 60 * MINUTE$1;
|
|
4834
|
+
const DAY = 24 * HOUR;
|
|
4835
|
+
const WEEK = 7 * DAY;
|
|
4836
|
+
const MONTH = 30 * DAY;
|
|
4837
|
+
const YEAR = 365 * DAY;
|
|
4838
|
+
const UNITS = [
|
|
4839
|
+
["year", YEAR],
|
|
4840
|
+
["month", MONTH],
|
|
4841
|
+
["week", WEEK],
|
|
4842
|
+
["day", DAY],
|
|
4843
|
+
["hour", HOUR],
|
|
4844
|
+
["minute", MINUTE$1],
|
|
4845
|
+
];
|
|
4846
|
+
/**
|
|
4847
|
+
* Formats `date` relative to `now` ("3 minutes ago", "in 2 days", "now").
|
|
4848
|
+
* Picks the largest unit that fits and truncates toward zero, so "5 minutes
|
|
4849
|
+
* ago" holds until the sixth minute has passed.
|
|
4850
|
+
*/
|
|
4851
|
+
function formatRelative(date, now, locale) {
|
|
4852
|
+
const diff = date.getTime() - now;
|
|
4853
|
+
const abs = Math.abs(diff);
|
|
4854
|
+
const [unit, size] = UNITS.find(([, size]) => abs >= size) ?? ["second", SECOND$1];
|
|
4855
|
+
// `|| 0` turns -0 into 0, which formats as "now" instead of "0 seconds ago".
|
|
4856
|
+
return relativeTimeFormat(locale).format(Math.trunc(diff / size) || 0, unit);
|
|
4857
|
+
}
|
|
4858
|
+
/**
|
|
4859
|
+
* How often a relative date needs re-rendering. Labels change fastest close to
|
|
4860
|
+
* now, so the cadence slows down as the date gets further away.
|
|
4861
|
+
*/
|
|
4862
|
+
function relativePeriod(date, now) {
|
|
4863
|
+
const abs = Math.abs(date.getTime() - now);
|
|
4864
|
+
if (abs < HOUR)
|
|
4865
|
+
return SECOND$1;
|
|
4866
|
+
if (abs < DAY)
|
|
4867
|
+
return MINUTE$1;
|
|
4868
|
+
return 15 * MINUTE$1;
|
|
4869
|
+
}
|
|
4870
|
+
|
|
4871
|
+
const PRESETS = ["short", "medium", "long", "full"];
|
|
4872
|
+
const DEFAULT_PRESET = "medium";
|
|
4873
|
+
/**
|
|
4874
|
+
* Interprets the `format`, `date-style` and `time-style` attributes.
|
|
4875
|
+
*
|
|
4876
|
+
* - `relative` → relative time ("3 minutes ago").
|
|
4877
|
+
* - A preset name (`short`, `medium`, `long`, `full`) or empty → `Intl`
|
|
4878
|
+
* `dateStyle` + `timeStyle`, each overridable (or omitted with `none`).
|
|
4879
|
+
* - Anything else → a moment-style token pattern.
|
|
4880
|
+
*/
|
|
4881
|
+
function resolveFormat(format, dateStyle, timeStyle) {
|
|
4882
|
+
const value = format.trim();
|
|
4883
|
+
if (value === "relative")
|
|
4884
|
+
return { kind: "relative" };
|
|
4885
|
+
if (value && !PRESETS.includes(value))
|
|
4886
|
+
return { kind: "pattern", pattern: format };
|
|
4887
|
+
const preset = (value || DEFAULT_PRESET);
|
|
4888
|
+
const date = pickStyle(dateStyle, preset);
|
|
4889
|
+
const time = pickStyle(timeStyle, preset);
|
|
4890
|
+
// Both parts omitted would render nothing; keep the date.
|
|
4891
|
+
if (!date && !time)
|
|
4892
|
+
return { kind: "preset", dateStyle: preset };
|
|
4893
|
+
return { kind: "preset", dateStyle: date, timeStyle: time };
|
|
4894
|
+
}
|
|
4895
|
+
function pickStyle(override, preset) {
|
|
4896
|
+
if (override === "none")
|
|
4897
|
+
return undefined;
|
|
4898
|
+
return override && PRESETS.includes(override) ? override : preset;
|
|
4899
|
+
}
|
|
4900
|
+
/** Renders `date` according to a resolved format. */
|
|
4901
|
+
function formatDate(date, spec, { locale, timeZone, now }) {
|
|
4902
|
+
switch (spec.kind) {
|
|
4903
|
+
case "relative":
|
|
4904
|
+
return formatRelative(date, now, locale);
|
|
4905
|
+
case "pattern":
|
|
4906
|
+
return formatPattern(date, spec.pattern, locale, timeZone);
|
|
4907
|
+
case "preset":
|
|
4908
|
+
return dateTimeFormat(locale, { dateStyle: spec.dateStyle, timeStyle: spec.timeStyle, timeZone }).format(date);
|
|
4909
|
+
}
|
|
4910
|
+
}
|
|
4911
|
+
/** A full, unambiguous rendering used for the tooltip of relative dates. */
|
|
4912
|
+
function formatFull(date, locale, timeZone) {
|
|
4913
|
+
return dateTimeFormat(locale, { dateStyle: "full", timeStyle: "long", timeZone }).format(date);
|
|
4914
|
+
}
|
|
4915
|
+
|
|
4916
|
+
/**
|
|
4917
|
+
* Resolves the locale for an element: its `locale` attribute, then the
|
|
4918
|
+
* nearest `lang` attribute (crossing shadow roots), then the browser default
|
|
4919
|
+
* (`undefined`). Invalid tags are skipped.
|
|
4920
|
+
*/
|
|
4921
|
+
function resolveLocale(host, explicit) {
|
|
4922
|
+
return canonical(explicit) ?? canonical(closestLang(host));
|
|
4923
|
+
}
|
|
4924
|
+
function closestLang(host) {
|
|
4925
|
+
let node = host;
|
|
4926
|
+
while (node) {
|
|
4927
|
+
if (node instanceof Element) {
|
|
4928
|
+
const lang = node.closest("[lang]")?.getAttribute("lang");
|
|
4929
|
+
if (lang)
|
|
4930
|
+
return lang;
|
|
4931
|
+
}
|
|
4932
|
+
const root = node.getRootNode();
|
|
4933
|
+
node = root instanceof ShadowRoot ? root.host : null;
|
|
4934
|
+
}
|
|
4935
|
+
return "";
|
|
4936
|
+
}
|
|
4937
|
+
function canonical(tag) {
|
|
4938
|
+
if (!tag?.trim())
|
|
4939
|
+
return undefined;
|
|
4940
|
+
try {
|
|
4941
|
+
return Intl.getCanonicalLocales(tag.trim())[0];
|
|
4942
|
+
}
|
|
4943
|
+
catch {
|
|
4944
|
+
return undefined;
|
|
4945
|
+
}
|
|
4946
|
+
}
|
|
4947
|
+
|
|
4948
|
+
/**
|
|
4949
|
+
* Turns parsed fields into an instant, or `undefined` when a field is out of
|
|
4950
|
+
* range (e.g. month 13 or 31 February).
|
|
4951
|
+
*
|
|
4952
|
+
* Defaults follow moment: a missing year is the current one; when no date
|
|
4953
|
+
* part was given at all the date is today; a missing month/day is 1; missing
|
|
4954
|
+
* time fields are 0. Wall times are read in `timeZone` (local when unset)
|
|
4955
|
+
* unless the input carried its own offset.
|
|
4956
|
+
*/
|
|
4957
|
+
function assemble(parsed, { timeZone, now }) {
|
|
4958
|
+
if (parsed.timestamp !== undefined)
|
|
4959
|
+
return Number.isFinite(parsed.timestamp) ? new Date(parsed.timestamp) : undefined;
|
|
4960
|
+
const today = getFields(new Date(now), timeZone);
|
|
4961
|
+
const hasDate = parsed.year !== undefined || parsed.month !== undefined || parsed.day !== undefined;
|
|
4962
|
+
const fields = {
|
|
4963
|
+
year: parsed.year ?? today.year,
|
|
4964
|
+
month: parsed.month ?? (hasDate ? 1 : today.month),
|
|
4965
|
+
day: parsed.day ?? (hasDate ? 1 : today.day),
|
|
4966
|
+
hour: parsed.hour ?? 0,
|
|
4967
|
+
minute: parsed.minute ?? 0,
|
|
4968
|
+
second: parsed.second ?? 0,
|
|
4969
|
+
millisecond: parsed.millisecond ?? 0,
|
|
4970
|
+
};
|
|
4971
|
+
if (parsed.pm !== undefined) {
|
|
4972
|
+
if (fields.hour < 1 || fields.hour > 12)
|
|
4973
|
+
return undefined;
|
|
4974
|
+
fields.hour = (fields.hour % 12) + (parsed.pm ? 12 : 0);
|
|
4975
|
+
}
|
|
4976
|
+
if (!inRange(fields))
|
|
4977
|
+
return undefined;
|
|
4978
|
+
if (parsed.offset !== undefined)
|
|
4979
|
+
return new Date(utcFromFields(fields) - parsed.offset * 60_000);
|
|
4980
|
+
return fromFields(fields, timeZone);
|
|
4981
|
+
}
|
|
4982
|
+
function inRange(f) {
|
|
4983
|
+
return (Object.values(f).every(Number.isInteger) &&
|
|
4984
|
+
f.month >= 1 &&
|
|
4985
|
+
f.month <= 12 &&
|
|
4986
|
+
f.day >= 1 &&
|
|
4987
|
+
f.day <= daysInMonth(f.year, f.month) &&
|
|
4988
|
+
f.hour >= 0 &&
|
|
4989
|
+
f.hour <= 23 &&
|
|
4990
|
+
f.minute >= 0 &&
|
|
4991
|
+
f.minute <= 59 &&
|
|
4992
|
+
f.second >= 0 &&
|
|
4993
|
+
f.second <= 59 &&
|
|
4994
|
+
f.millisecond >= 0 &&
|
|
4995
|
+
f.millisecond <= 999);
|
|
4996
|
+
}
|
|
4997
|
+
/** Two-digit years: `00`-`68` → 2000s, `69`-`99` → 1900s (same pivot as moment). */
|
|
4998
|
+
function expandYear(digits) {
|
|
4999
|
+
const value = Number(digits);
|
|
5000
|
+
if (digits.length > 2)
|
|
5001
|
+
return value;
|
|
5002
|
+
return value + (value <= 68 ? 2000 : 1900);
|
|
5003
|
+
}
|
|
5004
|
+
/** Fractional seconds (`5`, `50`, `500`, `500123`) → milliseconds. */
|
|
5005
|
+
function fractionToMs(digits) {
|
|
5006
|
+
return Number(digits.slice(0, 3).padEnd(3, "0"));
|
|
5007
|
+
}
|
|
5008
|
+
/** `Z`, `+03:00`, `+0300` or `+03` → minutes east of UTC. */
|
|
5009
|
+
function parseOffset(value) {
|
|
5010
|
+
if (/^z$/i.test(value))
|
|
5011
|
+
return 0;
|
|
5012
|
+
const match = /^([+-])(\d{2}):?(\d{2})?$/.exec(value);
|
|
5013
|
+
if (!match)
|
|
5014
|
+
return undefined;
|
|
5015
|
+
const minutes = Number(match[2]) * 60 + Number(match[3] ?? 0);
|
|
5016
|
+
return match[1] === "-" ? -minutes : minutes;
|
|
5017
|
+
}
|
|
5018
|
+
|
|
5019
|
+
const ISO = /^([+-]\d{6}|\d{4})-(\d{2})(?:-(\d{2}))?(?:[T\s](\d{2}):(\d{2})(?::(\d{2})(?:[.,](\d+))?)?\s*(Z|[+-]\d{2}(?::?\d{2})?)?)?$/i;
|
|
5020
|
+
const EPOCH = /^-?\d+$/;
|
|
5021
|
+
/**
|
|
5022
|
+
* Parses ISO 8601 (`2024-01-22`, `2024-01-22T14:30`, `2024-01-22T14:30:05.123+03:00`)
|
|
5023
|
+
* and epoch milliseconds (`1705923000000`).
|
|
5024
|
+
*
|
|
5025
|
+
* Unlike `Date.parse`, a date-only ISO string is a local (or `time-zone`) date
|
|
5026
|
+
* rather than UTC midnight, so `2024-01-22` never renders as the 21st.
|
|
5027
|
+
*/
|
|
5028
|
+
function parseIso(input) {
|
|
5029
|
+
if (EPOCH.test(input) && input.replace("-", "").length > 4)
|
|
5030
|
+
return { timestamp: Number(input) };
|
|
5031
|
+
const match = ISO.exec(input);
|
|
5032
|
+
if (!match)
|
|
5033
|
+
return undefined;
|
|
5034
|
+
const [, year, month, day, hour, minute, second, fraction, offset] = match;
|
|
5035
|
+
const parsed = {
|
|
5036
|
+
year: expandYear(year),
|
|
5037
|
+
month: Number(month),
|
|
5038
|
+
day: day === undefined ? 1 : Number(day),
|
|
5039
|
+
};
|
|
5040
|
+
if (hour !== undefined) {
|
|
5041
|
+
parsed.hour = Number(hour);
|
|
5042
|
+
parsed.minute = Number(minute);
|
|
5043
|
+
parsed.second = second === undefined ? 0 : Number(second);
|
|
5044
|
+
parsed.millisecond = fraction === undefined ? 0 : fractionToMs(fraction);
|
|
5045
|
+
}
|
|
5046
|
+
if (offset !== undefined) {
|
|
5047
|
+
parsed.offset = parseOffset(offset.toUpperCase());
|
|
5048
|
+
if (parsed.offset === undefined)
|
|
5049
|
+
return undefined;
|
|
5050
|
+
}
|
|
5051
|
+
return parsed;
|
|
5052
|
+
}
|
|
5053
|
+
|
|
5054
|
+
const NUMERIC = /^(\d{1,4})\s*[./-]\s*(\d{1,4})\s*[./-]\s*(\d{1,4})(?:(?:\s+|\s*[T,]\s*)(\d{1,2})[:.](\d{2})(?:[:.](\d{2}))?\s*(am|pm)?)?$/i;
|
|
5055
|
+
const orders = new Map();
|
|
5056
|
+
/**
|
|
5057
|
+
* Order of day, month and year in the locale's numeric date
|
|
5058
|
+
* (`en-US` → month/day/year, `tr`/`en-GB` → day/month/year, `ja` → year/month/day).
|
|
5059
|
+
*/
|
|
5060
|
+
function localeDateOrder(locale) {
|
|
5061
|
+
const key = locale ?? "";
|
|
5062
|
+
let order = orders.get(key);
|
|
5063
|
+
if (!order) {
|
|
5064
|
+
order = dateTimeFormat(locale, { timeZone: "UTC", year: "numeric", month: "numeric", day: "numeric" })
|
|
5065
|
+
.formatToParts(Date.UTC(2000, 0, 2))
|
|
5066
|
+
.map(part => part.type)
|
|
5067
|
+
.filter((type) => type === "year" || type === "month" || type === "day");
|
|
5068
|
+
orders.set(key, order);
|
|
5069
|
+
}
|
|
5070
|
+
return order;
|
|
5071
|
+
}
|
|
5072
|
+
/**
|
|
5073
|
+
* Fallback for numeric dates without an `input-format`, such as `22/01/2024`
|
|
5074
|
+
* or `22.01.2024 14:30`. Day, month and year are read in the locale's order,
|
|
5075
|
+
* so `01/02/2024` is 1 February in `tr` and January 2 in `en-US`. Values that
|
|
5076
|
+
* don't fit that order (day 22 as a US month) are rejected, never swapped.
|
|
5077
|
+
*/
|
|
5078
|
+
function parseNumeric(input, locale) {
|
|
5079
|
+
const match = NUMERIC.exec(input);
|
|
5080
|
+
const order = localeDateOrder(locale);
|
|
5081
|
+
if (!match || order.length !== 3)
|
|
5082
|
+
return undefined;
|
|
5083
|
+
const [, first, second, third, hour, minute, seconds, period] = match;
|
|
5084
|
+
const parsed = {};
|
|
5085
|
+
[first, second, third].forEach((value, index) => {
|
|
5086
|
+
const part = order[index];
|
|
5087
|
+
parsed[part] = part === "year" ? expandYear(value) : Number(value);
|
|
5088
|
+
});
|
|
5089
|
+
if (hour !== undefined) {
|
|
5090
|
+
parsed.hour = Number(hour);
|
|
5091
|
+
parsed.minute = Number(minute);
|
|
5092
|
+
parsed.second = seconds === undefined ? 0 : Number(seconds);
|
|
5093
|
+
if (period)
|
|
5094
|
+
parsed.pm = period.toLowerCase() === "pm";
|
|
5095
|
+
}
|
|
5096
|
+
return parsed;
|
|
5097
|
+
}
|
|
5098
|
+
|
|
5099
|
+
const number = (source, key) => ({
|
|
5100
|
+
source: () => source,
|
|
5101
|
+
apply: (parsed, value) => {
|
|
5102
|
+
parsed[key] = Number(value);
|
|
5103
|
+
},
|
|
5104
|
+
});
|
|
5105
|
+
const names = (list, key) => ({
|
|
5106
|
+
source: locale => alternation(list(locale)),
|
|
5107
|
+
apply: (parsed, value, locale) => {
|
|
5108
|
+
if (!key)
|
|
5109
|
+
return true;
|
|
5110
|
+
const index = indexOfName(list(locale), value, locale);
|
|
5111
|
+
if (index === -1)
|
|
5112
|
+
return false;
|
|
5113
|
+
parsed[key] = index + 1;
|
|
5114
|
+
return true;
|
|
5115
|
+
},
|
|
5116
|
+
});
|
|
5117
|
+
const period = {
|
|
5118
|
+
source: locale => alternation([...dayPeriods(locale), "am", "pm"]),
|
|
5119
|
+
apply: (parsed, value, locale) => {
|
|
5120
|
+
const [am, pm] = dayPeriods(locale).map(name => name.toLocaleLowerCase(locale));
|
|
5121
|
+
const lower = value.toLocaleLowerCase(locale);
|
|
5122
|
+
parsed.pm = lower === pm || lower === "pm" ? true : lower === am || lower === "am" ? false : undefined;
|
|
5123
|
+
},
|
|
5124
|
+
};
|
|
5125
|
+
const offset = {
|
|
5126
|
+
source: () => "Z|[+-]\\d{2}:?\\d{2}",
|
|
5127
|
+
apply: (parsed, value) => {
|
|
5128
|
+
parsed.offset = parseOffset(value.toUpperCase());
|
|
5129
|
+
},
|
|
5130
|
+
};
|
|
5131
|
+
/** Parsing is lenient like moment: padded tokens also accept one digit. */
|
|
5132
|
+
const RULES = {
|
|
5133
|
+
YYYY: { source: () => "[+-]?\\d{4,6}", apply: (parsed, value) => void (parsed.year = Number(value)) },
|
|
5134
|
+
YY: { source: () => "\\d{2}", apply: (parsed, value) => void (parsed.year = expandYear(value)) },
|
|
5135
|
+
MMMM: names(locale => monthNames(locale, "long"), "month"),
|
|
5136
|
+
MMM: names(locale => monthNames(locale, "short"), "month"),
|
|
5137
|
+
MM: number("\\d{1,2}", "month"),
|
|
5138
|
+
M: number("\\d{1,2}", "month"),
|
|
5139
|
+
DD: number("\\d{1,2}", "day"),
|
|
5140
|
+
D: number("\\d{1,2}", "day"),
|
|
5141
|
+
dddd: names(locale => weekdayNames(locale, "long")),
|
|
5142
|
+
ddd: names(locale => weekdayNames(locale, "short")),
|
|
5143
|
+
d: { source: () => "[0-6]", apply: () => undefined },
|
|
5144
|
+
HH: number("\\d{1,2}", "hour"),
|
|
5145
|
+
H: number("\\d{1,2}", "hour"),
|
|
5146
|
+
hh: number("\\d{1,2}", "hour"),
|
|
5147
|
+
h: number("\\d{1,2}", "hour"),
|
|
5148
|
+
mm: number("\\d{1,2}", "minute"),
|
|
5149
|
+
m: number("\\d{1,2}", "minute"),
|
|
5150
|
+
ss: number("\\d{1,2}", "second"),
|
|
5151
|
+
s: number("\\d{1,2}", "second"),
|
|
5152
|
+
SSS: { source: () => "\\d{1,9}", apply: (parsed, value) => void (parsed.millisecond = fractionToMs(value)) },
|
|
5153
|
+
A: period,
|
|
5154
|
+
a: period,
|
|
5155
|
+
ZZ: offset,
|
|
5156
|
+
Z: offset,
|
|
5157
|
+
X: { source: () => "-?\\d+(?:\\.\\d+)?", apply: (parsed, value) => void (parsed.timestamp = Math.round(Number(value) * 1000)) },
|
|
5158
|
+
x: { source: () => "-?\\d+", apply: (parsed, value) => void (parsed.timestamp = Number(value)) },
|
|
5159
|
+
};
|
|
5160
|
+
const cache = new Map();
|
|
5161
|
+
function compile(pattern, locale) {
|
|
5162
|
+
const key = `${locale ?? ""}|${pattern}`;
|
|
5163
|
+
let compiled = cache.get(key);
|
|
5164
|
+
if (!compiled) {
|
|
5165
|
+
const tokens = [];
|
|
5166
|
+
const source = tokenize(pattern)
|
|
5167
|
+
.map(part => {
|
|
5168
|
+
if ("literal" in part)
|
|
5169
|
+
return escape(part.literal).replace(/\s+/g, "\\s+");
|
|
5170
|
+
tokens.push(part.token);
|
|
5171
|
+
return `(${RULES[part.token].source(locale)})`;
|
|
5172
|
+
})
|
|
5173
|
+
.join("");
|
|
5174
|
+
compiled = { regex: new RegExp(`^${source}$`, "iu"), tokens };
|
|
5175
|
+
cache.set(key, compiled);
|
|
5176
|
+
}
|
|
5177
|
+
return compiled;
|
|
5178
|
+
}
|
|
5179
|
+
/**
|
|
5180
|
+
* Parses `input` with a moment-style pattern (`DD/MM/YYYY HH:mm`). Month,
|
|
5181
|
+
* weekday and AM/PM names are matched case-insensitively in `locale`;
|
|
5182
|
+
* whitespace in the pattern matches any run of whitespace.
|
|
5183
|
+
*/
|
|
5184
|
+
function parsePattern(input, pattern, locale) {
|
|
5185
|
+
const { regex, tokens } = compile(pattern, locale);
|
|
5186
|
+
const match = regex.exec(input);
|
|
5187
|
+
if (!match)
|
|
5188
|
+
return undefined;
|
|
5189
|
+
const parsed = {};
|
|
5190
|
+
for (const [index, token] of tokens.entries()) {
|
|
5191
|
+
if (RULES[token].apply(parsed, match[index + 1], locale) === false)
|
|
5192
|
+
return undefined;
|
|
5193
|
+
}
|
|
5194
|
+
if (parsed.pm !== undefined && !tokens.some(token => token === "h" || token === "hh")) {
|
|
5195
|
+
// AM/PM next to a 24-hour token: trust the 24-hour value.
|
|
5196
|
+
parsed.pm = undefined;
|
|
5197
|
+
}
|
|
5198
|
+
return parsed;
|
|
5199
|
+
}
|
|
5200
|
+
function alternation(list) {
|
|
5201
|
+
return [...new Set(list.filter(Boolean))]
|
|
5202
|
+
.sort((a, b) => b.length - a.length)
|
|
5203
|
+
.map(escape)
|
|
5204
|
+
.join("|");
|
|
5205
|
+
}
|
|
5206
|
+
function indexOfName(list, value, locale) {
|
|
5207
|
+
const lower = value.toLocaleLowerCase(locale);
|
|
5208
|
+
return list.findIndex(name => name.toLocaleLowerCase(locale) === lower);
|
|
5209
|
+
}
|
|
5210
|
+
function escape(text) {
|
|
5211
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
5212
|
+
}
|
|
5213
|
+
|
|
5214
|
+
/**
|
|
5215
|
+
* Parses a date input. Returns `undefined` for empty or invalid input.
|
|
5216
|
+
*
|
|
5217
|
+
* - `Date` → used as is (if valid). `number` → epoch milliseconds.
|
|
5218
|
+
* - Strings with `inputFormat` → that pattern only.
|
|
5219
|
+
* - Other strings → ISO 8601 / epoch ms, then a numeric date in the locale's
|
|
5220
|
+
* day/month/year order (`22/01/2024`, `22.01.2024 14:30`).
|
|
5221
|
+
*/
|
|
5222
|
+
function parseDate(input, options) {
|
|
5223
|
+
if (input instanceof Date)
|
|
5224
|
+
return valid(new Date(input.getTime()));
|
|
5225
|
+
if (typeof input === "number")
|
|
5226
|
+
return valid(new Date(input));
|
|
5227
|
+
const text = input?.trim();
|
|
5228
|
+
if (!text)
|
|
5229
|
+
return undefined;
|
|
5230
|
+
const parsed = options.inputFormat?.trim()
|
|
5231
|
+
? parsePattern(text, options.inputFormat, options.locale)
|
|
5232
|
+
: (parseIso(text) ?? parseNumeric(text, options.locale));
|
|
5233
|
+
return valid(parsed && assemble(parsed, options));
|
|
5234
|
+
}
|
|
5235
|
+
/** Drops `Invalid Date`s (NaN, or beyond the ±8.64e15 ms range `Date` supports). */
|
|
5236
|
+
function valid(date) {
|
|
5237
|
+
return date && !Number.isNaN(date.getTime()) ? date : undefined;
|
|
5238
|
+
}
|
|
5239
|
+
|
|
5240
|
+
const SECOND = 1000;
|
|
5241
|
+
const MINUTE = 60 * SECOND;
|
|
5242
|
+
/**
|
|
5243
|
+
* Tick periods per unit. Hour and day changes are checked every 15 minutes:
|
|
5244
|
+
* every current UTC offset is a multiple of 15 minutes, so a 15-minute,
|
|
5245
|
+
* epoch-aligned tick lands exactly on every zone's hour and midnight.
|
|
5246
|
+
* Milliseconds tick at ~20 fps; faster is invisible.
|
|
5247
|
+
*/
|
|
5248
|
+
const PERIODS = {
|
|
5249
|
+
millisecond: 50,
|
|
5250
|
+
second: SECOND,
|
|
5251
|
+
minute: MINUTE,
|
|
5252
|
+
hour: 15 * MINUTE,
|
|
5253
|
+
day: 15 * MINUTE,
|
|
5254
|
+
};
|
|
5255
|
+
const TOKEN_UNITS = {
|
|
5256
|
+
SSS: "millisecond",
|
|
5257
|
+
x: "millisecond",
|
|
5258
|
+
ss: "second",
|
|
5259
|
+
s: "second",
|
|
5260
|
+
X: "second",
|
|
5261
|
+
mm: "minute",
|
|
5262
|
+
m: "minute",
|
|
5263
|
+
HH: "hour",
|
|
5264
|
+
H: "hour",
|
|
5265
|
+
hh: "hour",
|
|
5266
|
+
h: "hour",
|
|
5267
|
+
A: "hour",
|
|
5268
|
+
a: "hour",
|
|
5269
|
+
// Offsets change at DST transitions, which happen on the hour.
|
|
5270
|
+
ZZ: "hour",
|
|
5271
|
+
Z: "hour",
|
|
5272
|
+
};
|
|
5273
|
+
const TIME_STYLE_UNITS = {
|
|
5274
|
+
short: "minute",
|
|
5275
|
+
medium: "second",
|
|
5276
|
+
long: "second",
|
|
5277
|
+
full: "second",
|
|
5278
|
+
};
|
|
5279
|
+
/**
|
|
5280
|
+
* How often (ms) a date rendered with `spec` can change. Ticks are aligned to
|
|
5281
|
+
* multiples of this period, so second/minute changes show up exactly on time.
|
|
5282
|
+
*/
|
|
5283
|
+
function tickPeriod(spec, date, now) {
|
|
5284
|
+
switch (spec.kind) {
|
|
5285
|
+
case "relative":
|
|
5286
|
+
return relativePeriod(date, now);
|
|
5287
|
+
case "preset":
|
|
5288
|
+
return PERIODS[spec.timeStyle ? TIME_STYLE_UNITS[spec.timeStyle] : "day"];
|
|
5289
|
+
case "pattern":
|
|
5290
|
+
return Math.min(PERIODS.day, ...tokenize(spec.pattern).map(part => ("token" in part ? PERIODS[TOKEN_UNITS[part.token] ?? "day"] : PERIODS.day)));
|
|
5291
|
+
}
|
|
5292
|
+
}
|
|
5293
|
+
|
|
5294
|
+
const dateStyles = css `
|
|
5295
|
+
:host {
|
|
5296
|
+
display: inline;
|
|
5297
|
+
}
|
|
5298
|
+
|
|
5299
|
+
:host([hidden]) {
|
|
5300
|
+
display: none;
|
|
5301
|
+
}
|
|
5302
|
+
|
|
5303
|
+
/* Tabular digits keep live dates from jittering as numbers change. */
|
|
5304
|
+
time {
|
|
5305
|
+
font-variant-numeric: var(--tp-date-numeric, tabular-nums);
|
|
5306
|
+
}
|
|
5307
|
+
`;
|
|
5308
|
+
|
|
5309
|
+
/**
|
|
5310
|
+
* One shared clock for every `<tp-date>`. Elements subscribe with the period
|
|
5311
|
+
* they need; each period gets a single timer aligned to its boundaries (e.g.
|
|
5312
|
+
* the start of every minute), so a page full of dates updates in sync with a
|
|
5313
|
+
* handful of timers. `setTimeout` is re-armed from the real clock on every
|
|
5314
|
+
* tick, so it never drifts the way `setInterval` does.
|
|
5315
|
+
*
|
|
5316
|
+
* Timers stop while the document is hidden and every subscriber is notified
|
|
5317
|
+
* right away when it becomes visible again.
|
|
5318
|
+
*/
|
|
5319
|
+
const channels = new Map();
|
|
5320
|
+
let visibilityBound = false;
|
|
5321
|
+
/** Subscribes `listener` to ticks every `period` ms. Returns the unsubscribe function. */
|
|
5322
|
+
function subscribe(period, listener) {
|
|
5323
|
+
let channel = channels.get(period);
|
|
5324
|
+
if (!channel) {
|
|
5325
|
+
channel = { listeners: new Set() };
|
|
5326
|
+
channels.set(period, channel);
|
|
5327
|
+
}
|
|
5328
|
+
channel.listeners.add(listener);
|
|
5329
|
+
bindVisibility();
|
|
5330
|
+
arm(period, channel);
|
|
5331
|
+
return () => {
|
|
5332
|
+
channel.listeners.delete(listener);
|
|
5333
|
+
if (channel.listeners.size === 0) {
|
|
5334
|
+
clearTimeout(channel.timer);
|
|
5335
|
+
channels.delete(period);
|
|
5336
|
+
}
|
|
5337
|
+
};
|
|
5338
|
+
}
|
|
5339
|
+
function arm(period, channel) {
|
|
5340
|
+
if (channel.timer !== undefined || isHidden())
|
|
5341
|
+
return;
|
|
5342
|
+
channel.timer = setTimeout(() => {
|
|
5343
|
+
channel.timer = undefined;
|
|
5344
|
+
notify(channel);
|
|
5345
|
+
if (channels.get(period) === channel)
|
|
5346
|
+
arm(period, channel);
|
|
5347
|
+
}, period - (Date.now() % period));
|
|
5348
|
+
}
|
|
5349
|
+
function notify(channel) {
|
|
5350
|
+
for (const listener of [...channel.listeners])
|
|
5351
|
+
listener();
|
|
5352
|
+
}
|
|
5353
|
+
function isHidden() {
|
|
5354
|
+
return typeof document !== "undefined" && document.visibilityState === "hidden";
|
|
5355
|
+
}
|
|
5356
|
+
function bindVisibility() {
|
|
5357
|
+
if (visibilityBound || typeof document === "undefined")
|
|
5358
|
+
return;
|
|
5359
|
+
visibilityBound = true;
|
|
5360
|
+
document.addEventListener("visibilitychange", () => {
|
|
5361
|
+
// Snapshot: listeners may move to another period while being notified.
|
|
5362
|
+
for (const [period, channel] of [...channels]) {
|
|
5363
|
+
if (isHidden()) {
|
|
5364
|
+
clearTimeout(channel.timer);
|
|
5365
|
+
channel.timer = undefined;
|
|
5366
|
+
}
|
|
5367
|
+
else {
|
|
5368
|
+
notify(channel);
|
|
5369
|
+
if (channels.get(period) === channel)
|
|
5370
|
+
arm(period, channel);
|
|
5371
|
+
}
|
|
5372
|
+
}
|
|
5373
|
+
});
|
|
5374
|
+
}
|
|
5375
|
+
|
|
5376
|
+
/** Lower bound for a custom `interval`, roughly one frame. */
|
|
5377
|
+
const MIN_INTERVAL = 16;
|
|
5378
|
+
/** Inputs that change which instant is shown. */
|
|
5379
|
+
const SOURCE_KEYS = ["value", "datetime", "text", "inputFormat", "locale", "timeZone"];
|
|
5380
|
+
/**
|
|
5381
|
+
* `<tp-date>` — a localized, optionally live date/time.
|
|
5382
|
+
*
|
|
5383
|
+
* - **Source** (first non-empty wins): the `value` property, the `datetime`
|
|
5384
|
+
* attribute, the text content. Accepts ISO 8601, epoch milliseconds, or a
|
|
5385
|
+
* numeric date in the locale's order (`22/01/2024` in `tr`); `input-format`
|
|
5386
|
+
* pins a moment-style pattern instead.
|
|
5387
|
+
* - **Fallback**: empty or invalid input renders the current time. Invalid
|
|
5388
|
+
* input also sets the `invalid` attribute and fires `tp-date-invalid`.
|
|
5389
|
+
* - **Format**: a preset (`short` / `medium` / `long` / `full`, via `Intl`),
|
|
5390
|
+
* `relative`, or a moment-style pattern such as `DD/MM/YYYY HH:mm`.
|
|
5391
|
+
* - **Live updates**: the current time and relative dates re-render on a
|
|
5392
|
+
* shared, boundary-aligned clock at the cadence the format needs (each
|
|
5393
|
+
* second for `ss`, each minute for `mm`, ...). Fixed dates in an absolute
|
|
5394
|
+
* format never tick. Ticking pauses while the tab is hidden.
|
|
5395
|
+
* - The text content stays in light DOM as the no-JS / pre-upgrade fallback;
|
|
5396
|
+
* the rendered value is a `<time datetime>` in shadow DOM.
|
|
5397
|
+
*
|
|
5398
|
+
* @element tp-date
|
|
5399
|
+
*
|
|
5400
|
+
* @attr {string} [format=""] - `short`, `medium` (default), `long`, `full`, `relative`, or a moment-style pattern.
|
|
5401
|
+
* @attr {"short" | "medium" | "long" | "full" | "none"} date-style - Presets only. Overrides the date part; `none` omits it.
|
|
5402
|
+
* @attr {"short" | "medium" | "long" | "full" | "none"} time-style - Presets only. Overrides the time part; `none` omits it.
|
|
5403
|
+
* @attr {string} [input-format=""] - Moment-style pattern used to parse the input, e.g. `DD/MM/YYYY`.
|
|
5404
|
+
* @attr {string} [datetime=""] - The date to show. Takes precedence over the text content.
|
|
5405
|
+
* @attr {string} [locale=""] - BCP 47 locale. Defaults to the nearest `lang` attribute, then the browser.
|
|
5406
|
+
* @attr {string} [time-zone=""] - IANA time zone (`UTC`, `Europe/Istanbul`) for display and for parsing wall times. Defaults to local.
|
|
5407
|
+
* @attr {boolean} [live=false] - Forces ticking even for a fixed date in an absolute format.
|
|
5408
|
+
* @attr {number} [interval=0] - Custom tick period in ms. `0` derives it from the format.
|
|
5409
|
+
* @attr {boolean} invalid - Read-only. Present when the input could not be parsed.
|
|
5410
|
+
*
|
|
5411
|
+
* @fires tp-date-invalid - When a non-empty input cannot be parsed. `detail: { input }`.
|
|
5412
|
+
*
|
|
5413
|
+
* @csspart time - The rendered `<time>` element.
|
|
5414
|
+
*
|
|
5415
|
+
* @cssprop --tp-date-numeric - `font-variant-numeric` of the output. Defaults to `tabular-nums`.
|
|
5416
|
+
*/
|
|
5417
|
+
class DateElement extends LitElement {
|
|
5418
|
+
constructor() {
|
|
5419
|
+
super(...arguments);
|
|
5420
|
+
/** `short`, `medium` (default), `long`, `full`, `relative`, or a moment-style pattern. */
|
|
5421
|
+
this.format = "";
|
|
5422
|
+
/** Moment-style pattern used to parse string input. */
|
|
5423
|
+
this.inputFormat = "";
|
|
5424
|
+
/** The date to show as a string. Takes precedence over the text content. */
|
|
5425
|
+
this.datetime = "";
|
|
5426
|
+
/** BCP 47 locale. Defaults to the nearest `lang` attribute, then the browser. */
|
|
5427
|
+
this.locale = "";
|
|
5428
|
+
/** IANA time zone. Defaults to the local zone. */
|
|
5429
|
+
this.timeZone = "";
|
|
5430
|
+
/** Forces ticking even for a fixed date in an absolute format. */
|
|
5431
|
+
this.live = false;
|
|
5432
|
+
/** Custom tick period in ms. `0` derives it from the format. */
|
|
5433
|
+
this.interval = 0;
|
|
5434
|
+
/** Read-only. `true` when the input could not be parsed (the current time is shown instead). */
|
|
5435
|
+
this.invalid = false;
|
|
5436
|
+
/** Text content of the light DOM. */
|
|
5437
|
+
this.text = "";
|
|
5438
|
+
this.#now = Date.now();
|
|
5439
|
+
this.#period = 0;
|
|
5440
|
+
}
|
|
5441
|
+
static { this.styles = dateStyles; }
|
|
5442
|
+
#fixed;
|
|
5443
|
+
#now;
|
|
5444
|
+
#observer;
|
|
5445
|
+
#unsubscribe;
|
|
5446
|
+
#period;
|
|
5447
|
+
#invalidInput;
|
|
5448
|
+
#lastInvalid;
|
|
5449
|
+
/** The instant currently shown (the parsed input, or the current time). */
|
|
5450
|
+
get date() {
|
|
5451
|
+
return new Date(this.#fixed?.getTime() ?? this.#now);
|
|
5452
|
+
}
|
|
5453
|
+
connectedCallback() {
|
|
5454
|
+
super.connectedCallback();
|
|
5455
|
+
this.#observer ??= new MutationObserver(() => this.#syncText());
|
|
5456
|
+
this.#observer.observe(this, { childList: true, characterData: true, subtree: true });
|
|
5457
|
+
this.#syncText();
|
|
5458
|
+
// Re-subscribe to the ticker after a reconnect.
|
|
5459
|
+
this.requestUpdate();
|
|
5460
|
+
}
|
|
5461
|
+
disconnectedCallback() {
|
|
5462
|
+
super.disconnectedCallback();
|
|
5463
|
+
this.#observer?.disconnect();
|
|
5464
|
+
this.#setPeriod(0);
|
|
5465
|
+
}
|
|
5466
|
+
willUpdate(changed) {
|
|
5467
|
+
this.#now = Date.now();
|
|
5468
|
+
if (SOURCE_KEYS.some(key => changed.has(key)))
|
|
5469
|
+
this.#parse();
|
|
5470
|
+
}
|
|
5471
|
+
render() {
|
|
5472
|
+
const spec = this.#spec();
|
|
5473
|
+
const date = this.date;
|
|
5474
|
+
const locale = this.#locale();
|
|
5475
|
+
const timeZone = this.#timeZone();
|
|
5476
|
+
return html$1 `<time
|
|
5477
|
+
part="time"
|
|
5478
|
+
datetime=${date.toISOString()}
|
|
5479
|
+
title=${spec.kind === "relative" ? formatFull(date, locale, timeZone) : nothing}
|
|
5480
|
+
>${formatDate(date, spec, { locale, timeZone, now: this.#now })}</time
|
|
5481
|
+
>`;
|
|
5482
|
+
}
|
|
5483
|
+
updated() {
|
|
5484
|
+
this.#schedule();
|
|
5485
|
+
if (this.#invalidInput !== undefined) {
|
|
5486
|
+
const input = this.#invalidInput;
|
|
5487
|
+
this.#invalidInput = undefined;
|
|
5488
|
+
this.dispatchEvent(new CustomEvent("tp-date-invalid", { bubbles: true, composed: true, detail: { input } }));
|
|
5489
|
+
}
|
|
5490
|
+
}
|
|
5491
|
+
#parse() {
|
|
5492
|
+
const input = this.value ?? (this.datetime.trim() || this.text);
|
|
5493
|
+
const fixed = parseDate(input, {
|
|
5494
|
+
inputFormat: this.inputFormat,
|
|
5495
|
+
locale: this.#locale(),
|
|
5496
|
+
timeZone: this.#timeZone(),
|
|
5497
|
+
now: this.#now,
|
|
5498
|
+
});
|
|
5499
|
+
const empty = input === null || input === undefined || (typeof input === "string" && !input.trim());
|
|
5500
|
+
const invalid = !fixed && !empty;
|
|
5501
|
+
const key = String(input);
|
|
5502
|
+
// Fire once per distinct invalid input, not on every re-parse.
|
|
5503
|
+
if (invalid && key !== this.#lastInvalid)
|
|
5504
|
+
this.#invalidInput = key;
|
|
5505
|
+
this.#lastInvalid = invalid ? key : undefined;
|
|
5506
|
+
this.#fixed = fixed;
|
|
5507
|
+
this.invalid = invalid;
|
|
5508
|
+
}
|
|
5509
|
+
#spec() {
|
|
5510
|
+
return resolveFormat(this.format, this.dateStyle, this.timeStyle);
|
|
5511
|
+
}
|
|
5512
|
+
#locale() {
|
|
5513
|
+
return resolveLocale(this, this.locale);
|
|
5514
|
+
}
|
|
5515
|
+
#timeZone() {
|
|
5516
|
+
const zone = this.timeZone.trim();
|
|
5517
|
+
return zone && isValidTimeZone(zone) ? zone : undefined;
|
|
5518
|
+
}
|
|
5519
|
+
/** Subscribes to the shared ticker at the cadence the current format needs, or not at all. */
|
|
5520
|
+
#schedule() {
|
|
5521
|
+
const spec = this.#spec();
|
|
5522
|
+
const live = this.live || !this.#fixed || spec.kind === "relative";
|
|
5523
|
+
if (!this.isConnected || !live)
|
|
5524
|
+
return this.#setPeriod(0);
|
|
5525
|
+
const interval = Number(this.interval);
|
|
5526
|
+
this.#setPeriod(interval > 0 ? Math.max(interval, MIN_INTERVAL) : tickPeriod(spec, this.date, this.#now));
|
|
5527
|
+
}
|
|
5528
|
+
#setPeriod(period) {
|
|
5529
|
+
if (period === this.#period)
|
|
5530
|
+
return;
|
|
5531
|
+
this.#unsubscribe?.();
|
|
5532
|
+
this.#unsubscribe = undefined;
|
|
5533
|
+
this.#period = period;
|
|
5534
|
+
if (period)
|
|
5535
|
+
this.#unsubscribe = subscribe(period, () => this.requestUpdate());
|
|
5536
|
+
}
|
|
5537
|
+
#syncText() {
|
|
5538
|
+
this.text = (this.textContent ?? "").trim();
|
|
5539
|
+
}
|
|
5540
|
+
}
|
|
5541
|
+
__decorate([
|
|
5542
|
+
property(),
|
|
5543
|
+
__metadata("design:type", Object)
|
|
5544
|
+
], DateElement.prototype, "format", void 0);
|
|
5545
|
+
__decorate([
|
|
5546
|
+
property({ attribute: "date-style" }),
|
|
5547
|
+
__metadata("design:type", String)
|
|
5548
|
+
], DateElement.prototype, "dateStyle", void 0);
|
|
5549
|
+
__decorate([
|
|
5550
|
+
property({ attribute: "time-style" }),
|
|
5551
|
+
__metadata("design:type", String)
|
|
5552
|
+
], DateElement.prototype, "timeStyle", void 0);
|
|
5553
|
+
__decorate([
|
|
5554
|
+
property({ attribute: "input-format" }),
|
|
5555
|
+
__metadata("design:type", Object)
|
|
5556
|
+
], DateElement.prototype, "inputFormat", void 0);
|
|
5557
|
+
__decorate([
|
|
5558
|
+
property(),
|
|
5559
|
+
__metadata("design:type", Object)
|
|
5560
|
+
], DateElement.prototype, "datetime", void 0);
|
|
5561
|
+
__decorate([
|
|
5562
|
+
property(),
|
|
5563
|
+
__metadata("design:type", Object)
|
|
5564
|
+
], DateElement.prototype, "locale", void 0);
|
|
5565
|
+
__decorate([
|
|
5566
|
+
property({ attribute: "time-zone" }),
|
|
5567
|
+
__metadata("design:type", Object)
|
|
5568
|
+
], DateElement.prototype, "timeZone", void 0);
|
|
5569
|
+
__decorate([
|
|
5570
|
+
property({ type: Boolean, reflect: true }),
|
|
5571
|
+
__metadata("design:type", Object)
|
|
5572
|
+
], DateElement.prototype, "live", void 0);
|
|
5573
|
+
__decorate([
|
|
5574
|
+
property({ type: Number }),
|
|
5575
|
+
__metadata("design:type", Object)
|
|
5576
|
+
], DateElement.prototype, "interval", void 0);
|
|
5577
|
+
__decorate([
|
|
5578
|
+
property({ attribute: false }),
|
|
5579
|
+
__metadata("design:type", Object)
|
|
5580
|
+
], DateElement.prototype, "value", void 0);
|
|
5581
|
+
__decorate([
|
|
5582
|
+
property({ type: Boolean, reflect: true }),
|
|
5583
|
+
__metadata("design:type", Object)
|
|
5584
|
+
], DateElement.prototype, "invalid", void 0);
|
|
5585
|
+
__decorate([
|
|
5586
|
+
state(),
|
|
5587
|
+
__metadata("design:type", Object)
|
|
5588
|
+
], DateElement.prototype, "text", void 0);
|
|
5589
|
+
if (!customElements.get("tp-date"))
|
|
5590
|
+
customElements.define("tp-date", DateElement);
|
|
5591
|
+
|
|
5592
|
+
export { ButtonElement, DateElement, DetailsSetElement, DragElement, DragHandleElement, DropZoneElement, OptionElement, PopoverElement, PortalElement, PortalOutletElement, PortalOutletFallbackElement, ROUTE_ERROR_EVENT, ROUTE_LOAD_EVENT, RouteElement, RouterConfigElement, RouterElement, RouterLinkElement, RouterOutletElement, RouterParamElement, SelectChangeEvent, SelectElement, SelectedContentElement, ThaDragEvent, defaultRouteSourceLoader, formatDate, formatPattern, formatRelative, parseDate, resolveFormat };
|
|
4542
5593
|
//# sourceMappingURL=elements.mjs.map
|