bdd75c4b55
--- Conflict resolution: Trivial (package.json package-lock.json) Done differently: Removed typescript types. (cherry picked from commit ce4d909bd693fe903761eb835f7661e476a937ca)
38 lines
1.3 KiB
JavaScript
38 lines
1.3 KiB
JavaScript
export function toAbsoluteLocaleDate(date, lang, opts) {
|
|
return new Date(date).toLocaleString(lang || [], opts);
|
|
}
|
|
|
|
window.customElements.define('absolute-date', class extends HTMLElement {
|
|
static observedAttributes = ['date', 'year', 'month', 'weekday', 'day'];
|
|
|
|
initialized = false;
|
|
|
|
update = () => {
|
|
const opt = {};
|
|
for (const attr of ['year', 'month', 'weekday', 'day']) {
|
|
if (this.getAttribute(attr)) opt[attr] = this.getAttribute(attr);
|
|
}
|
|
const lang = this.closest('[lang]')?.getAttribute('lang') ||
|
|
this.ownerDocument.documentElement.getAttribute('lang') || '';
|
|
|
|
// only use the date part, it is guaranteed to be in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ)
|
|
let date = this.getAttribute('date');
|
|
let dateSep = date.indexOf('T');
|
|
dateSep = dateSep === -1 ? date.indexOf(' ') : dateSep;
|
|
date = dateSep === -1 ? date : date.substring(0, dateSep);
|
|
|
|
if (!this.shadowRoot) this.attachShadow({mode: 'open'});
|
|
this.shadowRoot.textContent = toAbsoluteLocaleDate(date, lang, opt);
|
|
};
|
|
|
|
attributeChangedCallback(_name, oldValue, newValue) {
|
|
if (!this.initialized || oldValue === newValue) return;
|
|
this.update();
|
|
}
|
|
|
|
connectedCallback() {
|
|
this.initialized = false;
|
|
this.update();
|
|
this.initialized = true;
|
|
}
|
|
});
|