X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Ftemplate-helper.js;h=7e1693c857758c127d1063d30804c6c5fab9b2e6;hb=2a4abfad091846a4fa528b44a31962c5150e6b0a;hp=00937a0d554318e026b2083190379cead7eaa71c;hpb=4ab85bf22ece77cccd4e92435208bca5858816f6;p=squeep-html-template-helper diff --git a/lib/template-helper.js b/lib/template-helper.js index 00937a0..7e1693c 100644 --- a/lib/template-helper.js +++ b/lib/template-helper.js @@ -4,6 +4,8 @@ * 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 @@ -25,6 +27,41 @@ const dateOrNot = (date, otherwise) => { }; +/** + * Why is rendering a Date as a string this complicated? + * @param {Date|Number} 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 + || (!(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 @@ -70,8 +107,9 @@ function htmlHead(pagePathLevel, ctx, options) { } = options; return `\t \t\t -\t\t -\t\t +\t\t +\t\t +\t\t ${headElements.map((e) => '\t\t' + e).join('\n')} \t\t${pageTitle} \t`; @@ -91,8 +129,9 @@ function htmlBody(pagePathLevel, ctx, options, main = []) { const { bodyAttributes = {}, } = options; + const firefoxFix = '\n'; // This fixes a layout rendering flash on load in Firefox; do not know why this works, but it does. return ` -\t +\t${firefoxFix} ${htmlHeader(pagePathLevel, ctx, options)} ${htmlMessages(ctx, options)} \t\t
@@ -145,7 +184,7 @@ ${spacer}` : ''; * @returns {String} */ function htmlHeader(pagePathLevel, ctx, options) { - const rootPathPfx = '../'.repeat(Math.max(pagePathLevel - 1, 0)); + const rootPathPfx = '../'.repeat(pagePathLevel); const { logoUrl = '', pageTitle = '', @@ -281,9 +320,9 @@ ${UL(ctx.notifications, 1)} * @param {Object[]=} options.navLinks * @param {String[]=} options.footerEntries * @param {String=} options.errorHeading - * @param {String=} options.errorContent + * @param {String[]=} options.errorContent * @param {String=} options.notificationHeading - * @param {String=} options.notificationContent + * @param {String[]=} options.notificationContent * @param {String[]} main - contents * @returns {String} */ @@ -313,6 +352,7 @@ function htmlPage(pagePathLevel, ctx, options, main = []) { module.exports = { dateOrNot, + dateFormat, secondsToPeriod, htmlHead, htmlBody,