2 * StatusNet - a distributed open-source microblogging tool
3 * Copyright (C) 2008, StatusNet, Inc.
5 * Add a notice encoded as JSON into the current timeline
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.
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.
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/>.
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/
39 _windowhasfocus: true,
44 init: function(userid, replyurl, favorurl, repeaturl, deleteurl)
46 RealtimeUpdate._userid = userid;
47 RealtimeUpdate._replyurl = replyurl;
48 RealtimeUpdate._favorurl = favorurl;
49 RealtimeUpdate._repeaturl = repeaturl;
50 RealtimeUpdate._deleteurl = deleteurl;
52 RealtimeUpdate._documenttitle = document.title;
54 $(window).bind('focus', function(){ RealtimeUpdate._windowhasfocus = true; });
56 $(window).bind('blur', function() {
57 $('#notices_primary .notice').removeClass('mark-top');
59 $('#notices_primary .notice:first').addClass('mark-top');
61 RealtimeUpdate._updatecounter = 0;
62 document.title = RealtimeUpdate._documenttitle;
63 RealtimeUpdate._windowhasfocus = false;
69 receive: function(data)
71 if (RealtimeUpdate._paused === false) {
72 RealtimeUpdate.purgeLastNoticeItem();
74 RealtimeUpdate.insertNoticeItem(data);
77 RealtimeUpdate._queuedNotices.push(data);
79 RealtimeUpdate.updateQueuedCounter();
82 RealtimeUpdate.updateWindowCounter();
85 insertNoticeItem: function(data) {
86 // Don't add it if it already exists
87 if ($("#notice-"+data.id).length > 0) {
91 var noticeItem = RealtimeUpdate.makeNoticeItem(data);
92 var noticeItemID = $(noticeItem).attr('id');
94 $("#notices_primary .notices").prepend(noticeItem);
95 $("#notices_primary .notice:first").css({display:"none"});
96 $("#notices_primary .notice:first").fadeIn(1000);
98 SN.U.FormXHR($('#'+noticeItemID+' .form_favor'));
99 SN.U.NoticeReplyTo($('#'+noticeItemID));
100 SN.U.FormXHR($('#'+noticeItemID+' .form_repeat'));
101 SN.U.NoticeWithAttachment($('#'+noticeItemID));
104 purgeLastNoticeItem: function() {
105 if ($('#notices_primary .notice').length > RealtimeUpdate._maxnotices) {
106 $("#notices_primary .notice:last").remove();
110 updateWindowCounter: function() {
111 if (RealtimeUpdate._windowhasfocus === false) {
112 RealtimeUpdate._updatecounter += 1;
113 document.title = '('+RealtimeUpdate._updatecounter+') ' + RealtimeUpdate._documenttitle;
117 makeNoticeItem: function(data)
119 if (data.hasOwnProperty('retweeted_status')) {
120 original = data['retweeted_status'];
123 unique = repeat['id'];
124 responsible = repeat['user'];
129 responsible = data['user'];
133 html = data['html'].replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/&/g,'&');
134 source = data['source'].replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/&/g,'&');
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>"+
144 "<p class=\"entry-content\">"+html+"</p>"+
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>"+
150 "<span class=\"source\">"+
152 "<span class=\"device\">"+source+"</span>"+ // may have a link
154 if (data['in_reply_to_status_id']) {
155 ni = ni+" <a class=\"response\" href=\""+data['in_reply_to_status_url']+"\">in context</a>";
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>";
167 ni = ni + "<div class=\"notice-options\">";
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);
187 makeFavoriteForm: function(id, session_key)
191 ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
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\"/>"+
202 makeReplyLink: function(id, nickname)
205 rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
209 makeRepeatForm: function(id, session_key)
212 rf = "<form id=\"repeat-"+id+"\" class=\"form_repeat\" method=\"post\" action=\""+RealtimeUpdate._repeaturl+"\">"+
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\"/>"+
224 makeDeleteLink: function(id)
227 delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
229 dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
234 initActions: function(url, timeline, path)
236 $('#notices_primary').prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
238 RealtimeUpdate._pluginPath = path;
240 RealtimeUpdate.initPlayPause();
241 RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
244 initPlayPause: function()
246 if (typeof(localStorage) == 'undefined') {
247 RealtimeUpdate.showPause();
250 if (localStorage.getItem('RealtimeUpdate_paused') === 'true') {
251 RealtimeUpdate.showPlay();
254 RealtimeUpdate.showPause();
259 showPause: function()
261 RealtimeUpdate.setPause(false);
262 RealtimeUpdate.showQueuedNotices();
263 RealtimeUpdate.addNoticesHover();
265 $('#realtime_playpause').remove();
266 $('#realtime_actions').prepend('<li id="realtime_playpause"><button id="realtime_pause" class="pause" title="Pause">Pause</button></li>');
268 $('#realtime_pause').bind('click', function() {
269 RealtimeUpdate.removeNoticesHover();
270 RealtimeUpdate.showPlay();
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>');
281 $('#realtime_play').bind('click', function() {
282 RealtimeUpdate.showPause();
287 setPause: function(state)
289 RealtimeUpdate._paused = state;
290 if (typeof(localStorage) != 'undefined') {
291 localStorage.setItem('RealtimeUpdate_paused', RealtimeUpdate._paused);
295 showQueuedNotices: function()
297 $.each(RealtimeUpdate._queuedNotices, function(i, n) {
298 RealtimeUpdate.insertNoticeItem(n);
301 RealtimeUpdate._queuedNotices = [];
303 RealtimeUpdate.removeQueuedCounter();
306 updateQueuedCounter: function()
308 $('#realtime_playpause #queued_counter').html('('+RealtimeUpdate._queuedNotices.length+')');
311 removeQueuedCounter: function()
313 $('#realtime_playpause #queued_counter').empty();
316 addNoticesHover: function()
318 $('#notices_primary .notices').hover(
320 if (RealtimeUpdate._paused === false) {
321 RealtimeUpdate.showPlay();
325 if (RealtimeUpdate._paused === true) {
326 RealtimeUpdate.showPause();
332 removeNoticesHover: function()
334 $('#notices_primary .notices').unbind();
337 initAddPopup: function(url, timeline, path)
339 $('#realtime_timeline').append('<button id="realtime_popup" title="Pop up in a window">Pop up</button>');
341 $('#realtime_popup').bind('click', function() {
344 'toolbar=no,resizable=yes,scrollbars=yes,status=no,menubar=no,personalbar=no,location=no,width=500,height=550');
350 initPopupWindow: function()
352 $('.notices .entry-title a, .notices .entry-content a').bind('click', function() {
353 window.open(this.href, '');
358 $('#showstream .entity_profile').css({'width':'69%'});