]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LinkPreview/linkpreview.js
LinkPreview: clear preview thumbnails & data on form submission/reset
[quix0rs-gnu-social.git] / plugins / LinkPreview / linkpreview.js
1 /**
2  * (c) 2010 StatusNet, Inc.
3  */
4
5 (function() {
6     var oEmbed = {
7         api: 'http://oohembed.com/oohembed',
8         width: 100,
9         height: 75,
10         cache: {},
11         callbacks: {},
12
13         /**
14          * Do a cached oEmbed lookup for the given URL.
15          *
16          * @param {String} url
17          * @param {function} callback
18          */
19         lookup: function(url, callback)
20         {
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];
27
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++) {
33                         callbacks[i](data);
34                     }
35                 });
36             } else {
37                 // A lookup is in progress.
38                 oEmbed.callbacks[url].push(callback);
39             }
40         },
41
42         /**
43          * Do an oEmbed lookup for the given URL.
44          *
45          * @fixme proxy through ourselves if possible?
46          * @fixme use the global thumbnail size settings
47          *
48          * @param {String} url
49          * @param {function} callback
50          */
51         rawLookup: function(url, callback)
52         {
53             var params = {
54                 url: url,
55                 format: 'json',
56                 maxwidth: oEmbed.width,
57                 maxheight: oEmbed.height
58             };
59             $.get(oEmbed.api, params, function(data, xhr) {
60                 callback(data);
61             }, 'json');
62         }
63     };
64
65     var LinkPreview = {
66         links: [],
67
68         /**
69          * Find URL links from the source text that may be interesting.
70          *
71          * @param {String} text
72          * @return {Array} list of URLs
73          */
74         findLinks: function (text)
75         {
76             // @fixme match this to core code
77             var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
78             var links = [];
79             var matches;
80             while ((matches = re.exec(text)) !== null) {
81                 links.push(matches[1]);
82             }
83             return links;
84         },
85
86         /**
87          * Start looking up info for a link preview...
88          * May start async data loads.
89          *
90          * @param {String} id
91          * @param {String} url
92          */
93         prepLinkPreview: function(id, url)
94         {
95             oEmbed.lookup(url, function(data) {
96                 var thumb = null;
97                 var width = 100;
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;
103                         }
104                     }
105                 } else if (data.type == 'photo' && typeof data.url == "string") {
106                     thumb = data.url;
107                     if (typeof data.width !== "undefined") {
108                         if (data.width < width) {
109                             width = data.width;
110                         }
111                     }
112                 }
113                 if (thumb) {
114                     var link = $('<span class="inline-attachment"><a><img/></a></span>');
115                     link.find('a')
116                             .attr('href', url)
117                             .attr('target', '_blank')
118                             .last()
119                         .find('img')
120                             .attr('src', thumb)
121                             .attr('width', width)
122                             .attr('title', data.title || data.url || url);
123                     $('#' + id).append(link);
124                 }
125             });
126         },
127
128         /**
129          * Update the live preview section with links found in the given text.
130          * May start async data loads.
131          *
132          * @param {String} text: free-form input text
133          */
134         previewLinks: function(text)
135         {
136             var old = LinkPreview.links;
137             var links = LinkPreview.findLinks(text);
138
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]);
146                 }
147             }
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]);
154                 }
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();
160                 }
161             }
162
163             LinkPreview.links = links;
164         },
165
166         /**
167          * Clear out any link preview data.
168          */
169         clear: function() {
170             LinkPreview.links = [];
171             $('#link-preview').empty();
172         }
173     };
174
175     SN.Init.LinkPreview = function(params) {
176         if (params.api) oEmbed.api = params.api;
177         if (params.width) oEmbed.width = params.width;
178         if (params.height) oEmbed.height = params.height;
179
180         $('#form_notice')
181             .append('<div id="link-preview" class="thumbnails"></div>')
182             .bind('reset', function() {
183                 LinkPreview.clear();
184             });
185
186         // Piggyback on the counter update...
187         var origCounter = SN.U.Counter;
188         SN.U.Counter = function(form) {
189             LinkPreview.previewLinks($('#notice_data-text').val());
190             return origCounter(form);
191         }
192     }
193 })();