]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LinkPreview/linkpreview.js
LinkPreview: restructure a bit so we can pass config over
[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                 callback: '?'
59             };
60             $.get(oEmbed.api, params, function(data, xhr) {
61                 callback(data);
62             }, 'jsonp');
63         }
64     };
65
66     /**
67      * Find URL links from the source text that may be interesting.
68      *
69      * @param {String} text
70      * @return {Array} list of URLs
71      */
72     function findLinks(text)
73     {
74         // @fixme match this to core code
75         var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
76         var links = [];
77         var matches;
78         while ((matches = re.exec(text)) !== null) {
79             links.push(matches[1]);
80         }
81         return links;
82     }
83
84     /**
85      * Start looking up info for a link preview...
86      * May start async data loads.
87      *
88      * @param {String} id
89      * @param {String} url
90      */
91     function prepLinkPreview(id, url)
92     {
93         oEmbed.lookup(url, function(data) {
94             var thumb = null;
95             var width = 100;
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;
101                     }
102                 }
103             } else if (data.type == 'photo' && typeof data.url == "string") {
104                 thumb = data.url;
105                 if (typeof data.width !== "undefined") {
106                     if (data.width < width) {
107                         width = data.width;
108                     }
109                 }
110             }
111             if (thumb) {
112                 var link = $('<span class="inline-attachment"><a><img/></a></span>');
113                 link.find('a')
114                         .attr('href', url)
115                         .attr('target', '_blank')
116                         .last()
117                     .find('img')
118                         .attr('src', thumb)
119                         .attr('width', width)
120                         .attr('title', data.title || data.url || url);
121                 $('#' + id).append(link);
122             }
123         });
124     }
125
126     /**
127      * Update the live preview section with links found in the given text.
128      * May start async data loads.
129      *
130      * @param {String} text: free-form input text
131      */
132     function previewLinks(text)
133     {
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]);
140         }
141     }
142
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;
147
148         $('#form_notice').append('<div id="link-preview" class="thumbnails"></div>');
149
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);
155         }
156     }
157 })();