generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
/
footer.js
363 lines (309 loc) · 13.1 KB
/
footer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import {
createTag,
decorateAutoBlock,
decorateLinks,
getConfig,
getMetadata,
loadBlock,
} from '../../utils/utils.js';
import { analyticsDecorateList } from '../../martech/attributes.js';
import { getSVGsfromFile } from '../share/share.js';
const { miloLibs, codeRoot, locale } = getConfig();
const base = miloLibs || codeRoot;
const GLOBE_IMG = '<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" focusable="false" class="footer-region-img" loading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3 31.4 46.7zM45 35.7h-8.1c0.7-3.1 1.1-6.3 1.2-9.5h9.5C47.5 29.5 46.6 32.8 45 35.7z"></path></svg>';
const SPECTRUM_CHEVRON = '<svg aria-hidden="true" class="icon-chevron-down" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><defs><style>.spectrum-chevron-down{fill:none;stroke-linecap:round;stroke-linejoin:round;}</style></defs><path stroke="currentColor" class="spectrum-chevron-down" d="M3.47,5.74l4.53,4.53,4.54-4.53"/></svg>';
const ADCHOICE_IMG = `<img class="footer-link-img" loading="lazy" alt="AdChoices icon" src="${base}/blocks/footer/adchoices-small.svg" height="9" width="9">`;
const SUPPORTED_SOCIAL = ['facebook', 'instagram', 'twitter', 'linkedin', 'pinterest', 'discord', 'behance', 'youtube', 'weibo', 'social-media'];
export class Footer {
constructor(body, footerEl) {
this.footerEl = footerEl;
this.body = body;
this.desktop = window.matchMedia('(min-width: 900px)');
}
init = async () => {
this.desktop.addEventListener('change', this.onMediaChange);
const wrapper = createTag('div', { class: 'footer-wrapper' });
const grid = this.decorateGrid();
if (grid) {
wrapper.append(grid);
}
const infoRow = await this.createInfoRow();
if (infoRow.hasChildNodes()) {
wrapper.append(infoRow);
}
this.addAnalytics(wrapper);
decorateLinks(wrapper);
this.footerEl.append(wrapper);
};
createInfoRow = async () => {
const infoRow = createTag('div', { class: 'footer-info' });
const infoColumnLeft = createTag('div', { class: 'footer-info-column' });
const infoColumnRight = createTag('div', { class: 'footer-info-column' });
const region = await this.decorateRegion();
if (region) {
infoColumnLeft.append(region);
infoRow.classList.add('has-region');
}
const social = await this.decorateSocial();
if (social) {
infoColumnLeft.append(social);
infoRow.classList.add('has-social');
}
const privacy = this.decoratePrivacy();
if (privacy) {
infoColumnRight.append(privacy);
infoRow.classList.add('has-privacy');
}
if (infoColumnLeft.hasChildNodes()) {
infoRow.append(infoColumnLeft);
}
if (infoColumnRight.hasChildNodes()) {
infoRow.append(infoColumnRight);
}
return infoRow;
};
decorateGrid = () => {
const navGrid = createTag('div', { class: 'footer-nav-grid' });
const columns = [...this.body.querySelectorAll('body > div')]
.filter((col) => col.firstElementChild?.nodeName === 'H2');
if (!columns.length) {
this.footerEl.classList.add('footer-small');
}
const titles = [];
columns.forEach((column) => {
column.classList.add('footer-nav-column');
const headings = column.querySelectorAll('h2');
headings.forEach((heading) => {
const titleId = heading.textContent.trim().toLowerCase().replace(/ /g, '-');
const title = createTag('p', { class: 'footer-nav-item-title', id: titleId });
title.textContent = heading.textContent;
const navItem = createTag('div', { class: 'footer-nav-item' });
navItem.append(title);
titles.push(title);
const linksContainer = heading.nextElementSibling;
linksContainer.classList = 'footer-nav-item-links';
linksContainer.id = `${titleId}-menu`;
const links = linksContainer.querySelectorAll('li');
links.forEach((link) => {
link.classList.add('footer-nav-item-link');
});
navItem.append(linksContainer);
column.append(heading);
heading.replaceWith(navItem);
});
navGrid.append(column);
});
if (!this.desktop.matches) {
this.setMobileTitles(titles);
}
return navGrid;
};
decorateRegion = async () => {
let regionButton = this.body.querySelector('.region-selector a');
if (!regionButton) return null;
const regionTextContent = regionButton.textContent;
regionButton.textContent = '';
const regionContainer = createTag('div', { class: 'footer-region' });
const url = new URL(regionButton.href);
if (url.hash !== '') {
// if there is a hash, it is a modal-dialog
decorateAutoBlock(regionButton);
loadBlock(regionButton);
} else {
// if there is no hash, it is an inline-dialog
const inlineDialogContainer = regionButton.cloneNode(false);
regionButton = createTag('button', { type: 'button', 'aria-expanded': 'false' });
regionContainer.append(inlineDialogContainer);
decorateAutoBlock(inlineDialogContainer);
loadBlock(inlineDialogContainer);
regionButton.addEventListener('click', () => {
regionButton.classList.toggle('inline-dialog-active');
const ariaExpanded = regionButton.classList.contains('inline-dialog-active');
regionButton.setAttribute('aria-expanded', ariaExpanded);
});
document.addEventListener('click', (e) => {
if (regionButton.classList.contains('inline-dialog-active') && !e.target.closest('.footer-region')) {
regionButton.setAttribute('aria-expanded', false);
regionButton.classList.remove('inline-dialog-active');
}
});
}
regionButton.className = 'footer-region-button';
regionButton.setAttribute('aria-haspopup', true);
regionButton.setAttribute('aria-label', regionTextContent);
regionButton.setAttribute('role', 'button');
regionButton.setAttribute('tabindex', 0);
const regionText = createTag('span', { class: 'footer-region-text' }, regionTextContent);
regionButton.insertAdjacentHTML('afterbegin', GLOBE_IMG);
regionButton.append(regionText);
regionText.insertAdjacentHTML('afterend', SPECTRUM_CHEVRON);
regionContainer.prepend(regionButton);
return regionContainer;
};
decorateSocial = async () => {
const block = this.body.querySelector('.social');
if (!block) return null;
const socialList = createTag('ul', { class: 'footer-social' });
const svgEls = await getSVGsfromFile(`${base}/blocks/footer/footer-social.svg`, SUPPORTED_SOCIAL);
if (!svgEls || svgEls.length === 0) return null;
SUPPORTED_SOCIAL.forEach((platform) => {
const a = block.querySelector(`a[href*="${platform}"]`);
const svg = svgEls.find((el) => el.name === platform);
if (!a || !svg) return;
const icon = svg.svg;
const li = createTag('li', { class: 'footer-social-icon' });
icon.classList.add('footer-social-img');
icon.setAttribute('alt', `${platform} logo`);
icon.setAttribute('height', 20);
icon.setAttribute('width', 20);
a.setAttribute('aria-label', platform);
a.textContent = '';
a.append(icon);
li.append(a);
socialList.append(li);
});
return socialList;
};
decoratePrivacy = () => {
const copyrightEl = this.body.querySelector('div > p > em');
if (!copyrightEl) return null;
const container = copyrightEl.closest('div');
const privacyWrapper = createTag('div', { class: 'footer-privacy' });
const copyright = createTag('p', { class: 'footer-privacy-copyright' });
const year = new Date().getFullYear();
copyright.textContent = `Copyright © ${year} ${copyrightEl.textContent}`;
privacyWrapper.append(copyright);
const adchoice = container.querySelector('a[href*="#interest-based-ads"]');
adchoice?.insertAdjacentHTML('afterbegin', ADCHOICE_IMG);
const ulClass = 'footer-privacy-links';
const liClass = 'footer-privacy-link';
let ul = container.querySelector('ul');
if (ul) {
ul.classList.add(ulClass);
const listItems = ul.querySelectorAll('li');
[...listItems].forEach((item) => {
item.classList.add(liClass);
});
} else {
const links = container.querySelectorAll('a');
if (!links) return null;
ul = createTag('ul', { class: ulClass });
links.forEach((link) => {
const li = createTag('li', { class: liClass });
li.append(link);
ul.append(li);
});
}
privacyWrapper.append(ul);
const secondLine = container.querySelector('p:nth-of-type(2)');
if (secondLine) {
secondLine.classList.add('footer-copyright-second');
privacyWrapper.append(secondLine);
}
return privacyWrapper;
};
addAnalytics = (el) => {
if (el.nodeName === 'DIV' && el.childElementCount > 0) {
[...el.children].forEach((child) => this.addAnalytics(child));
return;
}
if (el.nodeName === 'UL') {
if (el.previousElementSibling?.classList.contains('footer-nav-item-title')) {
el.setAttribute('daa-lh', el.previousElementSibling.textContent);
}
[...el.children].forEach(analyticsDecorateList);
}
};
toggleMenu = (e) => {
const button = e.target.closest('[role=button]');
const expanded = button.getAttribute('aria-expanded');
if (expanded === 'true') {
this.closeMenu(button);
} else {
this.openMenu(button);
}
};
closeMenu = (el) => {
if (el.id === 'region-button') {
window.removeEventListener('keydown', this.closeOnEscape);
window.removeEventListener('click', this.closeOnDocClick);
}
el.setAttribute('aria-expanded', false);
};
openMenu = (el) => {
const type = el.classList[0];
const expandedMenu = document.querySelector(`.${type}[aria-expanded=true]`);
if (expandedMenu) { this.closeMenu(expandedMenu); }
if (el.id === 'region-button') {
window.addEventListener('keydown', this.closeOnEscape);
window.addEventListener('click', this.closeOnDocClick);
}
el.setAttribute('aria-expanded', true);
};
toggleOnKey = (e) => {
if (e.code === 'Space' || e.code === 'Enter') {
this.toggleMenu(e);
}
};
setDesktopTitles = (titles) => {
titles?.forEach((title) => {
title.removeAttribute('role');
title.removeAttribute('tabindex');
title.removeAttribute('aria-expanded');
title.removeAttribute('aria-controls');
window.removeEventListener('keydown', this.toggleOnKey);
title.removeEventListener('click', this.toggleMenu);
});
};
setMobileTitles = (titles) => {
titles?.forEach((title) => {
title.setAttribute('role', 'button');
title.setAttribute('tabindex', 0);
title.setAttribute('aria-expanded', false);
title.setAttribute('aria-controls', `${title.id}-menu`);
title.addEventListener('click', this.toggleMenu);
title.addEventListener('focus', () => {
window.addEventListener('keydown', this.toggleOnKey);
});
title.addEventListener('blur', () => {
window.removeEventListener('keydown', this.toggleOnKey);
});
});
};
onMediaChange = (e) => {
const footerTitles = document.querySelectorAll('.footer-nav-item-title');
if (e.matches) {
this.setDesktopTitles(footerTitles);
} else {
this.setMobileTitles(footerTitles);
}
};
}
async function fetchFooter(url) {
const resp = await fetch(`${url}.plain.html`);
const respText = await resp.text();
if (!resp.ok) {
return { error: respText }; // can pass additional Response info if needed
}
return { html: respText };
}
export default async function init(block) {
const url = getMetadata('footer-source') || `${locale.contentRoot}/footer`;
if (url) {
const { html, error } = await fetchFooter(url);
if (error) {
console.log(`Could not create footer: ${error}`);
return;
}
if (html) {
try {
const parser = new DOMParser();
const footerDoc = parser.parseFromString(html, 'text/html');
const footer = new Footer(footerDoc.body, block);
footer.init();
} catch {
console.log('Could not create footer.');
}
}
}
}