forked from hsbakshi/reddit-check
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
comment.js
280 lines (263 loc) · 11.1 KB
/
comment.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
function iterateComments(index, comment, archived, $element) {
const $collapsed = renderCollapsedComment(comment);
const $comment = renderComment(comment, archived);
if (comment.replies.length > 0) {
$comment.append('<ul>');
let $children = $comment.children('ul');
$.each(comment.replies, (index, child) => {
iterateComments(index, child, archived, $children);
});
$comment.append($children);
}
$element.append($collapsed).append($comment);
}
function renderCollapsedComment(comment) {
return $(`
<li class="s136il31-0 cMWqxb" id="${comment.id}-collapsed" tabindex="-1" style="display:none">
<div class="Comment ${comment.id} c497l3-5 MAIAY">
<button class="${comment.id} c497l3-0 jtKgEe">
<i class="icon icon-expand qjrkk1-0 JnYFK"></i>
</button>
<div class="c497l3-4 bWacBs">
<div class="c497l3-3 cFXBbI">
<div>
<a class="s1461iz-1 RVnoX" href="https://www.reddit.com/user/${comment.author}" target="_blank">${comment.author}</a>
</div>
<span class="h5svje-0 cFQOcm">${numToString(comment.score)} points</span>
<span class="h5svje-0 cFQOcm"> · </span>
<a class="s17xjtj0-13 hsxhRU" href="https://www.reddit.com/${comment.permalink}" id="CommentTopMeta--Created--t1_e7i7pcvinOverlay" rel="nofollow" target="_blank">
<span>${getAge(comment.created_utc)}</span>
</a>
</div>
</div>
</div>
</li>
`)
}
function renderComment(comment, archived) {
const commentModel = new ContentModel(comment);
commentModel.replyable_content_type = 'comment';
let $comment = $(`
<li id='${comment.id}' class="s136il31-0 cMWqxb" tabindex="-1">
<div class="fxv3b9-1 jDSCcP">
<div class="fxv3b9-2 czhQfm">
<div class="${comment.id} fxv3b9-0 efNcNS"><i class="threadline"></i></div>
</div>
</div>
<div class="Comment ${comment.id} c497l3-5 MAIAY">
<div class="comment_vote">
</div>
<div class="c497l3-4 jHfOJm">
<span class="s1dqr9jy-0 imyGpC">level 1</span>
<div class="c497l3-3 clkVGJ s17xjtj0-21 heFPGG">
<div class="wx076j-0 hPglCh">
<a class="s1461iz-1 RVnoX" href="https://www.reddit.com/user/${comment.author}" target="_blank">${comment.author}</a>
</div>
<span class="h5svje-0 cFQOcm">${numToString(comment.score)} points</span>
<span class="h5svje-0 cFQOcm"> · </span>
<a class="s17xjtj0-13 hsxhRU" href="https://www.reddit.com/${comment.permalink}" id="CommentTopMeta--Created--t1_e7i7pcvinOverlay" rel="nofollow" target="_blank">
<span>${getAge(comment.created_utc)}</span>
</a>
</div>
<div>
<div class="c497l3-6 eCeBkc s1hmcfrd-0 ckueCN">
${comment.body_html}
</div>
<div>
<div class="s5kaj4p-8 dtnsqo">
${archived ? "":
`<button class="s5kaj4p-9 hNSNDN reply_button">
<i class="icon icon-comment _3ch9jJ0painNf41PmU4F9i s5kaj4p-0 domCcm"></i>Reply
</button>`
}
<div class="comment_buttons"></div>
</div>
</div>
<form></form>
</div>
</div>
<form></form>
</div>
</li>`);
let $vote_arrows_div = $comment.find('.comment_vote');
vote_arrows_dom = document.createElement('div');
$buttons_div = $comment.find('.comment_buttons');
buttons_dom = document.createElement('div');
$vote_arrows_div.append(vote_arrows_dom);
$buttons_div.append(buttons_dom);
ReactDOM.render(
React.createElement(CommentVote, commentModel),
vote_arrows_dom
);
ReactDOM.render(
React.createElement(CommentButtons, commentModel),
buttons_dom
);
return $comment;
}
function appendComment(index, comment, archived, $element) {
iterateComments(index, comment, archived, $element);
$('.reply_button').off('click').click(function() {
const $this = $( this );
logInReddit(function(status) {
var comment_id = $this.closest('li').attr('id');
const $form = $this.parents().eq(1).siblings('form');;
displayReplyComment(comment_id, $form, 'comment');
});
});
$('.threadline').off('click').click(function() {
const comment_id = $( this ).parent()[0].classList[0];
$('#' + comment_id).hide();
$('#' + comment_id + '-collapsed').show();
});
$('.icon-expand').off('click').click(function() {
const comment_id = $( this ).parent()[0].classList[0];
$('#' + comment_id).show();
$('#' + comment_id + '-collapsed').hide();
});
$('.ckueCN a').each(function () {
$( this ).attr('title', $( this ).attr('href'));
});
}
function makeDisplay(submission) {
var redditComments = submission.comments;
var archived = submission.archived;
var submission_id = submission.id;
const submissionModel = new ContentModel(submission);
submissionModel.replyable_content_type = 'submission';
if (archived) {
$("#archived").show();
}
if (redditComments.length > 0) {
$.each(redditComments, function(index, comment) {
appendComment(index, comment, archived, $('#comments'));
});
} else {
$("#no_results").show();
}
if (!archived) {
const $form = $('#reply_post');
$form.click(function () {
logInReddit(status => {});
});
displayReplyComment(submission_id, $form, 'submission');
}
ReactDOM.render(
React.createElement(SubmissionExpand, submissionModel),
document.getElementById('post'));
$('.ckueCN a').each(function () {
$( this ).attr('title', $( this ).attr('href'));
});
scrollToSearchResult();
$("#loading").hide();
}
function scrollToSearchResult() {
const fragment = new URI(window.location.href).fragment();
if (fragment) {
const search_result_elem = document.getElementById(fragment);
if (search_result_elem) {
search_result_elem.scrollIntoView({
behavior: 'smooth'
});
search_result_elem.style.backgroundColor = '#E6F7FA';
} else {
$('#element_not_found').show();
}
}
}
function displayReplyComment(comment_id, $form, replyable_content_type) {
if ($form.children().length == 0) {
$form.append(renderReplyComment(comment_id, replyable_content_type));
const converter = new Markdown.getSanitizingConverter();
const editor = new Markdown.Editor(converter, '-' + comment_id);
editor.run();
// TODO: better handling of this function
$('.cancel_reply').click(function(event) {
const $this = $( this );
event.preventDefault();
$this.closest('form').toggle();
});
// TODO: better handling of this function
$form.submit(function(event) {
event.preventDefault();
$form.find('button').prop('disabled', true);
leaveComment(comment_id,
$(`#wmd-input-${comment_id}`).val(),
replyable_content_type,
function (response) {
$form.find('button').prop('disabled', false);
if (response.id) {
if (replyable_content_type === 'comment') {
$form.hide(0);
}
$(`#wmd-input-${comment_id}`).val('');
$(`#wmd-preview-${comment_id}`).html('');
const parent_id = comment_id;
let $parent;
if (replyable_content_type == 'comment') {
if ($('#' + parent_id).children('ul').length == 0) {
$('#' + parent_id).append('<ul></ul>');
}
$parent = $('#' + parent_id).children('ul');
} else if (replyable_content_type == 'submission') {
$parent = $('<ul>');
$('#comments').prepend($parent); // new comment is at top of thread
}
appendComment(-1, response, false, $parent);
// TODO: individual status div per reply box
$("#status").html("<span>Successful post</span>")
} else {
// TODO: better error handling
if (replyable_content_type === 'comment') {
$form.hide(0);
}
$("#status").html(`<span>${response}</span>`);
console.log('Status of failed post:');
console.log(response);
}
});
});
} else {
$form.toggle();
}
}
function renderReplyComment(comment_id, replyable_content_type) {
return `
<div id="wmd-button-bar-${comment_id}"></div>
<textarea id="wmd-input-${comment_id}" class="wmd-input" placeholder="Write your comment here using Markdown. You'll see a live preview of how your comment will look below this box."></textarea>
<div class="jvfwx7-0 gJASDC">
<div class="s1ly6fn0-7 dxkxnq">
<div class="s1htkqit-0 dkOnao">
<div class="s1l8ionp-0 dmKBhh">
<div class="s1ly6fn0-0 knfJib">
Live Preview
</div>
</div>
</div>
<div class="s1ly6fn0-9 btsuju">
<div id="wmd-preview-${comment_id}" class="wmd-panel wmd-preview"></div>
</div>
</div>
</div>
<div class="s1htkqit-4 kALOUK">
<button class="s1htkqit-1 htkhll" type="submit">Reply</button>
${replyable_content_type=='comment' ? `
<button class="s1htkqit-3 lcYWzy cancel_reply" type="reset">
CANCEL
</button>`: ''
}
</div>
`;
}
$(document).ready(function(){
$('#back_button').click(() => window.history.back());
var window_url = new URI(window.location.href);
var query = window_url.search(true);
var submission = SubmissionLscache.get(query.id) || {};
if ((submission.comments && submission.comments.length > 0) ||
query.num_comments === 0) {
makeDisplay(submission);
} else {
getSubmission(query.id, makeDisplay);
}
});