From: Justin Wind Date: Fri, 8 Mar 2024 21:26:55 +0000 (-0800) Subject: minor cleanups, add logoAlt X-Git-Tag: v1.5.2~1 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=77f0fd50c8de53072f73052a01f69c6c6fc783f8;p=squeep-html-template-helper minor cleanups, add logoAlt --- diff --git a/lib/template-helper.js b/lib/template-helper.js index 7e51bcf..747c525 100644 --- a/lib/template-helper.js +++ b/lib/template-helper.js @@ -232,6 +232,7 @@ ${spacer}` : ''; * @param {Object} ctx * @param {Object} options * @param {String[]=} options.logoUrl + * @param {String[]=} options.logoAlt * @param {String[]=} options.pageTitle * @returns {String} */ @@ -239,9 +240,10 @@ function htmlHeader(pagePathLevel, ctx, options) { const rootPathPfx = '../'.repeat(pagePathLevel); const { logoUrl = '', + logoAlt = 'logo', pageTitle = '', } = options; - const logoImg = logoUrl ? `` : ''; + const logoImg = logoUrl ? `logo` : ''; return `\t\t
\t\t\t

${logoImg}${pageTitle}

${htmlNav(ctx, options)} @@ -338,16 +340,16 @@ ${spacer}`; * @param {String[]=} options.notificationContent */ function htmlMessages(ctx, options) { - const errorHeading = options && options.errorHeading ? ` + const errorHeading = options?.errorHeading ? ` \t

${options.errorHeading}

` : ''; - const errorContent = options && options.errorContent && options.errorContent.length ? '\n' + options.errorContent.map(((content) => `\t${content}`)).join('\n') : ''; - const notificationHeading = options && options.notificationHeading ? `\n\t

${options.notificationHeading}

` : ''; - const notificationContent = options && options.notificationContent && options.notificationContent.length ? '\n' + options.notificationContent.map(((content) => `\t${content}`)).join('\n') : ''; - const errors = ctx && ctx.errors && ctx.errors.length ? ` + const errorContent = options?.errorContent?.length ? '\n' + options.errorContent.map(((content) => `\t${content}`)).join('\n') : ''; + const notificationHeading = options?.notificationHeading ? `\n\t

${options.notificationHeading}

` : ''; + const notificationContent = options?.notificationContent?.length ? '\n' + options.notificationContent.map(((content) => `\t${content}`)).join('\n') : ''; + const errors = ctx?.errors?.length ? `
${errorHeading}${errorContent} ${UL(ctx.errors, 1)}
` : ''; - const notifications = ctx && ctx.notifications && ctx.notifications.length ? ` + const notifications = ctx?.notifications?.length ? `
${notificationHeading}${notificationContent} ${UL(ctx.notifications, 1)}
` : ''; @@ -367,6 +369,7 @@ ${UL(ctx.notifications, 1)} * @param {Object} options * @param {String=} options.pageTitle * @param {String=} options.logoUrl + * @param {String=} options.logoAlt * @param {Object[]=} options.bodyAttributes * @param {String[]=} options.headElements * @param {Object[]=} options.navLinks @@ -379,16 +382,19 @@ ${UL(ctx.notifications, 1)} * @returns {String} */ function htmlPage(pagePathLevel, ctx, options, main = []) { - const user = (ctx && ctx.session && ctx.session.authenticatedProfile) || (ctx && ctx.session && ctx.session.authenticatedIdentifier); + const user = ctx?.session?.authenticatedProfile || ctx?.session?.authenticatedIdentifier; if (user) { if (!options.navLinks) { options.navLinks = []; } - const logoutRedirect = ctx && ctx.url ? `?r=${encodeURIComponent(ctx.url)}` : ''; - const logoutPath = (pagePathLevel > 0) ? `${'../'.repeat(pagePathLevel - 1)}` : 'admin/'; + const logoutRedirect = ctx?.url ? `?r=${encodeURIComponent(ctx.url)}` : ''; + const adminPath = (pagePathLevel > 0) ? `${'../'.repeat(pagePathLevel - 1)}` : 'admin/'; options.navLinks.push({ text: `Logout (${user})`, - href: `${logoutPath}logout${logoutRedirect}`, + href: `${adminPath}logout${logoutRedirect}`, + }, { + text: 'Settings', + href: `${adminPath}settings`, }); } diff --git a/test/lib/template-helper.js b/test/lib/template-helper.js index ec7ab5d..75481ad 100644 --- a/test/lib/template-helper.js +++ b/test/lib/template-helper.js @@ -16,9 +16,7 @@ describe('Template Helper', function () { beforeEach(function () { pagePathLevel = 2; ctx = {}; - options = { - pageTitle: 'Test Page', - }; + options = {}; }); describe('initContext', function () { @@ -264,7 +262,15 @@ describe('Template Helper', function () { describe('htmlPage', function () { let main; beforeEach(function () { - main = []; + th.initContext(ctx); + ctx.errors.push('an error'); + ctx.notifications.push('a notice'); + options.headElements = ['']; + options.pageTitle = 'Test Page'; + main = [ + th.UL(['an item', 'another item']), + th.timeElement(new Date(), { title: 'now' }), + ]; }); it('covers', async function () { const result = th.htmlPage(pagePathLevel, ctx, options, main);