Repository navigation
Expand file tree
/
Copy pathui.js
More file actions
223 lines (186 loc) · 7.64 KB
/
Copy pathui.js
File metadata and controls
223 lines (186 loc) · 7.64 KB
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
document.addEventListener("DOMContentLoaded", function() {
var container = document.getElementById("episodes-container");
if (!container) return;
var podcastTitle = "The Azure Security Podcast";
var episodesByNumber = {};
var fullEpisodesPromise = null;
function padEpisodeNumber(n) {
return n >= 100 ? String(n).padStart(4, '0') : String(n).padStart(3, '0');
}
function getEpisodeFile(ep) {
return ep.file || ('Episode' + padEpisodeNumber(ep.number) + '.json');
}
function getEpisodePage(ep) {
return 'episodes/' + getEpisodeFile(ep).replace(/\.json$/i, '.html');
}
function renderEpisode(ep) {
var n = ep.number;
var p = padEpisodeNumber(n);
var headingId = 'Ep' + p;
var anchorId = 'ep' + p;
var contentId = 'Content-' + headingId;
return '<article>'
+ '<div class="row add-bottom">'
+ '<div class="nine columns offset-2">'
+ '<h3 class="Heading" id="' + headingId + '">'
+ '<button type="button" class="EpisodeToggle" aria-expanded="false" aria-controls="' + contentId + '">'
+ ' <img id="ColExp-' + headingId + '" class="ColExpIcon" width="16" height="16" src="images/Expand.png" alt=""> '
+ '<span style="font-weight:bold">Episode ' + n + '</span> - ' + ep.date + ' - [<span id="' + anchorId + '">' + ep.title + '</span>] '
+ '</button>'
+ '<a href="' + getEpisodePage(ep) + '" title="Link to episode ' + n + '"><i class="fa-solid fa-link" style="font-size: 0.8em; color: #666;"></i></a>'
+ '</h3>'
+ '<div class="EpisodeContent" id="' + contentId + '" hidden></div>'
+ '</div></div></article>';
}
function scrollToHash() {
var hash = window.location.hash;
if (hash) {
var target = document.getElementById(hash.substring(1));
if (target) target.scrollIntoView();
}
}
function fetchJson(url) {
return fetch(url).then(function(r) {
if (!r.ok) throw new Error("HTTP " + r.status);
return r.json();
});
}
function loadFullEpisodes() {
if (!fullEpisodesPromise) {
fullEpisodesPromise = fetchJson("data/episodes.json").then(function(data) {
(data.episodes || []).forEach(function(ep) {
episodesByNumber[ep.number] = ep;
});
return data.episodes || [];
});
}
return fullEpisodesPromise;
}
function loadEpisode(ep) {
if (ep.contentHtml) return Promise.resolve(ep);
if (ep.loadPromise) return ep.loadPromise;
ep.loadPromise = fetchJson("data/" + getEpisodeFile(ep))
.then(function(fullEpisode) {
episodesByNumber[fullEpisode.number] = fullEpisode;
ep.contentHtml = fullEpisode.contentHtml;
return ep;
})
.catch(function(err) {
console.warn("Falling back to episodes.json for episode " + ep.number + ":", err);
return loadFullEpisodes().then(function() {
var fullEpisode = episodesByNumber[ep.number];
if (!fullEpisode || !fullEpisode.contentHtml) {
throw new Error("Episode " + ep.number + " content was not found.");
}
ep.contentHtml = fullEpisode.contentHtml;
return ep;
});
});
return ep.loadPromise;
}
function getEpisodeFromHeading(h3) {
var number = parseInt(h3.id.replace(/^Ep/, ''), 10);
return episodesByNumber[number];
}
function updatePageTitle() {
var title = podcastTitle;
var hash = window.location.hash;
if (hash) {
var target = document.getElementById(hash.substring(1));
var heading = target && target.closest("h3.Heading");
var ep = heading && getEpisodeFromHeading(heading);
if (ep) title += " - Episode " + ep.number + " - " + ep.title;
}
document.title = title;
var openGraphTitle = document.querySelector('meta[property="og:title"]');
var twitterTitle = document.querySelector('meta[name="twitter:title"]');
if (openGraphTitle) openGraphTitle.setAttribute("content", title);
if (twitterTitle) twitterTitle.setAttribute("content", title);
}
function openEpisode(h3) {
var content = document.getElementById("Content-" + h3.id);
var icon = document.getElementById("ColExp-" + h3.id);
var toggle = h3.querySelector("button.EpisodeToggle");
var ep = getEpisodeFromHeading(h3);
if (!content || !ep) return Promise.resolve();
if (toggle) toggle.setAttribute("aria-expanded", "true");
if (content.dataset.loaded === "true") {
content.hidden = false;
if (icon) icon.src = "images/Collapse.png";
return Promise.resolve();
}
content.innerHTML = '<p>Loading episode...</p>';
content.hidden = false;
if (icon) icon.src = "images/Collapse.png";
return loadEpisode(ep)
.then(function(fullEpisode) {
content.innerHTML = fullEpisode.contentHtml;
content.dataset.loaded = "true";
var iframe = content.querySelector('iframe');
if (iframe && h3 === container.querySelector('h3.Heading')) {
iframe.removeAttribute('loading');
}
})
.catch(function(err) {
console.error("Failed to load episode:", err);
content.innerHTML = '<p>Failed to load this episode. Please refresh.</p>';
});
}
function closeEpisode(h3) {
var content = document.getElementById("Content-" + h3.id);
var icon = document.getElementById("ColExp-" + h3.id);
var toggle = h3.querySelector("button.EpisodeToggle");
if (content) content.hidden = true;
if (icon) icon.src = "images/Expand.png";
if (toggle) toggle.setAttribute("aria-expanded", "false");
}
function getInitialHeading() {
var hash = window.location.hash;
if (hash) {
var target = document.getElementById(hash.substring(1));
if (target) {
var heading = target.closest("h3.Heading");
if (heading) return heading;
}
}
return container.querySelector("h3.Heading");
}
// Event delegation: single listener handles all expand/collapse clicks
container.addEventListener("click", function(e) {
var toggle = e.target.closest("button.EpisodeToggle");
if (!toggle) return;
var h3 = toggle.closest("h3.Heading");
var content = document.getElementById("Content-" + h3.id);
if (!content) return;
if (content.hidden) {
openEpisode(h3);
} else {
closeEpisode(h3);
}
});
fetchJson("data/episodes-index.json")
.catch(function(err) {
console.warn("Falling back to full episodes list:", err);
return loadFullEpisodes().then(function(episodes) {
return { episodes: episodes };
});
})
.then(function(data) {
var episodes = data.episodes || [];
if (episodes.length === 0) return;
episodes.forEach(function(ep) {
episodesByNumber[ep.number] = ep;
});
container.innerHTML = episodes.map(renderEpisode).join('\n');
updatePageTitle();
var initialHeading = getInitialHeading();
if (initialHeading) {
openEpisode(initialHeading).then(scrollToHash);
}
})
.catch(function(err) {
console.error("Failed to load episodes:", err);
container.innerHTML = '<div class="row add-bottom"><div class="nine columns offset-2"><p>Failed to load episodes. Please refresh.</p></div></div>';
});
window.addEventListener("hashchange", updatePageTitle);
});