| 
									
										
										
										
											2010-11-16 12:41:35 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * (c) 2010 StatusNet, Inc. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-16 14:27:01 -08:00
										 |  |  | (function() { | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Quickie wrapper around ooembed JSON lookup | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-11-16 14:16:23 -08:00
										 |  |  |     var oEmbed = { | 
					
						
							|  |  |  |         api: 'http://oohembed.com/oohembed', | 
					
						
							| 
									
										
										
										
											2010-11-16 14:27:01 -08:00
										 |  |  |         width: 100, | 
					
						
							|  |  |  |         height: 75, | 
					
						
							| 
									
										
										
										
											2010-11-16 14:16:23 -08:00
										 |  |  |         cache: {}, | 
					
						
							|  |  |  |         callbacks: {}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * Do a cached oEmbed lookup for the given URL. | 
					
						
							|  |  |  |          * | 
					
						
							|  |  |  |          * @param {String} url | 
					
						
							|  |  |  |          * @param {function} callback | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         lookup: function(url, callback) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (typeof oEmbed.cache[url] == "object") { | 
					
						
							|  |  |  |                 // We already have a successful lookup.
 | 
					
						
							|  |  |  |                 callback(oEmbed.cache[url]); | 
					
						
							|  |  |  |             } else if (typeof oEmbed.callbacks[url] == "undefined") { | 
					
						
							|  |  |  |                 // No lookup yet... Start it!
 | 
					
						
							|  |  |  |                 oEmbed.callbacks[url] = [callback]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 oEmbed.rawLookup(url, function(data) { | 
					
						
							|  |  |  |                     oEmbed.cache[url] = data; | 
					
						
							|  |  |  |                     var callbacks = oEmbed.callbacks[url]; | 
					
						
							|  |  |  |                     oEmbed.callbacks[url] = undefined; | 
					
						
							|  |  |  |                     for (var i = 0; i < callbacks.length; i++) { | 
					
						
							|  |  |  |                         callbacks[i](data); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 // A lookup is in progress.
 | 
					
						
							|  |  |  |                 oEmbed.callbacks[url].push(callback); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * Do an oEmbed lookup for the given URL. | 
					
						
							|  |  |  |          * | 
					
						
							|  |  |  |          * @fixme proxy through ourselves if possible? | 
					
						
							|  |  |  |          * @fixme use the global thumbnail size settings | 
					
						
							|  |  |  |          * | 
					
						
							|  |  |  |          * @param {String} url | 
					
						
							|  |  |  |          * @param {function} callback | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         rawLookup: function(url, callback) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             var params = { | 
					
						
							|  |  |  |                 url: url, | 
					
						
							|  |  |  |                 format: 'json', | 
					
						
							| 
									
										
										
										
											2010-11-16 14:27:01 -08:00
										 |  |  |                 maxwidth: oEmbed.width, | 
					
						
							| 
									
										
										
										
											2010-11-16 15:36:53 -08:00
										 |  |  |                 maxheight: oEmbed.height, | 
					
						
							|  |  |  |                 token: $('#token').val() | 
					
						
							| 
									
										
										
										
											2010-11-16 14:16:23 -08:00
										 |  |  |             }; | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |             $.ajax({ | 
					
						
							|  |  |  |                 url: oEmbed.api, | 
					
						
							|  |  |  |                 data: params, | 
					
						
							|  |  |  |                 dataType: 'json', | 
					
						
							|  |  |  |                 success: function(data, xhr) { | 
					
						
							|  |  |  |                     callback(data); | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |                 error: function(xhr, textStatus, errorThrown) { | 
					
						
							|  |  |  |                     callback(null); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2010-11-16 14:16:23 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2010-11-16 13:49:23 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |     var LinkPreview = { | 
					
						
							|  |  |  |         links: [], | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |         state: [], | 
					
						
							|  |  |  |         refresh: [], | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * Find URL links from the source text that may be interesting. | 
					
						
							|  |  |  |          * | 
					
						
							|  |  |  |          * @param {String} text | 
					
						
							|  |  |  |          * @return {Array} list of URLs | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         findLinks: function (text) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // @fixme match this to core code
 | 
					
						
							|  |  |  |             var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg; | 
					
						
							|  |  |  |             var links = []; | 
					
						
							|  |  |  |             var matches; | 
					
						
							|  |  |  |             while ((matches = re.exec(text)) !== null) { | 
					
						
							|  |  |  |                 links.push(matches[1]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return links; | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2010-11-16 14:27:01 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |         /** | 
					
						
							|  |  |  |          * Start looking up info for a link preview... | 
					
						
							|  |  |  |          * May start async data loads. | 
					
						
							|  |  |  |          * | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |          * @param {number} col: column number to insert preview into | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |          */ | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |         prepLinkPreview: function(col) | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |             var id = 'link-preview-' + col; | 
					
						
							|  |  |  |             var url = LinkPreview.links[col]; | 
					
						
							|  |  |  |             LinkPreview.refresh[col] = false; | 
					
						
							|  |  |  |             LinkPreview.markLoading(col); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |             oEmbed.lookup(url, function(data) { | 
					
						
							|  |  |  |                 var thumb = null; | 
					
						
							|  |  |  |                 var width = 100; | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |                 if (data && typeof data.thumbnail_url == "string") { | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |                     thumb = data.thumbnail_url; | 
					
						
							|  |  |  |                     if (typeof data.thumbnail_width !== "undefined") { | 
					
						
							|  |  |  |                         if (data.thumbnail_width < width) { | 
					
						
							|  |  |  |                             width = data.thumbnail_width; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2010-11-16 13:49:23 -08:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |                 } else if (data && data.type == 'photo' && typeof data.url == "string") { | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |                     thumb = data.url; | 
					
						
							|  |  |  |                     if (typeof data.width !== "undefined") { | 
					
						
							|  |  |  |                         if (data.width < width) { | 
					
						
							|  |  |  |                             width = data.width; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2010-11-16 13:49:23 -08:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |                 if (thumb) { | 
					
						
							|  |  |  |                     var link = $('<span class="inline-attachment"><a><img/></a></span>'); | 
					
						
							|  |  |  |                     link.find('a') | 
					
						
							|  |  |  |                             .attr('href', url) | 
					
						
							|  |  |  |                             .attr('target', '_blank') | 
					
						
							|  |  |  |                             .last() | 
					
						
							|  |  |  |                         .find('img') | 
					
						
							|  |  |  |                             .attr('src', thumb) | 
					
						
							|  |  |  |                             .attr('width', width) | 
					
						
							|  |  |  |                             .attr('title', data.title || data.url || url); | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |                     $('#' + id).empty(); | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |                     $('#' + id).append(link); | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |                 } 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); | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2010-11-16 13:16:25 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |         /** | 
					
						
							|  |  |  |          * Update the live preview section with links found in the given text. | 
					
						
							|  |  |  |          * May start async data loads. | 
					
						
							|  |  |  |          * | 
					
						
							|  |  |  |          * @param {String} text: free-form input text | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         previewLinks: function(text) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |             var i; | 
					
						
							| 
									
										
										
										
											2010-11-16 15:20:37 -08:00
										 |  |  |             var old = LinkPreview.links; | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |             var links = LinkPreview.findLinks(text); | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |             LinkPreview.links = links; | 
					
						
							| 
									
										
										
										
											2010-11-16 15:20:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Check for existing common elements...
 | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |             for (i = 0; i < old.length && i < links.length; i++) { | 
					
						
							| 
									
										
										
										
											2010-11-16 15:20:37 -08:00
										 |  |  |                 if (links[i] != old[i]) { | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |                     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); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2010-11-16 15:20:37 -08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (links.length > old.length) { | 
					
						
							|  |  |  |                 // Adding new entries, whee!
 | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |                 for (i = old.length; i < links.length; i++) { | 
					
						
							|  |  |  |                     LinkPreview.addPreviewArea(i); | 
					
						
							|  |  |  |                     LinkPreview.prepLinkPreview(i); | 
					
						
							| 
									
										
										
										
											2010-11-16 15:20:37 -08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } else if (old.length > links.length) { | 
					
						
							|  |  |  |                 // Remove preview entries for links that have been removed.
 | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |                 for (i = links.length; i < old.length; i++) { | 
					
						
							|  |  |  |                     LinkPreview.clearLink(i); | 
					
						
							| 
									
										
										
										
											2010-11-16 15:20:37 -08:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2010-11-16 15:20:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-06 14:49:24 -08:00
										 |  |  |         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'); | 
					
						
							| 
									
										
										
										
											2010-11-16 15:31:03 -08:00
										 |  |  |         }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * Clear out any link preview data. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         clear: function() { | 
					
						
							|  |  |  |             LinkPreview.links = []; | 
					
						
							|  |  |  |             $('#link-preview').empty(); | 
					
						
							| 
									
										
										
										
											2010-11-16 13:16:25 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2010-11-16 14:16:23 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-16 14:27:01 -08:00
										 |  |  |     SN.Init.LinkPreview = function(params) { | 
					
						
							|  |  |  |         if (params.api) oEmbed.api = params.api; | 
					
						
							|  |  |  |         if (params.width) oEmbed.width = params.width; | 
					
						
							|  |  |  |         if (params.height) oEmbed.height = params.height; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-16 15:31:03 -08:00
										 |  |  |         $('#form_notice') | 
					
						
							|  |  |  |             .append('<div id="link-preview" class="thumbnails"></div>') | 
					
						
							|  |  |  |             .bind('reset', function() { | 
					
						
							|  |  |  |                 LinkPreview.clear(); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2010-11-16 14:27:01 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Piggyback on the counter update...
 | 
					
						
							|  |  |  |         var origCounter = SN.U.Counter; | 
					
						
							|  |  |  |         SN.U.Counter = function(form) { | 
					
						
							| 
									
										
										
										
											2010-11-16 14:57:35 -08:00
										 |  |  |             LinkPreview.previewLinks($('#notice_data-text').val()); | 
					
						
							| 
									
										
										
										
											2010-11-16 14:27:01 -08:00
										 |  |  |             return origCounter(form); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-11-16 14:16:23 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-11-16 14:27:01 -08:00
										 |  |  | })(); |