2 * (c) 2010 StatusNet, Inc.
7 api: 'http://oohembed.com/oohembed',
14 * Do a cached oEmbed lookup for the given URL.
17 * @param {function} callback
19 lookup: function(url, callback)
21 if (typeof oEmbed.cache[url] == "object") {
22 // We already have a successful lookup.
23 callback(oEmbed.cache[url]);
24 } else if (typeof oEmbed.callbacks[url] == "undefined") {
25 // No lookup yet... Start it!
26 oEmbed.callbacks[url] = [callback];
28 oEmbed.rawLookup(url, function(data) {
29 oEmbed.cache[url] = data;
30 var callbacks = oEmbed.callbacks[url];
31 oEmbed.callbacks[url] = undefined;
32 for (var i = 0; i < callbacks.length; i++) {
37 // A lookup is in progress.
38 oEmbed.callbacks[url].push(callback);
43 * Do an oEmbed lookup for the given URL.
45 * @fixme proxy through ourselves if possible?
46 * @fixme use the global thumbnail size settings
49 * @param {function} callback
51 rawLookup: function(url, callback)
56 maxwidth: oEmbed.width,
57 maxheight: oEmbed.height
59 $.get(oEmbed.api, params, function(data, xhr) {
66 * Find URL links from the source text that may be interesting.
68 * @param {String} text
69 * @return {Array} list of URLs
71 function findLinks(text)
73 // @fixme match this to core code
74 var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
77 while ((matches = re.exec(text)) !== null) {
78 links.push(matches[1]);
84 * Start looking up info for a link preview...
85 * May start async data loads.
90 function prepLinkPreview(id, url)
92 oEmbed.lookup(url, function(data) {
95 if (typeof data.thumbnail_url == "string") {
96 thumb = data.thumbnail_url;
97 if (typeof data.thumbnail_width !== "undefined") {
98 if (data.thumbnail_width < width) {
99 width = data.thumbnail_width;
102 } else if (data.type == 'photo' && typeof data.url == "string") {
104 if (typeof data.width !== "undefined") {
105 if (data.width < width) {
111 var link = $('<span class="inline-attachment"><a><img/></a></span>');
114 .attr('target', '_blank')
118 .attr('width', width)
119 .attr('title', data.title || data.url || url);
120 $('#' + id).append(link);
126 * Update the live preview section with links found in the given text.
127 * May start async data loads.
129 * @param {String} text: free-form input text
131 function previewLinks(text)
133 var links = findLinks(text);
134 $('#link-preview').html('');
135 for (var i = 0; i < links.length; i++) {
136 var id = 'link-preview-' + i;
137 $('#link-preview').append('<span id="' + id + '"></span>');
138 prepLinkPreview(id, links[i]);
142 SN.Init.LinkPreview = function(params) {
143 if (params.api) oEmbed.api = params.api;
144 if (params.width) oEmbed.width = params.width;
145 if (params.height) oEmbed.height = params.height;
147 $('#form_notice').append('<div id="link-preview" class="thumbnails"></div>');
149 // Piggyback on the counter update...
150 var origCounter = SN.U.Counter;
151 SN.U.Counter = function(form) {
152 previewLinks($('#notice_data-text').val());
153 return origCounter(form);