forked from adobe/theblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
156 lines (145 loc) · 4.46 KB
/
scripts.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
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/**
* Loads a JS module.
* @param {string} src The path to the JS module
*/
function loadJSModule(src) {
const module = document.createElement('script');
module.setAttribute('type', 'module');
module.setAttribute('src', src);
document.head.appendChild(module);
};
/**
* Loads a CSS file.
* @param {string} href The path to the CSS file
*/
function loadCSS(href) {
const link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('href', href);
document.head.appendChild(link);
};
/**
* Return the correct CMP integration ID based on the domain name
*/
function getOtDomainId() {
const domains = {
'adobe.com': '7a5eb705-95ed-4cc4-a11d-0cc5760e93db',
'hlx.page': '3a6a37fe-9e07-4aa9-8640-8f358a623271',
'project-helix.page': '45a95a10-dff7-4048-a2f3-a235b5ec0492',
'helix-demo.xyz': 'ff276bfd-1218-4a19-88d4-392a537b6ce3',
};
const currentDomain = Object.keys(domains).find(domain => window.location.host.indexOf(domain) > -1);
//TODO: remove `-test` once integration is ready for production
return `${domains[currentDomain] || domains[Object.keys(domains)[0]]}-test`;
};
// Prep images for lazy loading and use adequate sizes
const imgWidth = window.innerWidth === 375 ? 750 : // double size for retina
window.innerWidth < 900 ? 900 : 2048;
let imgCount = 0;
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
// only handle images with src=/hlx_*
if (node.tagName === 'IMG' && /\/hlx_/.test(node.src)) {
const img = node;
if (!/\?width\=\d/.test(img.src)) {
// add reasonable width if missing
img.setAttribute('src', `${img.src}?width=${imgWidth}&auto=webp`);
}
if (imgCount > 0) {
// skip lazyloading for hero image
img.setAttribute('data-src', img.src);
img.removeAttribute('src');
img.classList.add('lazyload');
}
imgCount++;
}
});
});
});
observer.observe(document, { childList: true, subtree: true });
// Blog config
window.blog = function() {
const TYPE = {
HOME: 'home',
POST: 'post',
AUTHOR: 'author',
TOPIC: 'topic',
PRODUCT: 'product',
};
const LANG = {
EN: 'en',
DE: 'de',
FR: 'fr',
JP: 'jp',
PT: 'pt',
KO: 'ko',
ES: 'es',
};
const context = '/';
let language = LANG.EN;
let pageType = TYPE.HOME;
const segs = window.location.pathname
.split('/')
.filter(seg => seg !== '');
if (segs.length > 0) {
if (segs.length >= 1) {
// language
for (let [key, value] of Object.entries(LANG)) {
if (value === segs[0]) {
language = value;
break;
}
}
}
if (segs.length >= 2) {
// post pages
if (segs[1] === 'drafts' || segs[1] === 'publish' || segs[1] === 'documentation' || /\d{4}\/\d{2}\/\d{2}/.test(segs.join('/'))) {
pageType = TYPE.POST;
} else {
for (let [key, value] of Object.entries(TYPE)) {
if (segs[1].startsWith(value)) {
pageType = value;
break;
}
}
}
}
}
return { context, language, pageType, TYPE, LANG };
}();
// Adobe config
window.fedsConfig = {
locale: window.blog.language,
content: {
experience: 'blog-gnav'
},
disableSticky: false,
privacy: {
otDomainId: getOtDomainId(),
footerLinkSelector: '[data-feds-action="open-adchoices-modal"]',
},
};
window.adobeid = {
client_id: 'theblog-helix',
scope: 'AdobeID,openid',
locale: window.blog.language,
};
// Load page specific code
loadCSS(`/style/${window.blog.pageType}.css`);
loadJSModule(`/scripts/${window.blog.pageType}.js`);
// Load language specific CSS overlays
if (window.blog.language !== window.blog.LANG.EN) { // skip for en
loadCSS(`/dict.${window.blog.language}.css`);
}