2010-11-17 05:41:35 +09:00
|
|
|
/**
|
|
|
|
* (c) 2010 StatusNet, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(function() {
|
2010-11-17 06:16:25 +09:00
|
|
|
/**
|
|
|
|
* Find URL links from the source text that may be interesting.
|
|
|
|
*
|
|
|
|
* @param {String} text
|
|
|
|
* @return {Array} list of URLs
|
|
|
|
*/
|
|
|
|
function findLinks(text)
|
|
|
|
{
|
|
|
|
// @fixme match this to core code
|
2010-11-17 06:49:23 +09:00
|
|
|
var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
|
2010-11-17 06:16:25 +09:00
|
|
|
var links = [];
|
|
|
|
var matches;
|
|
|
|
while ((matches = re.exec(text)) !== null) {
|
|
|
|
links.push(matches[1]);
|
|
|
|
}
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
2010-11-17 07:16:23 +09:00
|
|
|
var oEmbed = {
|
|
|
|
api: 'http://oohembed.com/oohembed',
|
|
|
|
cache: {},
|
|
|
|
callbacks: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do a cached oEmbed lookup for the given URL.
|
|
|
|
*
|
|
|
|
* @param {String} url
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
|
|
|
lookup: function(url, callback)
|
|
|
|
{
|
|
|
|
if (typeof oEmbed.cache[url] == "object") {
|
|
|
|
// We already have a successful lookup.
|
|
|
|
callback(oEmbed.cache[url]);
|
|
|
|
} else if (typeof oEmbed.callbacks[url] == "undefined") {
|
|
|
|
// No lookup yet... Start it!
|
|
|
|
oEmbed.callbacks[url] = [callback];
|
|
|
|
|
|
|
|
oEmbed.rawLookup(url, function(data) {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
};
|
2010-11-17 06:49:23 +09:00
|
|
|
|
2010-11-17 06:16:25 +09:00
|
|
|
/**
|
|
|
|
* Start looking up info for a link preview...
|
|
|
|
* May start async data loads.
|
|
|
|
*
|
|
|
|
* @param {String} id
|
|
|
|
* @param {String} url
|
|
|
|
*/
|
|
|
|
function prepLinkPreview(id, url)
|
|
|
|
{
|
2010-11-17 07:16:23 +09:00
|
|
|
oEmbed.lookup(url, function(data) {
|
2010-11-17 06:49:23 +09:00
|
|
|
var thumb = null;
|
|
|
|
var width = 100;
|
|
|
|
if (typeof data.thumbnail_url == "string") {
|
|
|
|
thumb = data.thumbnail_url;
|
|
|
|
if (typeof data.thumbnail_width !== "undefined") {
|
|
|
|
if (data.thumbnail_width < width) {
|
|
|
|
width = data.thumbnail_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (data.type == 'photo' && typeof data.url == "string") {
|
|
|
|
thumb = data.url;
|
|
|
|
if (typeof data.width !== "undefined") {
|
|
|
|
if (data.width < width) {
|
|
|
|
width = data.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (thumb) {
|
2010-11-17 07:16:23 +09:00
|
|
|
var link = $('<span class="inline-attachment"><a><img/></a></span>');
|
|
|
|
link.find('a')
|
|
|
|
.attr('href', url)
|
|
|
|
.attr('target', '_blank')
|
|
|
|
.last()
|
2010-11-17 06:58:22 +09:00
|
|
|
.find('img')
|
|
|
|
.attr('src', thumb)
|
|
|
|
.attr('width', width)
|
|
|
|
.attr('title', data.title || data.url || url);
|
|
|
|
$('#' + id).append(link);
|
2010-11-17 06:49:23 +09:00
|
|
|
}
|
|
|
|
});
|
2010-11-17 06:16:25 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the live preview section with links found in the given text.
|
|
|
|
* May start async data loads.
|
|
|
|
*
|
|
|
|
* @param {String} text: free-form input text
|
|
|
|
*/
|
|
|
|
function previewLinks(text)
|
|
|
|
{
|
|
|
|
var links = findLinks(text);
|
2010-11-17 06:49:23 +09:00
|
|
|
$('#link-preview').html('');
|
2010-11-17 06:16:25 +09:00
|
|
|
for (var i = 0; i < links.length; i++) {
|
|
|
|
var id = 'link-preview-' + i;
|
2010-11-17 06:49:23 +09:00
|
|
|
$('#link-preview').append('<span id="' + id + '"></span>');
|
2010-11-17 06:16:25 +09:00
|
|
|
prepLinkPreview(id, links[i]);
|
|
|
|
}
|
|
|
|
}
|
2010-11-17 07:16:23 +09:00
|
|
|
$('#form_notice').append('<div id="link-preview" class="thumbnails"></div>');
|
|
|
|
|
|
|
|
// Piggyback on the counter update...
|
|
|
|
var origCounter = SN.U.Counter;
|
|
|
|
SN.U.Counter = function(form) {
|
|
|
|
previewLinks($('#notice_data-text').val());
|
|
|
|
return origCounter(form);
|
|
|
|
}
|
2010-11-17 05:41:35 +09:00
|
|
|
});
|