* A bunch of shorthand to put together common parts of an HTML page.
*/
+const { lazy } = require('@squeep/lazy');
+
/**
* Some fields may have values outside normal dates, handle them here.
* @param {Date} date
};
+/**
+ * Why is rendering a Date as a string this complicated?
+ * We handle the infinities because pg-promise might provide those in
+ * lieu of a Date object from timestamp fields.
+ * @param {Date|Number|String} date
+ * @param {String=} pInf
+ * @param {String=} nInf
+ * @param {String=} otherwise
+ */
+const dateFormat = (date, pInf = 'Never', nInf = 'Forever', otherwise = '') => {
+ const isDatableType = ['number', 'string'].includes(typeof date);
+ switch (date) {
+ case Infinity:
+ return pInf;
+ case -Infinity:
+ return nInf;
+ default:
+ if (!date
+ || Number.isNaN(date.valueOf())
+ || (!(date instanceof Date) && !isDatableType)) {
+ return otherwise;
+ }
+ }
+ if (isDatableType) {
+ date = new Date(date);
+ }
+ const parts = dateFormat._dtf.formatToParts(date);
+ return parts.map((p) => p.value).join('');
+};
+lazy(dateFormat, '_dtf', () => {
+ const dateTimeFormatOptions = {
+ dateStyle: 'medium',
+ timeStyle: 'long',
+ };
+ return new Intl.DateTimeFormat(undefined, dateTimeFormatOptions);
+});
+
+
/**
* Render a duration.
* @param {Number} seconds
} = options;
return `\t<head>
\t\t<meta charset="utf-8">
-\t\t<link rel="stylesheet" href="${rootPathPfx}static/theme.css">
-\t\t<link rel="stylesheet" href="${rootPathPfx}static/custom.css">
+\t\t<meta name="viewport" content="width=device-width,initial-scale=1">
+\t\t<link rel="stylesheet" href="${rootPathPfx}static/theme.css" title="Default">
+\t\t<link rel="stylesheet" href="${rootPathPfx}static/custom.css" title="Site Specific">
${headElements.map((e) => '\t\t' + e).join('\n')}
\t\t<title>${pageTitle}</title>
\t</head>`;
const {
bodyAttributes = {},
} = options;
+ const firefoxFix = '\n<script>0</script>'; // This fixes a layout rendering flash on load in Firefox; do not know why this works, but it does.
return `
-\t<body${elementAttributes(bodyAttributes)}>
+\t<body${elementAttributes(bodyAttributes)}>${firefoxFix}
${htmlHeader(pagePathLevel, ctx, options)}
${htmlMessages(ctx, options)}
\t\t<main>
module.exports = {
dateOrNot,
+ dateFormat,
secondsToPeriod,
htmlHead,
htmlBody,