]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/realtimeupdate.js
f764ca738db14bc457341def6681ccedaeef051c
[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      _messages:{},
44
45      init: function(userid, replyurl, favorurl, repeaturl, deleteurl)
46      {
47         RealtimeUpdate._userid = userid;
48         RealtimeUpdate._replyurl = replyurl;
49         RealtimeUpdate._favorurl = favorurl;
50         RealtimeUpdate._repeaturl = repeaturl;
51         RealtimeUpdate._deleteurl = deleteurl;
52
53         RealtimeUpdate._documenttitle = document.title;
54
55         $(window).bind('focus', function(){ RealtimeUpdate._windowhasfocus = true; });
56
57         $(window).bind('blur', function() {
58           $('#notices_primary .notice').removeClass('mark-top');
59
60           $('#notices_primary .notice:first').addClass('mark-top');
61
62           RealtimeUpdate._updatecounter = 0;
63           document.title = RealtimeUpdate._documenttitle;
64           RealtimeUpdate._windowhasfocus = false;
65
66           return false;
67         });
68      },
69
70      receive: function(data)
71      {
72           if (RealtimeUpdate._paused === false) {
73               RealtimeUpdate.purgeLastNoticeItem();
74
75               RealtimeUpdate.insertNoticeItem(data);
76           }
77           else {
78               RealtimeUpdate._queuedNotices.push(data);
79
80               RealtimeUpdate.updateQueuedCounter();
81           }
82
83           RealtimeUpdate.updateWindowCounter();
84      },
85
86      insertNoticeItem: function(data) {
87         // Don't add it if it already exists
88         if ($("#notice-"+data.id).length > 0) {
89             return;
90         }
91
92         var noticeItem = RealtimeUpdate.makeNoticeItem(data);
93         var noticeItemID = $(noticeItem).attr('id');
94
95         $("#notices_primary .notices").prepend(noticeItem);
96         $("#notices_primary .notice:first").css({display:"none"});
97         $("#notices_primary .notice:first").fadeIn(1000);
98
99         SN.U.NoticeReplyTo($('#'+noticeItemID));
100         SN.U.NoticeWithAttachment($('#'+noticeItemID));
101      },
102
103      purgeLastNoticeItem: function() {
104         if ($('#notices_primary .notice').length > RealtimeUpdate._maxnotices) {
105             $("#notices_primary .notice:last").remove();
106         }
107      },
108
109      updateWindowCounter: function() {
110           if (RealtimeUpdate._windowhasfocus === false) {
111               RealtimeUpdate._updatecounter += 1;
112               document.title = '('+RealtimeUpdate._updatecounter+') ' + RealtimeUpdate._documenttitle;
113           }
114      },
115
116      makeNoticeItem: function(data)
117      {
118           if (data.hasOwnProperty('retweeted_status')) {
119                original = data['retweeted_status'];
120                repeat   = data;
121                data     = original;
122                unique   = repeat['id'];
123                responsible = repeat['user'];
124           } else {
125                original = null;
126                repeat = null;
127                unique = data['id'];
128                responsible = data['user'];
129           }
130
131           user = data['user'];
132           html = data['html'].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&amp;/g,'&');
133           source = data['source'].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&amp;/g,'&');
134
135           ni = "<li class=\"hentry notice\" id=\"notice-"+unique+"\">"+
136                "<div class=\"entry-title\">"+
137                "<span class=\"vcard author\">"+
138                "<a href=\""+user['profile_url']+"\" class=\"url\" title=\""+user['name']+"\">"+
139                "<img src=\""+user['profile_image_url']+"\" class=\"avatar photo\" width=\"48\" height=\"48\" alt=\""+user['screen_name']+"\"/>"+
140                "<span class=\"nickname fn\">"+user['screen_name']+"</span>"+
141                "</a>"+
142                "</span>"+
143                "<p class=\"entry-content\">"+html+"</p>"+
144                "</div>"+
145                "<div class=\"entry-content\">"+
146                "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+
147                "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+
148                "</a> "+
149                "<span class=\"source\">"+
150                "from "+
151                 "<span class=\"device\">"+source+"</span>"+ // may have a link
152                "</span>";
153           if (data['conversation_url']) {
154                ni = ni+" <a class=\"response\" href=\""+data['conversation_url']+"\">in context</a>";
155           }
156
157           if (repeat) {
158                ru = repeat['user'];
159                ni = ni + "<span class=\"repeat vcard\">Repeated by " +
160                     "<a href=\"" + ru['profile_url'] + "\" class=\"url\">" +
161                     "<span class=\"nickname\">"+ ru['screen_name'] + "</span></a></span>";
162           }
163
164           ni = ni+"</div>";
165
166           ni = ni + "<div class=\"notice-options\">";
167
168           if (RealtimeUpdate._userid != 0) {
169                var input = $("form#form_notice fieldset input#token");
170                var session_key = input.val();
171                ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
172                ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
173                if (RealtimeUpdate._userid == responsible['id']) {
174                     ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
175                } else if (RealtimeUpdate._userid != user['id']) {
176                     ni = ni+RealtimeUpdate.makeRepeatForm(data['id'],  session_key);
177                }
178           }
179
180           ni = ni+"</div>";
181
182           ni = ni+"</li>";
183           return ni;
184      },
185
186      makeFavoriteForm: function(id, session_key)
187      {
188           var ff;
189
190           ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
191                 "<fieldset>"+
192                "<legend>Favor this notice</legend>"+
193                "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
194                "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
195                "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
196                 "</fieldset>"+
197                "</form>";
198           return ff;
199      },
200
201      makeReplyLink: function(id, nickname)
202      {
203           var rl;
204           rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
205           return rl;
206      },
207
208      makeRepeatForm: function(id, session_key)
209      {
210           var rf;
211           rf = "<form id=\"repeat-"+id+"\" class=\"form_repeat\" method=\"post\" action=\""+RealtimeUpdate._repeaturl+"\">"+
212                "<fieldset>"+
213                "<legend>Repeat this notice?</legend>"+
214                "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
215                "<input name=\"notice\" type=\"hidden\" id=\"notice-"+id+"\" value=\""+id+"\"/>"+
216                "<input type=\"submit\" id=\"repeat-submit-"+id+"\" name=\"repeat-submit-"+id+"\" class=\"submit\" value=\"Yes\" title=\"Repeat this notice\"/>"+
217                "</fieldset>"+
218                "</form>";
219
220           return rf;
221      },
222
223      makeDeleteLink: function(id)
224      {
225           var dl, delurl;
226           delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
227
228           dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
229
230           return dl;
231      },
232
233      initActions: function(url, timeline, path)
234      {
235         $('#notices_primary').prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
236
237         RealtimeUpdate._pluginPath = path;
238
239         RealtimeUpdate.initPlayPause();
240         RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
241      },
242
243      initPlayPause: function()
244      {
245         if (typeof(localStorage) == 'undefined') {
246             RealtimeUpdate.showPause();
247         }
248         else {
249             if (localStorage.getItem('RealtimeUpdate_paused') === 'true') {
250                 RealtimeUpdate.showPlay();
251             }
252             else {
253                 RealtimeUpdate.showPause();
254             }
255         }
256      },
257
258      showPause: function()
259      {
260         RealtimeUpdate.setPause(false);
261         RealtimeUpdate.showQueuedNotices();
262         RealtimeUpdate.addNoticesHover();
263
264         $('#realtime_playpause').remove();
265         $('#realtime_actions').prepend('<li id="realtime_playpause"><button id="realtime_pause" class="pause"></button></li>');
266         $('#realtime_pause').text(RealtimeUpdate._messages['pause'])
267                             .attr('title', RealtimeUpdate._messages['pause_tooltip'])
268                             .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"></button></li>');
280         $('#realtime_play').text(RealtimeUpdate._messages['play'])
281                            .attr('title', RealtimeUpdate._messages['play_tooltip'])
282                            .bind('click', function() {
283             RealtimeUpdate.showPause();
284             return false;
285         });
286      },
287
288      setPause: function(state)
289      {
290         RealtimeUpdate._paused = state;
291         if (typeof(localStorage) != 'undefined') {
292             localStorage.setItem('RealtimeUpdate_paused', RealtimeUpdate._paused);
293         }
294      },
295
296      showQueuedNotices: function()
297      {
298         $.each(RealtimeUpdate._queuedNotices, function(i, n) {
299             RealtimeUpdate.insertNoticeItem(n);
300         });
301
302         RealtimeUpdate._queuedNotices = [];
303
304         RealtimeUpdate.removeQueuedCounter();
305      },
306
307      updateQueuedCounter: function()
308      {
309         $('#realtime_playpause #queued_counter').html('('+RealtimeUpdate._queuedNotices.length+')');
310      },
311
312      removeQueuedCounter: function()
313      {
314         $('#realtime_playpause #queued_counter').empty();
315      },
316
317      addNoticesHover: function()
318      {
319         $('#notices_primary .notices').hover(
320             function() {
321                 if (RealtimeUpdate._paused === false) {
322                     RealtimeUpdate.showPlay();
323                 }
324             },
325             function() {
326                 if (RealtimeUpdate._paused === true) {
327                     RealtimeUpdate.showPause();
328                 }
329             }
330         );
331      },
332
333      removeNoticesHover: function()
334      {
335         $('#notices_primary .notices').unbind();
336      },
337
338      initAddPopup: function(url, timeline, path)
339      {
340          $('#realtime_timeline').append('<button id="realtime_popup"></button>');
341          $('#realtime_popup').text(RealtimeUpdate._messages['popup'])
342                              .attr('title', RealtimeUpdate._messages['popup_tooltip'])
343                              .bind('click', function() {
344                 window.open(url,
345                          '',
346                          'toolbar=no,resizable=yes,scrollbars=yes,status=no,menubar=no,personalbar=no,location=no,width=500,height=550');
347
348              return false;
349          });
350      },
351
352      initPopupWindow: function()
353      {
354          $('.notices .entry-title a, .notices .entry-content a').bind('click', function() {
355             window.open(this.href, '');
356
357             return false;
358          });
359
360          $('#showstream .entity_profile').css({'width':'69%'});
361      }
362 }
363