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,
60 $.get(oEmbed.api, params, function(data, xhr) {
67 * Find URL links from the source text that may be interesting.
69 * @param {String} text
70 * @return {Array} list of URLs
72 function findLinks(text)
74 // @fixme match this to core code
75 var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
78 while ((matches = re.exec(text)) !== null) {
79 links.push(matches[1]);
85 * Start looking up info for a link preview...
86 * May start async data loads.
91 function prepLinkPreview(id, url)
93 oEmbed.lookup(url, function(data) {
96 if (typeof data.thumbnail_url == "string") {
97 thumb = data.thumbnail_url;
98 if (typeof data.thumbnail_width !== "undefined") {
99 if (data.thumbnail_width < width) {
100 width = data.thumbnail_width;
103 } else if (data.type == 'photo' && typeof data.url == "string") {
105 if (typeof data.width !== "undefined") {
106 if (data.width < width) {
112 var link = $('<span class="inline-attachment"><a><img/></a></span>');
115 .attr('target', '_blank')
119 .attr('width', width)
120 .attr('title', data.title || data.url || url);
121 $('#' + id).append(link);
127 * Update the live preview section with links found in the given text.
128 * May start async data loads.
130 * @param {String} text: free-form input text
132 function previewLinks(text)
134 var links = findLinks(text);
135 $('#link-preview').html('');
136 for (var i = 0; i < links.length; i++) {
137 var id = 'link-preview-' + i;
138 $('#link-preview').append('<span id="' + id + '"></span>');
139 prepLinkPreview(id, links[i]);
143 SN.Init.LinkPreview = function(params) {
144 if (params.api) oEmbed.api = params.api;
145 if (params.width) oEmbed.width = params.width;
146 if (params.height) oEmbed.height = params.height;
148 $('#form_notice').append('<div id="link-preview" class="thumbnails"></div>');
150 // Piggyback on the counter update...
151 var origCounter = SN.U.Counter;
152 SN.U.Counter = function(form) {
153 previewLinks($('#notice_data-text').val());
154 return origCounter(form);