X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=inline;f=src%2Ftemplate%2Ftemplate-helper.js;h=bb4dad1e3f574f2332683c9e4bc990c9bc98b3a2;hb=refs%2Ftags%2Fv1.3.5;hp=0ff90772ec7ebd2479344979b08e7d4c075fdb01;hpb=4807a77eca2858e8dc23d9ec2247a778814988d7;p=websub-hub
diff --git a/src/template/template-helper.js b/src/template/template-helper.js
index 0ff9077..bb4dad1 100644
--- a/src/template/template-helper.js
+++ b/src/template/template-helper.js
@@ -1,56 +1,6 @@
'use strict';
-/**
- * A bunch of shorthand to put together common parts of an HTML page.
- */
-
-/**
- * Some fields may have values outside normal dates, handle them here.
- * @param {Date} date
- * @param {String} otherwise
- */
-const dateOrNot = (date, otherwise) => {
- if (!date) {
- return otherwise;
- }
- if (typeof date === 'number') {
- date = new Date(date);
- }
- const dateMs = date.getTime();
- if (!Number.isFinite(dateMs)
- || dateMs == 0) {
- return otherwise;
- }
- return date.toString();
-};
-
-
-/**
- * Render a duration.
- * @param {Number} seconds
- * @returns {String}
- */
-const secondsToPeriod = (seconds) => {
- let value = seconds;
- const result = [];
-
- const nextResult = (factor, label) => {
- const r = factor ? value % factor : value;
- if (r) {
- result.push(`${r} ${label}${r != 1 ? 's' : ''}`);
- }
- value = factor ? Math.floor(value / factor) : value;
- }
-
- nextResult(60, 'second');
- nextResult(60, 'minute');
- nextResult(24, 'hour');
- nextResult(30, 'day');
- nextResult(undefined, 'month');
-
- result.reverse();
- return result.join(' ');
-};
+const { TemplateHelper } = require('@squeep/html-template-helper');
/**
@@ -69,17 +19,17 @@ function renderTopicRow(topic, subscribers, detailsLink = true) {
return `
${detailsLink ? '' : ''}${topic.url}${detailsLink ? '' : ''} |
${subscribers.length} |
- ${dateOrNot(topic.created, 'Unknown')} |
- ${secondsToPeriod(topic.leaseSecondsPreferred)} |
- ${secondsToPeriod(topic.leaseSecondsMin)} |
- ${secondsToPeriod(topic.leaseSecondsMax)} |
+ ${TemplateHelper.dateOrNot(topic.created, 'Unknown')} |
+ ${TemplateHelper.secondsToPeriod(topic.leaseSecondsPreferred)} |
+ ${TemplateHelper.secondsToPeriod(topic.leaseSecondsMin)} |
+ ${TemplateHelper.secondsToPeriod(topic.leaseSecondsMax)} |
${topic.publisherValidationUrl ? topic.publisherValidationUrl : 'None'} |
${topic.isActive} |
${topic.isDeleted} |
- ${dateOrNot(topic.lastPublish, 'Never')} |
- ${dateOrNot(topic.contentFetchNextAttempt, 'Next Publish')} |
+ ${TemplateHelper.dateOrNot(topic.lastPublish, 'Never')} |
+ ${TemplateHelper.dateOrNot(topic.contentFetchNextAttempt, 'Next Publish')} |
${topic.contentFetchAttemptsSinceSuccess} |
- ${dateOrNot(topic.contentUpdated, 'Never')} |
+ ${TemplateHelper.dateOrNot(topic.contentUpdated, 'Never')} |
${topic.contentType} |
${topic.id} |
`;
@@ -124,16 +74,16 @@ function renderSubscriptionRow(subscription) {
}
return `
${subscription.callback} |
- ${dateOrNot(subscription.created, 'Unknown')} |
- ${dateOrNot(subscription.verified, 'Never')} |
- ${dateOrNot(subscription.expires, 'Never')} |
+ ${TemplateHelper.dateOrNot(subscription.created, 'Unknown')} |
+ ${TemplateHelper.dateOrNot(subscription.verified, 'Never')} |
+ ${TemplateHelper.dateOrNot(subscription.expires, 'Never')} |
${!!subscription.secret} |
${subscription.signatureAlgorithm} |
${subscription.httpRemoteAddr} |
${subscription.httpFrom} |
- ${dateOrNot(subscription.contentDelivered, 'Never')} |
+ ${TemplateHelper.dateOrNot(subscription.contentDelivered, 'Never')} |
${subscription.deliveryAttemptsSinceSuccess} |
- ${dateOrNot(subscription.deliveryNextAttempt, 'Next Publish')} |
+ ${TemplateHelper.dateOrNot(subscription.deliveryNextAttempt, 'Next Publish')} |
${subscription.id} |
`;
}
@@ -162,135 +112,9 @@ function renderSubscriptionRowHeader() {
}
-/**
- * Render the preamble for an HTML page, up through body.
- * @param {Number} pagePathLevel number of paths below root this page is
- * @param {String} pageTitle
- * @param {String[]} headElements
- * @returns
- */
-function htmlHead(pagePathLevel, pageTitle, headElements = []) {
- const rootPathPfx = '../'.repeat(pagePathLevel);
- return `
-
-
- ` +
- headElements.map((e) => `${' '.repeat(2)}${e}`).join('\n') + `
- ${pageTitle}
-
-
- `;
-}
-
-
-/**
- * Closes remainder of HTML page body.
- * @returns {String}
- */
-function htmlTail() {
- return `
-`;
-}
-
-
-/**
- * Render a navigation link for the header section.
- * @param {Object} nav
- * @param {String} nav.href
- * @param {String} nav.class
- * @param {String} nav.text
- * @returns {String}
- */
-function renderNavLink(nav) {
- return `
- ${nav.text}
-`;
-}
-
-
-/**
- * Render the navigation header, and open the main section.
- * @param {String} pageTitle
- * @param {Object[]} navLinks
- * @returns {String}
- */
-function htmlHeader(pageTitle, navLinks = []) {
- return `
- ${pageTitle}
-
-
- `;
-}
-
-
-/**
- * Close the main section and finish off with boilerplate.
- * @param {String[]} footerEntries
- * @returns {String}
- */
-function htmlFooter(footerEntries = []) {
- return `
- `;
-}
-
-
-/**
- * Render all parts of an HTML page. Adds user logout nav link automatically.
- * @param {Object} ctx
- * @param {Number} pagePathLevel
- * @param {String} pageTitle
- * @param {String[]} headElements
- * @param {Object[]} navLinks
- * @param {String[]} main
- * @param {String[]} footerEntries
- * @returns {String}
- */
-function htmlTemplate(ctx, pagePathLevel, pageTitle, headElements = [], navLinks = [], main = [], footerEntries = []) {
- const user = (ctx && ctx.session && ctx.session.authenticatedProfile) || (ctx && ctx.session && ctx.session.authenticatedIdentifier);
- if (user) {
- let logoutPath;
- if (pagePathLevel > 0) {
- logoutPath = `${'../'.repeat(pagePathLevel - 1)}`;
- } else {
- logoutPath = 'admin/';
- }
- navLinks.push({
- text: `Logout (${user})`,
- href: `${logoutPath}logout`,
- });
- }
- return [
- htmlHead(pagePathLevel, pageTitle, headElements),
- htmlHeader(pageTitle, navLinks),
- ...main,
- htmlFooter(footerEntries),
- htmlTail(),
- ].join('\n');
-}
-
-
-module.exports = {
- dateOrNot,
- secondsToPeriod,
- htmlHeader,
- htmlFooter,
- htmlHead,
- htmlTail,
- renderNavLink,
+module.exports = Object.assign(Object.create(TemplateHelper), {
renderTopicRowHeader,
renderTopicRow,
renderSubscriptionRowHeader,
renderSubscriptionRow,
- htmlTemplate,
-};
\ No newline at end of file
+});
\ No newline at end of file