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 api: 'http://oohembed.com/oohembed',
30 * Do a cached oEmbed lookup for the given URL.
33 * @param {function} callback
35 lookup: function(url, callback)
37 if (typeof oEmbed.cache[url] == "object") {
38 // We already have a successful lookup.
39 callback(oEmbed.cache[url]);
40 } else if (typeof oEmbed.callbacks[url] == "undefined") {
41 // No lookup yet... Start it!
42 oEmbed.callbacks[url] = [callback];
44 oEmbed.rawLookup(url, function(data) {
45 oEmbed.cache[url] = data;
46 var callbacks = oEmbed.callbacks[url];
47 oEmbed.callbacks[url] = undefined;
48 for (var i = 0; i < callbacks.length; i++) {
53 // A lookup is in progress.
54 oEmbed.callbacks[url].push(callback);
59 * Do an oEmbed lookup for the given URL.
61 * @fixme proxy through ourselves if possible?
62 * @fixme use the global thumbnail size settings
65 * @param {function} callback
67 rawLookup: function(url, callback)
76 $.get(oEmbed.api, params, function(data, xhr) {
83 * Start looking up info for a link preview...
84 * May start async data loads.
89 function prepLinkPreview(id, url)
91 oEmbed.lookup(url, function(data) {
94 if (typeof data.thumbnail_url == "string") {
95 thumb = data.thumbnail_url;
96 if (typeof data.thumbnail_width !== "undefined") {
97 if (data.thumbnail_width < width) {
98 width = data.thumbnail_width;
101 } else if (data.type == 'photo' && typeof data.url == "string") {
103 if (typeof data.width !== "undefined") {
104 if (data.width < width) {
110 var link = $('<span class="inline-attachment"><a><img/></a></span>');
113 .attr('target', '_blank')
117 .attr('width', width)
118 .attr('title', data.title || data.url || url);
119 $('#' + id).append(link);
125 * Update the live preview section with links found in the given text.
126 * May start async data loads.
128 * @param {String} text: free-form input text
130 function previewLinks(text)
132 var links = findLinks(text);
133 $('#link-preview').html('');
134 for (var i = 0; i < links.length; i++) {
135 var id = 'link-preview-' + i;
136 $('#link-preview').append('<span id="' + id + '"></span>');
137 prepLinkPreview(id, links[i]);
140 $('#form_notice').append('<div id="link-preview" class="thumbnails"></div>');
142 // Piggyback on the counter update...
143 var origCounter = SN.U.Counter;
144 SN.U.Counter = function(form) {
145 previewLinks($('#notice_data-text').val());
146 return origCounter(form);