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) {
77 SN.Init.LinkPreview = function(params) {
78 if (params.api) oEmbed.api = params.api;
79 if (params.width) oEmbed.width = params.width;
80 if (params.height) oEmbed.height = params.height;
83 // Piggyback on the counter update...
84 var origCounter = SN.U.Counter;
85 SN.U.Counter = function(form) {
86 var preview = form.data('LinkPreview');
88 preview.previewLinks(form.find('.notice_data-text:first').val());
90 return origCounter(form);
93 // Customize notice form init...
94 var origSetup = SN.Init.NoticeFormSetup;
95 SN.Init.NoticeFormSetup = function(form) {
99 .bind('reset', function() {
109 * Find URL links from the source text that may be interesting.
111 * @param {String} text
112 * @return {Array} list of URLs
114 findLinks: function (text)
116 // @fixme match this to core code
117 var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
120 while ((matches = re.exec(text)) !== null) {
121 links.push(matches[1]);
126 ensureArea: function() {
127 if (form.find('.link-preview').length < 1) {
128 form.append('<div class="notice-status link-preview thumbnails"></div>');
133 * Start looking up info for a link preview...
134 * May start async data loads.
136 * @param {number} col: column number to insert preview into
138 prepLinkPreview: function(col)
140 var id = 'link-preview-' + col;
141 var url = LinkPreview.links[col];
142 LinkPreview.refresh[col] = false;
143 LinkPreview.markLoading(col);
145 oEmbed.lookup(url, function(data) {
148 if (data && typeof data.thumbnail_url == "string") {
149 thumb = data.thumbnail_url;
150 if (typeof data.thumbnail_width !== "undefined") {
151 if (data.thumbnail_width < width) {
152 width = data.thumbnail_width;
155 } else if (data && data.type == 'photo' && typeof data.url == "string") {
157 if (typeof data.width !== "undefined") {
158 if (data.width < width) {
165 LinkPreview.ensureArea();
166 var link = $('<span class="inline-attachment"><a><img/></a></span>');
169 .attr('target', '_blank')
173 .attr('width', width)
174 .attr('title', data.title || data.url || url);
179 // No thumbnail available or error retriving it.
180 LinkPreview.clearLink(col);
183 if (LinkPreview.refresh[col]) {
184 // Darn user has typed more characters.
185 // Go fetch another link!
186 LinkPreview.prepLinkPreview(col);
188 LinkPreview.markDone(col);
194 * Update the live preview section with links found in the given text.
195 * May start async data loads.
197 * @param {String} text: free-form input text
199 previewLinks: function(text)
202 var old = LinkPreview.links;
203 var links = LinkPreview.findLinks(text);
204 LinkPreview.links = links;
206 // Check for existing common elements...
207 for (i = 0; i < old.length && i < links.length; i++) {
208 if (links[i] != old[i]) {
209 if (LinkPreview.state[i] == "loading") {
210 // Slate this column for a refresh when this one's done.
211 LinkPreview.refresh[i] = true;
213 // Change an existing entry!
214 LinkPreview.prepLinkPreview(i);
218 if (links.length > old.length) {
219 // Adding new entries, whee!
220 for (i = old.length; i < links.length; i++) {
221 LinkPreview.addPreviewArea(i);
222 LinkPreview.prepLinkPreview(i);
224 } else if (old.length > links.length) {
225 // Remove preview entries for links that have been removed.
226 for (i = links.length; i < old.length; i++) {
227 LinkPreview.clearLink(i);
230 if (links.length == 0) {
235 addPreviewArea: function(col) {
236 LinkPreview.ensureArea();
237 var id = 'link-preview-' + col;
238 if (form.find('.' + id).length < 1) {
239 form.find('.link-preview').append('<span class="' + id + '"></span>');
243 clearLink: function(col) {
244 var id = 'link-preview-' + col;
245 form.find('.' + id).html('');
248 markLoading: function(col) {
249 LinkPreview.state[col] = "loading";
250 var id = 'link-preview-' + col;
251 form.find('.' + id).attr('style', 'opacity: 0.5');
254 markDone: function(col) {
255 LinkPreview.state[col] = "done";
256 var id = 'link-preview-' + col;
257 form.find('.' + id).removeAttr('style');
261 * Clear out any link preview data.
264 LinkPreview.links = [];
265 form.find('.link-preview').remove();
268 form.data('LinkPreview', LinkPreview);