-
-
Save dstrelau/1042725 to your computer and use it in GitHub Desktop.
caveatPatchor.js file for use in Propane
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var displayAvatars = true; | |
var displayCloudAppImages = true; | |
var displayGists = true; | |
if (displayAvatars) { | |
Object.extend(Campfire.Message.prototype, { | |
addAvatar: function() { | |
if (this.actsLikeTextMessage()) { | |
var author = this.authorElement(); | |
if (author.visible()) { | |
author.hide(); | |
if (this.bodyCell.select('strong').length == 0) { | |
this.bodyCell.insert({top: '<strong>'+this.author()+'</strong><br>'}); | |
author.insert({after: '<img alt="'+this.author()+'" width="24" height="24" align="top" style="margin-left: 5px; border-radius:3px" src="'+author.getAttribute('data-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; | |
chat.transcript.element.childElements().each(function(elem){ | |
if (elem.match('tr')) { | |
var msg = new Campfire.Message(chat, elem); | |
msg.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"); | |
} | |
/* | |
Display CloudApp images inline. | |
This responder illustrates using Propane's requestJSON service to request | |
JSON from remote (non-authenticated) servers and have the results passed | |
to a callback of your choosing. | |
*/ | |
if (displayCloudAppImages) { | |
Campfire.CloudAppExpander = Class.create({ | |
initialize: function(chat) { | |
this.chat = chat; | |
var messages = this.chat.transcript.messages; | |
for (var i = 0; i < messages.length; i++) { | |
this.detectCloudAppURL(messages[i]); | |
} | |
}, | |
detectCloudAppURL: function(message) { | |
/* we are going to use the messageID to uniquely identify our requestJSON request | |
so we don't check pending messages */ | |
if (!message.pending() && message.kind === 'text') { | |
var links = message.bodyElement().select('a:not(image)'); | |
if (links.length != 1) { | |
return; | |
} | |
var href = links[0].getAttribute('href'); | |
var match = href.match(/^https?:\/\/cl.ly\/[A-Za-z0-9]+\/?$/); | |
if (!match) return; | |
window.propane.requestJSON(message.id(), href, 'window.chat.cloudappexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed'); | |
} | |
}, | |
onEmbedDataLoaded: function(messageID, data) { | |
var message = window.chat.transcript.getMessageById(messageID); | |
if (!message) return; | |
if (data['item_type'] === 'image') { | |
var imageURL = data['content_url']; | |
message.resize((function() { | |
message.bodyCell.insert({bottom: '<div style="width:100%; margin-top:5px; padding-top: 5px; border-top:1px dotted #ccc;"><a href="'+imageURL+'" class="image loading" target="_blank">' + '<img src="'+imageURL+'" onload="$dispatch("inlineImageLoaded", this)" onerror="$dispatch("inlineImageLoadFailed", this)" /></a></div>'}); | |
}).bind(this)); | |
} | |
}, | |
onEmbedDataFailed: function(messageID) { | |
/* No cleanup required, we only alter the HTML after we get back a succesful load from the data */ | |
}, | |
onMessagesInsertedBeforeDisplay: function(messages) { | |
for (var i = 0; i < messages.length; i++) { | |
this.detectCloudAppURL(messages[i]); | |
} | |
}, | |
onMessageAccepted: function(message, messageID) { | |
this.detectCloudAppURL(message); | |
} | |
}); | |
Campfire.Responders.push("CloudAppExpander"); | |
window.chat.installPropaneResponder("CloudAppExpander", "cloudappexpander"); | |
} | |
/* | |
Display Gists inline. | |
This responder illustrates using Propane's requestJSON service to request | |
JSON from remote (non-authenticated) servers and have the results passed | |
to a callback of your choosing. | |
*/ | |
if (displayGists) { | |
Campfire.GistExpander = Class.create({ | |
initialize: function(chat) { | |
this.chat = chat; | |
var messages = this.chat.transcript.messages; | |
for (var i = 0; i < messages.length; i++) { | |
this.detectGistURL(messages[i]); | |
} | |
}, | |
detectGistURL: function(message) { | |
/* we are going to use the messageID to uniquely identify our requestJSON request | |
so we don't check pending messages */ | |
if (!message.pending() && message.kind === 'text') { | |
var links = message.bodyElement().select('a:not(image)'); | |
if (links.length != 1) { | |
return; | |
} | |
var href = links[0].getAttribute('href'); | |
var match = href.match(/^https?:\/\/gist.github.com\/[A-Za-z0-9]+\/?$/); | |
var id = match[0].replace(/^https?:\/\/gist.github.com\/([A-Za-z0-9]+)\/?$/, '$1') | |
if (!match) return; | |
window.propane.requestJSON(message.id(), 'https://api.github.com/gists/' + id, 'window.chat.gistexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed'); | |
} | |
}, | |
onEmbedDataLoaded: function(messageID, data) { | |
var message = window.chat.transcript.getMessageById(messageID); | |
if (!message) return; | |
files = data['files'] | |
for (f in files) { | |
var file = files[f]; | |
var content = file['content'].replace(/&/g,'&'). | |
replace(/>/g,'>'). | |
replace(/</g,'<'). | |
replace(/"/g,'"'). | |
match(/^.*([\n\r]+|$)/gm). | |
slice(0,5). | |
join(""); | |
var link = file['raw_url']; | |
var file_name = file['filename']; | |
message.resize((function() { | |
message.bodyCell.insert({bottom: '<div style="width:100%; margin-top:5px; padding-top: 5px; border-top:1px dotted #ccc;border-bottom:1px dotted #ccc;"><a href="' + link + '" target="_blank">' + file_name + '</a><pre><code>' + content + '</code></pre></div>'}); | |
}).bind(this)); | |
} | |
}, | |
onEmbedDataFailed: function(messageID) { | |
/* No cleanup required, we only alter the HTML after we get back a succesful load from the data */ | |
}, | |
onMessagesInsertedBeforeDisplay: function(messages) { | |
for (var i = 0; i < messages.length; i++) { | |
this.detectGistURL(messages[i]); | |
} | |
}, | |
onMessageAccepted: function(message, messageID) { | |
this.detectGistURL(message); | |
} | |
}); | |
Campfire.Responders.push("GistExpander"); | |
window.chat.installPropaneResponder("GistExpander", "gistexpander"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment