forked from adobe/pages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
155 lines (127 loc) · 3.72 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
/**
* Loads a JS module.
* @param {string} src The path to the JS module
*/
document.title=document.title.split('<br>').join(' ');
function loadJSModule(src) {
const module = document.createElement('script');
module.setAttribute('type', 'module');
module.setAttribute('src', src);
document.head.appendChild(module);
};
/**
* Creates a tag with the given name and attributes.
* @param {string} name The tag name
* @param {object} attrs An object containing the attributes
* @returns The new tag
*/
function createTag(name, attrs) {
const el = document.createElement(name);
if (typeof attrs === 'object') {
for (let [key, value] of Object.entries(attrs)) {
el.setAttribute(key, value);
}
}
return el;
}
async function insertLocalResource(type) {
if (window.pages.product && window.pages.locale) {
const resp=await fetch(`/${type}s/${window.pages.product}/${window.pages.locale}/${window.pages.project}/${type}.plain.html`);
if (resp.status == 200) {
const html=await resp.text();
document.querySelector(type).innerHTML=html;
}
}
// temporary icon fix
fixIcons();
}
/* link out to external links */
// called inside decoratePage() twp3.js
function externalLinks(selector) {
const element = document.querySelector(selector);
const links = element.querySelectorAll('a[href]');
links.forEach((link_item) => {
const linkValue = link_item.getAttribute('href');
if(linkValue.includes('//') && !linkValue.includes('pages.adobe')) {
link_item.setAttribute('target', '_blank')
}
})
}
async function loadLocalHeader() {
await insertLocalResource('header');
}
async function loadLocalFooter() {
await insertLocalResource('footer');
}
/**
* Fixes helix icon functionality until
* https://github.com/adobe/helix-pipeline/issues/509
* is resolved.
*/
function fixIcons() {
document.querySelectorAll("use").forEach ((e) => {
var a=e.getAttribute("href");
if (a.startsWith('/icons/')) {
var name=a.split("/")[2].split(".")[0];
e.setAttribute("href", `/icons.svg#${name}`);
}
});
}
/**
* Checks if <main> is ready to appear
*/
function appearMain() {
if (window.pages.familyCssLoaded && window.pages.decorated) {
classify('main', 'appear');
}
}
/**
* 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);
link.onload = () => {
window.pages.familyCssLoaded=true;
appearMain();
}
link.onerror = () => {
window.pages.familyCssLoaded=true;
appearMain();
}
document.head.appendChild(link);
};
/**
* adds a class to an element.
* @param {string} qs querySelector string
* @param {string} cls css class to be added
* @param {number} parent uplevel
*/
function classify(qs, cls, parent) {
document.querySelectorAll(qs).forEach(($e) => {
for (let p=parent;p>0;p--) {
$e=$e.parentNode;
}
$e.classList.add(cls)
});
}
const pathSegments=window.location.pathname.match(/[\w-]+(?=\/)/g);
window.pages={};
if (pathSegments) {
const product=pathSegments[0];
const locale=pathSegments[1];
const project=pathSegments[2];
let family=project;
if (project.startsWith('twp') || project.startsWith('tl')) family=`twp3`;
window.pages = { product, locale, project, family };
}
// Load page specific code
if (window.pages.project) {
loadCSS(`/styles/${window.pages.product}/${window.pages.project}.css`);
loadJSModule(`/scripts/${window.pages.family}.js`);
} else {
loadCSS(`/styles/default.css`);
loadJSModule(`/scripts/default.js`);
}