Refactor chat notifications and the chatNewMessage hook
parent
8406cc95ab
commit
bcb92f25a6
|
@ -148,9 +148,14 @@ Called from: src/static/js/chat.js
|
|||
|
||||
Things in context:
|
||||
|
||||
1. msg - The message object
|
||||
1. authorName - The user that wrote this message
|
||||
2. author - The authorID of the user that wrote the message
|
||||
2. text - the message text
|
||||
3. sticky (boolean) - if you want the gritter notification bubble to fade out on its own or just sit there
|
||||
3. timestamp - the timestamp of the chat message
|
||||
4. timeStr - the timestamp as a formatted string
|
||||
|
||||
This hook is called on the client side whenever a user recieves a chat message from the server. This can be used to create different notifications for chat messages.
|
||||
This hook is called on the client side whenever a chat message is received from the server. It can be used to create different notifications for chat messages.
|
||||
|
||||
## collectContentPre
|
||||
Called from: src/static/js/contentcollector.js
|
||||
|
|
|
@ -100,85 +100,68 @@ var chat = (function()
|
|||
|
||||
var text = padutils.escapeHtmlWithClickableLinks(msg.text, "_blank");
|
||||
|
||||
/* Performs an action if your name is mentioned */
|
||||
var myName = $('#myusernameedit').val();
|
||||
myName = myName.toLowerCase();
|
||||
var chatText = text.toLowerCase();
|
||||
var wasMentioned = false;
|
||||
if (chatText.indexOf(myName) !== -1 && myName != "undefined"){
|
||||
wasMentioned = true;
|
||||
}
|
||||
/* End of new action */
|
||||
|
||||
var authorName = msg.userName == null ? _('pad.userlist.unnamed') : padutils.escapeHtml(msg.userName);
|
||||
|
||||
var html = "<p data-authorId='" + msg.userId + "' class='" + authorClass + "'><b>" + authorName + ":</b><span class='time " + authorClass + "'>" + timeStr + "</span> " + text + "</p>";
|
||||
if(isHistoryAdd)
|
||||
$(html).insertAfter('#chatloadmessagesbutton');
|
||||
else
|
||||
$("#chattext").append(html);
|
||||
// the hook args
|
||||
var ctx = {
|
||||
"authorName" : authorName,
|
||||
"author" : msg.userId,
|
||||
"text" : text,
|
||||
"sticky" : false,
|
||||
"timestamp" : msg.time,
|
||||
"timeStr" : timeStr
|
||||
}
|
||||
|
||||
//should we increment the counter??
|
||||
if(increment && !isHistoryAdd)
|
||||
{
|
||||
var count = Number($("#chatcounter").text());
|
||||
count++;
|
||||
// is the users focus already in the chatbox?
|
||||
var alreadyFocused = $("#chatinput").is(":focus");
|
||||
|
||||
// is the users focus already in the chatbox?
|
||||
var alreadyFocused = $("#chatinput").is(":focus");
|
||||
// does the user already have the chatbox open?
|
||||
var chatOpen = $("#chatbox").is(":visible");
|
||||
|
||||
// does the user already have the chatbox open?
|
||||
var chatOpen = $("#chatbox").is(":visible");
|
||||
// does this message contain this user's name? (is the curretn user mentioned?)
|
||||
var myName = $('#myusernameedit').val();
|
||||
var wasMentioned = (text.toLowerCase().indexOf(myName.toLowerCase()) !== -1 && myName != "undefined");
|
||||
|
||||
$("#chatcounter").text(count);
|
||||
// chat throb stuff -- Just make it throw for twice as long
|
||||
if(wasMentioned && !alreadyFocused && !isHistoryAdd && !chatOpen)
|
||||
{ // If the user was mentioned show for twice as long and flash the browser window
|
||||
$.gritter.add({
|
||||
// (string | mandatory) the heading of the notification
|
||||
title: authorName,
|
||||
// (string | mandatory) the text inside the notification
|
||||
text: text,
|
||||
// (bool | optional) if you want it to fade out on its own or just sit there
|
||||
sticky: true,
|
||||
// (int | optional) the time you want it to be alive for before fading out
|
||||
time: '2000'
|
||||
});
|
||||
if(wasMentioned && !alreadyFocused && !isHistoryAdd && !chatOpen)
|
||||
{ // If the user was mentioned show for twice as long and flash the browser window
|
||||
chatMentions++;
|
||||
Tinycon.setBubble(chatMentions);
|
||||
ctx.sticky = true;
|
||||
}
|
||||
|
||||
chatMentions++;
|
||||
Tinycon.setBubble(chatMentions);
|
||||
}
|
||||
// Call chat message hook
|
||||
hooks.aCallAll("chatNewMessage", ctx, function() {
|
||||
|
||||
var html = "<p data-authorId='" + msg.userId + "' class='" + authorClass + "'><b>" + authorName + ":</b><span class='time " + authorClass + "'>" + ctx.timeStr + "</span> " + ctx.text + "</p>";
|
||||
if(isHistoryAdd)
|
||||
$(html).insertAfter('#chatloadmessagesbutton');
|
||||
else
|
||||
$("#chattext").append(html);
|
||||
|
||||
//should we increment the counter??
|
||||
if(increment && !isHistoryAdd)
|
||||
{
|
||||
if(!chatOpen){
|
||||
// Update the counter of unread messages
|
||||
var count = Number($("#chatcounter").text());
|
||||
count++;
|
||||
$("#chatcounter").text(count);
|
||||
|
||||
if(!chatOpen) {
|
||||
$.gritter.add({
|
||||
// (string | mandatory) the heading of the notification
|
||||
title: authorName,
|
||||
title: ctx.authorName,
|
||||
// (string | mandatory) the text inside the notification
|
||||
text: text,
|
||||
|
||||
text: ctx.text,
|
||||
// (bool | optional) if you want it to fade out on its own or just sit there
|
||||
sticky: false,
|
||||
sticky: ctx.sticky,
|
||||
// (int | optional) the time you want it to be alive for before fading out
|
||||
time: '4000'
|
||||
});
|
||||
Tinycon.setBubble(count);
|
||||
var msg = {
|
||||
"authorName" : authorName,
|
||||
"text" : text,
|
||||
"sticky" : false,
|
||||
"time" : timeStr
|
||||
};
|
||||
hooks.aCallAll("chatNewMessage", {
|
||||
"authorName" : authorName,
|
||||
"text" : text,
|
||||
"sticky" : false,
|
||||
"time" : timeStr
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// Clear the chat mentions when the user clicks on the chat input box
|
||||
});
|
||||
|
||||
// Clear the chat mentions when the user clicks on the chat input box
|
||||
$('#chatinput').click(function(){
|
||||
chatMentions = 0;
|
||||
Tinycon.setBubble(0);
|
||||
|
|
Loading…
Reference in New Issue