]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/realtimeupdate.js
Merge branch 'master' 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.NoticeReplyTo($('#'+noticeItemID));
99         SN.U.NoticeWithAttachment($('#'+noticeItemID));
100      },
101
102      purgeLastNoticeItem: function() {
103         if ($('#notices_primary .notice').length > RealtimeUpdate._maxnotices) {
104             $("#notices_primary .notice:last").remove();
105         }
106      },
107
108      updateWindowCounter: function() {
109           if (RealtimeUpdate._windowhasfocus === false) {
110               RealtimeUpdate._updatecounter += 1;
111               document.title = '('+RealtimeUpdate._updatecounter+') ' + RealtimeUpdate._documenttitle;
112           }
113      },
114
115      makeNoticeItem: function(data)
116      {
117           if (data.hasOwnProperty('retweeted_status')) {
118                original = data['retweeted_status'];
119                repeat   = data;
120                data     = original;
121                unique   = repeat['id'];
122                responsible = repeat['user'];
123           } else {
124                original = null;
125                repeat = null;
126                unique = data['id'];
127                responsible = data['user'];
128           }
129
130           user = data['user'];
131           html = data['html'].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&amp;/g,'&');
132           source = data['source'].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&amp;/g,'&');
133
134           ni = "<li class=\"hentry notice\" id=\"notice-"+unique+"\">"+
135                "<div class=\"entry-title\">"+
136                "<span class=\"vcard author\">"+
137                "<a href=\""+user['profile_url']+"\" class=\"url\" title=\""+user['name']+"\">"+
138                "<img src=\""+user['profile_image_url']+"\" class=\"avatar photo\" width=\"48\" height=\"48\" alt=\""+user['screen_name']+"\"/>"+
139                "<span class=\"nickname fn\">"+user['screen_name']+"</span>"+
140                "</a>"+
141                "</span>"+
142                "<p class=\"entry-content\">"+html+"</p>"+
143                "</div>"+
144                "<div class=\"entry-content\">"+
145                "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+
146                "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+
147                "</a> "+
148                "<span class=\"source\">"+
149                "from "+
150                 "<span class=\"device\">"+source+"</span>"+ // may have a link
151                "</span>";
152           if (data['conversation_url']) {
153                ni = ni+" <a class=\"response\" href=\""+data['conversation_url']+"\">in context</a>";
154           }
155
156           if (repeat) {
157                ru = repeat['user'];
158                ni = ni + "<span class=\"repeat vcard\">Repeated by " +
159                     "<a href=\"" + ru['profile_url'] + "\" class=\"url\">" +
160                     "<span class=\"nickname\">"+ ru['screen_name'] + "</span></a></span>";
161           }
162
163           ni = ni+"</div>";
164
165           ni = ni + "<div class=\"notice-options\">";
166
167           if (RealtimeUpdate._userid != 0) {
168                var input = $("form#form_notice fieldset input#token");
169                var session_key = input.val();
170                ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
171                ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
172                if (RealtimeUpdate._userid == responsible['id']) {
173                     ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
174                } else if (RealtimeUpdate._userid != user['id']) {
175                     ni = ni+RealtimeUpdate.makeRepeatForm(data['id'],  session_key);
176                }
177           }
178
179           ni = ni+"</div>";
180
181           ni = ni+"</li>";
182           return ni;
183      },
184
185      makeFavoriteForm: function(id, session_key)
186      {
187           var ff;
188
189           ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
190                 "<fieldset>"+
191                "<legend>Favor this notice</legend>"+
192                "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
193                "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
194                "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
195                 "</fieldset>"+
196                "</form>";
197           return ff;
198      },
199
200      makeReplyLink: function(id, nickname)
201      {
202           var rl;
203           rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
204           return rl;
205      },
206
207      makeRepeatForm: function(id, session_key)
208      {
209           var rf;
210           rf = "<form id=\"repeat-"+id+"\" class=\"form_repeat\" method=\"post\" action=\""+RealtimeUpdate._repeaturl+"\">"+
211                "<fieldset>"+
212                "<legend>Repeat this notice?</legend>"+
213                "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
214                "<input name=\"notice\" type=\"hidden\" id=\"notice-"+id+"\" value=\""+id+"\"/>"+
215                "<input type=\"submit\" id=\"repeat-submit-"+id+"\" name=\"repeat-submit-"+id+"\" class=\"submit\" value=\"Yes\" title=\"Repeat this notice\"/>"+
216                "</fieldset>"+
217                "</form>";
218
219           return rf;
220      },
221
222      makeDeleteLink: function(id)
223      {
224           var dl, delurl;
225           delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
226
227           dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
228
229           return dl;
230      },
231
232      initActions: function(url, timeline, path)
233      {
234         $('#notices_primary').prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
235
236         RealtimeUpdate._pluginPath = path;
237
238         RealtimeUpdate.initPlayPause();
239         RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
240      },
241
242      initPlayPause: function()
243      {
244         if (typeof(localStorage) == 'undefined') {
245             RealtimeUpdate.showPause();
246         }
247         else {
248             if (localStorage.getItem('RealtimeUpdate_paused') === 'true') {
249                 RealtimeUpdate.showPlay();
250             }
251             else {
252                 RealtimeUpdate.showPause();
253             }
254         }
255      },
256
257      showPause: function()
258      {
259         RealtimeUpdate.setPause(false);
260         RealtimeUpdate.showQueuedNotices();
261         RealtimeUpdate.addNoticesHover();
262
263         $('#realtime_playpause').remove();
264         $('#realtime_actions').prepend('<li id="realtime_playpause"><button id="realtime_pause" class="pause"></button></li>');
265         $('#realtime_pause').text(SN.msg('realtime_pause'))
266                             .attr('title', SN.msg('realtime_pause_tooltip'))
267                             .bind('click', function() {
268             RealtimeUpdate.removeNoticesHover();
269             RealtimeUpdate.showPlay();
270             return false;
271         });
272      },
273
274      showPlay: function()
275      {
276         RealtimeUpdate.setPause(true);
277         $('#realtime_playpause').remove();
278         $('#realtime_actions').prepend('<li id="realtime_playpause"><span id="queued_counter"></span> <button id="realtime_play" class="play"></button></li>');
279         $('#realtime_play').text(SN.msg('realtime_play'))
280                            .attr('title', SN.msg('realtime_play_tooltip'))
281                            .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"></button>');
340          $('#realtime_popup').text(SN.msg('realtime_popup'))
341                              .attr('title', SN.msg('realtime_popup_tooltip'))
342                              .bind('click', function() {
343                 window.open(url,
344                          '',
345                          'toolbar=no,resizable=yes,scrollbars=yes,status=no,menubar=no,personalbar=no,location=no,width=500,height=550');
346
347              return false;
348          });
349      },
350
351      initPopupWindow: function()
352      {
353          $('.notices .entry-title a, .notices .entry-content a').bind('click', function() {
354             window.open(this.href, '');
355
356             return false;
357          });
358
359          $('#showstream .entity_profile').css({'width':'69%'});
360      }
361 }
362