Refactor chat notifications and the chatNewMessage hook

This commit is contained in:
Marcel Klehr 2013-03-19 20:21:27 +01:00
parent 8406cc95ab
commit bcb92f25a6
2 changed files with 55 additions and 67 deletions

View file

@ -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

View file

@ -78,7 +78,7 @@ var chat = (function()
$("#chatinput").val("");
},
addMessage: function(msg, increment, isHistoryAdd)
{
{
//correct the time
msg.time += this._pad.clientTimeOffset;
@ -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;
var authorName = msg.userName == null ? _('pad.userlist.unnamed') : padutils.escapeHtml(msg.userName);
// the hook args
var ctx = {
"authorName" : authorName,
"author" : msg.userId,
"text" : text,
"sticky" : false,
"timestamp" : msg.time,
"timeStr" : timeStr
}
/* 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);
//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");
// does the user already have the chatbox open?
var chatOpen = $("#chatbox").is(":visible");
// is the users focus already in the chatbox?
var alreadyFocused = $("#chatinput").is(":focus");
$("#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'
});
// does the user already have the chatbox open?
var chatOpen = $("#chatbox").is(":visible");
chatMentions++;
Tinycon.setBubble(chatMentions);
}
// 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)
$(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);