~cjwatson/ubuntukylin-default-settings/remove-skype-icons

« back to all changes in this revision

Viewing changes to ubiquity-slideshow/slides/en_US/link/twitter.js

  • Committer: ShuiLu Pi
  • Date: 2013-03-26 09:15:44 UTC
  • Revision ID: pishuilu1128@126.com-20130326091544-wrix9bgqub2d8zu7
add the slideshow zh_TW and en_US

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* requires jquery.tweet.js */
 
2
 
 
3
/* TODO: Fix tweet slideDown animation to actually slide down instead of changing height */
 
4
 
 
5
function escapeHTML(text) {
 
6
        return $('<div/>').text(text).html();
 
7
}
 
8
 
 
9
function spliceText(text, indices) {
 
10
        // Copyright 2010, Wade Simmons <https://gist.github.com/442463>
 
11
        // Licensed under the MIT license
 
12
        var result = "";
 
13
        var last_i = 0;
 
14
        var i = 0;
 
15
        
 
16
        for (i=0; i < text.length; ++i) {
 
17
                var ind = indices[i];
 
18
                if (ind) {
 
19
                        var end = ind[0];
 
20
                        var output = ind[1];
 
21
                        if (i > last_i) {
 
22
                                result += escapeHTML(text.substring(last_i, i));
 
23
                        }
 
24
                        result += output;
 
25
                        i = end - 1;
 
26
                        last_i = end;
 
27
                }
 
28
        }
 
29
        
 
30
        if (i > last_i) {
 
31
                result += escapeHTML(text.substring(last_i, i));
 
32
        }
 
33
        
 
34
        return result;
 
35
}
 
36
 
 
37
function Tweet(data) {
 
38
        var tweet = this;
 
39
        
 
40
        var innerData = data;
 
41
        if (data.retweeted_status) innerData = data.retweeted_status;
 
42
        
 
43
        var userID = innerData.from_user || innerData.user.id;
 
44
        var userRealName = innerData.from_user_name || innerData.user.name;
 
45
        var userScreenName = innerData.from_user || innerData.user.screen_name;
 
46
        
 
47
        var linkHashTag = function(hashTag) {
 
48
                return 'https://twitter.com/search?q='+encodeURIComponent('#'+hashTag);
 
49
        }
 
50
        
 
51
        var linkUser = function(userName) {
 
52
                return 'https://twitter.com/'+encodeURIComponent(userName);
 
53
        }
 
54
        
 
55
        var linkUserID = function(userID) {
 
56
                return 'https://twitter.com/account/redirect_by_id?id='+encodeURIComponent(userID);
 
57
        }
 
58
        
 
59
        var linkEntities = function(entities, text) {
 
60
                entityIndices = {};
 
61
                
 
62
                $.each(entities.media || [], function(i, entry) {
 
63
                        var link = '<a class="twitter-url twitter-media" href="'+encodeURI(entry.media_url)+'">'+escapeHTML(entry.display_url || entry.url)+'</a>';
 
64
                        entityIndices[entry.indices[0]] = [entry.indices[1], link];
 
65
                });
 
66
                
 
67
                $.each(entities.urls || [], function(i, entry) {
 
68
                        var link = '<a class="twitter-url" href="'+encodeURI(entry.url)+'">'+escapeHTML(entry.display_url || entry.url)+'</a>';
 
69
                        entityIndices[entry.indices[0]] = [entry.indices[1], link];
 
70
                });
 
71
                
 
72
                $.each(entities.hashtags || [], function(i, entry) {
 
73
                        var link = '<a class="twitter-hashtag" href="'+linkHashTag(entry.text)+'">'+escapeHTML('#'+entry.text)+'</a>';
 
74
                        entityIndices[entry.indices[0]] = [entry.indices[1], link];
 
75
                });
 
76
                
 
77
                $.each(entities.user_mentions || [], function(i, entry) {
 
78
                        var link = '<a class="twitter-mention" href="'+linkUserID(entry.id)+'">'+escapeHTML('@'+entry.screen_name)+'</a>';
 
79
                        entityIndices[entry.indices[0]] = [entry.indices[1], link];
 
80
                });
 
81
                
 
82
                return spliceText(text, entityIndices);
 
83
        }
 
84
        var linkedText = linkEntities(innerData.entities, innerData.text);
 
85
        
 
86
        this.getHtml = function() {
 
87
                var container = $('<div class="tweet">');
 
88
                
 
89
                var authorDetails = $('<a class="tweet-author-details">');
 
90
                authorDetails.attr('href', linkUserID(userID));
 
91
                
 
92
                var authorName = $('<span class="tweet-author-name">');
 
93
                authorName.text(userRealName);
 
94
                
 
95
                var authorID = $('<span class="tweet-author-id">');
 
96
                authorID.text(userScreenName);
 
97
                
 
98
                authorDetails.append(authorName, authorID);
 
99
                container.append(authorDetails);
 
100
                
 
101
                var text = $('<div class="tweet-text">');
 
102
                text.html(linkedText);
 
103
                container.append(text);
 
104
                
 
105
                return container;
 
106
        }
 
107
}
 
108
 
 
109
function TweetsList(container) {
 
110
        var tweetsList = this;
 
111
        
 
112
        container = $(container);
 
113
        var list = $('<ul class="tweets-list">');
 
114
        container.append(list);
 
115
        
 
116
        var cleanup = function() {
 
117
                var bottom = container.height();
 
118
                list.children().each(function(index, listItem) {
 
119
                        if ($(listItem).position().top > bottom) {
 
120
                                $(listItem).remove();
 
121
                        }
 
122
                });
 
123
        }
 
124
        
 
125
        this.showTweet = function(tweet) {
 
126
                var listItem = $('<li>');
 
127
                listItem.html(tweet.getHtml());
 
128
                listItem.hide();
 
129
                listItem.css('opacity', '0');
 
130
                
 
131
                list.prepend(listItem);
 
132
                
 
133
                var expandTime = listItem.height() * 8;
 
134
                listItem.animate({
 
135
                        'height': 'show',
 
136
                        'opacity': '1'
 
137
                }, expandTime, 'linear', function() {
 
138
                        cleanup();
 
139
                });
 
140
                
 
141
                /*listItem.slideDown(500);*/
 
142
        }
 
143
}
 
144
 
 
145
function TweetQuery(lang) {
 
146
        var tweetQuery = this;
 
147
        
 
148
        // request is tightly encapsulated because we might move that logic to a remote server
 
149
        
 
150
        var QUERY_URL = 'https://api.twitter.com/1/lists/statuses.json';
 
151
        var request = {
 
152
                'owner_screen_name' : 'hello_ubuntu',
 
153
                'slug' : 'installer-slideshow',
 
154
                'include_entities' : true,
 
155
                'include_rts' : true,
 
156
                'per_page' : 25
 
157
        }
 
158
        
 
159
        //var QUERY_URL = 'https://search.twitter.com/search.json';
 
160
        /*var request = {
 
161
                'q' : 'from:ubuntu OR from:ubuntudev OR from:planetubuntu OR from:ubuntul10n OR from:ubuntucloud OR from:ubuntuone OR from:ubuntudesigners OR from:ubuntuunity OR from:canonical',
 
162
                'lang' : 'all',
 
163
                'result_type' : 'recent',
 
164
                'rpp' : 25,
 
165
                'include_entities' : true
 
166
        }*/
 
167
        
 
168
        var lastUpdate = 0;
 
169
        
 
170
        /** Time since last update, in seconds */
 
171
        this.getTimeSinceUpdate = function() {
 
172
                var now = Date.now();
 
173
                return now - lastUpdate;
 
174
        }
 
175
        
 
176
        this.loadTweets = function(loadedCallback) {
 
177
                var newTweets = [];
 
178
                
 
179
                $.ajax({
 
180
                        url: QUERY_URL,
 
181
                        dataType: 'jsonp',
 
182
                        data: request,
 
183
                        timeout: 5000,
 
184
                        success: function(data, status, xhr) {
 
185
                                //var results = data.results || [];
 
186
                                var results = data || [];
 
187
                                if ('results' in results) results = results.results;
 
188
                                $.each(results, function(index, tweetData) {
 
189
                                        var tweet = new Tweet(tweetData);
 
190
                                        newTweets.unshift(tweet);
 
191
                                });
 
192
                        },
 
193
                        complete: function(xhr, status) {
 
194
                                loadedCallback(newTweets);
 
195
                        }
 
196
                });
 
197
                lastUpdate = Date.now();
 
198
        }
 
199
}
 
200
 
 
201
function TweetBuffer() {
 
202
        var tweetBuffer = this;
 
203
        
 
204
        var query = new TweetQuery('all');
 
205
        
 
206
        var tweets = [];
 
207
        var nextTweetIndex = 0;
 
208
        
 
209
        var loadedCallback = function(newTweets) {
 
210
                if (newTweets.length > 0) {
 
211
                        tweets = newTweets;
 
212
                }
 
213
                nextTweet = 0;
 
214
        }
 
215
        
 
216
        var getNextTweet = function(returnTweet) {
 
217
                if (nextTweetIndex < tweets.length) {
 
218
                        returnTweet(tweets[nextTweetIndex]);
 
219
                } else {
 
220
                        nextTweetIndex = 0;
 
221
                        if (query.getTimeSinceUpdate() > 15 * 60 * 1000) {
 
222
                                // load new tweets every 15 minutes
 
223
                                query.loadTweets(function(newTweets) {
 
224
                                        loadedCallback(newTweets);
 
225
                                        returnTweet(tweets[nextTweetIndex]);
 
226
                                });
 
227
                        } else {
 
228
                                returnTweet(tweets[nextTweetIndex]);
 
229
                        }
 
230
                }
 
231
        }
 
232
        
 
233
        
 
234
        this.dataIsAvailable = function(response) {
 
235
                getNextTweet(function(tweet) {
 
236
                        response ( (tweet !== undefined) );
 
237
                });
 
238
        }
 
239
        
 
240
        /* Loads (if necessary) the next tweet and sends it asynchronously to
 
241
         * the tweetReceived(tweet) callback. The tweet parameter is undefined
 
242
         * if no tweets are available.
 
243
         */
 
244
        this.popTweet = function(tweetReceived) {
 
245
                getNextTweet(function(tweet) {
 
246
                        nextTweetIndex += 1;
 
247
                        tweetReceived(tweet);
 
248
                });
 
249
        }
 
250
}
 
251
 
 
252
function TwitterStream(streamContainer) {
 
253
        var twitterStream = this;
 
254
        
 
255
        var tweetsContainer = $(streamContainer).children('.twitter-stream-tweets');
 
256
        var tweetsList = new TweetsList(tweetsContainer);
 
257
        
 
258
        var tweetBuffer = new TweetBuffer();
 
259
        
 
260
        var showNextInterval = undefined;
 
261
        
 
262
        var showNextTweet = function() {
 
263
                tweetBuffer.popTweet(function(tweet) {
 
264
                        if (tweet) {
 
265
                                twitterStream.enable();
 
266
                                tweetsList.showTweet(tweet);
 
267
                        } else {
 
268
                                // this isn't working, so we'll hide the stream
 
269
                                twitterStream.disable();
 
270
                        }
 
271
                });
 
272
        }
 
273
        
 
274
        var _enabled = false;
 
275
        this.isEnabled = function() {
 
276
                return _enabled;
 
277
        }
 
278
        this.enable = function(immediate) {
 
279
                if (_enabled) return;
 
280
                if (immediate) {
 
281
                        $(streamContainer).show();
 
282
                } else {
 
283
                        $(streamContainer).fadeIn(150);
 
284
                }
 
285
                _enabled = true;
 
286
        }
 
287
        this.disable = function(immediate) {
 
288
                if (! _enabled) return;
 
289
                if (immediate) {
 
290
                        $(streamContainer).hide();
 
291
                } else {
 
292
                        $(streamContainer).fadeOut(150);
 
293
                }
 
294
                _enabled = false;
 
295
                this.stop();
 
296
        }
 
297
        
 
298
        this.start = function() {
 
299
                this.stop();
 
300
                showNextInterval = window.setInterval(showNextTweet, 6000);
 
301
        }
 
302
        this.stop = function() {
 
303
                if (showNextInterval) window.clearInterval(showNextInterval);
 
304
        }
 
305
        
 
306
        var _init = function() {
 
307
                tweetBuffer.dataIsAvailable(function(available) {
 
308
                        if (available) {
 
309
                                twitterStream.enable(true);
 
310
                                // make sure there is some content visible from the start
 
311
                                showNextTweet();
 
312
                        } else {
 
313
                                twitterStream.disable(true);
 
314
                        }
 
315
                });
 
316
        }
 
317
        _init();
 
318
}
 
319
 
 
320
/* Only show the Twitter stuff if the slideshow is supposed to be English */
 
321
if ('locale' in INSTANCE_OPTIONS) {
 
322
        var locale_data = parse_locale_code(INSTANCE_OPTIONS['locale']);
 
323
        var language = locale_data['language'];
 
324
} else {
 
325
        var language = 'C';
 
326
}
 
327
 
 
328
var twitterLanguages = ['en', 'C'];
 
329
if (twitterLanguages.indexOf(language) >= 0) {
 
330
        var doTwitter = true;
 
331
} else {
 
332
        var doTwitter = false;
 
333
}
 
334
 
 
335
// Turn off Twitter for security reason
 
336
doTwitter = false;
 
337
 
 
338
Signals.watch('slideshow-loaded', function() {
 
339
        if (doTwitter) {
 
340
                $('.twitter-stream').each(function(index, streamContainer) {
 
341
                        var stream = new TwitterStream(streamContainer);
 
342
                        $(streamContainer).data('stream-object', stream);
 
343
                        // TODO: test connection, show immediately if connection is good
 
344
                });
 
345
        
 
346
                $('.twitter-post-status-link').each(function(index, linkContent) {
 
347
                        // Twitter-post-status-link is a <div> to avoid being translated. We need to wrap it around an <a> tag
 
348
                        var statusText = $(linkContent).children('.twitter-post-status-text').text();
 
349
                        var link = $('<a>');
 
350
                        link.attr('href', 'https://twitter.com/home?status='+encodeURIComponent(statusText));
 
351
                        link.insertBefore(linkContent);
 
352
                        $(linkContent).appendTo(link);
 
353
                });
 
354
        } else {
 
355
                $('.twitter-stream').hide();
 
356
                /* TODO: show something charming? */
 
357
        }
 
358
});
 
359
 
 
360
Signals.watch('slide-opened', function(slide) {
 
361
        if (! doTwitter) return;
 
362
        
 
363
        var streamContainers = $('.twitter-stream', slide);
 
364
        streamContainers.each(function(index, streamContainer) {
 
365
                var stream = $(streamContainer).data('stream-object');
 
366
                if (stream) {
 
367
                        stream.start();
 
368
                }
 
369
        });
 
370
});
 
371
 
 
372
Signals.watch('slide-closing', function(slide) {
 
373
        if (! doTwitter) return;
 
374
        
 
375
        var streamContainers = $('.twitter-stream', slide);
 
376
        streamContainers.each(function(index, streamContainer) {
 
377
                var stream = $(streamContainer).data('stream-object');
 
378
                if (stream) {
 
379
                        stream.stop();
 
380
                }
 
381
        });
 
382
});
 
383