2 * (c) 2010 StatusNet, Inc.
7 * Find URL links from the source text that may be interesting.
10 * @return {Array} list of URLs
12 function findLinks(text)
14 // @fixme match this to core code
15 var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
18 while ((matches = re.exec(text)) !== null) {
19 links.push(matches[1]);
25 * Do an oEmbed lookup for the given URL.
27 * @fixme proxy through ourselves if possible?
28 * @fixme use the global thumbnail size settings
31 * @param {function} callback
33 function oEmbedLookup(url, callback)
35 var api = 'http://oohembed.com/oohembed';
43 $.get(api, params, function(data, xhr) {
49 * Start looking up info for a link preview...
50 * May start async data loads.
55 function prepLinkPreview(id, url)
57 oEmbedLookup(url, function(data) {
60 if (typeof data.thumbnail_url == "string") {
61 thumb = data.thumbnail_url;
62 if (typeof data.thumbnail_width !== "undefined") {
63 if (data.thumbnail_width < width) {
64 width = data.thumbnail_width;
67 } else if (data.type == 'photo' && typeof data.url == "string") {
69 if (typeof data.width !== "undefined") {
70 if (data.width < width) {
79 .attr('title', data.title || data.url || url);
80 $('#' + id).append(img);
86 * Update the live preview section with links found in the given text.
87 * May start async data loads.
89 * @param {String} text: free-form input text
91 function previewLinks(text)
93 var links = findLinks(text);
94 $('#link-preview').html('');
95 for (var i = 0; i < links.length; i++) {
96 var id = 'link-preview-' + i;
97 $('#link-preview').append('<span id="' + id + '"></span>');
98 prepLinkPreview(id, links[i]);
101 $('#form_notice').append('<div id="link-preview"></div>');
102 $('#notice_data-text').change(function() {
103 var text = $(this).val();