]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/realtimeupdate.js
Added counter beside the play button. When paused, it will update the
[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      _deleteurl: '',
36      _updatecounter: 0,
37      _maxnotices: 50,
38      _windowhasfocus: true,
39      _documenttitle: '',
40      _paused:false,
41      _queuedNotices:[],
42
43      init: function(userid, replyurl, favorurl, deleteurl)
44      {
45         RealtimeUpdate._userid = userid;
46         RealtimeUpdate._replyurl = replyurl;
47         RealtimeUpdate._favorurl = favorurl;
48         RealtimeUpdate._deleteurl = deleteurl;
49
50         RealtimeUpdate._documenttitle = document.title;
51
52         $(window).bind('focus', function(){ RealtimeUpdate._windowhasfocus = true; });
53
54         $(window).bind('blur', function() {
55           $('#notices_primary .notice').removeClass('mark-top');
56
57           $('#notices_primary .notice:first').addClass('mark-top');
58
59           RealtimeUpdate._updatecounter = 0;
60           document.title = RealtimeUpdate._documenttitle;
61           RealtimeUpdate._windowhasfocus = false;
62
63           return false;
64         });
65      },
66
67      receive: function(data)
68      {
69           id = data.id;
70
71           // Don't add it if it already exists
72           if ($("#notice-"+id).length > 0) {
73                return;
74           }
75
76           if (RealtimeUpdate._paused === false) {
77               RealtimeUpdate.purgeLastNoticeItem();
78
79               RealtimeUpdate.insertNoticeItem(data);
80           }
81           else {
82               RealtimeUpdate._queuedNotices.push(data);
83
84               RealtimeUpdate.updateQueuedCounter();
85           }
86
87           RealtimeUpdate.updateWindowCounter();
88      },
89
90      insertNoticeItem: function(data) {
91         var noticeItem = RealtimeUpdate.makeNoticeItem(data);
92         $("#notices_primary .notices").prepend(noticeItem);
93         $("#notices_primary .notice:first").css({display:"none"});
94         $("#notices_primary .notice:first").fadeIn(1000);
95
96         SN.U.NoticeReply();
97         SN.U.NoticeFavor();
98      },
99
100      purgeLastNoticeItem: function() {
101         if ($('#notices_primary .notice').length > RealtimeUpdate._maxnotices) {
102             $("#notices_primary .notice:last .form_disfavor").unbind('submit');
103             $("#notices_primary .notice:last .form_favor").unbind('submit');
104             $("#notices_primary .notice:last .notice_reply").unbind('click');
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           user = data['user'];
119           html = data['html'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
120           source = data['source'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
121
122           ni = "<li class=\"hentry notice\" id=\"notice-"+data['id']+"\">"+
123                "<div class=\"entry-title\">"+
124                "<span class=\"vcard author\">"+
125                "<a href=\""+user['profile_url']+"\" class=\"url\">"+
126                "<img src=\""+user['profile_image_url']+"\" class=\"avatar photo\" width=\"48\" height=\"48\" alt=\""+user['screen_name']+"\"/>"+
127                "<span class=\"nickname fn\">"+user['screen_name']+"</span>"+
128                "</a>"+
129                "</span>"+
130                "<p class=\"entry-content\">"+html+"</p>"+
131                "</div>"+
132                "<div class=\"entry-content\">"+
133                "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+
134                "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+
135                "</a> "+
136                "<span class=\"source\">"+
137                "from "+
138                 "<span class=\"device\">"+source+"</span>"+ // may have a link
139                "</span>";
140           if (data['in_reply_to_status_id']) {
141                ni = ni+" <a class=\"response\" href=\""+data['in_reply_to_status_url']+"\">in context</a>";
142           }
143
144           ni = ni+"</div>"+
145             "<div class=\"notice-options\">";
146
147           if (RealtimeUpdate._userid != 0) {
148                var input = $("form#form_notice fieldset input#token");
149                var session_key = input.val();
150                ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
151                ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
152                if (RealtimeUpdate._userid == data['user']['id']) {
153                     ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
154                }
155           }
156
157           ni = ni+"</div>"+
158                "</li>";
159           return ni;
160      },
161
162      makeFavoriteForm: function(id, session_key)
163      {
164           var ff;
165
166           ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
167                 "<fieldset>"+
168                "<legend>Favor this notice</legend>"+
169                "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
170                "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
171                "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
172                 "</fieldset>"+
173                "</form>";
174           return ff;
175      },
176
177      makeReplyLink: function(id, nickname)
178      {
179           var rl;
180           rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
181           return rl;
182         },
183
184      makeDeleteLink: function(id)
185      {
186           var dl, delurl;
187           delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
188
189           dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
190
191           return dl;
192      },
193
194      initActions: function(url, timeline, path)
195      {
196         var NP = $('#notices_primary');
197         NP.prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
198         NP.css({'position':'relative'});
199
200         $('#realtime_actions').css({
201              'position':'absolute',
202              'top':'-20px',
203              'right':'0',
204              'margin':'0 0 11px 0'
205         });
206
207         $('#realtime_actions li').css({
208             'margin-left':'18px',
209             'list-style-type':'none',
210             'float':'left'
211         });
212
213         RealtimeUpdate._pluginPath = path;
214
215         RealtimeUpdate.initPlayPause();
216         RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
217      },
218
219      initPlayPause: function()
220      {
221         RealtimeUpdate.showPause();
222      },
223
224      showPause: function()
225      {
226         RT_PP = $('#realtime_playpause');
227         RT_PP.empty();
228         RT_PP.append('<button id="realtime_pause" class="pause" title="Pause">Pause</button>');
229
230         RT_P = $('#realtime_pause');
231         $('#realtime_pause').css({
232             'background':'url('+RealtimeUpdate._pluginPath+'icon_pause.gif) no-repeat 47% 47%',
233              'width':'16px',
234              'height':'16px',
235              'display':'block',
236              'border':'none',
237              'cursor':'pointer',
238              'text-indent':'-9999px',
239              'float':'left'
240         });
241         RT_P.bind('click', function() {
242             RealtimeUpdate._paused = true;
243
244             RealtimeUpdate.showPlay();
245             return false;
246         });
247      },
248
249      showPlay: function()
250      {
251         RT_PP = $('#realtime_playpause');
252         RT_PP.empty();
253         RT_PP.append('<span id="queued_counter"></span> <button id="realtime_play" class="play" title="Play">Play</button>');
254
255         $('#queued_counter').css({
256             'float':'left',
257             'line-height':'1.2'
258         });
259
260         RT_P = $('#realtime_play');
261         RT_P.css({
262             'background':'url('+RealtimeUpdate._pluginPath+'icon_play.gif) no-repeat 47% 47%',
263              'width':'16px',
264              'height':'16px',
265              'display':'block',
266              'border':'none',
267              'cursor':'pointer',
268              'text-indent':'-9999px',
269              'float':'left',
270              'margin-left':'4px'
271         });
272         RT_P.bind('click', function() {
273             RealtimeUpdate._paused = false;
274
275             RealtimeUpdate.showPause();
276
277             RealtimeUpdate.showQueuedNotices();
278
279             return false;
280         });
281      },
282
283      showQueuedNotices: function()
284      {
285         $.each(RealtimeUpdate._queuedNotices, function(i, n) {
286             RealtimeUpdate.insertNoticeItem(n);
287         });
288
289         RealtimeUpdate._queuedNotices = [];
290
291         RealtimeUpdate.removeQueuedCounter();
292      },
293
294      updateQueuedCounter: function()
295      {
296         QC = $('#realtime_playpause #queued_counter').html('('+RealtimeUpdate._queuedNotices.length+')');
297      },
298
299      removeQueuedCounter: function()
300      {
301         $('#realtime_playpause #queued_counter').empty();
302      },
303
304      initAddPopup: function(url, timeline, path)
305      {
306          var NP = $('#realtime_timeline');
307          NP.append('<button id="realtime_popup" title="Pop up in a window">Pop up</button>');
308
309          var PP = $('#realtime_popup');
310          PP.css({
311              'background':'transparent url('+ path + 'icon_external.gif) no-repeat 0 30%',
312              'width':'16px',
313              'height':'16px',
314              'display':'block',
315              'border':'none',
316              'cursor':'pointer',
317              'text-indent':'-9999px',
318              'float':'left'
319          });
320          $('#showstream #notices_primary').css({'margin-top':'18px'});
321
322          PP.bind('click', function() {
323              window.open(url,
324                          '',
325                          'toolbar=no,resizable=yes,scrollbars=yes,status=yes,width=500,height=550');
326
327              return false;
328          });
329      },
330
331      initPopupWindow: function()
332      {
333          $('address').hide();
334          $('#content').css({'width':'93.5%'});
335
336          $('#form_notice').css({
337             'margin':'18px 0 18px 1.795%',
338             'width':'93%',
339             'max-width':'451px'
340          });
341
342          $('#form_notice label[for=notice_data-text], h1').css({'display': 'none'});
343
344          $('.notices li:first-child').css({'border-top-color':'transparent'});
345
346          $('#form_notice label[for="notice_data-attach"], #form_notice #notice_data-attach').css({'top':'0'});
347
348          $('#form_notice #notice_data-attach').css({
349             'left':'auto',
350             'right':'0'
351          });
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 }
360