make date header a bit more accessible

This commit is contained in:
Bruno Windels 2022-11-25 16:31:44 +01:00
parent 2136b051a0
commit c538f5dbb1
5 changed files with 18 additions and 4 deletions

View File

@ -17,6 +17,7 @@ import type {Options} from "../../../../ViewModel";
export class DateTile extends ViewModel implements ITile<BaseEventEntry> {
private _emitUpdate?: EmitUpdateFn;
private _dateString?: string;
private _machineReadableString?: string;
constructor(private _firstTileInDay: ITile<BaseEventEntry>, options: Options) {
super(options);
@ -46,13 +47,20 @@ export class DateTile extends ViewModel implements ITile<BaseEventEntry> {
return this.compareEntry(tile.upperEntry);
}
get date(): string {
get relativeDate(): string {
if (!this._dateString) {
this._dateString = this.timeFormatter.formatRelativeDate(new Date(this.refEntry.timestamp));
}
return this._dateString;
}
get machineReadableDate(): string {
if (!this._machineReadableString) {
this._machineReadableString = this.timeFormatter.formatMachineReadableDate(new Date(this.refEntry.timestamp));
}
return this._machineReadableString;
}
get shape(): TileShape {
return TileShape.DateHeader;
}
@ -131,8 +139,9 @@ export class DateTile extends ViewModel implements ITile<BaseEventEntry> {
this._firstTileInDay = next;
const prevDateString = this._dateString;
this._dateString = undefined;
if (prevDateString && prevDateString !== this.date) {
this._emitUpdate?.(this, "date");
this._machineReadableString = undefined;
if (prevDateString && prevDateString !== this.relativeDate) {
this._emitUpdate?.(this, "relativeDate");
}
}

View File

@ -46,4 +46,5 @@ export type File = {
export interface ITimeFormatter {
formatRelativeDate(date: Date): string;
formatMachineReadableDate(date: Date): string;
}

View File

@ -48,6 +48,9 @@ export class TimeFormatter implements ITimeFormatter {
day: 'numeric'
});
}
formatMachineReadableDate(date: Date): string {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
}
formatRelativeDate(date: Date): string {
let daysDiff = Math.floor((date.getTime() - this.todayMidnight.getTime()) / TimeScope.Day);

View File

@ -428,4 +428,5 @@ only loads when the top comes into view*/
font-weight: bold;
padding: 12px 4px;
text-align: center;
font-size: 1.5rem;
}

View File

@ -25,7 +25,7 @@ export class DateHeaderView extends TemplateView<DateTile> {
}
render(t, vm) {
return t.div({className: "DateHeader"}, t.time(vm.date));
return t.h2({className: "DateHeader"}, t.time({dateTime: vm.machineReadableDate}, vm.relativeDate));
}
/* This is called by the parent ListView, which just has 1 listener for the whole list */