diff options
author | Brion Vibber <brion@pobox.com> | 2010-12-06 15:14:47 -0800 |
---|---|---|
committer | Brion Vibber <brion@pobox.com> | 2010-12-06 15:14:47 -0800 |
commit | 2e27cbcadd16eca075e363fa154682cba09a9849 (patch) | |
tree | 8311827e7308ba54338b0f07e7836b156cb3e1c4 | |
parent | 1fb506c27d8d3cd076d0cab8d6b9493cc131e730 (diff) | |
parent | c40fde900a6b966752fb56eb8b23cf4c983fe349 (diff) |
Merge branch 'linkpreview-fix' into 0.9.x
-rw-r--r-- | plugins/LinkPreview/LinkPreviewPlugin.php | 2 | ||||
-rw-r--r-- | plugins/LinkPreview/linkpreview.js | 95 | ||||
-rw-r--r-- | plugins/LinkPreview/linkpreview.min.js | 1 | ||||
-rw-r--r-- | plugins/LinkPreview/oembedproxyaction.php | 3 |
4 files changed, 79 insertions, 22 deletions
diff --git a/plugins/LinkPreview/LinkPreviewPlugin.php b/plugins/LinkPreview/LinkPreviewPlugin.php index 39d2c9bf3..65f896ca2 100644 --- a/plugins/LinkPreview/LinkPreviewPlugin.php +++ b/plugins/LinkPreview/LinkPreviewPlugin.php @@ -51,7 +51,7 @@ class LinkPreviewPlugin extends Plugin { $user = common_current_user(); if ($user && common_config('attachments', 'process_links')) { - $action->script('plugins/LinkPreview/linkpreview.js'); + $action->script('plugins/LinkPreview/linkpreview.min.js'); $data = json_encode(array( 'api' => common_local_url('oembedproxy'), 'width' => common_config('attachments', 'thumbwidth'), diff --git a/plugins/LinkPreview/linkpreview.js b/plugins/LinkPreview/linkpreview.js index 0c0eb734e..407934c5a 100644 --- a/plugins/LinkPreview/linkpreview.js +++ b/plugins/LinkPreview/linkpreview.js @@ -3,6 +3,9 @@ */ (function() { + /** + * Quickie wrapper around ooembed JSON lookup + */ var oEmbed = { api: 'http://oohembed.com/oohembed', width: 100, @@ -57,14 +60,24 @@ maxheight: oEmbed.height, token: $('#token').val() }; - $.get(oEmbed.api, params, function(data, xhr) { - callback(data); - }, 'json'); + $.ajax({ + url: oEmbed.api, + data: params, + dataType: 'json', + success: function(data, xhr) { + callback(data); + }, + error: function(xhr, textStatus, errorThrown) { + callback(null); + } + }); } }; var LinkPreview = { links: [], + state: [], + refresh: [], /** * Find URL links from the source text that may be interesting. @@ -88,22 +101,26 @@ * Start looking up info for a link preview... * May start async data loads. * - * @param {String} id - * @param {String} url + * @param {number} col: column number to insert preview into */ - prepLinkPreview: function(id, url) + prepLinkPreview: function(col) { + var id = 'link-preview-' + col; + var url = LinkPreview.links[col]; + LinkPreview.refresh[col] = false; + LinkPreview.markLoading(col); + oEmbed.lookup(url, function(data) { var thumb = null; var width = 100; - if (typeof data.thumbnail_url == "string") { + if (data && 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") { + } else if (data && data.type == 'photo' && typeof data.url == "string") { thumb = data.url; if (typeof data.width !== "undefined") { if (data.width < width) { @@ -111,6 +128,7 @@ } } } + if (thumb) { var link = $('<span class="inline-attachment"><a><img/></a></span>'); link.find('a') @@ -121,7 +139,19 @@ .attr('src', thumb) .attr('width', width) .attr('title', data.title || data.url || url); + $('#' + id).empty(); $('#' + id).append(link); + } else { + // No thumbnail available or error retriving it. + LinkPreview.clearLink(col); + } + + if (LinkPreview.refresh[col]) { + // Darn user has typed more characters. + // Go fetch another link! + LinkPreview.prepLinkPreview(col); + } else { + LinkPreview.markDone(col); } }); }, @@ -134,34 +164,57 @@ */ previewLinks: function(text) { + var i; var old = LinkPreview.links; var links = LinkPreview.findLinks(text); + LinkPreview.links = links; // Check for existing common elements... - for (var i = 0; i < old.length && i < links.length; i++) { + for (i = 0; i < old.length && i < links.length; i++) { if (links[i] != old[i]) { - // Change an existing entry! - var id = 'link-preview-' + i; - $('#' + id).html(''); - LinkPreview.prepLinkPreview(id, links[i]); + if (LinkPreview.state[i] == "loading") { + // Slate this column for a refresh when this one's done. + LinkPreview.refresh[i] = true; + } else { + // Change an existing entry! + LinkPreview.prepLinkPreview(i); + } } } if (links.length > old.length) { // Adding new entries, whee! - for (var i = old.length; i < links.length; i++) { - var id = 'link-preview-' + i; - $('#link-preview').append('<span id="' + id + '"></span>'); - LinkPreview.prepLinkPreview(id, links[i]); + for (i = old.length; i < links.length; i++) { + LinkPreview.addPreviewArea(i); + LinkPreview.prepLinkPreview(i); } } else if (old.length > links.length) { // Remove preview entries for links that have been removed. - for (var i = links.length; i < old.length; i++) { - var id = 'link-preview-' + i; - $('#' + id).remove(); + for (i = links.length; i < old.length; i++) { + LinkPreview.clearLink(i); } } + }, - LinkPreview.links = links; + addPreviewArea: function(col) { + var id = 'link-preview-' + col; + $('#link-preview').append('<span id="' + id + '"></span>'); + }, + + clearLink: function(col) { + var id = 'link-preview-' + col; + $('#' + id).html(''); + }, + + markLoading: function(col) { + LinkPreview.state[col] = "loading"; + var id = 'link-preview-' + col; + $('#' + id).attr('style', 'opacity: 0.5'); + }, + + markDone: function(col) { + LinkPreview.state[col] = "done"; + var id = 'link-preview-' + col; + $('#' + id).removeAttr('style'); }, /** diff --git a/plugins/LinkPreview/linkpreview.min.js b/plugins/LinkPreview/linkpreview.min.js new file mode 100644 index 000000000..a6fb9dba8 --- /dev/null +++ b/plugins/LinkPreview/linkpreview.min.js @@ -0,0 +1 @@ +(function(){var a={api:"http://oohembed.com/oohembed",width:100,height:75,cache:{},callbacks:{},lookup:function(c,d){if(typeof a.cache[c]=="object"){d(a.cache[c])}else{if(typeof a.callbacks[c]=="undefined"){a.callbacks[c]=[d];a.rawLookup(c,function(g){a.cache[c]=g;var f=a.callbacks[c];a.callbacks[c]=undefined;for(var e=0;e<f.length;e++){f[e](g)}})}else{a.callbacks[c].push(d)}}},rawLookup:function(c,e){var d={url:c,format:"json",maxwidth:a.width,maxheight:a.height,token:$("#token").val()};$.ajax({url:a.api,data:d,dataType:"json",success:function(f,g){e(f)},error:function(g,h,f){e(null)}})}};var b={links:[],state:[],refresh:[],findLinks:function(f){var d=/(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;var c=[];var e;while((e=d.exec(f))!==null){c.push(e[1])}return c},prepLinkPreview:function(d){var e="link-preview-"+d;var c=b.links[d];b.refresh[d]=false;b.markLoading(d);a.lookup(c,function(i){var f=null;var g=100;if(i&&typeof i.thumbnail_url=="string"){f=i.thumbnail_url;if(typeof i.thumbnail_width!=="undefined"){if(i.thumbnail_width<g){g=i.thumbnail_width}}}else{if(i&&i.type=="photo"&&typeof i.url=="string"){f=i.url;if(typeof i.width!=="undefined"){if(i.width<g){g=i.width}}}}if(f){var h=$('<span class="inline-attachment"><a><img/></a></span>');h.find("a").attr("href",c).attr("target","_blank").last().find("img").attr("src",f).attr("width",g).attr("title",i.title||i.url||c);$("#"+e).empty();$("#"+e).append(h)}else{b.clearLink(d)}if(b.refresh[d]){b.prepLinkPreview(d)}else{b.markDone(d)}})},previewLinks:function(f){var e;var c=b.links;var d=b.findLinks(f);b.links=d;for(e=0;e<c.length&&e<d.length;e++){if(d[e]!=c[e]){if(b.state[e]=="loading"){b.refresh[e]=true}else{b.prepLinkPreview(e)}}}if(d.length>c.length){for(e=c.length;e<d.length;e++){b.addPreviewArea(e);b.prepLinkPreview(e)}}else{if(c.length>d.length){for(e=d.length;e<c.length;e++){b.clearLink(e)}}}},addPreviewArea:function(c){var d="link-preview-"+c;$("#link-preview").append('<span id="'+d+'"></span>')},clearLink:function(c){var d="link-preview-"+c;$("#"+d).html("")},markLoading:function(c){b.state[c]="loading";var d="link-preview-"+c;$("#"+d).attr("style","opacity: 0.5")},markDone:function(c){b.state[c]="done";var d="link-preview-"+c;$("#"+d).removeAttr("style")},clear:function(){b.links=[];$("#link-preview").empty()}};SN.Init.LinkPreview=function(c){if(c.api){a.api=c.api}if(c.width){a.width=c.width}if(c.height){a.height=c.height}$("#form_notice").append('<div id="link-preview" class="thumbnails"></div>').bind("reset",function(){b.clear()});var d=SN.U.Counter;SN.U.Counter=function(e){b.previewLinks($("#notice_data-text").val());return d(e)}}})();
\ No newline at end of file diff --git a/plugins/LinkPreview/oembedproxyaction.php b/plugins/LinkPreview/oembedproxyaction.php index 470f78073..bc80ee5cf 100644 --- a/plugins/LinkPreview/oembedproxyaction.php +++ b/plugins/LinkPreview/oembedproxyaction.php @@ -50,6 +50,9 @@ class OembedproxyAction extends OembedAction function handle($args) { + // Trigger short error responses; not a human-readable web page. + StatusNet::setApi(true); + // We're not a general oEmbed proxy service; limit to valid sessions. $token = $this->trimmed('token'); if (!$token || $token != common_session_token()) { |