LinkPreview: restructure a bit so we can pass config over

This commit is contained in:
Brion Vibber 2010-11-16 14:27:01 -08:00
parent eeb7f02b98
commit b5fc71253c
2 changed files with 43 additions and 28 deletions

View File

@ -52,6 +52,12 @@ class LinkPreviewPlugin extends Plugin
$user = common_current_user(); $user = common_current_user();
if ($user) { if ($user) {
$action->script('plugins/LinkPreview/linkpreview.js'); $action->script('plugins/LinkPreview/linkpreview.js');
$data = json_encode(array(
'api' => common_config('oohembed', 'endpoint'),
'width' => common_config('attachments', 'thumbwidth'),
'height' => common_config('attachments', 'thumbheight'),
));
$action->inlineScript('$(function() {SN.Init.LinkPreview && SN.Init.LinkPreview('.$data.');})');
} }
return true; return true;
} }

View File

@ -2,27 +2,11 @@
* (c) 2010 StatusNet, Inc. * (c) 2010 StatusNet, Inc.
*/ */
$(function() { (function() {
/**
* 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
var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
var links = [];
var matches;
while ((matches = re.exec(text)) !== null) {
links.push(matches[1]);
}
return links;
}
var oEmbed = { var oEmbed = {
api: 'http://oohembed.com/oohembed', api: 'http://oohembed.com/oohembed',
width: 100,
height: 75,
cache: {}, cache: {},
callbacks: {}, callbacks: {},
@ -69,8 +53,8 @@ $(function() {
var params = { var params = {
url: url, url: url,
format: 'json', format: 'json',
maxwidth: 100, maxwidth: oEmbed.width,
maxheight: 75, maxheight: oEmbed.height,
callback: '?' callback: '?'
}; };
$.get(oEmbed.api, params, function(data, xhr) { $.get(oEmbed.api, params, function(data, xhr) {
@ -79,6 +63,24 @@ $(function() {
} }
}; };
/**
* 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
var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
var links = [];
var matches;
while ((matches = re.exec(text)) !== null) {
links.push(matches[1]);
}
return links;
}
/** /**
* Start looking up info for a link preview... * Start looking up info for a link preview...
* May start async data loads. * May start async data loads.
@ -137,12 +139,19 @@ $(function() {
prepLinkPreview(id, links[i]); prepLinkPreview(id, links[i]);
} }
} }
$('#form_notice').append('<div id="link-preview" class="thumbnails"></div>');
// Piggyback on the counter update... SN.Init.LinkPreview = function(params) {
var origCounter = SN.U.Counter; if (params.api) oEmbed.api = params.api;
SN.U.Counter = function(form) { if (params.width) oEmbed.width = params.width;
previewLinks($('#notice_data-text').val()); if (params.height) oEmbed.height = params.height;
return origCounter(form);
$('#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);
}
} }
}); })();