2 * (c) 2010 StatusNet, Inc.
7 * Quickie wrapper around ooembed JSON lookup
10 api: 'http://oohembed.com/oohembed',
17 * Do a cached oEmbed lookup for the given URL.
20 * @param {function} callback
22 lookup: function(url, callback)
24 if (typeof oEmbed.cache[url] == "object") {
25 // We already have a successful lookup.
26 callback(oEmbed.cache[url]);
27 } else if (typeof oEmbed.callbacks[url] == "undefined") {
28 // No lookup yet... Start it!
29 oEmbed.callbacks[url] = [callback];
31 oEmbed.rawLookup(url, function(data) {
32 oEmbed.cache[url] = data;
33 var callbacks = oEmbed.callbacks[url];
34 oEmbed.callbacks[url] = undefined;
35 for (var i = 0; i < callbacks.length; i++) {
40 // A lookup is in progress.
41 oEmbed.callbacks[url].push(callback);
46 * Do an oEmbed lookup for the given URL.
48 * @fixme proxy through ourselves if possible?
49 * @fixme use the global thumbnail size settings
52 * @param {function} callback
54 rawLookup: function(url, callback)
59 maxwidth: oEmbed.width,
60 maxheight: oEmbed.height,
61 token: $('#token').val()
67 success: function(data, xhr) {
70 error: function(xhr, textStatus, errorThrown) {
83 * Find URL links from the source text that may be interesting.
85 * @param {String} text
86 * @return {Array} list of URLs
88 findLinks: function (text)
90 // @fixme match this to core code
91 var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
94 while ((matches = re.exec(text)) !== null) {
95 links.push(matches[1]);
101 * Start looking up info for a link preview...
102 * May start async data loads.
104 * @param {number} col: column number to insert preview into
106 prepLinkPreview: function(col)
108 var id = 'link-preview-' + col;
109 var url = LinkPreview.links[col];
110 LinkPreview.refresh[col] = false;
111 LinkPreview.markLoading(col);
113 oEmbed.lookup(url, function(data) {
116 if (data && typeof data.thumbnail_url == "string") {
117 thumb = data.thumbnail_url;
118 if (typeof data.thumbnail_width !== "undefined") {
119 if (data.thumbnail_width < width) {
120 width = data.thumbnail_width;
123 } else if (data && data.type == 'photo' && typeof data.url == "string") {
125 if (typeof data.width !== "undefined") {
126 if (data.width < width) {
133 var link = $('<span class="inline-attachment"><a><img/></a></span>');
136 .attr('target', '_blank')
140 .attr('width', width)
141 .attr('title', data.title || data.url || url);
143 $('#' + id).append(link);
145 // No thumbnail available or error retriving it.
146 LinkPreview.clearLink(col);
149 if (LinkPreview.refresh[col]) {
150 // Darn user has typed more characters.
151 // Go fetch another link!
152 LinkPreview.prepLinkPreview(col);
154 LinkPreview.markDone(col);
160 * Update the live preview section with links found in the given text.
161 * May start async data loads.
163 * @param {String} text: free-form input text
165 previewLinks: function(text)
168 var old = LinkPreview.links;
169 var links = LinkPreview.findLinks(text);
170 LinkPreview.links = links;
172 // Check for existing common elements...
173 for (i = 0; i < old.length && i < links.length; i++) {
174 if (links[i] != old[i]) {
175 if (LinkPreview.state[i] == "loading") {
176 // Slate this column for a refresh when this one's done.
177 LinkPreview.refresh[i] = true;
179 // Change an existing entry!
180 LinkPreview.prepLinkPreview(i);
184 if (links.length > old.length) {
185 // Adding new entries, whee!
186 for (i = old.length; i < links.length; i++) {
187 LinkPreview.addPreviewArea(i);
188 LinkPreview.prepLinkPreview(i);
190 } else if (old.length > links.length) {
191 // Remove preview entries for links that have been removed.
192 for (i = links.length; i < old.length; i++) {
193 LinkPreview.clearLink(i);
198 addPreviewArea: function(col) {
199 var id = 'link-preview-' + col;
200 $('#link-preview').append('<span id="' + id + '"></span>');
203 clearLink: function(col) {
204 var id = 'link-preview-' + col;
205 $('#' + id).html('');
208 markLoading: function(col) {
209 LinkPreview.state[col] = "loading";
210 var id = 'link-preview-' + col;
211 $('#' + id).attr('style', 'opacity: 0.5');
214 markDone: function(col) {
215 LinkPreview.state[col] = "done";
216 var id = 'link-preview-' + col;
217 $('#' + id).removeAttr('style');
221 * Clear out any link preview data.
224 LinkPreview.links = [];
225 $('#link-preview').empty();
229 SN.Init.LinkPreview = function(params) {
230 if (params.api) oEmbed.api = params.api;
231 if (params.width) oEmbed.width = params.width;
232 if (params.height) oEmbed.height = params.height;
235 .append('<div id="link-preview" class="thumbnails"></div>')
236 .bind('reset', function() {
240 // Piggyback on the counter update...
241 var origCounter = SN.U.Counter;
242 SN.U.Counter = function(form) {
243 LinkPreview.previewLinks($('#notice_data-text').val());
244 return origCounter(form);