Skip to content

Instantly share code, notes, and snippets.

@pjaspers
Created July 17, 2012 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjaspers/065d1dc1906c153e4edc to your computer and use it in GitHub Desktop.
Save pjaspers/065d1dc1906c153e4edc to your computer and use it in GitHub Desktop.
/*
Display avatars in the chat view - based on code originally by @tmm1
*/
Object.extend(Campfire.Message.prototype, {
addAvatar: function() {
if (this.actsLikeTextMessage()) {
var author = this.authorElement();
var avatar = '';
if (author.visible()) {
author.hide();
if (this.bodyCell.select('strong').length === 0) {
this.bodyCell.insert({top: '<strong style="color:#333;">'+author.textContent+'</strong><br>'});
avatar = author.getAttribute('data-avatar') || 'http://asset1.37img.com/global/missing/avatar.png?r=3';
author.insert({after: '<img alt="'+this.author()+'" width="32" height="32" align="top" style="opacity: 1.0; margin-left: 5px; border-radius:3px" src="'+avatar+'">'});
}
}
}
}
});
/* if you can wrap rather than rewrite, use swizzle like this: */
swizzle(Campfire.Message, {
setAuthorVisibilityInRelationTo: function($super, message) {
$super(message);
this.addAvatar();
}
});
/* defining a new responder is probably the best way to insulate your hacks from Campfire and Propane */
Campfire.AvatarMangler = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
message.addAvatar();
}
this.chat.layoutmanager.layout();
this.chat.windowmanager.scrollToBottom();
},
onMessagesInserted: function(messages) {
var scrolledToBottom = this.chat.windowmanager.isScrolledToBottom();
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
message.addAvatar();
}
if (scrolledToBottom) {
this.chat.windowmanager.scrollToBottom();
}
}
});
/* Here is how to install your responder into the running chat */
Campfire.Responders.push("AvatarMangler");
window.chat.installPropaneResponder("AvatarMangler", "avatarmangler");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment