-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathshared.js
139 lines (122 loc) · 4.98 KB
/
shared.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
function getRelativePathToIndex() {
const path = window.location.pathname;
const pathSegments = path.split('/').filter(segment => segment !== '');
let relativePath = '';
if (pathSegments.length <= 1) {
// At root directory or /index.html
relativePath = 'index.html';
} else if (pathSegments.length >= 2) {
// In a subdirectory
relativePath = '../'.repeat(pathSegments.length - 1) + 'index.html';
}
return relativePath;
}
function insertLogo() {
const path = window.location.pathname;
const isHomePage = path === '/' || path === '/index.html';
let logoHTML = `
<div class="logo-line">
<span class="edge-logo-symbol">+</span>
<span class="horizontal-logo-symbol">~</span>
<span class="horizontal-logo-symbol"> </span>
<span class="horizontal-logo-symbol">-</span>
<span class="horizontal-logo-symbol">+</span>
<span class="horizontal-logo-symbol">-</span>
<span class="horizontal-logo-symbol">+</span>
<span class="horizontal-logo-symbol"> </span>
<span class="horizontal-logo-symbol">-</span>
<span class="horizontal-logo-symbol">+</span>
<span class="horizontal-logo-symbol">-</span>
<span class="horizontal-logo-symbol">~</span>
<span class="horizontal-logo-symbol">~</span>
<span class="edge-logo-symbol">*</span>
</div>
<div class="logo-line">
<span class="vertical-logo-symbol">|</span>
<span class="logo-text"> commonware </span>
<span class="vertical-logo-symbol"> </span>
</div>
<div class="logo-line">
<span class="edge-logo-symbol">*</span>
<span class="horizontal-logo-symbol">~</span>
<span class="horizontal-logo-symbol">+</span>
<span class="horizontal-logo-symbol">+</span>
<span class="horizontal-logo-symbol">-</span>
<span class="horizontal-logo-symbol"> </span>
<span class="horizontal-logo-symbol">~</span>
<span class="horizontal-logo-symbol">-</span>
<span class="horizontal-logo-symbol">+</span>
<span class="horizontal-logo-symbol"> </span>
<span class="horizontal-logo-symbol">-</span>
<span class="horizontal-logo-symbol">*</span>
<span class="horizontal-logo-symbol">-</span>
<span class="edge-logo-symbol">+</span>
</div>
`;
if (!isHomePage) {
// Wrap the logo in an anchor tag linking back to the homepage
const hrefToIndex = getRelativePathToIndex();
logoHTML = `
<a href="${hrefToIndex}" class="logo-link">
${logoHTML}
</a>
`;
}
document.getElementById('logo-placeholder').innerHTML = logoHTML;
initializeLogoAnimations();
}
function initializeLogoAnimations() {
const horizontalSymbols = [" ", "*", "+", "-", "~"];
const verticalSymbols = [" ", "*", "+", "|"];
const edgeSymbols = [" ", "*", "+"];
function getRandomItem(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function getRandomDuration(min) {
return Math.random() * (10000 - min) + min;
}
function updateSymbol(symbol, choices) {
symbol.innerText = getRandomItem(choices);
setTimeout(() => updateSymbol(symbol, choices), getRandomDuration(500));
}
document.querySelectorAll('.horizontal-logo-symbol').forEach(symbol => {
setTimeout(() => updateSymbol(symbol, horizontalSymbols), getRandomDuration(1500));
});
document.querySelectorAll('.vertical-logo-symbol').forEach(symbol => {
setTimeout(() => updateSymbol(symbol, verticalSymbols), getRandomDuration(1500));
});
document.querySelectorAll('.edge-logo-symbol').forEach(symbol => {
setTimeout(() => updateSymbol(symbol, edgeSymbols), getRandomDuration(1500));
});
}
function setExternalLinksToOpenInNewTab() {
for (const link of document.querySelectorAll('a')) {
const href = link.getAttribute('href');
if (href && href.startsWith('http')) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
}
}
}
function insertFooter() {
const currentYear = new Date().getFullYear();
const footerHTML = `
<div class="footer">
<div class="socials">
<a href="/benchmarks.html">Benchmarks</a>
<a href="https://github.com/commonwarexyz/monorepo">GitHub</a>
<a href="https://github.com/commonwarexyz/monorepo/discussions">Discussions</a>
<a href="https://x.com/commonwarexyz">X</a>
<a href="https://podcasts.apple.com/us/podcast/how-things-work/id1794554748">Podcast</a>
</div>
© ${currentYear} Commonware, Inc. All rights reserved.
</div>
`
document.getElementById('footer-placeholder').innerHTML = footerHTML;
}
// Load the logo when the DOM content is loaded
document.addEventListener('DOMContentLoaded', () => {
insertLogo();
insertFooter();
setExternalLinksToOpenInNewTab();
});