X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftemplate%2Ftemplate-helper.js;h=a3e113179928d3a878400c9b9c513961a930f2c9;hb=737fbd003d5c4dfea81b667ef906f1c106a60612;hp=d3962f99f263a07c485501236d0b1b3c3797848d;hpb=409ff988982a5edfcd51c02c681187969db57d0a;p=websub-hub
diff --git a/src/template/template-helper.js b/src/template/template-helper.js
index d3962f9..a3e1131 100644
--- a/src/template/template-helper.js
+++ b/src/template/template-helper.js
@@ -1,57 +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');
/**
* Render a topic as a row of details.
@@ -61,20 +10,25 @@ const secondsToPeriod = (seconds) => {
* @returns {String}
*/
function renderTopicRow(topic, subscribers, detailsLink = true) {
+ if (!topic) {
+ return `
+ (topic not found) |
+
`;
+ }
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} |
`;
@@ -112,18 +66,23 @@ function renderTopicRowHeader() {
* @returns {String}
*/
function renderSubscriptionRow(subscription) {
+ if (!subscription) {
+ return `
+ (topic not found) |
+
`;
+ }
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} |
`;
}
@@ -153,125 +112,30 @@ 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}
+ * Escape some xml things in strings.
+ * @param {String} string
*/
-function htmlTail() {
- return `
-