Refactor chat notifications and the chatNewMessage hook
This commit is contained in:
parent
8406cc95ab
commit
bcb92f25a6
|
@ -148,9 +148,14 @@ Called from: src/static/js/chat.js
|
||||||
|
|
||||||
Things in context:
|
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
|
## collectContentPre
|
||||||
Called from: src/static/js/contentcollector.js
|
Called from: src/static/js/contentcollector.js
|
||||||
|
|
|
@ -100,19 +100,39 @@ var chat = (function()
|
||||||
|
|
||||||
var text = padutils.escapeHtmlWithClickableLinks(msg.text, "_blank");
|
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 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>";
|
// the hook args
|
||||||
|
var ctx = {
|
||||||
|
"authorName" : authorName,
|
||||||
|
"author" : msg.userId,
|
||||||
|
"text" : text,
|
||||||
|
"sticky" : false,
|
||||||
|
"timestamp" : msg.time,
|
||||||
|
"timeStr" : timeStr
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 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");
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
if(isHistoryAdd)
|
||||||
$(html).insertAfter('#chatloadmessagesbutton');
|
$(html).insertAfter('#chatloadmessagesbutton');
|
||||||
else
|
else
|
||||||
|
@ -121,63 +141,26 @@ var chat = (function()
|
||||||
//should we increment the counter??
|
//should we increment the counter??
|
||||||
if(increment && !isHistoryAdd)
|
if(increment && !isHistoryAdd)
|
||||||
{
|
{
|
||||||
|
// Update the counter of unread messages
|
||||||
var count = Number($("#chatcounter").text());
|
var count = Number($("#chatcounter").text());
|
||||||
count++;
|
count++;
|
||||||
|
|
||||||
// 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");
|
|
||||||
|
|
||||||
$("#chatcounter").text(count);
|
$("#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'
|
|
||||||
});
|
|
||||||
|
|
||||||
chatMentions++;
|
|
||||||
Tinycon.setBubble(chatMentions);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(!chatOpen) {
|
if(!chatOpen) {
|
||||||
$.gritter.add({
|
$.gritter.add({
|
||||||
// (string | mandatory) the heading of the notification
|
// (string | mandatory) the heading of the notification
|
||||||
title: authorName,
|
title: ctx.authorName,
|
||||||
// (string | mandatory) the text inside the notification
|
// (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
|
// (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
|
// (int | optional) the time you want it to be alive for before fading out
|
||||||
time: '4000'
|
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(){
|
$('#chatinput').click(function(){
|
||||||
chatMentions = 0;
|
chatMentions = 0;
|
||||||
|
|
Loading…
Reference in New Issue