Skip to content

Instantly share code, notes, and snippets.

@pjaspers
Created September 30, 2013 10:30
Show Gist options
  • Save pjaspers/6761954 to your computer and use it in GitHub Desktop.
Save pjaspers/6761954 to your computer and use it in GitHub Desktop.
Propane helper to display cloudup images inline
/*
Display CloudUp images inline.
*/
Campfire.CloudUpExpander = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for (var i = 0; i < messages.length; i++) {
this.detectCloudUpURL(messages[i]);
}
},
detectCloudUpURL: 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') {
console.log("Detecting links");
var links = message.bodyElement().select('a:not(image)');
if (links.length != 1) {
return;
}
// Despite what their docs says, just point to their domain
var baseURL = "https://cloudup.com";
var link = links[0].getAttribute('href');
console.log("Found a link: ." + link + ".");
var match = link.match(/^https?:\/\/(cloudup.com\/)?[A-Za-z0-9]+\/?$/);
if (match) {
var urlToFetch = baseURL + "/oembed?url=" + link;
console.log("Fetching " + urlToFetch);
window.propane.requestJSON(message.id(), urlToFetch, 'window.chat.cloudupexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed');
}
}
},
// https://dev.cloudup.com
onEmbedDataLoaded: function(messageID, data) {
var message = window.chat.transcript.getMessageById(messageID);
if (!message) return;
var imageURL = data['thumbnail_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(&quot;inlineImageLoaded&quot;, this)" onerror="$dispatch(&quot;inlineImageLoadFailed&quot;, 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.detectCloudUpURL(messages[i]);
}
},
onMessageAccepted: function(message, messageID) {
this.detectCloudUpURL(message);
}
});
Campfire.Responders.push("CloudUpExpander");
window.chat.installPropaneResponder("CloudUpExpander", "cloudupexpander");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment