This repository was archived by the owner on May 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.js
More file actions
158 lines (132 loc) · 4.44 KB
/
Copy pathtimeline.js
File metadata and controls
158 lines (132 loc) · 4.44 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
import di from "../di.js";
let postTemplate = null;
const isOnIndexPage = window.location.pathname.endsWith("index.html");
export default function showTimeline(type) {
if (isOnIndexPage) return;
const loggedUser = di.sessionStorageUserService.getLoggedUser();
if (type == "main") {
const cardBlock = document.getElementById("card-block");
if (!cardBlock) return;
// Use cached template
if (!postTemplate) return;
const onlyFriendsToggle = document.getElementById("only-friends-toggle");
const onlyFriends = onlyFriendsToggle ? onlyFriendsToggle.checked : false;
// Fetch posts (synchronous for now based on current impl)
const postsVector = di.homeTimelineHandler.ReadHomeTimeline(
loggedUser.userid,
0,
10,
onlyFriends,
);
const posts = [];
for (let i = 0; i < postsVector.size(); i++) {
posts.push(postsVector.get(i));
}
// Sort by timestamp based on toggle button
const sortBtn = document.getElementById("sort-toggle-btn");
const sortAsc = sortBtn
? sortBtn.getAttribute("data-sort") === "asc"
: false;
posts.sort((a, b) => {
const valA = Number(a.timestamp);
const valB = Number(b.timestamp);
if (valA > valB) return sortAsc ? 1 : -1;
if (valA < valB) return sortAsc ? -1 : 1;
return 0;
});
// Clear current posts
cardBlock.innerHTML = "";
for (const p of posts) {
const date = new Date(Number(p.timestamp) * 1000);
const clone = postTemplate.cloneNode(true);
clone.style.display = "block";
// Fill data
clone.querySelector(".post-text").innerText = p.text;
clone.querySelector(".post-time").innerText = date.toString();
const creatorEl = clone.querySelector(".post-creator");
if (creatorEl) creatorEl.innerText = p.creator.username;
// Hook buttons
const deleteBtn = clone.querySelector(".delete-post-btn");
if (deleteBtn) {
deleteBtn.addEventListener("click", () => {
$("#deletePostModal").data("post-id", p.post_id).modal("show");
});
}
const editBtn = clone.querySelector(".edit-post-btn");
if (editBtn) {
editBtn.addEventListener("click", () => {
$("#editPostModal").data("post-id", p.post_id);
const editPostTextarea = document.getElementById("editPostTextarea");
editPostTextarea.value = p.text;
$("#editPostModal").modal("show");
});
}
cardBlock.appendChild(clone);
}
}
}
// Global exposure
window.showTimeline = showTimeline;
function initTimeline() {
if (isOnIndexPage) return;
if (
!sessionStorage.getItem("user") ||
!di.sessionStorageUserService.getLoggedUser()
) {
console.log("User not logged in, redirecting to login page.");
window.location.href = "../index.html";
return;
}
const allCards = document.getElementsByClassName("post-card");
if (allCards.length > 0) {
postTemplate = allCards[0].cloneNode(true);
allCards[0].remove();
} else {
console.error("timeline.js: No post-card template found!");
}
showTimeline("main");
$("#confirmDeletePostBtn").on("click", () => {
const postId = $("#deletePostModal").data("post-id");
if (postId) {
di.postStorageHandler.DeletePost(postId);
$("#deletePostModal").modal("hide");
showTimeline("main");
//setTimeout(() => { showTimeline("main"); }, 500);
}
});
$("#confirmEditPostBtn").on("click", () => {
const postId = $("#editPostModal").data("post-id");
if (postId) {
const editPostTextarea = document.getElementById("editPostTextarea");
di.postStorageHandler.EditPostText(postId, editPostTextarea.value);
$("#editPostModal").modal("hide");
showTimeline("main");
//setTimeout(() => { showTimeline("main"); }, 500);
}
});
const logoutBtn = document.getElementById("logout-btn");
if (logoutBtn) {
logoutBtn.addEventListener("click", (e) => {
e.preventDefault();
logout();
});
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initTimeline);
} else {
initTimeline();
}
export function logout() {
try {
if (di.sessionStorageUserService) {
di.sessionStorageUserService.removeLoggedUser();
}
} catch (e) {
console.error("Error during logout:", e);
// Fallback if binding fails
sessionStorage.removeItem("user");
}
window.location.href = "../index.html";
}
window.logout = logout;