add more setup documentation
[squeep-indie-auther] / template / template-helper.js
1 'use strict';
2
3 const { TemplateHelper } = require('@squeep/html-template-helper');
4
5
6 /**
7 * Escape a string to be suitable as a CSS name.
8 * @param {String} unsafeName
9 * @returns {String}
10 */
11 function escapeCSS(unsafeName) {
12 return unsafeName.replace(/([^0-9a-zA-Z-])/g, '\\$1');
13 }
14
15
16 /**
17 * Given a pair of Array tuples containing scope names and scope details,
18 * return the comparison between the two, for sorting.
19 * Scopes are sorted such that they are grouped by application, then name.
20 * Empty applications are sorted ahead of extant applications.
21 * @param {Array} a
22 * @param {Array} b
23 * @returns {Number}
24 */
25 function scopeCompare([aScope, aDetails], [bScope, bDetails]) {
26 const { application: aApp } = aDetails;
27 const { application: bApp } = bDetails;
28 if ((aApp || bApp) && (aApp !== bApp)) {
29 if (!aApp) {
30 return -1;
31 }
32 if (!bApp) {
33 return 1;
34 }
35 if (aApp > bApp) {
36 return 1;
37 }
38 return -1;
39 }
40
41 if (aScope > bScope) {
42 return 1;
43 } else if (aScope < bScope) {
44 return -1;
45 }
46 return 0;
47 }
48
49
50 /**
51 * Populate common navLinks for page templates.
52 * @param {Number} pagePathLevel
53 * @param {Object} ctx
54 * @param {Object} options
55 */
56 function navLinks(pagePathLevel, ctx, options) {
57 if (!options.navLinks) {
58 options.navLinks = [];
59 }
60 const rootPath = '../'.repeat(pagePathLevel);
61
62 if (options.pageIdentifier !== 'admin') {
63 options.navLinks.push({
64 text: 'Admin',
65 href: `${rootPath}admin/`,
66 });
67 }
68 if (options.pageIdentifier !== 'ticketProffer') {
69 options.navLinks.push({
70 text: 'Ticket',
71 href: `${rootPath}admin/ticket`,
72 });
73 }
74 }
75
76 module.exports = Object.assign(Object.create(TemplateHelper), {
77 escapeCSS,
78 scopeCompare,
79 navLinks,
80 });