-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathretriveprofile.js
575 lines (471 loc) · 20.9 KB
/
retriveprofile.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Firebase configuration
var firebaseConfig = {
//Add Config Files here
apiKey: "AIzaSyBSiO9d5tHuyyAeUCt37pxDWTT7jPSigaU",
authDomain: "awesome-github-profiles.firebaseapp.com",
databaseURL: "https://awesome-github-profiles-default-rtdb.firebaseio.com",
projectId: "awesome-github-profiles",
storageBucket: "awesome-github-profiles.appspot.com",
messagingSenderId: "490821849262",
appId: "1:490821849262:web:7e97984d98f578b81f9d3f",
measurementId: "G-WM33JZYEV0"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
let currentPage=1;
const profilesPerPage = 20; // Number of profiles per page
document.addEventListener("DOMContentLoaded", function () {
let contributors = [];
const noProfilesMessage = document.querySelector(".no-profiles-message");
noProfilesMessage.style.display="none"
function renderProfiles(filter = "", page=1, profilesPerPage=12) {
const container = document.querySelector(".profiles");
container.innerHTML = "";
const noProfilesMessage = document.querySelector(".no-profiles-message");
const filteredContributors = contributors.filter(contributor =>
contributor.login.toLowerCase().includes(filter.toLowerCase())
);
if (filteredContributors.length === 0) {
noProfilesMessage.style.display = "block";
console.log('sdsds')
return
}
// Calculate start and end index for pagination
const startIndex = (page - 1) * profilesPerPage;
const endIndex = startIndex + profilesPerPage;
// Slice the filtered contributors based on the current page
const paginatedContributors = filteredContributors.slice(startIndex, endIndex);
paginatedContributors.forEach((contributor,index) => {
noProfilesMessage.style.display = "none";
if (contributor.login.toLowerCase().includes(filter.toLowerCase())) {
const card = document.createElement("a");
card.href = `https://github.com/${contributor.login}`;
card.className = "profile";
card.target = "_blank";
const imgContainer = document.createElement("div");
imgContainer.className = "img-container";
const img = document.createElement("img");
const screenshotSrc = `https://raw.githubusercontent.com/recodehive/awesome-github-profiles/main/screenshots/${contributor.login}.png`;
img.src = screenshotSrc;
img.alt = `Avatar of ${contributor.login}`;
img.className = "profile-img";
img.onload = function () {
if (img.src === screenshotSrc) {
imgContainer.classList.add("scroll-on-hover");
imgContainer.dataset.imgHeight = img.naturalHeight;
}
};
img.onerror = function () {
img.src = contributor.avatar_url;
imgContainer.classList.remove("scroll-on-hover");
};
imgContainer.appendChild(img);
const name = document.createElement("p");
name.textContent = contributor.login;
const viewCount = document.createElement("p");
viewCount.className = "view-count";
viewCount.innerHTML = '<i class="fa fa-eye"></i> Views: Loading...'; // Placeholder text
// Retrieve and listen to view count from Firebase
const profileRef = firebase.database().ref(`profiles/${contributor.login}/views`);
const profileRefLikes = firebase.database().ref(`profiles/${contributor.login}/likes`);
profileRef.on("value", (snapshot) => {
if (snapshot.exists()) {
viewCount.innerHTML = `<i class="fa fa-eye"></i> Views: ${snapshot.val()}`;
} else {
// Handle new profile
profileRef.set(0);
viewCount.innerHTML = '<i class="fa fa-eye"></i> Views: 0';
}
});
let masterDiv=document.createElement('p')
masterDiv.classList='views-likes'
let div=document.createElement('p')
div.className="view"
div.id=contributor.login
profileRefLikes.on("value", (snapshot) => {
let isRed=false
let item=JSON.parse(localStorage.getItem('isLike'))
if(item&&item.includes(contributor.login)){
isRed=true
}
if (snapshot.exists()) {
div.innerHTML = `<i class="fa fa-heart ${isRed?"like-red":""}"></i> Likes: ${snapshot.val()}`;
} else {
// Handle new profile
profileRef.set(0);
div.innerHTML = '<i class="fa fa-heart"></i> Likes: 0';
}
});
// Increment view count on click
card.addEventListener("click", (e) => {
e.preventDefault();
const viewedProfiles = JSON.parse(localStorage.getItem('viewedProfiles')) || [];
if (!viewedProfiles.includes(contributor.login)) {
// Increment view count
profileRef.transaction((currentViews) => (currentViews || 0) + 1).then(() => {
// Mark the profile as viewed in localStorage
viewedProfiles.push(contributor.login);
localStorage.setItem('viewedProfiles', JSON.stringify(viewedProfiles));
// Open the profile in a new tab
window.open(card.href, "_blank");
});
} else {
// If the profile has been viewed, just open it in a new tab
window.open(card.href, "_blank");
}
});
card.appendChild(imgContainer);
card.appendChild(name);
masterDiv.appendChild(viewCount)
masterDiv.appendChild(div)
card.appendChild(masterDiv);
card.classList.add("profile-card");
container.appendChild(card);
div.addEventListener('click', (e) => {
e.preventDefault(); // Prevent the default action
e.stopPropagation(); // Prevent event bubbling
const targetId = div.id; // Use div.id for the profile ID
const heartIcon = div.querySelector(".fa-heart");
// Get current 'isLike' list from LocalStorage
let likedProfiles = JSON.parse(localStorage.getItem('isLike')) || [];
if (likedProfiles.includes(targetId)) {
// If already liked, unlike it (remove from localStorage and decrement count)
likedProfiles = likedProfiles.filter(id => id !== targetId);
localStorage.setItem('isLike', JSON.stringify(likedProfiles));
showToast("You UnLiked the Profile", "like");
// Change the heart icon color back and decrement the like count
heartIcon.classList.remove("like-red");
profileRefLikes.transaction((currentLikes) => (currentLikes || 1) - 1);
} else {
// If not liked yet, like it (add to localStorage and increment count)
likedProfiles.push(targetId);
localStorage.setItem('isLike', JSON.stringify(likedProfiles));
showToast("You Liked the Profile", "like");
// Change the heart icon color to red and increment the like count
heartIcon.classList.add("like-red");
profileRefLikes.transaction((currentLikes) => (currentLikes || 0) + 1);
}
// Call renderProfiles to update the UI
// renderProfiles();
});
}
});
// Update pagination controls
updatePaginationControls(page, profilesPerPage, filteredContributors.length);
}
function updatePaginationControls(currentPage, profilesPerPage, totalProfiles) {
const totalPages = Math.ceil(totalProfiles / profilesPerPage);
const pageInfo = document.getElementById("page-info");
pageInfo.textContent = `Page ${currentPage} of ${totalPages}`;
// Disable previous button if on the first page
document.getElementById("prev-page").disabled = currentPage === 1;
// Disable next button if on the last page
document.getElementById("next-page").disabled = currentPage === totalPages;
}
document.getElementById("prev-page").addEventListener("click", function () {
if (currentPage > 1) {
currentPage--;
renderProfiles("", currentPage, profilesPerPage);
}
});
document.getElementById("next-page").addEventListener("click", function () {
currentPage++;
renderProfiles("", currentPage, profilesPerPage);
});
// Fetch contributors data
fetch("https://raw.githubusercontent.com/recodehive/awesome-github-profiles/main/.all-contributorsrc")
.then((response) => response.json())
.then((data) => {
contributors = data.contributors;
renderProfiles("", 1, profilesPerPage); //Start on page one
});
// Add event listener to the search bar
const searchBar = document.querySelector(".search-input");
searchBar.addEventListener("input", () => {
renderProfiles(searchBar.value);
});
searchBar.addEventListener("click", () => {
renderProfiles(searchBar.value);
});
});
document.addEventListener("mouseover", function (e) {
if (e.target.tagName === "IMG" && e.target.closest(".scroll-on-hover")) {
const imgContainer = e.target.closest(".img-container");
const imgHeight = e.target.naturalHeight;
const containerHeight = imgContainer.clientHeight;
if (imgHeight > containerHeight) {
const translateValue = ((imgHeight - containerHeight - 1000) / imgHeight) * 100;
e.target.style.transform = `translateY(-${translateValue}%)`;
}
}
});
document.addEventListener("mouseout", function (e) {
if (e.target.tagName === "IMG" && e.target.closest(".scroll-on-hover")) {
e.target.style.transform = "translateY(0)";
}
});
function renderProfiles(filter = "") {
const container = document.querySelector(".profiles");
container.innerHTML = "";
const noProfilesMessage = document.querySelector(".no-profiles-message");
const filteredContributors = contributors.filter(contributor =>
contributor.login.toLowerCase().includes(filter.toLowerCase())
);
if (filteredContributors.length === 0) {
noProfilesMessage.style.display = "block";
console.log('sdsds')
return
}
contributors.forEach((contributor,index) => {
noProfilesMessage.style.display = "none";
if (contributor.login.toLowerCase().includes(filter.toLowerCase())) {
const card = document.createElement("a");
card.href = `https://github.com/${contributor.login}`;
card.className = "profile";
card.target = "_blank";
const imgContainer = document.createElement("div");
imgContainer.className = "img-container";
const img = document.createElement("img");
const screenshotSrc = `https://raw.githubusercontent.com/recodehive/awesome-github-profiles/main/screenshots/${contributor.login}.png`;
img.src = screenshotSrc;
img.alt = `Avatar of ${contributor.login}`;
img.className = "profile-img";
img.onload = function () {
if (img.src === screenshotSrc) {
imgContainer.classList.add("scroll-on-hover");
imgContainer.dataset.imgHeight = img.naturalHeight;
}
};
img.onerror = function () {
img.src = contributor.avatar_url;
imgContainer.classList.remove("scroll-on-hover");
};
imgContainer.appendChild(img);
const name = document.createElement("p");
name.textContent = contributor.login;
const viewCount = document.createElement("p");
viewCount.className = "view-count";
viewCount.innerHTML = '<i class="fa fa-eye"></i> Views: Loading...'; // Placeholder text
// Retrieve and listen to view count from Firebase
const profileRef = firebase.database().ref(`profiles/${contributor.login}/views`);
const profileRefLikes = firebase.database().ref(`profiles/${contributor.login}/likes`);
profileRef.on("value", (snapshot) => {
if (snapshot.exists()) {
viewCount.innerHTML = `<i class="fa fa-eye"></i> Views: ${snapshot.val()}`;
} else {
// Handle new profile
profileRef.set(0);
viewCount.innerHTML = '<i class="fa fa-eye"></i> Views: 0';
}
});
let masterDiv=document.createElement('p')
masterDiv.classList='views-likes'
let div=document.createElement('p')
div.className="view"
div.id=contributor.login
profileRefLikes.on("value", (snapshot) => {
let isRed=false
let item=JSON.parse(localStorage.getItem('isLike'))
if(item&&item.includes(contributor.login)){
isRed=true
}
if (snapshot.exists()) {
div.innerHTML = `<i class="fa fa-heart ${isRed?"like-red":""}"></i> Likes: ${snapshot.val()}`;
} else {
// Handle new profile
profileRef.set(0);
div.innerHTML = '<i class="fa fa-heart"></i> Likes: 0';
}
});
// Increment view count on click
card.addEventListener("click", (e) => {
e.preventDefault();
const viewedProfiles = JSON.parse(localStorage.getItem('viewedProfiles')) || [];
if (!viewedProfiles.includes(contributor.login)) {
// Increment view count
profileRef.transaction((currentViews) => (currentViews || 0) + 1).then(() => {
// Mark the profile as viewed in localStorage
viewedProfiles.push(contributor.login);
localStorage.setItem('viewedProfiles', JSON.stringify(viewedProfiles));
// Open the profile in a new tab
window.open(card.href, "_blank");
});
} else {
// If the profile has been viewed, just open it in a new tab
window.open(card.href, "_blank");
}
});
card.appendChild(imgContainer);
card.appendChild(name);
masterDiv.appendChild(viewCount)
masterDiv.appendChild(div)
card.appendChild(masterDiv);
card.classList.add("profile-card");
container.appendChild(card);
div.addEventListener('click', (e) => {
e.preventDefault(); // Prevent the default action
e.stopPropagation(); // Prevent event bubbling
const targetId = e.target.id;
// Get current 'isLike' list from LocalStorage
let item = JSON.parse(localStorage.getItem('isLike')) || [];
if (item.includes(targetId)) {
// If the target ID is already in the 'isLike' list, remove it
const idx = item.indexOf(targetId);
item.splice(idx, 1);
// Update LocalStorage
if (item.length === 0) {
localStorage.removeItem('isLike');
} else {
localStorage.setItem('isLike', JSON.stringify(item));
}
showToast("You UnLiked the Profile","like")
// Decrement the like count
profileRefLikes.transaction((currentViews) => (currentViews || 1) - 1);
} else {
// If the target ID is not in the 'isLike' list, add it
item.push(targetId);
localStorage.setItem('isLike', JSON.stringify(item));
showToast("You Liked the Profile","like")
// Increment the like count
profileRefLikes.transaction((currentViews) => (currentViews || 0) + 1);
}
// // Call renderProfiles to update the UI
// renderProfiles();
});
}
});
}
document.addEventListener('DOMContentLoaded', () => {
const modal = document.getElementById('myModal');
const caretDown = document.getElementById('caret-down');
const closeButton = document.getElementById('close-button');
const views = document.getElementsByClassName('views');
Array.from(views).map((ele)=>{
ele.addEventListener(('click'),(e)=>{
close()
if(e.target.innerHTML=="Least Views"){
fetch("https://raw.githubusercontent.com/recodehive/awesome-github-profiles/main/.all-contributorsrc")
.then((response) => response.json())
.then((data) => {
contributors = data.contributors;
contributors.map((data)=>{
const profileRef = firebase.database().ref(`profiles/${data.login}/views`)
profileRef.on("value", (snapshot) => {
if (snapshot.exists()) {
data['views']=snapshot.val()
} else {
// Handle new profile
profileRef.set(0);
}
});
})
contributors=contributors.sort((a,b)=>a.views-b.views)
renderProfiles();
});
}
else if(e.target.innerHTML=="Most Views"){
fetch("https://raw.githubusercontent.com/recodehive/awesome-github-profiles/main/.all-contributorsrc")
.then((response) => response.json())
.then((data) => {
contributors = data.contributors;
contributors.map((data)=>{
const profileRef = firebase.database().ref(`profiles/${data.login}/views`)
profileRef.on("value", (snapshot) => {
if (snapshot.exists()) {
data['views']=snapshot.val()
} else {
// Handle new profile
profileRef.set(0);
}
});
})
contributors=contributors.sort((a,b)=>b.views-a.views)
renderProfiles();
});
}else if(e.target.innerHTML=="Most Likes"){
console.log('sdsdsdsds')
fetch("https://raw.githubusercontent.com/recodehive/awesome-github-profiles/main/.all-contributorsrc")
.then((response) => response.json())
.then((data) => {
contributors = data.contributors;
contributors.map((data)=>{
const profileRef = firebase.database().ref(`profiles/${data.login}/likes`)
profileRef.on("value", (snapshot) => {
if (snapshot.exists()) {
data['likes']=snapshot.val()
} else {
// Handle new profile
profileRef.set(0);
}
});
})
contributors=contributors.sort((a,b)=>b.likes-a.likes)
renderProfiles();
});
}else{
fetch("https://raw.githubusercontent.com/recodehive/awesome-github-profiles/main/.all-contributorsrc")
.then((response) => response.json())
.then((data) => {
contributors = data.contributors;
contributors.map((data)=>{
const profileRef = firebase.database().ref(`profiles/${data.login}/likes`)
profileRef.on("value", (snapshot) => {
if (snapshot.exists()) {
data['likes']=snapshot.val()
} else {
// Handle new profile
profileRef.set(0);
}
});
})
contributors=contributors.sort((a,b)=>a.likes-b.likes)
renderProfiles();
});
}
})
})
caretDown.addEventListener('click', (event) => {
if(modal.style.display=="block"){
modal.style.display="none"
return
}
event.stopPropagation(); // Prevents event from bubbling up
const rect = caretDown.getBoundingClientRect();
modal.style.display = 'block';
modal.style.top = `${rect.bottom+30}px`; // Position modal just below the caret-down
modal.style.left = `${rect.left-170}px`; // Align modal with the caret-down
});
let close=() => {
modal.style.display = 'none';
};
// Close the modal if clicked outside of it
window.onclick = function (event) {
close()
};
});
// Function to create and show toast notifications
function showToast(message, type) {
const toastContainer = document.getElementById('toast-container');
// Create toast element
const toast = document.createElement('div');
toast.className = `toast ${type} show`;
toast.textContent = message;
// Add close button
const closeBtn = document.createElement('span');
let line=document.createElement('div')
closeBtn.className = 'close-btn';
closeBtn.textContent = '×';
closeBtn.onclick = () => {
toast.remove();
};
line.className="line"
toast.appendChild(closeBtn);
toastContainer.appendChild(line)
// Append toast to container
toastContainer.appendChild(toast);
// Remove toast after a delay
setTimeout(() => {
toast.classList.remove('show');
toastContainer.removeChild(line)
}, 3000);
}