]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/realtimeupdate.js
Merge branch '0.9.x' of gitorious.org:statusnet/mainline 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      _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           if (RealtimeUpdate._paused === false) {
70               RealtimeUpdate.purgeLastNoticeItem();
71
72               RealtimeUpdate.insertNoticeItem(data);
73           }
74           else {
75               RealtimeUpdate._queuedNotices.push(data);
76
77               RealtimeUpdate.updateQueuedCounter();
78           }
79
80           RealtimeUpdate.updateWindowCounter();
81      },
82
83      insertNoticeItem: function(data) {
84         // Don't add it if it already exists
85         if ($("#notice-"+data.id).length > 0) {
86             return;
87         }
88
89         var noticeItem = RealtimeUpdate.makeNoticeItem(data);
90         $("#notices_primary .notices").prepend(noticeItem);
91         $("#notices_primary .notice:first").css({display:"none"});
92         $("#notices_primary .notice:first").fadeIn(1000);
93
94         SN.U.NoticeReply();
95         SN.U.NoticeFavor();
96      },
97
98      purgeLastNoticeItem: function() {
99         if ($('#notices_primary .notice').length > RealtimeUpdate._maxnotices) {
100             $("#notices_primary .notice:last .form_disfavor").unbind('submit');
101             $("#notices_primary .notice:last .form_favor").unbind('submit');
102             $("#notices_primary .notice:last .notice_reply").unbind('click');
103             $("#notices_primary .notice:last").remove();
104         }
105      },
106
107      updateWindowCounter: function() {
108           if (RealtimeUpdate._windowhasfocus === false) {
109               RealtimeUpdate._updatecounter += 1;
110               document.title = '('+RealtimeUpdate._updatecounter+') ' + RealtimeUpdate._documenttitle;
111           }
112      },
113
114      makeNoticeItem: function(data)
115      {
116           user = data['user'];
117           html = data['html'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
118           source = data['source'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
119
120           ni = "<li class=\"hentry notice\" id=\"notice-"+data['id']+"\">"+
121                "<div class=\"entry-title\">"+
122                "<span class=\"vcard author\">"+
123                "<a href=\""+user['profile_url']+"\" class=\"url\">"+
124                "<img src=\""+user['profile_image_url']+"\" class=\"avatar photo\" width=\"48\" height=\"48\" alt=\""+user['screen_name']+"\"/>"+
125                "<span class=\"nickname fn\">"+user['screen_name']+"</span>"+
126                "</a>"+
127                "</span>"+
128                "<p class=\"entry-content\">"+html+"</p>"+
129                "</div>"+
130                "<div class=\"entry-content\">"+
131                "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+
132                "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+
133                "</a> "+
134                "<span class=\"source\">"+
135                "from "+
136                 "<span class=\"device\">"+source+"</span>"+ // may have a link
137                "</span>";
138           if (data['in_reply_to_status_id']) {
139                ni = ni+" <a class=\"response\" href=\""+data['in_reply_to_status_url']+"\">in context</a>";
140           }
141
142           ni = ni+"</div>"+
143             "<div class=\"notice-options\">";
144
145           if (RealtimeUpdate._userid != 0) {
146                var input = $("form#form_notice fieldset input#token");
147                var session_key = input.val();
148                ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
149                ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
150                if (RealtimeUpdate._userid == data['user']['id']) {
151                     ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
152                }
153           }
154
155           ni = ni+"</div>"+
156                "</li>";
157           return ni;
158      },
159
160      makeFavoriteForm: function(id, session_key)
161      {
162           var ff;
163
164           ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
165                 "<fieldset>"+
166                "<legend>Favor this notice</legend>"+
167                "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
168                "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
169                "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
170                 "</fieldset>"+
171                "</form>";
172           return ff;
173      },
174
175      makeReplyLink: function(id, nickname)
176      {
177           var rl;
178           rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
179           return rl;
180         },
181
182      makeDeleteLink: function(id)
183      {
184           var dl, delurl;
185           delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
186
187           dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
188
189           return dl;
190      },
191
192      initActions: function(url, timeline, path)
193      {
194         var NP = $('#notices_primary');
195         NP.prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
196
197         RealtimeUpdate._pluginPath = path;
198
199         RealtimeUpdate.initPlayPause();
200         RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
201      },
202
203      initPlayPause: function()
204      {
205         RealtimeUpdate.showPause();
206      },
207
208      showPause: function()
209      {
210         RT_PP = $('#realtime_playpause');
211         RT_PP.empty();
212         RT_PP.append('<button id="realtime_pause" class="pause" title="Pause">Pause</button>');
213
214         RT_P = $('#realtime_pause');
215         RT_P.bind('click', function() {
216             RealtimeUpdate._paused = true;
217
218             RealtimeUpdate.showPlay();
219             return false;
220         });
221      },
222
223      showPlay: function()
224      {
225         RT_PP = $('#realtime_playpause');
226         RT_PP.empty();
227         RT_PP.append('<span id="queued_counter"></span> <button id="realtime_play" class="play" title="Play">Play</button>');
228
229         RT_P = $('#realtime_play');
230         RT_P.bind('click', function() {
231             RealtimeUpdate._paused = false;
232
233             RealtimeUpdate.showPause();
234
235             RealtimeUpdate.showQueuedNotices();
236
237             return false;
238         });
239      },
240
241      showQueuedNotices: function()
242      {
243         $.each(RealtimeUpdate._queuedNotices, function(i, n) {
244             RealtimeUpdate.insertNoticeItem(n);
245         });
246
247         RealtimeUpdate._queuedNotices = [];
248
249         RealtimeUpdate.removeQueuedCounter();
250      },
251
252      updateQueuedCounter: function()
253      {
254         $('#realtime_playpause #queued_counter').html('('+RealtimeUpdate._queuedNotices.length+')');
255      },
256
257      removeQueuedCounter: function()
258      {
259         $('#realtime_playpause #queued_counter').empty();
260      },
261
262      initAddPopup: function(url, timeline, path)
263      {
264          var NP = $('#realtime_timeline');
265          NP.append('<button id="realtime_popup" title="Pop up in a window">Pop up</button>');
266
267          var PP = $('#realtime_popup');
268          PP.bind('click', function() {
269              window.open(url,
270                          '',
271                          'toolbar=no,resizable=yes,scrollbars=yes,status=yes,width=500,height=550');
272
273              return false;
274          });
275      },
276
277      initPopupWindow: function()
278      {
279          $('address').hide();
280          $('#content').css({'width':'93.5%'});
281
282          $('#form_notice').css({
283             'margin':'18px 0 18px 1.795%',
284             'width':'93%',
285             'max-width':'451px'
286          });
287
288          $('#form_notice label[for=notice_data-text], h1').css({'display': 'none'});
289
290          $('.notices li:first-child').css({'border-top-color':'transparent'});
291
292          $('#form_notice label[for="notice_data-attach"], #form_notice #notice_data-attach').css({'top':'0'});
293
294          $('#form_notice #notice_data-attach').css({
295             'left':'auto',
296             'right':'0'
297          });
298
299          $('.notices .entry-title a, .notices .entry-content a').bind('click', function() {
300             window.open(this.href, '');
301             
302             return false;
303          });
304      }
305 }
306