-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
246 lines (210 loc) · 9.51 KB
/
script.js
File metadata and controls
246 lines (210 loc) · 9.51 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Ana sayfaya yönlendirme fonksiyonu
function goHome() {
window.location.href = "index.html";
}
// ATab functions tailwind
function showTab(tabId) {
document.querySelectorAll('.tab-content').forEach(el => el.classList.add('hidden'));
document.getElementById(tabId).classList.remove('hidden');
}
// Önceden üretilen isimleri saklamak için değişken
let previousNames = new Set();
const netlifyFontsApiUrl = "/.netlify/functions/get-fonts"; // Netlify Functions API
// Etiketleri saklamak için değişken
let tags = [];
// Rastgele renk paleti
const colorPalette = [
"#FFB6C1", "#FFDAB9", "#E6E6FA", "#FFFACD", "#D8BFD8", "#D3D3D3", "#FFC0CB", "#ADD8E6", "#F08080", "#FAFAD2",
"#D4AF37", "#B5A642", "#C0C0C0", "#A9A9A9", "#708090", "#778899", "#B0C4DE", "#4682B4",
"#5F9EA0", "#7B68EE", "#6A5ACD", "#4169E1", "#1E90FF", "#6495ED", "#2E8B57", "#228B22",
"#8FBC8F", "#66CDAA", "#20B2AA", "#008080", "#556B2F", "#6B8E23", "#BDB76B", "#DAA520",
"#CD853F", "#8B4513", "#A0522D", "#D2691E", "#BC8F8F", "#F4A460", "#C3B091", "#D2B48C",
"#DEB887", "#A52A2A", "#8B0000", "#800000", "#B22222", "#DC143C", "#E9967A", "#FA8072",
"#FF8C00", "#FF7F50", "#FFA07A", "#F08080", "#D3D3D3", "#C0C0C0", "#A9A9A9", "#696969",
"#808080", "#333333"
];
// Rastgele renk seçme fonksiyonu
function getRandomColor() {
return colorPalette[Math.floor(Math.random() * colorPalette.length)];
}
// Kontrast rengi belirleme fonksiyonu
function getContrastColor(bgColor) {
const color = bgColor.charAt(0) === '#' ? bgColor.substring(1, 7) : bgColor;
const r = parseInt(color.substring(0, 2), 16);
const g = parseInt(color.substring(2, 4), 16);
const b = parseInt(color.substring(4, 6), 16);
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
return brightness > 155 ? 'black' : 'white';
}
// Netlify Functions üzerinden rastgele font çekme
async function getRandomFont() {
try {
const response = await fetch(netlifyFontsApiUrl);
const data = await response.json();
if (data.fonts && data.fonts.length > 0) {
return data.fonts[Math.floor(Math.random() * data.fonts.length)];
}
} catch (error) {
console.error("Netlify Fonts API request failed:", error);
}
return "Arial"; // Hata olursa varsayılan font
}
// Etiket ekleme fonksiyonu
function handleKeyDown(event) {
const input = event.target;
const tagContainer = document.getElementById("tag-container");
const errorMessage = document.getElementById("error-message");
if (event.key === "Enter" && input.value.trim() !== "") {
event.preventDefault();
if (tags.length >= 5) {
errorMessage.classList.remove("hidden");
return;
}
tags.push(input.value.trim());
input.value = "";
errorMessage.classList.add("hidden");
updateTags(tagContainer);
}
}
// Etiketleri güncelleme fonksiyonu
function updateTags(container) {
container.innerHTML = "";
tags.forEach((tag, index) => {
const tagElement = document.createElement("div");
tagElement.className = "tag bg-blue-500 text-white rounded-full px-3 py-1 flex items-center";
tagElement.innerHTML = `${tag} <button class="ml-2" onclick="removeTag(${index})">X</button>`;
container.appendChild(tagElement);
});
const input = document.createElement("input");
input.type = "text";
input.id = "keywords-input";
input.placeholder = "Enter keywords...";
input.className = "flex-1 bg-transparent text-gray-700 text-lg border-none focus:outline-none px-4";
input.onkeydown = handleKeyDown;
container.appendChild(input);
}
// Etiket kaldırma fonksiyonu
function removeTag(index) {
tags.splice(index, 1);
updateTags(document.getElementById("tag-container"));
}
// API'den isim üretme ve sonuçları ekrana yerleştirme (Benzersiz isimler + Dinamik Font + Rastgele Renk)
async function generateNames() {
const keywords = JSON.parse(sessionStorage.getItem("keywords")) || null;
const selectedCategory = sessionStorage.getItem("category") || null; // Hata burada düzeltildi
const resultsContainer = document.getElementById("results-container");
// 🔄 Loading Animasyonu Ekle (Tam Ortada)
const loadingDiv = document.createElement("div");
loadingDiv.className = "loading-container";
loadingDiv.innerHTML = `<div class="spinner"></div>`;
document.body.appendChild(loadingDiv);
setTimeout(async () => {
try {
let uniqueNames = new Set();
let attempts = 0;
const maxAttempts = 5;
const requestBody = keywords ? { keywords } : { category: selectedCategory };
while (uniqueNames.size < 4 && attempts < maxAttempts) {
const response = await fetch("/.netlify/functions/generate-name", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(requestBody)
});
const data = await response.json();
if (data.names && data.names.length > 0) {
data.names.forEach(name => {
if (!previousNames.has(name) && uniqueNames.size < 4) {
uniqueNames.add(name);
previousNames.add(name);
}
});
}
attempts++;
}
// 🔄 Loading ekranını güvenli şekilde gizle
if (loadingDiv) {
loadingDiv.classList.add('hidden');
}
// 🟢 Ana içeriği güvenli şekilde göster
const mainContent = document.getElementById('main-content');
if (mainContent) {
mainContent.classList.remove('hidden');
}
document.body.removeChild(loadingDiv);
if (uniqueNames.size > 0) {
resultsContainer.innerHTML = "";
[...uniqueNames].forEach(async (name, index) => {
const card = document.createElement("div");
const randomFont = await getRandomFont();
const randomColor = getRandomColor();
const contrastColor = getContrastColor(randomColor);
const link = document.createElement("link");
link.href = `https://fonts.googleapis.com/css2?family=${randomFont.replace(/ /g, '+')}&display=swap`;
link.rel = "stylesheet";
document.head.appendChild(link);
card.style.fontFamily = `"${randomFont}", sans-serif`;
card.style.backgroundColor = randomColor;
card.style.color = contrastColor;
card.className = "card cursor-pointer transition duration-300 hover:shadow-lg";
card.innerText = name;
resultsContainer.appendChild(card);
card.addEventListener("click", function () {
const selectedName = this.innerText.trim();
const selectedFont = randomFont; // Font bilgisini de al
const selectedBgColor = randomColor; // Background rengini al
window.location.href = `/customize?name=${encodeURIComponent(selectedName)}&font=${encodeURIComponent(selectedFont)}&bgColor=${encodeURIComponent(selectedBgColor)}`;
});
setTimeout(() => {
card.classList.add("show");
}, 500 + index * 500);
});
} else {
resultsContainer.innerHTML = "<p class='text-red-500'>No unique names available. Try again.</p>";
}
} catch (error) {
console.error("API request error:", error);
document.body.removeChild(loadingDiv);
}
}, 8000);
}
// Kategori seçimi için fonksiyon
function selectCategory(category) {
sessionStorage.setItem("category", category);
sessionStorage.removeItem("keywords");
window.location.href = "results.html";
}
// Sonuç sayfasına yönlendirme
function redirectToResults() {
const selectedCategory = document.getElementById("category-select").value;
if (tags.length >= 3 && tags.length <= 5) {
sessionStorage.setItem("keywords", JSON.stringify(tags));
sessionStorage.removeItem("category");
} else if (selectedCategory) {
sessionStorage.setItem("category", selectedCategory);
sessionStorage.removeItem("keywords");
} else {
document.getElementById("error-message").classList.remove("hidden");
return;
}
window.location.href = "results.html";
}
// Sayfa yüklendiğinde sonuçları üret
if (window.location.pathname.includes("results.html")) {
window.onload = generateNames;
}
// "Generate New" butonuna tıklama olayını dinle
document.addEventListener("DOMContentLoaded", function () {
const generateNewButton = document.getElementById("generate-new");
if (generateNewButton) {
generateNewButton.addEventListener("click", generateNames);
}
});
// Header ve Footer'ı yükleme fonksiyonu
document.addEventListener("DOMContentLoaded", function () {
fetch("header.html")
.then(response => response.text())
.then(data => document.getElementById("header-placeholder").innerHTML = data);
fetch("footer.html")
.then(response => response.text())
.then(data => document.getElementById("footer-placeholder").innerHTML = data);
});