@brandlab-digital/site-core 3.8.0 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analytics.d.ts.map +1 -1
- package/dist/analytics.js +9 -0
- package/dist/analytics.js.map +1 -1
- package/dist/contact.d.ts +9 -0
- package/dist/contact.d.ts.map +1 -1
- package/dist/contact.js +15 -2
- package/dist/contact.js.map +1 -1
- package/dist/navigation.d.ts +145 -2
- package/dist/navigation.d.ts.map +1 -1
- package/dist/navigation.js +682 -2
- package/dist/navigation.js.map +1 -1
- package/dist/strategy.d.ts +5 -1
- package/dist/strategy.d.ts.map +1 -1
- package/dist/strategy.js +28 -4
- package/dist/strategy.js.map +1 -1
- package/package.json +1 -1
package/dist/navigation.js
CHANGED
|
@@ -3,8 +3,688 @@
|
|
|
3
3
|
* A `menu` item is a real link to its hub plus a separate disclosure button (the WAI-ARIA disclosure navigation
|
|
4
4
|
* pattern); its panel always repeats the hub link first. Prices never appear in menus.
|
|
5
5
|
*
|
|
6
|
-
* `defineBrandlabNavigation` (3.9.0)
|
|
7
|
-
*
|
|
6
|
+
* `defineBrandlabNavigation` (3.9.0) takes the menu as data plus the rules context (which pages exist, the money
|
|
7
|
+
* services, the recorded order) and enforces the N1 rules, throwing a precise error when one is broken.
|
|
8
|
+
* `validateBrandlabNavigation` returns the same findings without throwing; the conformance rule applies it to a
|
|
9
|
+
* rendered header. The types below are the contract the ui-kit header renders.
|
|
8
10
|
*/
|
|
9
11
|
export const BRANDLAB_NAV_PANELS = ['list', 'columns', 'featured'];
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Closed labels, the pages a menu can link to and the rules context
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
/**
|
|
16
|
+
* The top-level labels N1 allows (v1 5.2, rule 6). A money-service name is allowed too (split mode), and so is a
|
|
17
|
+
* label Romie recorded for the site ("Locations" on a live site until its next approved change).
|
|
18
|
+
*/
|
|
19
|
+
export const BRANDLAB_NAV_LABELS = [
|
|
20
|
+
'Home',
|
|
21
|
+
'About',
|
|
22
|
+
'Services',
|
|
23
|
+
'More services',
|
|
24
|
+
'Areas',
|
|
25
|
+
'Gallery',
|
|
26
|
+
'Projects',
|
|
27
|
+
'Our work',
|
|
28
|
+
'Reviews',
|
|
29
|
+
'Guides',
|
|
30
|
+
'Blog',
|
|
31
|
+
'FAQs',
|
|
32
|
+
'Resources',
|
|
33
|
+
'Prices',
|
|
34
|
+
'Contact',
|
|
35
|
+
];
|
|
36
|
+
export const BRANDLAB_NAV_PAGE_KEYS = [
|
|
37
|
+
'about',
|
|
38
|
+
'services',
|
|
39
|
+
'areas',
|
|
40
|
+
'gallery',
|
|
41
|
+
'projects',
|
|
42
|
+
'guides',
|
|
43
|
+
'faqs',
|
|
44
|
+
'reviews',
|
|
45
|
+
'prices',
|
|
46
|
+
'contact',
|
|
47
|
+
];
|
|
48
|
+
/** The paths a page is usually served at, for reading a site's pages from its routes or its rendered links. */
|
|
49
|
+
export const BRANDLAB_NAV_PAGE_ALIASES = Object.freeze({
|
|
50
|
+
about: ['/about', '/about-us'],
|
|
51
|
+
services: ['/services', '/our-services'],
|
|
52
|
+
areas: ['/areas', '/locations', '/service-areas', '/areas-we-service', '/areas-we-serve'],
|
|
53
|
+
gallery: ['/gallery'],
|
|
54
|
+
projects: ['/projects', '/our-projects', '/portfolio', '/our-work'],
|
|
55
|
+
guides: ['/guides', '/blog', '/cost-guides'],
|
|
56
|
+
faqs: ['/faqs', '/faq'],
|
|
57
|
+
reviews: ['/reviews'],
|
|
58
|
+
prices: ['/prices', '/pricing'],
|
|
59
|
+
contact: ['/contact', '/contact-us'],
|
|
60
|
+
});
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// Findings
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
export const BRANDLAB_NAV_RULES = [
|
|
65
|
+
'shape',
|
|
66
|
+
'label',
|
|
67
|
+
'label-page',
|
|
68
|
+
'proof-label',
|
|
69
|
+
'required-link',
|
|
70
|
+
'about-early',
|
|
71
|
+
'services-before-areas',
|
|
72
|
+
'contact-last',
|
|
73
|
+
'home-first',
|
|
74
|
+
'hub',
|
|
75
|
+
'price',
|
|
76
|
+
'split',
|
|
77
|
+
'single-child',
|
|
78
|
+
'panel',
|
|
79
|
+
'areas-mode',
|
|
80
|
+
'recorded',
|
|
81
|
+
];
|
|
82
|
+
export class BrandlabNavigationError extends Error {
|
|
83
|
+
findings;
|
|
84
|
+
constructor(findings) {
|
|
85
|
+
super(`The navigation breaks ${findings.length === 1 ? 'an N1 rule' : `${findings.length} N1 rules`}:\n` +
|
|
86
|
+
findings.map((finding) => `- ${finding.message}`).join('\n'));
|
|
87
|
+
this.name = 'BrandlabNavigationError';
|
|
88
|
+
this.findings = findings;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
// Reading paths and keys
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
function isText(value) {
|
|
95
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
96
|
+
}
|
|
97
|
+
function pathKey(href) {
|
|
98
|
+
const [beforeHash = ''] = href.split('#');
|
|
99
|
+
const [beforeQuery = ''] = beforeHash.split('?');
|
|
100
|
+
if (beforeQuery === '')
|
|
101
|
+
return '';
|
|
102
|
+
const trimmed = beforeQuery.length > 1 ? beforeQuery.replace(/\/+$/, '') : beforeQuery;
|
|
103
|
+
return (trimmed || '/').toLowerCase();
|
|
104
|
+
}
|
|
105
|
+
function pathsOf(value) {
|
|
106
|
+
if (value === undefined)
|
|
107
|
+
return [];
|
|
108
|
+
const list = typeof value === 'string' ? [value] : [...value];
|
|
109
|
+
return list.filter(isText).map(pathKey);
|
|
110
|
+
}
|
|
111
|
+
function slug(text) {
|
|
112
|
+
return text
|
|
113
|
+
.toLowerCase()
|
|
114
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
115
|
+
.replace(/(^-|-$)/g, '');
|
|
116
|
+
}
|
|
117
|
+
function usableHref(href) {
|
|
118
|
+
return isText(href) && !href.trim().startsWith('#') && !/^javascript:/i.test(href.trim());
|
|
119
|
+
}
|
|
120
|
+
/** The pages named in the route catalogue (by kind first, then by usual path). Always-required pages fall back to usual paths. */
|
|
121
|
+
export function navigationPagesFromRoutes(routes) {
|
|
122
|
+
const paths = new Set(routes.map((route) => pathKey(route.path)));
|
|
123
|
+
const byKind = {};
|
|
124
|
+
const kinds = {
|
|
125
|
+
'service-index': 'services',
|
|
126
|
+
'location-index': 'areas',
|
|
127
|
+
'blog-index': 'guides',
|
|
128
|
+
'project-index': 'projects',
|
|
129
|
+
contact: 'contact',
|
|
130
|
+
};
|
|
131
|
+
for (const route of routes) {
|
|
132
|
+
const key = route.kind ? kinds[route.kind] : undefined;
|
|
133
|
+
if (key && !byKind[key])
|
|
134
|
+
byKind[key] = route.path;
|
|
135
|
+
}
|
|
136
|
+
const found = {};
|
|
137
|
+
for (const key of BRANDLAB_NAV_PAGE_KEYS) {
|
|
138
|
+
const named = byKind[key];
|
|
139
|
+
const aliased = BRANDLAB_NAV_PAGE_ALIASES[key].filter((alias) => paths.has(alias));
|
|
140
|
+
const all = [...new Set([...(named ? [named] : []), ...aliased])];
|
|
141
|
+
if (all.length > 0)
|
|
142
|
+
found[key] = all;
|
|
143
|
+
}
|
|
144
|
+
const required = (key) => found[key] ?? BRANDLAB_NAV_PAGE_ALIASES[key];
|
|
145
|
+
const pages = {
|
|
146
|
+
about: required('about'),
|
|
147
|
+
services: required('services'),
|
|
148
|
+
contact: required('contact'),
|
|
149
|
+
};
|
|
150
|
+
for (const key of ['areas', 'gallery', 'projects', 'guides', 'faqs', 'reviews', 'prices']) {
|
|
151
|
+
if (found[key])
|
|
152
|
+
pages[key] = found[key];
|
|
153
|
+
}
|
|
154
|
+
return pages;
|
|
155
|
+
}
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
// The N1 rules
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
/** What each closed label points at: the page keys it may link to. */
|
|
160
|
+
const LABEL_PAGES = {
|
|
161
|
+
About: ['about'],
|
|
162
|
+
Services: ['services'],
|
|
163
|
+
'More services': ['services'],
|
|
164
|
+
Areas: ['areas'],
|
|
165
|
+
Gallery: ['gallery'],
|
|
166
|
+
Projects: ['projects'],
|
|
167
|
+
'Our work': ['projects', 'gallery'],
|
|
168
|
+
Reviews: ['reviews'],
|
|
169
|
+
Guides: ['guides'],
|
|
170
|
+
Blog: ['guides'],
|
|
171
|
+
FAQs: ['faqs'],
|
|
172
|
+
Resources: ['guides', 'faqs'],
|
|
173
|
+
Prices: ['prices'],
|
|
174
|
+
Contact: ['contact'],
|
|
175
|
+
};
|
|
176
|
+
/** Menus whose panel counts the hub as one of its children. */
|
|
177
|
+
const HUB_COUNTS_AS_CHILD = new Set(['our work', 'resources']);
|
|
178
|
+
const SERVICES_LIST_MAX = 8;
|
|
179
|
+
const AREAS_LIST_MAX = 12;
|
|
180
|
+
const PRICE = /\$/;
|
|
181
|
+
function rolesOf(path, pages) {
|
|
182
|
+
const roles = new Set();
|
|
183
|
+
for (const key of BRANDLAB_NAV_PAGE_KEYS) {
|
|
184
|
+
if (path !== '' && pathsOf(pages[key]).includes(path))
|
|
185
|
+
roles.add(key);
|
|
186
|
+
}
|
|
187
|
+
return roles;
|
|
188
|
+
}
|
|
189
|
+
/** The recorded-order keys an item answers to: its id, the v1 `svc:` form and the page it links to. */
|
|
190
|
+
function keysOf(item, path, roles) {
|
|
191
|
+
const keys = new Set([item.id, `svc:${item.id}`]);
|
|
192
|
+
if (path === '/')
|
|
193
|
+
keys.add('home');
|
|
194
|
+
for (const role of roles)
|
|
195
|
+
keys.add(role);
|
|
196
|
+
if (roles.has('guides'))
|
|
197
|
+
keys.add('blog');
|
|
198
|
+
return [...keys].map((key) => key.toLowerCase());
|
|
199
|
+
}
|
|
200
|
+
function pageLabel(key) {
|
|
201
|
+
return key === 'faqs' ? 'FAQs' : key.charAt(0).toUpperCase() + key.slice(1);
|
|
202
|
+
}
|
|
203
|
+
function firstPath(pages, key) {
|
|
204
|
+
return pathsOf(pages[key])[0] ?? '';
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Applies the N1 rules to a menu and returns every finding (none when the menu passes). The rules follow v1 5.1 to
|
|
208
|
+
* 5.5: required links when their condition holds, About in the first two items, services before Areas, Contact last,
|
|
209
|
+
* labels from the closed list, every dropdown holding its hub link, no prices, split mode for two or three money
|
|
210
|
+
* services and a recorded order reproduced exactly (a recorded order replaces the three position rules).
|
|
211
|
+
*/
|
|
212
|
+
export function validateBrandlabNavigation(nav, context, options = {}) {
|
|
213
|
+
const findings = [];
|
|
214
|
+
const skip = new Set(options.skip ?? []);
|
|
215
|
+
const add = (rule, message, item) => {
|
|
216
|
+
if (skip.has(rule))
|
|
217
|
+
return;
|
|
218
|
+
findings.push(item ? { rule, message, item: item.id } : { rule, message });
|
|
219
|
+
};
|
|
220
|
+
const items = nav?.items;
|
|
221
|
+
if (!Array.isArray(items) || items.length === 0) {
|
|
222
|
+
add('shape', 'The navigation has no items.');
|
|
223
|
+
return findings;
|
|
224
|
+
}
|
|
225
|
+
const pages = context.pages;
|
|
226
|
+
const recorded = context.recorded;
|
|
227
|
+
const money = context.moneyServices;
|
|
228
|
+
const servicePaths = pathsOf(pages.services);
|
|
229
|
+
const name = (item, index) => (isText(item.label) ? `"${item.label}"` : `Item ${index + 1}`);
|
|
230
|
+
// Shape: every item is something the header can render.
|
|
231
|
+
const idOwners = new Map();
|
|
232
|
+
const facts = [];
|
|
233
|
+
for (const [index, item] of items.entries()) {
|
|
234
|
+
const who = name(item, index);
|
|
235
|
+
if (!isText(item.label))
|
|
236
|
+
add('shape', `${who} has no label.`, item);
|
|
237
|
+
else if (item.label !== item.label.trim())
|
|
238
|
+
add('shape', `${who} has spaces around its label.`, item);
|
|
239
|
+
if (!isText(item.id))
|
|
240
|
+
add('shape', `${who} has no id.`, item);
|
|
241
|
+
else if (idOwners.has(item.id))
|
|
242
|
+
add('shape', `${who} and ${idOwners.get(item.id)} both give the id "${item.id}".`, item);
|
|
243
|
+
else
|
|
244
|
+
idOwners.set(item.id, who);
|
|
245
|
+
if (!usableHref(item.href) && item.kind !== 'menu')
|
|
246
|
+
add('shape', `${who} does not link to a page.`, item);
|
|
247
|
+
const path = usableHref(item.href) ? pathKey(item.href) : '';
|
|
248
|
+
const roles = rolesOf(path, pages);
|
|
249
|
+
facts.push({ item, index, path, roles, keys: keysOf(item, path, roles) });
|
|
250
|
+
}
|
|
251
|
+
const menus = facts.filter((fact) => fact.item.kind === 'menu');
|
|
252
|
+
// Prices never appear in a menu.
|
|
253
|
+
const checkPrice = (text, where, item) => {
|
|
254
|
+
if (text && PRICE.test(text))
|
|
255
|
+
add('price', `${where} shows a price ("${text}"); menus never carry prices.`, item);
|
|
256
|
+
};
|
|
257
|
+
for (const { item, index } of facts) {
|
|
258
|
+
const who = name(item, index);
|
|
259
|
+
checkPrice(item.label, who, item);
|
|
260
|
+
if (item.kind !== 'menu')
|
|
261
|
+
continue;
|
|
262
|
+
checkPrice(item.hubLabel, `The hub link of ${who}`, item);
|
|
263
|
+
for (const link of item.links ?? []) {
|
|
264
|
+
checkPrice(link.label, `The ${who} panel link "${link.label}"`, item);
|
|
265
|
+
checkPrice(link.description, `The description of "${link.label}" in the ${who} panel`, item);
|
|
266
|
+
}
|
|
267
|
+
for (const group of item.groups ?? []) {
|
|
268
|
+
checkPrice(group.heading, `The "${group.heading}" column heading in the ${who} panel`, item);
|
|
269
|
+
}
|
|
270
|
+
if (item.featured) {
|
|
271
|
+
checkPrice(item.featured.label, `The featured card in the ${who} panel`, item);
|
|
272
|
+
checkPrice(item.featured.description, `The description of the featured card in the ${who} panel`, item);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
// Dropdowns: the hub link, the panel's shape and single-child items.
|
|
276
|
+
for (const { item, index, roles } of menus) {
|
|
277
|
+
const who = name(item, index);
|
|
278
|
+
if (!usableHref(item.href)) {
|
|
279
|
+
add('hub', `${who} is a dropdown whose label does not link to a hub page; the label links to the hub and the panel opens with the same link.`, item);
|
|
280
|
+
}
|
|
281
|
+
else if (!isText(item.hubLabel)) {
|
|
282
|
+
add('hub', `The ${who} panel does not open with a link to its hub (${item.href}); every dropdown holds its hub link, for example "All services".`, item);
|
|
283
|
+
}
|
|
284
|
+
const links = Array.isArray(item.links) ? item.links : [];
|
|
285
|
+
if (!Array.isArray(item.links))
|
|
286
|
+
add('shape', `${who} has no panel links.`, item);
|
|
287
|
+
const hubPath = usableHref(item.href) ? pathKey(item.href) : '';
|
|
288
|
+
for (const link of links) {
|
|
289
|
+
if (!isText(link.label) || !usableHref(link.href)) {
|
|
290
|
+
add('shape', `${who} has a panel link without ${isText(link.label) ? 'a page to link to' : 'a label'}.`, item);
|
|
291
|
+
}
|
|
292
|
+
else if (hubPath !== '' && pathKey(link.href) === hubPath) {
|
|
293
|
+
add('hub', `The ${who} panel lists its hub (${link.href}) again as "${link.label}"; the header adds the hub link first from the hub label.`, item);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (!HUB_COUNTS_AS_CHILD.has((isText(item.label) ? item.label : '').toLowerCase())) {
|
|
297
|
+
if (links.length === 0)
|
|
298
|
+
add('single-child', `${who} is a dropdown with nothing in it, so it stays a plain link.`, item);
|
|
299
|
+
else if (links.length === 1)
|
|
300
|
+
add('single-child', `${who} has a single child link, so it stays a plain link (a dropdown needs at least two).`, item);
|
|
301
|
+
}
|
|
302
|
+
else if (links.length === 0) {
|
|
303
|
+
add('single-child', `${who} is a dropdown with nothing in it, so it stays a plain link.`, item);
|
|
304
|
+
}
|
|
305
|
+
if (!BRANDLAB_NAV_PANELS.includes(item.panel)) {
|
|
306
|
+
add('panel', `${who} has an unknown panel type "${String(item.panel)}".`, item);
|
|
307
|
+
}
|
|
308
|
+
else if (item.panel === 'columns') {
|
|
309
|
+
const groups = item.groups ?? [];
|
|
310
|
+
const sizes = groups.map((group) => group.links.length);
|
|
311
|
+
if (groups.length < 2 || groups.length > 3) {
|
|
312
|
+
add('panel', `${who} uses ${groups.length === 0 ? 'no' : groups.length} column groups; a columns panel has two or three.`, item);
|
|
313
|
+
}
|
|
314
|
+
else if (Math.max(...sizes) - Math.min(...sizes) > 1) {
|
|
315
|
+
add('panel', `The ${who} columns are unbalanced (${sizes.join(', ')} links); they must be within one link of each other.`, item);
|
|
316
|
+
}
|
|
317
|
+
for (const group of groups) {
|
|
318
|
+
if (!isText(group.heading))
|
|
319
|
+
add('panel', `A column in the ${who} panel has no heading.`, item);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
else if (item.groups && item.groups.length > 0) {
|
|
323
|
+
add('panel', `${who} has column groups but a ${item.panel} panel.`, item);
|
|
324
|
+
}
|
|
325
|
+
if (item.panel === 'featured') {
|
|
326
|
+
const featured = item.featured;
|
|
327
|
+
if (!featured || !isText(featured.label) || !usableHref(featured.href) || !isText(featured.description)) {
|
|
328
|
+
add('panel', `${who} has a featured panel without a complete featured card (label, link and description).`, item);
|
|
329
|
+
}
|
|
330
|
+
else if (!isText(featured.image?.src) || !isText(featured.image?.alt) || !(featured.image.width > 0) || !(featured.image.height > 0)) {
|
|
331
|
+
add('panel', `The featured card in the ${who} panel needs an image with its alt text and size.`, item);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
else if (item.featured) {
|
|
335
|
+
add('panel', `${who} has a featured card but a ${item.panel} panel.`, item);
|
|
336
|
+
}
|
|
337
|
+
if (item.panel !== 'columns') {
|
|
338
|
+
const isAreas = roles.has('areas');
|
|
339
|
+
const cap = isAreas ? AREAS_LIST_MAX : roles.has('services') && item.label !== 'More services' ? SERVICES_LIST_MAX : undefined;
|
|
340
|
+
if (cap !== undefined && links.length > cap) {
|
|
341
|
+
add('panel', isAreas
|
|
342
|
+
? `The ${who} dropdown lists ${links.length} regions; it holds up to ${cap}.`
|
|
343
|
+
: `The ${who} dropdown lists ${links.length} services; more than ${cap} use a columns panel.`, item);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
// Labels: the closed list, a money-service name or a recorded label.
|
|
348
|
+
const recordedLabels = new Map();
|
|
349
|
+
for (const [key, label] of Object.entries(recorded?.labels ?? {}))
|
|
350
|
+
recordedLabels.set(key.toLowerCase(), label);
|
|
351
|
+
const recordedLabelFor = (fact) => fact.keys.map((key) => recordedLabels.get(key)).find((label) => label !== undefined);
|
|
352
|
+
const closed = new Set(BRANDLAB_NAV_LABELS);
|
|
353
|
+
const moneyFor = (fact) => money?.find((service) => pathKey(service.href) === fact.path);
|
|
354
|
+
const isServiceChild = (path) => servicePaths.some((root) => root !== '' && path.startsWith(`${root}/`));
|
|
355
|
+
const isServiceItem = (fact) => fact.roles.has('services') || moneyFor(fact) !== undefined || (money === undefined && isServiceChild(fact.path));
|
|
356
|
+
const closedLabels = BRANDLAB_NAV_LABELS.join(', ');
|
|
357
|
+
for (const fact of facts) {
|
|
358
|
+
const { item } = fact;
|
|
359
|
+
if (!isText(item.label))
|
|
360
|
+
continue;
|
|
361
|
+
const service = moneyFor(fact);
|
|
362
|
+
if (closed.has(item.label)) {
|
|
363
|
+
if (service && service.label !== item.label) {
|
|
364
|
+
add('label', `"${item.label}" links to ${item.href}, which is the money service "${service.label}"; name the item after the service.`, item);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
else if (service) {
|
|
368
|
+
if (service.label !== item.label)
|
|
369
|
+
add('label', `"${item.label}" must be named after its money service, "${service.label}".`, item);
|
|
370
|
+
}
|
|
371
|
+
else if (recordedLabelFor(fact) === item.label) {
|
|
372
|
+
// A label Romie recorded for this item.
|
|
373
|
+
}
|
|
374
|
+
else if (money === undefined && isServiceChild(fact.path)) {
|
|
375
|
+
// A money-service dropdown on a site that has not listed its money services.
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
add('label', `"${item.label}" is not a label the closed list allows (${closedLabels}, or the name of a money service).`, item);
|
|
379
|
+
}
|
|
380
|
+
const recordedLabel = recordedLabelFor(fact);
|
|
381
|
+
if (recordedLabel !== undefined && recordedLabel !== item.label) {
|
|
382
|
+
add('recorded', `The recorded label for "${item.id}" is "${recordedLabel}", but the menu says "${item.label}".`, item);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
// Each closed label links to its own page.
|
|
386
|
+
for (const fact of facts) {
|
|
387
|
+
const { item } = fact;
|
|
388
|
+
if (!isText(item.label) || !usableHref(item.href) || recordedLabelFor(fact) === item.label)
|
|
389
|
+
continue;
|
|
390
|
+
if (item.label === 'Home') {
|
|
391
|
+
if (fact.path !== '/')
|
|
392
|
+
add('label-page', `"Home" links to ${item.href}, but Home is the front page (/).`, item);
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const wanted = LABEL_PAGES[item.label];
|
|
396
|
+
if (!wanted)
|
|
397
|
+
continue;
|
|
398
|
+
const defined = wanted.filter((key) => pathsOf(pages[key]).length > 0);
|
|
399
|
+
if (!wanted.some((key) => fact.roles.has(key))) {
|
|
400
|
+
if (defined.length > 0) {
|
|
401
|
+
const where = defined.map((key) => firstPath(pages, key)).join(' or ');
|
|
402
|
+
add('label-page', `"${item.label}" links to ${item.href}, but the ${defined.map(pageLabel).join(' or ')} page is ${where}.`, item);
|
|
403
|
+
}
|
|
404
|
+
else if (fact.roles.size > 0) {
|
|
405
|
+
add('label-page', `"${item.label}" links to ${item.href}, which is the ${[...fact.roles].map(pageLabel).join(' or ')} page, not a ${wanted.map(pageLabel).join(' or ')} page.`, item);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if (item.label === 'Our work' && (pathsOf(pages.gallery).length === 0 || pathsOf(pages.projects).length === 0)) {
|
|
409
|
+
add('proof-label', '"Our work" is for a site with both a gallery and projects; use "Gallery" or "Projects" for the one it has.', item);
|
|
410
|
+
}
|
|
411
|
+
if (item.label === 'Our work') {
|
|
412
|
+
if (item.kind !== 'menu') {
|
|
413
|
+
add('proof-label', '"Our work" is a dropdown (Projects, Gallery and more), not a plain link; a site with one proof page uses its name instead.', item);
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
for (const key of ['gallery', 'projects']) {
|
|
417
|
+
const inPanel = fact.roles.has(key) || (item.links ?? []).some((link) => usableHref(link.href) && pathsOf(pages[key]).includes(pathKey(link.href)));
|
|
418
|
+
if (pathsOf(pages[key]).length > 0 && !inPanel) {
|
|
419
|
+
add('label-page', `The "Our work" panel has no link to the ${pageLabel(key)} page (${firstPath(pages, key)}).`, item);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
if (item.label === 'Resources') {
|
|
425
|
+
// Resources is the fit fold: Guides and FAQs together in one dropdown, and not also on their own.
|
|
426
|
+
if (item.kind !== 'menu') {
|
|
427
|
+
add('label-page', '"Resources" is a dropdown that folds Guides and FAQs together, not a plain link.', item);
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
const paths = (key) => [...pathsOf(pages[key]), ...BRANDLAB_NAV_PAGE_ALIASES[key]];
|
|
431
|
+
const holds = (key) => paths(key).includes(fact.path) || (item.links ?? []).some((link) => usableHref(link.href) && paths(key).includes(pathKey(link.href)));
|
|
432
|
+
if (!holds('guides') || !holds('faqs')) {
|
|
433
|
+
add('label-page', `"Resources" is only for folding Guides and FAQs into one dropdown, so its panel holds the Guides hub and FAQs.`, item);
|
|
434
|
+
}
|
|
435
|
+
const alone = facts.find((other) => other !== fact && (other.roles.has('guides') || other.roles.has('faqs')));
|
|
436
|
+
if (alone)
|
|
437
|
+
add('label-page', `"Resources" folds Guides and FAQs, but "${alone.item.label}" is also in the menu on its own.`, alone.item);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
// Required links, when their condition holds.
|
|
442
|
+
const topLevel = (key) => facts.some((fact) => fact.roles.has(key));
|
|
443
|
+
const inResources = (key) => menus.some(({ item }) => item.label === 'Resources' &&
|
|
444
|
+
(item.links ?? []).some((link) => usableHref(link.href) && pathsOf(pages[key]).includes(pathKey(link.href))));
|
|
445
|
+
const reach = (key) => topLevel(key) || ((key === 'guides' || key === 'faqs') && inResources(key));
|
|
446
|
+
const split = money !== undefined && money.length >= 2 && money.length <= 3 && money.every((service) => service.subServices >= 2);
|
|
447
|
+
const splitServed = split && money.every((service) => menus.some((menu) => menu.path === pathKey(service.href)));
|
|
448
|
+
const missing = (label, key, because) => add('required-link', `${label} is missing: the menu has no link to the ${pageLabel(key)} page (${firstPath(pages, key)}); ${because}.`);
|
|
449
|
+
if (!reach('about'))
|
|
450
|
+
missing('About', 'about', 'About is always required');
|
|
451
|
+
if (!reach('services') && !splitServed)
|
|
452
|
+
missing('Services', 'services', 'Services, or the money-service dropdowns, are always required');
|
|
453
|
+
if (pathsOf(pages.areas).length > 0 && !reach('areas'))
|
|
454
|
+
missing('Areas', 'areas', 'Areas is required whenever location routes exist');
|
|
455
|
+
const proof = ['gallery', 'projects'].filter((key) => pathsOf(pages[key]).length > 0);
|
|
456
|
+
if (proof.length > 0 && !proof.some((key) => reach(key))) {
|
|
457
|
+
add('required-link', `${proof.length === 2 ? 'Our work' : pageLabel(proof[0])} is missing: the menu links to none of ${proof.map((key) => `${pageLabel(key)} (${firstPath(pages, key)})`).join(' and ')}; the proof page is required when one exists.`);
|
|
458
|
+
}
|
|
459
|
+
if (pathsOf(pages.guides).length > 0 && !reach('guides'))
|
|
460
|
+
missing('Guides', 'guides', 'Guides is required when guides exist, and can sit inside Resources');
|
|
461
|
+
if (pathsOf(pages.faqs).length > 0 && !reach('faqs'))
|
|
462
|
+
missing('FAQs', 'faqs', 'FAQs is required when the page exists, and can sit inside Resources');
|
|
463
|
+
if (!reach('contact'))
|
|
464
|
+
missing('Contact', 'contact', 'Contact is always required');
|
|
465
|
+
// Split mode: two or three money services that each have sub-services.
|
|
466
|
+
if (money !== undefined) {
|
|
467
|
+
if (split) {
|
|
468
|
+
for (const service of money) {
|
|
469
|
+
const found = menus.find((menu) => menu.path === pathKey(service.href));
|
|
470
|
+
if (!found) {
|
|
471
|
+
const plain = facts.find((fact) => fact.path === pathKey(service.href));
|
|
472
|
+
add('split', plain
|
|
473
|
+
? `Split mode: "${service.label}" has ${service.subServices} sub-services, so it is a dropdown, not a plain link.`
|
|
474
|
+
: `Split mode: "${service.label}" needs its own dropdown (${service.href}); each of the ${money.length} money services gets one and the rest go under More services.`, plain?.item);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
for (const fact of facts) {
|
|
478
|
+
if (fact.item.label === 'Services') {
|
|
479
|
+
add('split', 'Split mode replaces the single Services dropdown with one dropdown per money service plus More services.', fact.item);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const more = facts.find((fact) => fact.item.label === 'More services');
|
|
483
|
+
if (context.otherServices !== undefined && context.otherServices > 0 && !more) {
|
|
484
|
+
add('split', `Split mode needs More services to hold the other ${context.otherServices} service${context.otherServices === 1 ? '' : 's'}.`);
|
|
485
|
+
}
|
|
486
|
+
if (context.otherServices === 0 && more)
|
|
487
|
+
add('split', 'More services has nothing to hold: every service is already under a money-service dropdown.', more.item);
|
|
488
|
+
if (more && more.item.kind !== 'menu')
|
|
489
|
+
add('split', 'More services is a dropdown holding the other services, not a plain link.', more.item);
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
for (const fact of facts) {
|
|
493
|
+
if (fact.item.label === 'More services') {
|
|
494
|
+
add('split', 'More services belongs to split mode, which needs two or three money services that each have sub-services.', fact.item);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
// Areas is a plain link unless the site records a dropdown.
|
|
500
|
+
const areasMode = recorded?.areas ?? context.areasMode ?? 'link';
|
|
501
|
+
for (const fact of facts) {
|
|
502
|
+
if (!fact.roles.has('areas'))
|
|
503
|
+
continue;
|
|
504
|
+
if (areasMode === 'link' && fact.item.kind === 'menu') {
|
|
505
|
+
add('areas-mode', `${name(fact.item, fact.index)} is a dropdown; Areas is a plain link to its hub unless the site records areas: 'menu'.`, fact.item);
|
|
506
|
+
}
|
|
507
|
+
else if (areasMode === 'menu' && fact.item.kind === 'link') {
|
|
508
|
+
add('areas-mode', `${name(fact.item, fact.index)} is a plain link but the site records areas: 'menu'.`, fact.item);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (recorded)
|
|
512
|
+
validateRecordedShape(recorded, add);
|
|
513
|
+
if (recorded?.order !== undefined) {
|
|
514
|
+
validateRecordedOrder(recorded, facts, add);
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
// Relative rules: only where no order is recorded.
|
|
518
|
+
const home = facts.find((fact) => fact.path === '/');
|
|
519
|
+
if (home && home.index > 0)
|
|
520
|
+
add('home-first', `Home comes first when it is shown, but it is item ${home.index + 1}.`, home.item);
|
|
521
|
+
const about = facts.find((fact) => fact.roles.has('about'));
|
|
522
|
+
if (about && about.index > 1) {
|
|
523
|
+
add('about-early', `About must be one of the first two items, but it is item ${about.index + 1}.`, about.item);
|
|
524
|
+
}
|
|
525
|
+
const areas = facts.find((fact) => fact.roles.has('areas'));
|
|
526
|
+
if (areas) {
|
|
527
|
+
const late = facts.filter((fact) => fact.index > areas.index && isServiceItem(fact));
|
|
528
|
+
for (const fact of late) {
|
|
529
|
+
add('services-before-areas', `Services come before Areas, but ${name(fact.item, fact.index)} follows ${name(areas.item, areas.index)}.`, fact.item);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
const contact = facts.find((fact) => fact.roles.has('contact'));
|
|
533
|
+
const last = facts[facts.length - 1];
|
|
534
|
+
if (contact && contact !== last) {
|
|
535
|
+
add('contact-last', `Contact must be the last item, but ${name(last.item, last.index)} follows it.`, contact.item);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return findings;
|
|
539
|
+
}
|
|
540
|
+
function validateRecordedShape(recorded, add) {
|
|
541
|
+
const date = recorded.recordedOn;
|
|
542
|
+
if (!isText(recorded.setBy) || typeof date !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(date) || Number.isNaN(Date.parse(date))) {
|
|
543
|
+
add('recorded', 'A recorded menu needs setBy (who set it) and recordedOn (the date as YYYY-MM-DD).');
|
|
544
|
+
}
|
|
545
|
+
if (recorded.order !== undefined) {
|
|
546
|
+
const order = recorded.order;
|
|
547
|
+
if (!Array.isArray(order) || order.length === 0 || !order.every(isText)) {
|
|
548
|
+
add('recorded', 'A recorded order lists the items in order, each by its id or key.');
|
|
549
|
+
}
|
|
550
|
+
else {
|
|
551
|
+
const wanted = order.map((key) => key.trim().toLowerCase());
|
|
552
|
+
const repeated = wanted.find((key, index) => wanted.indexOf(key) !== index);
|
|
553
|
+
if (repeated)
|
|
554
|
+
add('recorded', `The recorded order lists "${repeated}" twice.`);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (recorded.labels !== undefined) {
|
|
558
|
+
const labels = recorded.labels;
|
|
559
|
+
const entries = labels && typeof labels === 'object' && !Array.isArray(labels) ? Object.entries(labels) : undefined;
|
|
560
|
+
if (!entries || !entries.every(([key, label]) => isText(key) && isText(label))) {
|
|
561
|
+
add('recorded', 'Recorded labels pair an item key with its label, for example areas: "Locations".');
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
if (recorded.areas !== undefined && recorded.areas !== 'link' && recorded.areas !== 'menu') {
|
|
565
|
+
add('recorded', "The recorded Areas item is 'link' or 'menu'.");
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
function validateRecordedOrder(recorded, facts, add) {
|
|
569
|
+
const order = Array.isArray(recorded.order) ? recorded.order : [];
|
|
570
|
+
if (order.length === 0 || !order.every(isText))
|
|
571
|
+
return;
|
|
572
|
+
const wanted = order.map((key) => key.trim().toLowerCase());
|
|
573
|
+
const by = isText(recorded.setBy) && isText(recorded.recordedOn) ? ` by ${recorded.setBy} on ${recorded.recordedOn}` : '';
|
|
574
|
+
const count = Math.max(wanted.length, facts.length);
|
|
575
|
+
for (let index = 0; index < count; index += 1) {
|
|
576
|
+
const fact = facts[index];
|
|
577
|
+
const key = wanted[index];
|
|
578
|
+
if (fact && key !== undefined && fact.keys.includes(key))
|
|
579
|
+
continue;
|
|
580
|
+
if (!fact) {
|
|
581
|
+
add('recorded', `The menu ends after "${facts[facts.length - 1].item.label}", but the order recorded${by} continues with "${key}" at position ${index + 1}.`);
|
|
582
|
+
}
|
|
583
|
+
else if (key === undefined) {
|
|
584
|
+
add('recorded', `The menu has "${fact.item.label}" at position ${index + 1}, which the order recorded${by} does not list.`, fact.item);
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
add('recorded', `Position ${index + 1} should be "${key}" as recorded${by}, but the menu has "${fact.item.label}".`, fact.item);
|
|
588
|
+
}
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
function deepFreeze(value) {
|
|
593
|
+
if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
594
|
+
Object.freeze(value);
|
|
595
|
+
for (const child of Object.values(value))
|
|
596
|
+
deepFreeze(child);
|
|
597
|
+
}
|
|
598
|
+
return value;
|
|
599
|
+
}
|
|
600
|
+
function buildItem(input, index, problems) {
|
|
601
|
+
const id = isText(input?.label) ? slug(input.label) : '';
|
|
602
|
+
const activePaths = input.activePaths ? { activePaths: [...input.activePaths] } : {};
|
|
603
|
+
if (input.kind === 'link') {
|
|
604
|
+
return { kind: 'link', id, label: input.label, href: input.href, ...activePaths };
|
|
605
|
+
}
|
|
606
|
+
if (input.kind !== 'menu') {
|
|
607
|
+
problems.push({ rule: 'shape', message: `Item ${index + 1} is neither a link nor a menu.` });
|
|
608
|
+
}
|
|
609
|
+
const flattened = input.groups?.flatMap((group) => group.links.map((link) => ({ ...link })));
|
|
610
|
+
if (flattened && input.links && input.links.length > 0 && JSON.stringify(flattened) !== JSON.stringify(input.links)) {
|
|
611
|
+
problems.push({
|
|
612
|
+
rule: 'shape',
|
|
613
|
+
message: `"${input.label}" gives both links and column groups, and the links are not the groups' links in order; give the groups only.`,
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
const panel = input.panel ?? (input.featured ? 'featured' : input.groups ? 'columns' : 'list');
|
|
617
|
+
return {
|
|
618
|
+
kind: 'menu',
|
|
619
|
+
id,
|
|
620
|
+
label: input.label,
|
|
621
|
+
href: input.href,
|
|
622
|
+
hubLabel: input.hubLabel,
|
|
623
|
+
panel,
|
|
624
|
+
links: flattened ?? (input.links ?? []).map((link) => ({ ...link })),
|
|
625
|
+
...(input.groups ? { groups: input.groups.map((group) => ({ heading: group.heading, links: group.links.map((link) => ({ ...link })) })) } : {}),
|
|
626
|
+
...(input.featured ? { featured: { ...input.featured, image: { ...input.featured.image } } } : {}),
|
|
627
|
+
...activePaths,
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Builds the menu from data and enforces N1. Returns a frozen `BrandlabNavigation` with a stable id per item (the
|
|
632
|
+
* slug of its label), or throws a `BrandlabNavigationError` listing every rule the menu breaks.
|
|
633
|
+
*/
|
|
634
|
+
export function defineBrandlabNavigation(input) {
|
|
635
|
+
if (!input || !Array.isArray(input.items) || !input.rules?.pages) {
|
|
636
|
+
throw new BrandlabNavigationError([{ rule: 'shape', message: 'defineBrandlabNavigation needs items and rules.pages.' }]);
|
|
637
|
+
}
|
|
638
|
+
const problems = [];
|
|
639
|
+
const nav = { items: input.items.map((item, index) => buildItem(item, index, problems)) };
|
|
640
|
+
const findings = [...problems, ...validateBrandlabNavigation(nav, input.rules)];
|
|
641
|
+
if (findings.length > 0)
|
|
642
|
+
throw new BrandlabNavigationError(findings);
|
|
643
|
+
return deepFreeze(nav);
|
|
644
|
+
}
|
|
645
|
+
// ---------------------------------------------------------------------------
|
|
646
|
+
// Fit: the fold order
|
|
647
|
+
// ---------------------------------------------------------------------------
|
|
648
|
+
/** When the menu does not fit at the declared desktop width (check N7), these steps apply in this order. */
|
|
649
|
+
export const BRANDLAB_NAV_FOLD_ORDER = ['resources', 'hide-home', 'raise-breakpoint'];
|
|
650
|
+
/**
|
|
651
|
+
* Applies the model's fold steps in order, up to and including `step`: fold Guides and FAQs into "Resources ▾",
|
|
652
|
+
* then hide Home. The third step (raise the breakpoint) is the header's, not the menu's. A recorded order is never
|
|
653
|
+
* folded. The result is checked against N1 again.
|
|
654
|
+
*/
|
|
655
|
+
export function foldBrandlabNavigation(nav, step, options) {
|
|
656
|
+
if (options.rules.recorded) {
|
|
657
|
+
throw new BrandlabNavigationError([
|
|
658
|
+
{ rule: 'recorded', message: 'A recorded order is reproduced exactly, so it is not folded; raise the desktop breakpoint instead.' },
|
|
659
|
+
]);
|
|
660
|
+
}
|
|
661
|
+
let items = [...nav.items];
|
|
662
|
+
const pages = options.rules.pages;
|
|
663
|
+
const find = (key) => items.find((item) => item.kind === 'link' && pathsOf(pages[key]).includes(pathKey(item.href)));
|
|
664
|
+
const guides = find('guides');
|
|
665
|
+
const faqs = find('faqs');
|
|
666
|
+
if (guides && faqs) {
|
|
667
|
+
const resources = {
|
|
668
|
+
kind: 'menu',
|
|
669
|
+
id: 'resources',
|
|
670
|
+
label: 'Resources',
|
|
671
|
+
href: guides.href,
|
|
672
|
+
hubLabel: options.hubLabel,
|
|
673
|
+
panel: 'list',
|
|
674
|
+
links: [...(options.costGuides ?? []), { label: faqs.label, href: faqs.href }],
|
|
675
|
+
activePaths: [...(guides.activePaths ?? []), faqs.href, ...(faqs.activePaths ?? [])],
|
|
676
|
+
};
|
|
677
|
+
const first = Math.min(items.indexOf(guides), items.indexOf(faqs));
|
|
678
|
+
items = items.filter((item) => item !== guides && item !== faqs);
|
|
679
|
+
items.splice(first, 0, resources);
|
|
680
|
+
}
|
|
681
|
+
if (step === 'hide-home')
|
|
682
|
+
items = items.filter((item) => !(usableHref(item.href) && pathKey(item.href) === '/'));
|
|
683
|
+
return defineBrandlabNavigation({
|
|
684
|
+
items: items.map((item) => item.kind === 'link'
|
|
685
|
+
? item
|
|
686
|
+
: { ...item, panel: item.panel, links: item.groups ? undefined : item.links }),
|
|
687
|
+
rules: options.rules,
|
|
688
|
+
});
|
|
689
|
+
}
|
|
10
690
|
//# sourceMappingURL=navigation.js.map
|