-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathforumsignalr.js
147 lines (124 loc) · 5.45 KB
/
forumsignalr.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
function startNotifier(memberId, memberName, modelId, isMemberAdmin) {
var lastActivity = null;
// if it's been long since last activity, we'll remove the "working on reply" box
function checkActivity() {
var timeDiff = _.now() - lastActivity;
if (timeDiff > 60000) {
$("#reply-is-coming").fadeOut();
} else {
setTimeout(checkActivity, 5000);
}
}
$(function () {
var forum = $.connection.forumPostHub;
forum.client.someoneIsTyping = function (id, memberId, name) {
if (id !== modelId) {
return;
}
// update the date
lastActivity = _.now();
var replyPlaceholder = $("#reply-is-coming");
// update meta data
replyPlaceholder.find(".author").html(name);
$.ajax({
url: "/umbraco/api/Avatar/GetMemberAvatar/?memberId=" + memberId,
type: "GET"
}).done(function (avatarData) {
replyPlaceholder.find(".photo").attr("href", "/member/" + memberId);
replyPlaceholder.find("img").replaceWith(avatarData);
});
if (!replyPlaceholder.is(":visible")) {
replyPlaceholder.fadeIn(1000);
checkActivity();
}
};
forum.client.DeleteComment = function (threadId, commentId) {
if (threadId !== modelId) {
return;
}
var containerId = "#comment-" + commentId;
$(containerId).fadeOut(1000, function () { $(this).remove(); });
};
forum.client.returnLatestComment = function (data) {
if (data.topicId !== modelId) {
return;
}
var avatarUrl = "/umbraco/api/Avatar/GetMemberAvatar/?memberId=" + data.authorId;
$.ajax({
url: avatarUrl,
type: "GET"
}).done(function (avatarData) {
data.avatar = avatarData;
//new comment we'll use mustache to insert it into the dom
data.lower = function () {
return function (text, render) {
return render(text).toLowerCase();
}
};
// hide reply in progress
$("#reply-is-coming").fadeOut();
data.canHaveChildren = true;
data.isLoggedIn = memberId > 0;
data.isCommentOwner = data.authorId === memberId;
data.canManageComment = isMemberAdmin || data.isCommentOwner;
if (data.isSpam === false || data.isCommentOwner) {
var template = $("#comment-template").html();
var rendered = Mustache.render(template, data);
if (data.parent === 0) {
$(".comments").append(rendered);
$("div.replybutton").insertAfter($("#comment-" + data.id));
} else {
var allComments = $("li[data-parent='" + data.parent + "']");
if (allComments.length > 0) {
var lastComment = 0;
for (var i = 0; i < allComments.length; i++) {
lastComment = allComments[i];
}
$(lastComment).after(rendered);
} else {
$("#comment-" + data.parent).after(rendered);
}
}
var notify = new PNotify({
title: "New answer was added",
text: "Jump to new answer",
type: "success"
});
$("#comment-" + data.id).css("background", "rgba(14, 216, 61, 0.31)").fadeIn(200);
notify.get().css("cursor", "pointer").click(function(e) {
$(document).scrollTop($("#comment-" + data.id).offset().top - 80);
$("#comment-" + data.id).hide();
$("#comment-" + data.id).fadeIn();
});
}
});
};
forum.client.returnEditedComment = function (data) {
if (data.topicId !== modelId) {
return;
}
var container = $("#comment-" + data.id + " .body");
container.html(data.body);
var notify = new PNotify({
title: "Post was edited",
text: "Jump to modified answer",
});
$("#comment-" + data.id).css("background", "rgba(216, 209, 14, 0.31)").fadeIn(200);
notify.get().css("cursor", "pointer").click(function (e) {
$(document).scrollTop($("#comment-" + data.id).offset().top - 80);
$("#comment-" + data.id).hide();
$("#comment-" + data.id).fadeIn();
});
}
forum.client.notify = function () {
if ($("#wmd-input").val().length > 50) {
forum.server.someoneIsTyping(modelId, memberId, memberName);
}
}
var notifyChange = _.debounce(forum.client.notify, 500, { leading: true, trailing: false });
// Start the connection.
$.connection.hub.start().done(function () {
$("#wmd-input").bind("input propertychange", notifyChange);
});
});
}