generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlanding-page.js
111 lines (102 loc) · 3.99 KB
/
landing-page.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
/* eslint-disable linebreak-style */
import { div, h1, p } from '../../scripts/dom-helpers.js';
import { getMetadata, createOptimizedPicture } from '../../scripts/lib-franklin.js';
import { getCookie, isAuthorizedUser, loadScript } from '../../scripts/scripts.js';
import ffetch from '../../scripts/ffetch.js';
export async function iframeResizeHandler(id) {
await new Promise((resolve) => {
loadScript('/scripts/iframeResizer.min.js', () => { resolve(); }, '', true);
});
/* global iFrameResize */
setTimeout(() => {
iFrameResize({
log: false,
}, `#${id}`);
}, 1000);
}
function handleEmbed() {
try {
const cmpCookieValue = getCookie('cmp');
if (cmpCookieValue) {
document.querySelectorAll('.embed a').forEach((link) => {
const href = link.getAttribute('href');
const url = new URL(href);
if (url.searchParams.get('cmp')) {
url.searchParams.set('cmp', cmpCookieValue);
link.setAttribute('href', url.toString());
}
});
}
} catch (err) {
// eslint-disable-next-line no-console
console.error(`Failed to change the campaing ID: ${err.message}`);
}
const observer = new MutationObserver((mutations) => {
const embed = document.querySelector('main .embed.block.embed-is-loaded');
if (embed) {
iframeResizeHandler('iframeContent');
// adjust parent div's height dynamically
mutations.forEach((record) => {
const grandGrandParent = record.target.parentElement.parentElement.parentElement;
if (record.target.tagName === 'IFRAME'
&& grandGrandParent.classList.contains('embed')
) {
// iframeResizeHandler(iframeURL, iframeID, root);
const { height } = record.target.style;
if (height) {
const parent = record.target.parentElement;
parent.style.height = height;
}
}
});
}
});
observer.observe(document.querySelector('main'), {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['style'],
});
}
export default async function buildAutoBlocks() {
if (isAuthorizedUser()) {
const path = window.location.pathname;
const pageIndex = await ffetch('/query-index.json').sheet('gated-resources').all();
const foundPage = pageIndex.find((page) => page.gatedURL === path || page.gatedURL.endsWith(`moleculardevices.com${path}`));
if (foundPage) {
window.location.replace(foundPage.path);
}
}
const pageParam = (new URLSearchParams(window.location.search)).get('page');
if (pageParam && pageParam === 'thankyou') {
document.body.classList.add('thankyou');
const isThankyouBanner = document.querySelector('.hero.thankyou-banner');
if (!isThankyouBanner) {
const thankyouHeading = getMetadata('thankyou-heading') || 'Thank you.';
const thankyouSubHeading = getMetadata('thankyou-sub-heading') || `Your ${getMetadata('download-title') || 'document'} is on its way.`;
document.querySelector('.hero > div:nth-of-type(2)').replaceWith(div(
div(h1(thankyouHeading), p(thankyouSubHeading)),
div(createOptimizedPicture('/images/thank-you-spectra.png', 'Thank you Spectra', false, [{ width: '750' }])),
));
} else {
document.querySelector('.thankyou-banner > div:nth-of-type(2)').replaceWith(div(
div(h1('Thank you for your inquiry'), p('An expert from our team will be in touch shortly!')),
div(createOptimizedPicture('/images/thank-you-spectra.png', 'Thank you Spectra', false, [{ width: '750' }])),
));
}
}
handleEmbed();
setTimeout(() => {
const pdfAnchors = document.querySelectorAll('a[href$=".pdf"]');
const thankyouUrl = `${window.location.pathname}?page=thankyou`;
pdfAnchors.forEach((anchor) => {
const href = new URL(anchor.href).pathname;
anchor.setAttribute('download', href);
anchor.addEventListener('click', () => {
setTimeout(() => {
window.location.href = thankyouUrl;
}, 1000);
});
});
}, 800);
}