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.NoticeReplyTo($('#'+noticeItemID));
99 SN.U.NoticeWithAttachment($('#'+noticeItemID));
102 purgeLastNoticeItem: function() {
103 if ($('#notices_primary .notice').length > RealtimeUpdate._maxnotices) {
104 $("#notices_primary .notice:last").remove();
108 updateWindowCounter: function() {
109 if (RealtimeUpdate._windowhasfocus === false) {
110 RealtimeUpdate._updatecounter += 1;
111 document.title = '('+RealtimeUpdate._updatecounter+') ' + RealtimeUpdate._documenttitle;
115 makeNoticeItem: function(data)
117 if (data.hasOwnProperty('retweeted_status')) {
118 original = data['retweeted_status'];
121 unique = repeat['id'];
122 responsible = repeat['user'];
127 responsible = data['user'];
131 html = data['html'].replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/&/g,'&');
132 source = data['source'].replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/&/g,'&');
134 ni = "<li class=\"hentry notice\" id=\"notice-"+unique+"\">"+
135 "<div class=\"entry-title\">"+
136 "<span class=\"vcard author\">"+
137 "<a href=\""+user['profile_url']+"\" class=\"url\" title=\""+user['name']+"\">"+
138 "<img src=\""+user['profile_image_url']+"\" class=\"avatar photo\" width=\"48\" height=\"48\" alt=\""+user['screen_name']+"\"/>"+
139 "<span class=\"nickname fn\">"+user['screen_name']+"</span>"+
142 "<p class=\"entry-content\">"+html+"</p>"+
144 "<div class=\"entry-content\">"+
145 "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+
146 "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+
148 "<span class=\"source\">"+
150 "<span class=\"device\">"+source+"</span>"+ // may have a link
152 if (data['conversation_url']) {
153 ni = ni+" <a class=\"response\" href=\""+data['conversation_url']+"\">in context</a>";
158 ni = ni + "<span class=\"repeat vcard\">Repeated by " +
159 "<a href=\"" + ru['profile_url'] + "\" class=\"url\">" +
160 "<span class=\"nickname\">"+ ru['screen_name'] + "</span></a></span>";
165 ni = ni + "<div class=\"notice-options\">";
167 if (RealtimeUpdate._userid != 0) {
168 var input = $("form#form_notice fieldset input#token");
169 var session_key = input.val();
170 ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
171 ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
172 if (RealtimeUpdate._userid == responsible['id']) {
173 ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
174 } else if (RealtimeUpdate._userid != user['id']) {
175 ni = ni+RealtimeUpdate.makeRepeatForm(data['id'], session_key);
185 makeFavoriteForm: function(id, session_key)
189 ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
191 "<legend>Favor this notice</legend>"+
192 "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
193 "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
194 "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
200 makeReplyLink: function(id, nickname)
203 rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
207 makeRepeatForm: function(id, session_key)
210 rf = "<form id=\"repeat-"+id+"\" class=\"form_repeat\" method=\"post\" action=\""+RealtimeUpdate._repeaturl+"\">"+
212 "<legend>Repeat this notice?</legend>"+
213 "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
214 "<input name=\"notice\" type=\"hidden\" id=\"notice-"+id+"\" value=\""+id+"\"/>"+
215 "<input type=\"submit\" id=\"repeat-submit-"+id+"\" name=\"repeat-submit-"+id+"\" class=\"submit\" value=\"Yes\" title=\"Repeat this notice\"/>"+
222 makeDeleteLink: function(id)
225 delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
227 dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
232 initActions: function(url, timeline, path)
234 $('#notices_primary').prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
236 RealtimeUpdate._pluginPath = path;
238 RealtimeUpdate.initPlayPause();
239 RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
242 initPlayPause: function()
244 if (typeof(localStorage) == 'undefined') {
245 RealtimeUpdate.showPause();
248 if (localStorage.getItem('RealtimeUpdate_paused') === 'true') {
249 RealtimeUpdate.showPlay();
252 RealtimeUpdate.showPause();
257 showPause: function()
259 RealtimeUpdate.setPause(false);
260 RealtimeUpdate.showQueuedNotices();
261 RealtimeUpdate.addNoticesHover();
263 $('#realtime_playpause').remove();
264 $('#realtime_actions').prepend('<li id="realtime_playpause"><button id="realtime_pause" class="pause" title="Pause">Pause</button></li>');
266 $('#realtime_pause').bind('click', function() {
267 RealtimeUpdate.removeNoticesHover();
268 RealtimeUpdate.showPlay();
275 RealtimeUpdate.setPause(true);
276 $('#realtime_playpause').remove();
277 $('#realtime_actions').prepend('<li id="realtime_playpause"><span id="queued_counter"></span> <button id="realtime_play" class="play" title="Play">Play</button></li>');
279 $('#realtime_play').bind('click', function() {
280 RealtimeUpdate.showPause();
285 setPause: function(state)
287 RealtimeUpdate._paused = state;
288 if (typeof(localStorage) != 'undefined') {
289 localStorage.setItem('RealtimeUpdate_paused', RealtimeUpdate._paused);
293 showQueuedNotices: function()
295 $.each(RealtimeUpdate._queuedNotices, function(i, n) {
296 RealtimeUpdate.insertNoticeItem(n);
299 RealtimeUpdate._queuedNotices = [];
301 RealtimeUpdate.removeQueuedCounter();
304 updateQueuedCounter: function()
306 $('#realtime_playpause #queued_counter').html('('+RealtimeUpdate._queuedNotices.length+')');
309 removeQueuedCounter: function()
311 $('#realtime_playpause #queued_counter').empty();
314 addNoticesHover: function()
316 $('#notices_primary .notices').hover(
318 if (RealtimeUpdate._paused === false) {
319 RealtimeUpdate.showPlay();
323 if (RealtimeUpdate._paused === true) {
324 RealtimeUpdate.showPause();
330 removeNoticesHover: function()
332 $('#notices_primary .notices').unbind();
335 initAddPopup: function(url, timeline, path)
337 $('#realtime_timeline').append('<button id="realtime_popup" title="Pop up in a window">Pop up</button>');
339 $('#realtime_popup').bind('click', function() {
342 'toolbar=no,resizable=yes,scrollbars=yes,status=no,menubar=no,personalbar=no,location=no,width=500,height=550');
348 initPopupWindow: function()
350 $('.notices .entry-title a, .notices .entry-content a').bind('click', function() {
351 window.open(this.href, '');
356 $('#showstream .entity_profile').css({'width':'69%'});