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
|
var CommentsView = FormView.extend({
el: "#comments",
events: {
"focus textarea": "focus",
"blur textarea": "blur",
"click .remove": "remove",
},
initialize: function(){
this.__super__.initialize.call(this)
this.template = this.$(".template").html()
this.$formRow = this.$("#comment_form")
},
load: function(comments, thread){
if (thread.settings.hootbox) {
comments
.sort((a,b) => cmp(a.date, b.date))
.forEach(this.prependComment.bind(this))
this.$el.prepend(this.$formRow)
}
else if (thread.id < 4125) {
comments
.sort((a,b) => cmp(a.date, b.date))
.forEach(this.appendComment.bind(this))
}
else {
comments
.sort((a,b) => cmp(a.id, b.id))
.forEach(this.appendComment.bind(this))
}
},
parse: function(comment){
if (! comment.comment.length) return $('')
var datetime = verbose_date(comment.date, true)
var t = this.template.replace(/{{image}}/g, profile_image(comment.username))
.replace(/{{username}}/g, comment.username)
.replace(/{{id}}/g, comment.id)
.replace(/{{comment}}/g, tidy_urls(comment.comment))
.replace(/{{date}}/g, datetime[0])
.replace(/{{time}}/g, datetime[1])
var $t = $(t)
if (auth.user.username !== comment.username) {
$t.find(".edit-links").remove()
}
if (app.debug) {
$t.find('.date').prepend('#' + comment.id + ' ')
}
return $t
},
prependComment: function(comment){
var $el = this.parse(comment)
this.$el.prepend($el)
},
appendComment: function(comment){
var $el = this.parse(comment)
$el.insertBefore(this.$formRow)
},
success: function(){
this.prependComment(comment)
},
focus: function(){
app.typing = true
},
blur: function(){
app.typing = false
},
remove: function(e){
var id = $(e.target).data('id')
var should_remove = confirm("Are you sure you want to delete this comment? #" + id)
if (should_remove) {
$.ajax({
method: "DELETE",
url: "/api/comment/" + id,
headers: { "csrf-token": $("[name=_csrf]").attr("value") },
data: { csrf: csrf() },
success: function(){
window.location.reload()
},
})
}
},
})
|