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,
45 init: function(userid, replyurl, favorurl, repeaturl, deleteurl)
47 RealtimeUpdate._userid = userid;
48 RealtimeUpdate._replyurl = replyurl;
49 RealtimeUpdate._favorurl = favorurl;
50 RealtimeUpdate._repeaturl = repeaturl;
51 RealtimeUpdate._deleteurl = deleteurl;
53 RealtimeUpdate._documenttitle = document.title;
55 $(window).bind('focus', function(){ RealtimeUpdate._windowhasfocus = true; });
57 $(window).bind('blur', function() {
58 $('#notices_primary .notice').removeClass('mark-top');
60 $('#notices_primary .notice:first').addClass('mark-top');
62 RealtimeUpdate._updatecounter = 0;
63 document.title = RealtimeUpdate._documenttitle;
64 RealtimeUpdate._windowhasfocus = false;
70 receive: function(data)
72 if (RealtimeUpdate._paused === false) {
73 RealtimeUpdate.purgeLastNoticeItem();
75 RealtimeUpdate.insertNoticeItem(data);
78 RealtimeUpdate._queuedNotices.push(data);
80 RealtimeUpdate.updateQueuedCounter();
83 RealtimeUpdate.updateWindowCounter();
86 insertNoticeItem: function(data) {
87 // Don't add it if it already exists
88 if ($("#notice-"+data.id).length > 0) {
92 var noticeItem = RealtimeUpdate.makeNoticeItem(data);
93 var noticeItemID = $(noticeItem).attr('id');
95 $("#notices_primary .notices").prepend(noticeItem);
96 $("#notices_primary .notice:first").css({display:"none"});
97 $("#notices_primary .notice:first").fadeIn(1000);
99 SN.U.NoticeReplyTo($('#'+noticeItemID));
100 SN.U.NoticeWithAttachment($('#'+noticeItemID));
103 purgeLastNoticeItem: function() {
104 if ($('#notices_primary .notice').length > RealtimeUpdate._maxnotices) {
105 $("#notices_primary .notice:last").remove();
109 updateWindowCounter: function() {
110 if (RealtimeUpdate._windowhasfocus === false) {
111 RealtimeUpdate._updatecounter += 1;
112 document.title = '('+RealtimeUpdate._updatecounter+') ' + RealtimeUpdate._documenttitle;
116 makeNoticeItem: function(data)
118 if (data.hasOwnProperty('retweeted_status')) {
119 original = data['retweeted_status'];
122 unique = repeat['id'];
123 responsible = repeat['user'];
128 responsible = data['user'];
132 html = data['html'].replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/&/g,'&');
133 source = data['source'].replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/&/g,'&');
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>"+
143 "<p class=\"entry-content\">"+html+"</p>"+
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>"+
149 "<span class=\"source\">"+
151 "<span class=\"device\">"+source+"</span>"+ // may have a link
153 if (data['conversation_url']) {
154 ni = ni+" <a class=\"response\" href=\""+data['conversation_url']+"\">in context</a>";
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>";
166 ni = ni + "<div class=\"notice-options\">";
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);
186 makeFavoriteForm: function(id, session_key)
190 ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
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\"/>"+
201 makeReplyLink: function(id, nickname)
204 rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
208 makeRepeatForm: function(id, session_key)
211 rf = "<form id=\"repeat-"+id+"\" class=\"form_repeat\" method=\"post\" action=\""+RealtimeUpdate._repeaturl+"\">"+
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\"/>"+
223 makeDeleteLink: function(id)
226 delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
228 dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
233 initActions: function(url, timeline, path)
235 $('#notices_primary').prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
237 RealtimeUpdate._pluginPath = path;
239 RealtimeUpdate.initPlayPause();
240 RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
243 initPlayPause: function()
245 if (typeof(localStorage) == 'undefined') {
246 RealtimeUpdate.showPause();
249 if (localStorage.getItem('RealtimeUpdate_paused') === 'true') {
250 RealtimeUpdate.showPlay();
253 RealtimeUpdate.showPause();
258 showPause: function()
260 RealtimeUpdate.setPause(false);
261 RealtimeUpdate.showQueuedNotices();
262 RealtimeUpdate.addNoticesHover();
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();
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();
288 setPause: function(state)
290 RealtimeUpdate._paused = state;
291 if (typeof(localStorage) != 'undefined') {
292 localStorage.setItem('RealtimeUpdate_paused', RealtimeUpdate._paused);
296 showQueuedNotices: function()
298 $.each(RealtimeUpdate._queuedNotices, function(i, n) {
299 RealtimeUpdate.insertNoticeItem(n);
302 RealtimeUpdate._queuedNotices = [];
304 RealtimeUpdate.removeQueuedCounter();
307 updateQueuedCounter: function()
309 $('#realtime_playpause #queued_counter').html('('+RealtimeUpdate._queuedNotices.length+')');
312 removeQueuedCounter: function()
314 $('#realtime_playpause #queued_counter').empty();
317 addNoticesHover: function()
319 $('#notices_primary .notices').hover(
321 if (RealtimeUpdate._paused === false) {
322 RealtimeUpdate.showPlay();
326 if (RealtimeUpdate._paused === true) {
327 RealtimeUpdate.showPause();
333 removeNoticesHover: function()
335 $('#notices_primary .notices').unbind();
338 initAddPopup: function(url, timeline, path)
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() {
346 'toolbar=no,resizable=yes,scrollbars=yes,status=no,menubar=no,personalbar=no,location=no,width=500,height=550');
352 initPopupWindow: function()
354 $('.notices .entry-title a, .notices .entry-content a').bind('click', function() {
355 window.open(this.href, '');
360 $('#showstream .entity_profile').css({'width':'69%'});