]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/realtimeupdate.js
Merge commit 'origin/0.9.x' into 0.9.x
[quix0rs-gnu-social.git] / plugins / Realtime / realtimeupdate.js
1 /*
2  * StatusNet - a distributed open-source microblogging tool
3  * Copyright (C) 2008, StatusNet, Inc.
4  *
5  * Add a notice encoded as JSON into the current timeline
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * @category  Plugin
21  * @package   StatusNet
22  * @author    Evan Prodromou <evan@status.net>
23  * @author    Sarven Capadisli <csarven@status.net>
24  * @copyright 2009 StatusNet, Inc.
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26  * @link      http://status.net/
27  */
28
29 // TODO: i18n
30
31 RealtimeUpdate = {
32      _userid: 0,
33      _replyurl: '',
34      _favorurl: '',
35      _repeaturl: '',
36      _deleteurl: '',
37      _updatecounter: 0,
38      _maxnotices: 50,
39      _windowhasfocus: true,
40      _documenttitle: '',
41      _paused:false,
42      _queuedNotices:[],
43
44      init: function(userid, replyurl, favorurl, repeaturl, deleteurl)
45      {
46         RealtimeUpdate._userid = userid;
47         RealtimeUpdate._replyurl = replyurl;
48         RealtimeUpdate._favorurl = favorurl;
49         RealtimeUpdate._repeaturl = repeaturl;
50         RealtimeUpdate._deleteurl = deleteurl;
51
52         RealtimeUpdate._documenttitle = document.title;
53
54         $(window).bind('focus', function(){ RealtimeUpdate._windowhasfocus = true; });
55
56         $(window).bind('blur', function() {
57           $('#notices_primary .notice').removeClass('mark-top');
58
59           $('#notices_primary .notice:first').addClass('mark-top');
60
61           RealtimeUpdate._updatecounter = 0;
62           document.title = RealtimeUpdate._documenttitle;
63           RealtimeUpdate._windowhasfocus = false;
64
65           return false;
66         });
67      },
68
69      receive: function(data)
70      {
71           if (RealtimeUpdate._paused === false) {
72               RealtimeUpdate.purgeLastNoticeItem();
73
74               RealtimeUpdate.insertNoticeItem(data);
75           }
76           else {
77               RealtimeUpdate._queuedNotices.push(data);
78
79               RealtimeUpdate.updateQueuedCounter();
80           }
81
82           RealtimeUpdate.updateWindowCounter();
83      },
84
85      insertNoticeItem: function(data) {
86         // Don't add it if it already exists
87         if ($("#notice-"+data.id).length > 0) {
88             return;
89         }
90
91         var noticeItem = RealtimeUpdate.makeNoticeItem(data);
92         var noticeItemID = $(noticeItem).attr('id');
93
94         $("#notices_primary .notices").prepend(noticeItem);
95         $("#notices_primary .notice:first").css({display:"none"});
96         $("#notices_primary .notice:first").fadeIn(1000);
97
98         SN.U.FormXHR($('#'+noticeItemID+' .form_favor'));
99         SN.U.NoticeReplyTo($('#'+noticeItemID));
100         SN.U.FormXHR($('#'+noticeItemID+' .form_repeat'));
101         SN.U.NoticeWithAttachment($('#'+noticeItemID));
102      },
103
104      purgeLastNoticeItem: function() {
105         if ($('#notices_primary .notice').length > RealtimeUpdate._maxnotices) {
106             $("#notices_primary .notice:last").remove();
107         }
108      },
109
110      updateWindowCounter: function() {
111           if (RealtimeUpdate._windowhasfocus === false) {
112               RealtimeUpdate._updatecounter += 1;
113               document.title = '('+RealtimeUpdate._updatecounter+') ' + RealtimeUpdate._documenttitle;
114           }
115      },
116
117      makeNoticeItem: function(data)
118      {
119           if (data.hasOwnProperty('retweeted_status')) {
120                original = data['retweeted_status'];
121                repeat   = data;
122                data     = original;
123                unique   = repeat['id'];
124                responsible = repeat['user'];
125           } else {
126                original = null;
127                repeat = null;
128                unique = data['id'];
129                responsible = data['user'];
130           }
131
132           user = data['user'];
133           html = data['html'].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&amp;/g,'&');
134           source = data['source'].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&amp;/g,'&');
135
136           ni = "<li class=\"hentry notice\" id=\"notice-"+unique+"\">"+
137                "<div class=\"entry-title\">"+
138                "<span class=\"vcard author\">"+
139                "<a href=\""+user['profile_url']+"\" class=\"url\">"+
140                "<img src=\""+user['profile_image_url']+"\" class=\"avatar photo\" width=\"48\" height=\"48\" alt=\""+user['screen_name']+"\"/>"+
141                "<span class=\"nickname fn\">"+user['screen_name']+"</span>"+
142                "</a>"+
143                "</span>"+
144                "<p class=\"entry-content\">"+html+"</p>"+
145                "</div>"+
146                "<div class=\"entry-content\">"+
147                "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+
148                "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+
149                "</a> "+
150                "<span class=\"source\">"+
151                "from "+
152                 "<span class=\"device\">"+source+"</span>"+ // may have a link
153                "</span>";
154           if (data['in_reply_to_status_id']) {
155                ni = ni+" <a class=\"response\" href=\""+data['in_reply_to_status_url']+"\">in context</a>";
156           }
157
158           if (repeat) {
159                ru = repeat['user'];
160                ni = ni + "<span class=\"repeat vcard\">Repeated by " +
161                     "<a href=\"" + ru['profile_url'] + "\" class=\"url\">" +
162                     "<span class=\"nickname\">"+ ru['screen_name'] + "</span></a></span>";
163           }
164
165           ni = ni+"</div>";
166
167           ni = ni + "<div class=\"notice-options\">";
168
169           if (RealtimeUpdate._userid != 0) {
170                var input = $("form#form_notice fieldset input#token");
171                var session_key = input.val();
172                ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
173                ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
174                if (RealtimeUpdate._userid == responsible['id']) {
175                     ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
176                } else if (RealtimeUpdate._userid != user['id']) {
177                     ni = ni+RealtimeUpdate.makeRepeatForm(data['id'],  session_key);
178                }
179           }
180
181           ni = ni+"</div>";
182
183                "</li>";
184           return ni;
185      },
186
187      makeFavoriteForm: function(id, session_key)
188      {
189           var ff;
190
191           ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
192                 "<fieldset>"+
193                "<legend>Favor this notice</legend>"+
194                "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
195                "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
196                "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
197                 "</fieldset>"+
198                "</form>";
199           return ff;
200      },
201
202      makeReplyLink: function(id, nickname)
203      {
204           var rl;
205           rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
206           return rl;
207      },
208
209      makeRepeatForm: function(id, session_key)
210      {
211           var rf;
212           rf = "<form id=\"repeat-"+id+"\" class=\"form_repeat\" method=\"post\" action=\""+RealtimeUpdate._repeaturl+"\">"+
213                "<fieldset>"+
214                "<legend>Favor this notice</legend>"+
215                "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
216                "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
217                "<input type=\"submit\" id=\"repeat-submit-"+id+"\" name=\"repeat-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Repeat this notice\"/>"+
218                "</fieldset>"+
219                "</form>";
220
221           return rf;
222      },
223
224      makeDeleteLink: function(id)
225      {
226           var dl, delurl;
227           delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
228
229           dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
230
231           return dl;
232      },
233
234      initActions: function(url, timeline, path)
235      {
236         $('#notices_primary').prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
237
238         RealtimeUpdate._pluginPath = path;
239
240         RealtimeUpdate.initPlayPause();
241         RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
242      },
243
244      initPlayPause: function()
245      {
246         if (typeof(localStorage) == 'undefined') {
247             RealtimeUpdate.showPause();
248         }
249         else {
250             if (localStorage.getItem('RealtimeUpdate_paused') === 'true') {
251                 RealtimeUpdate.showPlay();
252             }
253             else {
254                 RealtimeUpdate.showPause();
255             }
256         }
257      },
258
259      showPause: function()
260      {
261         RealtimeUpdate.setPause(false);
262         RealtimeUpdate.showQueuedNotices();
263         RealtimeUpdate.addNoticesHover();
264
265         $('#realtime_playpause').remove();
266         $('#realtime_actions').prepend('<li id="realtime_playpause"><button id="realtime_pause" class="pause" title="Pause">Pause</button></li>');
267
268         $('#realtime_pause').bind('click', function() {
269             RealtimeUpdate.removeNoticesHover();
270             RealtimeUpdate.showPlay();
271             return false;
272         });
273      },
274
275      showPlay: function()
276      {
277         RealtimeUpdate.setPause(true);
278         $('#realtime_playpause').remove();
279         $('#realtime_actions').prepend('<li id="realtime_playpause"><span id="queued_counter"></span> <button id="realtime_play" class="play" title="Play">Play</button></li>');
280
281         $('#realtime_play').bind('click', function() {
282             RealtimeUpdate.showPause();
283             return false;
284         });
285      },
286
287      setPause: function(state)
288      {
289         RealtimeUpdate._paused = state;
290         if (typeof(localStorage) != 'undefined') {
291             localStorage.setItem('RealtimeUpdate_paused', RealtimeUpdate._paused);
292         }
293      },
294
295      showQueuedNotices: function()
296      {
297         $.each(RealtimeUpdate._queuedNotices, function(i, n) {
298             RealtimeUpdate.insertNoticeItem(n);
299         });
300
301         RealtimeUpdate._queuedNotices = [];
302
303         RealtimeUpdate.removeQueuedCounter();
304      },
305
306      updateQueuedCounter: function()
307      {
308         $('#realtime_playpause #queued_counter').html('('+RealtimeUpdate._queuedNotices.length+')');
309      },
310
311      removeQueuedCounter: function()
312      {
313         $('#realtime_playpause #queued_counter').empty();
314      },
315
316      addNoticesHover: function()
317      {
318         $('#notices_primary .notices').hover(
319             function() {
320                 if (RealtimeUpdate._paused === false) {
321                     RealtimeUpdate.showPlay();
322                 }
323             },
324             function() {
325                 if (RealtimeUpdate._paused === true) {
326                     RealtimeUpdate.showPause();
327                 }
328             }
329         );
330      },
331
332      removeNoticesHover: function()
333      {
334         $('#notices_primary .notices').unbind();
335      },
336
337      initAddPopup: function(url, timeline, path)
338      {
339          $('#realtime_timeline').append('<button id="realtime_popup" title="Pop up in a window">Pop up</button>');
340
341          $('#realtime_popup').bind('click', function() {
342              window.open(url,
343                          '',
344                          'toolbar=no,resizable=yes,scrollbars=yes,status=no,menubar=no,personalbar=no,location=no,width=500,height=550');
345
346              return false;
347          });
348      },
349
350      initPopupWindow: function()
351      {
352          $('.notices .entry-title a, .notices .entry-content a').bind('click', function() {
353             window.open(this.href, '');
354
355             return false;
356          });
357
358          $('#showstream .entity_profile').css({'width':'69%'});
359      }
360 }
361