-
Notifications
You must be signed in to change notification settings - Fork 0
/
assn1.js
158 lines (130 loc) · 3.86 KB
/
assn1.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
function toggleMode()
{
let body = document.body;
let nav = document.querySelector('nav');
if (body.classList.contains('dark-mode'))
{
body.classList.remove('dark-mode');
nav.setAttribute('data-mode', 'light');
}
else
{
body.classList.add('dark-mode');
nav.setAttribute('data-mode', 'dark');
}
}
function updateDynamicRole()
{
const roles = ['an Enthusiast', 'a Coder', 'a Problem Solver', 'a Learner'];
const dynamicRoleElement = document.getElementById('dynamic-role');
let currentIndex = 0;
setInterval(() => {
dynamicRoleElement.textContent = roles[currentIndex];
currentIndex = (currentIndex + 1) % roles.length;
}, 3000);
}
document.addEventListener('DOMContentLoaded', function ()
{
loadLikesAndComments('blog1');
loadLikesAndComments('blog2');
updateDynamicRole();
});
// function likePost(blogId)
// {
// let likes = getLikes(blogId) + 1;
// setLikes(blogId, likes);
// updateLikesDisplay(blogId);
// }
function likePost(blogId)
{
let likes = parseInt(localStorage.getItem(`${blogId}_likes`), 10) || 0;
likes++;
localStorage.setItem(`${blogId}_likes`, likes.toString());
updateLikesDisplay(blogId);
}
// function addComment(blogId)
// {
// let commentInput = document.getElementById(`commentInput-${blogId}`);
// let comment = commentInput.value.trim();
// if (comment !== '')
// {
// let comments = getComments(blogId);
// comments.push(comment);
// setComments(blogId, comments);
// updateCommentsDisplay(blogId);
// commentInput.value = '';
// }
// }
function addComment(blogId)
{
let commentInput = document.getElementById(`commentInput-${blogId}`);
let comment = commentInput.value.trim();
if (comment !== '')
{
let comments = localStorage.getItem(`${blogId}_comments`) || '';
comments += comment + '\n';
localStorage.setItem(`${blogId}_comments`, comments);
updateCommentsDisplay(blogId);
commentInput.value = '';
}
}
function loadLikesAndComments(blogId)
{
if (!localStorage.getItem(`${blogId}_likes`))
{
localStorage.setItem(`${blogId}_likes`, '0');
}
if (!localStorage.getItem(`${blogId}_comments`))
{
localStorage.setItem(`${blogId}_comments`, JSON.stringify([]));
}
updateLikesDisplay(blogId);
updateCommentsDisplay(blogId);
}
function getLikes(blogId)
{
return parseInt(localStorage.getItem(`${blogId}_likes`), 10) || 0;
}
function setLikes(blogId, likes)
{
localStorage.setItem(`${blogId}_likes`, likes.toString());
}
function updateLikesDisplay(blogId)
{
let likes = getLikes(blogId);
let likesDisplay = document.getElementById(`likes-${blogId}`);
likesDisplay.textContent = `${likes} Likes`;
}
// function getComments(blogId)
// {
// let comments = localStorage.getItem(`${blogId}_comments`);
// return JSON.parse(comments) || [];
// }
// function setComments(blogId, comments)
// {
// localStorage.setItem(`${blogId}_comments`, JSON.stringify(comments));
// }
function getComments(blogId)
{
let comments = localStorage.getItem(`${blogId}_comments`);
return comments ? comments.split(',') : [];
}
function setComments(blogId, comments)
{
localStorage.setItem(`${blogId}_comments`, comments.join(','));
}
function updateCommentsDisplay(blogId)
{
let comments = localStorage.getItem(`${blogId}_comments`) || '';
let commentList = document.getElementById(`commentList-${blogId}`);
commentList.innerHTML = '';
comments.split('\n').forEach(comment => {
let commentElement = document.createElement('div');
commentElement.textContent = comment;
commentList.appendChild(commentElement);
});
}
document.addEventListener('DOMContentLoaded', function ()
{
updateDynamicRole();
});