LinkPreview: piggyback on the counter update logic, cache lookups.

This commit is contained in:
Brion Vibber 2010-11-16 14:16:23 -08:00
parent f103a55052
commit eeb7f02b98

View File

@ -21,29 +21,63 @@ $(function() {
return links; return links;
} }
/** var oEmbed = {
* Do an oEmbed lookup for the given URL. api: 'http://oohembed.com/oohembed',
* cache: {},
* @fixme proxy through ourselves if possible? callbacks: {},
* @fixme use the global thumbnail size settings
* /**
* @param {String} url * Do a cached oEmbed lookup for the given URL.
* @param {function} callback *
*/ * @param {String} url
function oEmbedLookup(url, callback) * @param {function} callback
{ */
var api = 'http://oohembed.com/oohembed'; lookup: function(url, callback)
var params = { {
url: url, if (typeof oEmbed.cache[url] == "object") {
format: 'json', // We already have a successful lookup.
maxwidth: 100, callback(oEmbed.cache[url]);
maxheight: 75, } else if (typeof oEmbed.callbacks[url] == "undefined") {
callback: '?' // No lookup yet... Start it!
}; oEmbed.callbacks[url] = [callback];
$.get(api, params, function(data, xhr) {
callback(data); oEmbed.rawLookup(url, function(data) {
}, 'jsonp'); oEmbed.cache[url] = data;
} var callbacks = oEmbed.callbacks[url];
oEmbed.callbacks[url] = undefined;
for (var i = 0; i < callbacks.length; i++) {
callbacks[i](data);
}
});
} else {
// A lookup is in progress.
oEmbed.callbacks[url].push(callback);
}
},
/**
* Do an oEmbed lookup for the given URL.
*
* @fixme proxy through ourselves if possible?
* @fixme use the global thumbnail size settings
*
* @param {String} url
* @param {function} callback
*/
rawLookup: function(url, callback)
{
var params = {
url: url,
format: 'json',
maxwidth: 100,
maxheight: 75,
callback: '?'
};
$.get(oEmbed.api, params, function(data, xhr) {
callback(data);
}, 'jsonp');
}
};
/** /**
* Start looking up info for a link preview... * Start looking up info for a link preview...
@ -54,7 +88,7 @@ $(function() {
*/ */
function prepLinkPreview(id, url) function prepLinkPreview(id, url)
{ {
oEmbedLookup(url, function(data) { oEmbed.lookup(url, function(data) {
var thumb = null; var thumb = null;
var width = 100; var width = 100;
if (typeof data.thumbnail_url == "string") { if (typeof data.thumbnail_url == "string") {
@ -73,9 +107,11 @@ $(function() {
} }
} }
if (thumb) { if (thumb) {
var link = $('<a><img/></a>'); var link = $('<span class="inline-attachment"><a><img/></a></span>');
link.attr('href', url) link.find('a')
.attr('target', '_blank') .attr('href', url)
.attr('target', '_blank')
.last()
.find('img') .find('img')
.attr('src', thumb) .attr('src', thumb)
.attr('width', width) .attr('width', width)
@ -101,9 +137,12 @@ $(function() {
prepLinkPreview(id, links[i]); prepLinkPreview(id, links[i]);
} }
} }
$('#form_notice').append('<div id="link-preview"></div>'); $('#form_notice').append('<div id="link-preview" class="thumbnails"></div>');
$('#notice_data-text').change(function() {
var text = $(this).val(); // Piggyback on the counter update...
previewLinks(text); var origCounter = SN.U.Counter;
}); SN.U.Counter = function(form) {
previewLinks($('#notice_data-text').val());
return origCounter(form);
}
}); });