Skip to content

Commit

Permalink
v4.17.3: simplified-date
Browse files Browse the repository at this point in the history
  • Loading branch information
fergusean committed Nov 14, 2024
1 parent ce938d2 commit bb65c44
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@signal24/vue-foundation",
"type": "module",
"version": "4.17.2",
"version": "4.17.3",
"description": "Common components, directives, and helpers for Vue 3 apps",
"module": "./dist/vue-foundation.es.js",
"exports": {
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ interface IOptions {
unhandledErrorSupportText: string;
errorHandler: (err: Error) => void;
defaultDateFormat: string;
defaultDateTimeFormat: string;
defaultTimeFormat: string;
}

export const VfOptions: IOptions = {
unhandledErrorSupportText: 'please contact support',
errorHandler: err => console.error('Unhandled error:', err),
defaultDateFormat: 'M/d/yy',
defaultDateTimeFormat: 'M/d/yy H:mm'
defaultTimeFormat: 'H:mm'
};

export function configureVf(options: Partial<IOptions>) {
Expand Down
25 changes: 21 additions & 4 deletions src/directives/datetime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { format } from 'date-fns';
import { compact } from 'lodash';
import type { DirectiveBinding, ObjectDirective } from 'vue';

import { VfOptions } from '../config';
Expand Down Expand Up @@ -28,20 +29,36 @@ function getDateTimeValue(el: HTMLElement, binding: DirectiveBinding<string>) {
}

let formatSpec = el.attributes.getNamedItem('format')?.value;
const isDateOnly = el.attributes.getNamedItem('date-only') !== null;

if (!formatSpec && el.attributes.getNamedItem('relative-date') !== null) {
const now = new Date();
if (now.getFullYear() == theDate.getFullYear() && now.getMonth() == theDate.getMonth() && now.getDate() == theDate.getDate()) {
prefix = 'at';
if (now.getFullYear() === theDate.getFullYear() && now.getMonth() === theDate.getMonth() && now.getDate() === theDate.getDate()) {
prefix = 'at'; // ???
formatSpec = 'HH:mm';
}
}

if (!formatSpec && el.attributes.getNamedItem('simplified-date') !== null) {
let dateSpec: string | null = null;
const now = new Date();
if (now.getFullYear() === theDate.getFullYear()) {
if (now.getMonth() !== theDate.getMonth() || now.getDate() !== theDate.getDate()) {
dateSpec = 'M/d';
}
} else {
dateSpec = 'M/d/YY';
}

const timeSpec = isDateOnly ? null : VfOptions.defaultTimeFormat;
formatSpec = compact([dateSpec, timeSpec]).join(' ');
}

if (!formatSpec) {
if (el.attributes.getNamedItem('date-only') !== null) {
if (isDateOnly) {
formatSpec = VfOptions.defaultDateFormat;
} else {
formatSpec = VfOptions.defaultDateTimeFormat;
formatSpec = `${VfOptions.defaultDateFormat} ${VfOptions.defaultTimeFormat}`;
}
}

Expand Down

0 comments on commit bb65c44

Please sign in to comment.