forked from TobiasMue91/tobiasmue91.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.js
202 lines (165 loc) · 7.08 KB
/
timeline.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
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
let timelineStyles = '';
let timelineHtml = '';
let isLoading = false;
let lastRequestedHash = '';
let isCollapsed = true;
window.addEventListener('load', function (event) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "timeline.html", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var timeline = document.createElement("div");
timeline.innerHTML = xhr.responseText;
timelineStyles = timeline.querySelector('style').innerHTML;
document.body.appendChild(timeline);
init();
}
};
xhr.send();
});
const timelineDataUrl = "timeline_data.json"; // URL to the timeline JSON data
function loadIndexHtml(hash) {
if (isLoading || hash === lastRequestedHash) {
return;
}
lastRequestedHash = hash;
isLoading = true;
fetch(`https://raw.githubusercontent.com/TobiasMue91/gptgames/${hash}/index.html`)
.then((response) => response.text())
.then((html) => {
isLoading = false;
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
replaceLinks(doc, hash);
const thumbPosition = document.querySelector('.slider-thumb').style.left;
// Replace the head and body elements
document.head.innerHTML = doc.head.innerHTML;
document.body.innerHTML = doc.body.innerHTML;
// Preserve the timeline container
document.head.querySelector('style').innerHTML += timelineStyles;
document.body.innerHTML += timelineHtml;
document.querySelector('.slider-thumb').style.left = thumbPosition;
addSliderStepEventListeners();
init(false);
});
}
function replaceLinks(doc, hash) {
const anchors = doc.querySelectorAll("a");
anchors.forEach((anchor) => {
const href = anchor.getAttribute("href");
if (href.startsWith("http") && !href.startsWith("https://raw.githubusercontent.com/")) {
return;
}
const link = new URL(href, `https://github.com/TobiasMue91/gptgames/blob/${hash}/`);
anchor.setAttribute("href", link.href);
});
}
function onSliderStepClick(e) {
const hash = e.target.dataset.hash;
const sliderThumb = document.querySelector(".slider-thumb");
const clickedStep = e.target;
sliderThumb.style.left = (parseInt(clickedStep.style.left) - 2) + 'px';
loadIndexHtml(hash);
}
function addSliderStepEventListeners() {
const steps = document.querySelectorAll(".slider-step");
steps.forEach((step) => step.addEventListener("click", onSliderStepClick));
}
function loadTimelineData() {
fetch(timelineDataUrl)
.then((response) => response.json())
.then((data) => {
const slider = document.getElementById("slider");
const sliderThumb = slider.querySelector(".slider-thumb");
data.forEach((item, index) => {
const step = document.createElement("div");
step.className = "slider-step";
step.style.left = index * 40 + "px"; // Space the steps by 40 pixels
step.dataset.hash = item.hash;
step.dataset.timestamp = item.timestamp;
step.dataset.date = new Date(item.timestamp).toLocaleDateString();
if (item.GptVersion) {
step.dataset.gptVersion = item.GptVersion;
step.title = "GPT Version: " + item.GptVersion;
}
slider.appendChild(step);
});
// Add event listeners to slider steps
addSliderStepEventListeners();
// Set the slider max attribute to the number of steps - 1
slider.setAttribute("max", data.length - 1);
// Set the slider track width to the offsetLeft of the last item
const lastElement = slider.querySelector(".slider-step:last-child");
slider.querySelector('.slider-track').style.width = lastElement.offsetLeft + 'px';
timelineHtml = document.querySelector('#timeline-container').outerHTML;
});
}
function onSliderMouseDown(e) {
e.preventDefault();
document.addEventListener("mousemove", onSliderMouseMove);
document.addEventListener("mouseup", onSliderMouseUp);
}
function onSliderMouseMove(e) {
const slider = document.getElementById("slider");
const sliderThumb = slider.querySelector(".slider-thumb");
const sliderRect = slider.getBoundingClientRect();
const lastElement = slider.querySelector(".slider-step:last-child");
const leftBoundary = sliderRect.left;
const rightBoundary = lastElement.offsetLeft + sliderThumb.offsetWidth;
const sliderThumbPosition = e.clientX - sliderThumb.offsetWidth / 2;
if (sliderThumbPosition >= leftBoundary && sliderThumbPosition <= rightBoundary) {
sliderThumb.style.left = sliderThumbPosition - leftBoundary + "px";
}
const nearestStep = getNearestStep();
loadIndexHtml(nearestStep.dataset.hash);
}
function onSliderMouseUp(e) {
document.removeEventListener("mousemove", onSliderMouseMove);
document.removeEventListener("mouseup", onSliderMouseUp);
const sliderThumb = document.querySelector(".slider-thumb");
const nearestStep = getNearestStep();
sliderThumb.style.left = (parseInt(nearestStep.style.left) - 2) + 'px';
loadIndexHtml(nearestStep.dataset.hash);
}
function getNearestStep() {
const slider = document.getElementById("slider");
const sliderThumb = slider.querySelector(".slider-thumb");
const sliderThumbPosition = parseInt(sliderThumb.style.left, 10);
const steps = Array.from(document.querySelectorAll(".slider-step"));
let nearestStep = steps[0];
let minDistance = Math.abs(sliderThumbPosition - parseInt(nearestStep.style.left, 10));
steps.forEach((step) => {
const stepPosition = parseInt(step.style.left, 10);
const distance = Math.abs(sliderThumbPosition - stepPosition);
if (distance < minDistance) {
minDistance = distance;
nearestStep = step;
}
});
return nearestStep;
}
function toggleTimeline(event) {
const container = document.querySelector('#timeline-container');
const toggle = document.querySelector('#timeline-toggle');
if (container.classList.contains('collapsed')) {
container.classList.remove('collapsed');
toggle.textContent = '∨';
isCollapsed = false;
} else {
container.classList.add('collapsed');
toggle.textContent = '∧';
isCollapsed = true;
}
}
function init(loadData = true) {
if (loadData) {
loadTimelineData();
}
const sliderThumb = document.getElementById("slider").querySelector(".slider-thumb");
sliderThumb.addEventListener("mousedown", onSliderMouseDown);
document.querySelector('#timeline-toggle').addEventListener('click', toggleTimeline);
if (!isCollapsed) {
document.querySelector('#timeline-container').classList.remove('collapsed');
document.querySelector('#timeline-toggle').textContent = '∨'
}
}