Created
April 29, 2011 17:34
-
-
Save protocool/948666 to your computer and use it in GitHub Desktop.
A gist that shows how I'm embedding gists into Propane. Probably crappy but at least it feels a bit meta...
This file contains hidden or 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
if (displayExpandedGists) { | |
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\/([0-9]+)\/?$/); | |
if (!match) return; | |
href = 'https://api.github.com/gists/' + match[1]; | |
window.propane.requestJSON(message.id(), href, 'window.chat.gistexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed'); | |
} | |
}, | |
onEmbedDataLoaded: function(messageID, data) { | |
var message = window.chat.transcript.getMessageById(messageID); | |
if (!message) return; | |
var payload = ''; | |
var description = data['description']; | |
if (description) { | |
payload += '<strong>Gist:</strong> ' + description + '<br />'; | |
} | |
var files = data['files']; | |
if (files) { | |
payload += '<strong>Files:</strong> '; | |
for (var fileKey in files) { | |
var file = files[fileKey]; | |
payload += '<a target="_blank" href="' + file['raw_url'] + '">' + fileKey + '</a> '; | |
} | |
} | |
message.resize((function() { | |
message.bodyCell.insert({bottom: '<div style="width:100%; margin-top:5px; padding-top: 5px; border-top:1px dotted #ccc;">' + payload + '</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