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) {
69 * Find URL links from the source text that may be interesting.
71 * @param {String} text
72 * @return {Array} list of URLs
74 findLinks: function (text)
76 // @fixme match this to core code
77 var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
80 while ((matches = re.exec(text)) !== null) {
81 links.push(matches[1]);
87 * Start looking up info for a link preview...
88 * May start async data loads.
93 prepLinkPreview: function(id, url)
95 oEmbed.lookup(url, function(data) {
98 if (typeof data.thumbnail_url == "string") {
99 thumb = data.thumbnail_url;
100 if (typeof data.thumbnail_width !== "undefined") {
101 if (data.thumbnail_width < width) {
102 width = data.thumbnail_width;
105 } else if (data.type == 'photo' && typeof data.url == "string") {
107 if (typeof data.width !== "undefined") {
108 if (data.width < width) {
114 var link = $('<span class="inline-attachment"><a><img/></a></span>');
117 .attr('target', '_blank')
121 .attr('width', width)
122 .attr('title', data.title || data.url || url);
123 $('#' + id).append(link);
129 * Update the live preview section with links found in the given text.
130 * May start async data loads.
132 * @param {String} text: free-form input text
134 previewLinks: function(text)
136 var old = LinkPreview.links;
137 var links = LinkPreview.findLinks(text);
139 // Check for existing common elements...
140 for (var i = 0; i < old.length && i < links.length; i++) {
141 if (links[i] != old[i]) {
142 // Change an existing entry!
143 var id = 'link-preview-' + i;
144 $('#' + id).html('');
145 LinkPreview.prepLinkPreview(id, links[i]);
148 if (links.length > old.length) {
149 // Adding new entries, whee!
150 for (var i = old.length; i < links.length; i++) {
151 var id = 'link-preview-' + i;
152 $('#link-preview').append('<span id="' + id + '"></span>');
153 LinkPreview.prepLinkPreview(id, links[i]);
155 } else if (old.length > links.length) {
156 // Remove preview entries for links that have been removed.
157 for (var i = links.length; i < old.length; i++) {
158 var id = 'link-preview-' + i;
159 $('#' + id).remove();
163 LinkPreview.links = links;
167 SN.Init.LinkPreview = function(params) {
168 if (params.api) oEmbed.api = params.api;
169 if (params.width) oEmbed.width = params.width;
170 if (params.height) oEmbed.height = params.height;
172 $('#form_notice').append('<div id="link-preview" class="thumbnails"></div>');
174 // Piggyback on the counter update...
175 var origCounter = SN.U.Counter;
176 SN.U.Counter = function(form) {
177 LinkPreview.previewLinks($('#notice_data-text').val());
178 return origCounter(form);