-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
197 lines (154 loc) · 4.89 KB
/
script.js
File metadata and controls
197 lines (154 loc) · 4.89 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
document.addEventListener("DOMContentLoaded", function () {
// import projects from txt or csv
let projects = JSON.parse(localStorage.getItem("projects")) || [];
// save project
function saveProjects() {
localStorage.setItem("projects", JSON.stringify(projects));
}
// add project
function addProject() {
const input = document.getElementById("projectInput");
const name = input.value.trim();
if (name === "") return;
if (!projects.some(p => p.toLowerCase() === name.toLowerCase())) {
projects.push(name);
saveProjects();
renderProjects();
}
input.value = "";
input.focus();
}
// delete project
window.deleteProject = function(index) {
projects.splice(index, 1);
saveProjects();
renderProjects();
};
// delete entire list
window.clearAll = function() {
const confirmDelete = confirm("Do you really want to clear the list?");
if (!confirmDelete) return;
projects = [];
localStorage.removeItem("projects");
renderProjects();
document.getElementById("result").innerHTML = "";
};
// show list
function renderProjects() {
const list = document.getElementById("projectList");
list.innerHTML = "";
projects.forEach((project, index) => {
const li = document.createElement("li");
li.innerHTML = `
${project}
<button onclick="deleteProject(${index})">❌</button>
`;
list.appendChild(li);
});
}
// randomiser
window.roll = function() {
if (projects.length === 0) {
alert("No projects added yet!");
return;
}
const resultDiv = document.getElementById("result");
const button = document.getElementById("randomBtn");
button.classList.add("rolling");
const interval = setInterval(() => {
const randomIndex = Math.floor(Math.random() * projects.length);
resultDiv.textContent = projects[randomIndex];
}, 80);
setTimeout(() => {
clearInterval(interval);
button.classList.remove("rolling");
const finalIndex = Math.floor(Math.random() * projects.length);
const exclamations = [
"Oh look:",
"Bäm:",
"Take that:",
"Well:",
"Guess what:",
"Plot twist:",
"Lucky you:",
"Here we go:",
"Ta-da:",
"Et voilà:"
];
const randomExclamation =
exclamations[Math.floor(Math.random() * exclamations.length)];
// Reset glow animation only
resultDiv.classList.remove("winner-glow");
void resultDiv.offsetWidth;
// Only project name orange
resultDiv.innerHTML =
randomExclamation +
" <span class='result-highlight'>" +
projects[finalIndex] +
"</span>";
resultDiv.classList.add("winner-glow");
}, 2000);
};
const reloadBtn = document.getElementById("reloadBtn");
if (reloadBtn) {
reloadBtn.addEventListener("click", function() {
const url = window.location.pathname + "?v=" + Date.now();
window.location.href = url;
});
}
// import data
document.getElementById("fileInput").addEventListener("change", function(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
const content = e.target.result;
const entries = content.split(/[\r\n,;\t]+/);
let added = 0;
entries.forEach(entry => {
let trimmed = entry.trim();
trimmed = trimmed.replace(/^"(.*)"$/, '$1');
if (
trimmed !== "" &&
!projects.some(p => p.toLowerCase() === trimmed.toLowerCase())
) {
projects.push(trimmed);
added++;
}
});
saveProjects();
renderProjects();
alert(`Import finished: ${added} new projects added.`);
};
reader.readAsText(file);
});
// activate add on enter
document.getElementById("projectForm").addEventListener("submit", function(event) {
event.preventDefault();
addProject();
});
// help modal
const modal = document.getElementById("helpModal");
const helpBtn = document.getElementById("helpBtn");
const closeBtn = document.querySelector(".close-btn");
if (helpBtn && modal && closeBtn) {
helpBtn.addEventListener("click", function() {
modal.style.display = "block";
});
closeBtn.addEventListener("click", function() {
modal.style.display = "none";
});
window.addEventListener("click", function(event) {
if (event.target === modal) {
modal.style.display = "none";
}
});
document.addEventListener("keydown", function(event) {
if (event.key === "Escape") {
modal.style.display = "none";
}
});
}
// initial render
renderProjects();
});