1
difenduandada
2024-10-15 7fd2948ee35c8e147ed35ce6d8502f94a98ddd22
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
/*
CommentSystem v1.0.0 by CloudArcade
*/
class CommentSystem {
    constructor(gameId, config = {}) {
        this.gameId = gameId;
        this.commentOffset = 0;
        this.alertMsgTooshort = 'Your comment is too short. Please enter at least {{min}} characters.';
        this.config = {
            commentsPerLoad: config.commentsPerLoad || 5,  // Default to showing 5 comments
            maxReplies: config.maxReplies || 10,  // Default to showing 10 replies per comment
            minChars: config.minChars || 3,
            dateFormat: config.dateFormat || 'timeAgo' // timeAgo, ISO
        };
        this.init();
    }
 
    init() {
        this.loadComments();
        this.setupCommentPostForm();
        this.alertMsgTooshort = this.alertMsgTooshort.replace('{{min}}', this.config.minChars);
        document.getElementById('tpl-btn-load-more-comments').addEventListener('click', () => this.loadMoreComments());
        document.getElementById('tpl-comment-section').addEventListener('click', (event) => {
            let targetElement = event.target;
            while (targetElement != null) {
                if (targetElement.matches('.tpl-btn-show-replies')) {
                    const commentId = targetElement.getAttribute('data-id');
                    this.loadReplies(commentId);
                    return;
                } else if (targetElement.matches('.tpl-btn-hide-replies')) {
                    const commentId = targetElement.getAttribute('data-id');
                    this.hideReplies(commentId);
                    return;
                } else if (targetElement.matches('.tpl-comment-reply')) {
                    event.preventDefault();
                    const closestElement = targetElement.closest('.tpl-user-comment');
                    const commentId = closestElement.getAttribute('data-id');
                    this.renderReplyForm(commentId);
                    return;
                }
                targetElement = targetElement.parentElement;
            }
        });
    }
 
    getConvertedDate(dateString, serverDate) {
        const date = new Date(dateString); // Parse the date string to a Date object
        const now = new Date(serverDate); // Current server date and time
        if (this.config.dateFormat === 'ISO') {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            return `${year}-${month}-${day}`;
        } else if (this.config.dateFormat === 'timeAgo') {
            const diffInSeconds = Math.floor((now - date) / 1000);
            const years = Math.floor(diffInSeconds / (3600 * 24 * 365));
            const months = Math.floor(diffInSeconds / (3600 * 24 * 30));
            const days = Math.floor(diffInSeconds / (3600 * 24));
            const hours = Math.floor(diffInSeconds / 3600);
            const minutes = Math.floor(diffInSeconds / 60);
            if (years > 0) {
                return `${years} ${years === 1 ? 'year' : 'years'} ago`;
            } else if (months > 0) {
                return `${months} ${months === 1 ? 'month' : 'months'} ago`;
            } else if (days > 0) {
                return `${days} ${days === 1 ? 'day' : 'days'} ago`;
            } else if (hours > 0) {
                return `${hours} ${hours === 1 ? 'hour' : 'hours'} ago`;
            } else if (minutes > 0) {
                return `${minutes} ${minutes === 1 ? 'minute' : 'minutes'} ago`;
            } else {
                return "Just now";
            }
        } else {
            throw new Error('Invalid config value');
        }
    }
 
    renderReplyForm(commentId) {
        const existingReplyForm = document.querySelector('#tpl-comment-list .tpl-reply-form');
        if (existingReplyForm) existingReplyForm.remove();
        const commentElem = document.querySelector(`.tpl-user-comment[data-id="${commentId}"]`);
        const replyFormTemplate = document.querySelector('#tpl-comment-template .tpl-reply-form').cloneNode(true);
        const sendReplyBtn = replyFormTemplate.querySelector('.tpl-btn-send-reply');
        sendReplyBtn.setAttribute('data-id', commentId);
        replyFormTemplate.querySelector('.tpl-btn-send-reply').addEventListener('click', (e) => {
            e.preventDefault();
            const content = replyFormTemplate.querySelector('.tpl-reply-input').value;
            this.postComment(content, commentId);
            replyFormTemplate.remove();
        });
        const cancelReplyBtn = replyFormTemplate.querySelector('.tpl-btn-cancel-reply');
        if(cancelReplyBtn){
            cancelReplyBtn.addEventListener('click', (e) => {
                e.preventDefault();
                replyFormTemplate.remove();
            });
        }
        commentElem.querySelector('.tpl-reply-form-wrapper').appendChild(replyFormTemplate);
    }
 
    loadReplies(commentId, forceLoad = false) {
        const parentCommentElem = document.querySelector(`.tpl-user-comment[data-id="${commentId}"]`);
        const showRepliesBtn = parentCommentElem.querySelector('.tpl-btn-show-replies');
        const hideRepliesBtn = parentCommentElem.querySelector('.tpl-btn-hide-replies');
        const commentChildren = parentCommentElem.querySelector('.tpl-comment-children');
        if (showRepliesBtn) showRepliesBtn.style.display = 'none';
        const repliesHTML = commentChildren.innerHTML;
        if(repliesHTML && repliesHTML.trim() !== '' && !forceLoad){
            commentChildren.style.display = 'block';
            if (hideRepliesBtn) hideRepliesBtn.style.display = 'block';
        } else {
            fetch('/includes/comment.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `load_replies=true&amount=${this.config.maxReplies}&game_id=${this.gameId}&parent_id=${commentId}`
            })
            .then(response => {
                if (hideRepliesBtn) hideRepliesBtn.style.display = 'block';
                return response.json();
            })
            .then(replies => {
                if (replies && replies.length > 0) {
                    const transformedReplies = this.transformData(replies);
                    const childrenHtml = this.renderCommentsRecursive(transformedReplies, true);
                    commentChildren.innerHTML = childrenHtml;
                }
            })
            .catch(error => {
                console.error('Failed to load replies:', error);
            });
        }
    }
 
    hideReplies(commentId) {
        const parentCommentElem = document.querySelector(`.tpl-user-comment[data-id="${commentId}"]`);
        const hideRepliesBtn = parentCommentElem.querySelector('.tpl-btn-hide-replies');
        const commentChildren = parentCommentElem.querySelector('.tpl-comment-children');
        const showRepliesBtn = parentCommentElem.querySelector('.tpl-btn-show-replies');
        if (hideRepliesBtn) {
            hideRepliesBtn.style.display = 'none';
        }
        if (commentChildren) {
            commentChildren.style.display = 'none';
        }
        if (showRepliesBtn) {
            showRepliesBtn.style.display = 'block';
        }
    }
 
    setupCommentPostForm() {
        const tooShortElem = document.querySelector('#tpl-comment-form .tpl-alert-tooshort');
        if (tooShortElem) {
            this.alertMsgTooshort = tooShortElem.innerHTML;
            tooShortElem.parentNode.removeChild(tooShortElem);
        }
        const postCommentBtn = document.querySelector('#tpl-comment-form .tpl-post-comment-btn');
        const commentInput = document.querySelector('#tpl-comment-form .tpl-comment-input');
        if(postCommentBtn){
            postCommentBtn.addEventListener('click', () => {
                const commentContent = commentInput.value;
                if (commentContent) {
                    this.postComment(commentContent);
                    commentInput.value = '';  // Clear the textarea
                }
            });
        }
    }
 
    postComment(content, parent = null) {
        if (content.length < this.config.minChars) {
            alert(this.alertMsgTooshort);
            return;
        }
        const requestData = {
            'send_comment': true,
            'game_id': this.gameId,
            'parent': parent,
            'content': content
        };
        const formData = new URLSearchParams(requestData);
        fetch('/includes/comment.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: formData
        })
        .then(response => response.text())
        .then(response => {
            console.log(response);
            if (response === 'success') {
                if (parent !== null) {
                    // Is reply
                    this.loadReplies(parent, true);
                } else {
                    // Root comment
                    this.loadComments();
                }
            } else {
                console.log('fail');
            }
        })
        .catch(error => {
            console.error('Failed to post comment:', error);
        });
    }
 
    loadComments(isLoadMore = false) {
        const loadMoreButton = document.getElementById('tpl-btn-load-more-comments');
        if (loadMoreButton) {
            loadMoreButton.style.display = 'none';
        }
        if (!isLoadMore) {
            // Reset offset
            this.commentOffset = 0;
        }
        const requestData = {
            'load_root_comments': true,
            'game_id': encodeURIComponent(this.gameId),
            'offset': encodeURIComponent(this.commentOffset),
            'amount': encodeURIComponent(this.config.commentsPerLoad)
        };
        const formData = new URLSearchParams(requestData);
        fetch('/includes/comment.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: formData
        })
        .then(response => response.json())
        .then(comments => {
            if (comments && comments.length > 0) {
                const transformedComments = this.transformData(comments);
                if (isLoadMore) {
                    this.renderComments(transformedComments, true);
                    this.commentOffset += comments.length; // Update the offset
                } else {
                    this.renderComments(transformedComments);
                    this.commentOffset = comments.length;
                }
            }
            if (comments.length < this.config.commentsPerLoad || comments.length === 0) {
                if (loadMoreButton) {
                    loadMoreButton.remove();
                }
            } else {
                if (loadMoreButton) {
                    loadMoreButton.style.display = 'block';
                }
            }
        })
        .catch(error => {
            console.error('Failed to load comments:', error);
        });
    }
    
    loadMoreComments() {
        this.loadComments(true);
    }
 
    transformData(array) {
        return array.map(item => {
            return {
                id: Number(item.id),
                parent_id: item.parent_id,
                created: item.created_date,
                content: item.comment,
                has_replies: item.has_replies,
                server_date: item.server_date,
                fullname: item.sender_username || 'Anonymous',
                profile_picture_url: item.avatar
            };
        });
    }
 
    generateCommentHtmlFromTemplate(comment, isChildren) {
        const template = document.querySelector("#tpl-comment-template .tpl-user-comment").cloneNode(true);
        template.setAttribute("data-id", comment.id);
        const elementsWithDataId = template.querySelectorAll("[data-id]");
        elementsWithDataId.forEach(element => element.setAttribute("data-id", comment.id));
        const authorElement = template.querySelector(".tpl-comment-author");
        if (authorElement) authorElement.textContent = comment.fullname;
        const timestampElement = template.querySelector(".tpl-comment-timestamp");
        if (timestampElement) timestampElement.textContent = this.getConvertedDate(comment.created, comment.server_date);
        const textElement = template.querySelector(".tpl-comment-text");
        if (textElement) textElement.textContent = comment.content;
        const avatarElement = template.querySelector("img.tpl-user-comment-avatar");
        if (avatarElement) avatarElement.setAttribute("src", comment.profile_picture_url);
        if (isChildren) {
            const replyElement = template.querySelector(".tpl-comment-reply");
            const replyFormWrapperElement = template.querySelector(".tpl-reply-form-wrapper");
            if (replyElement) replyElement.remove();
            if (replyFormWrapperElement) replyFormWrapperElement.remove();
        }
        return template;
    }
 
    renderCommentsRecursive(data, isChildren = false) {
        let html = "";
        data.forEach((comment) => {
            const commentHtml = this.generateCommentHtmlFromTemplate(comment, isChildren);
            const showRepliesBtn = commentHtml.querySelector('.tpl-btn-show-replies');
            const hideRepliesBtn = commentHtml.querySelector('.tpl-btn-hide-replies');
            if (comment.has_replies) {
                if (showRepliesBtn) showRepliesBtn.style.display = 'block';
                if (hideRepliesBtn) hideRepliesBtn.style.display = 'none';
            } else {
                if (showRepliesBtn) showRepliesBtn.remove();
                if (hideRepliesBtn) hideRepliesBtn.remove();
            }
            html += commentHtml.outerHTML;
        });
        return html;
    }
 
    renderComments(data, isLoadMore = false) {
        const html = this.renderCommentsRecursive(data);
        const commentList = document.querySelector('#tpl-comment-list');
        if (isLoadMore) {
            commentList.insertAdjacentHTML('beforeend', html);
        } else {
            commentList.innerHTML = html;
        }
    }
}