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/
38 _windowhasfocus: true,
43 init: function(userid, replyurl, favorurl, deleteurl)
45 RealtimeUpdate._userid = userid;
46 RealtimeUpdate._replyurl = replyurl;
47 RealtimeUpdate._favorurl = favorurl;
48 RealtimeUpdate._deleteurl = deleteurl;
50 RealtimeUpdate._documenttitle = document.title;
52 $(window).bind('focus', function(){ RealtimeUpdate._windowhasfocus = true; });
54 $(window).bind('blur', function() {
55 $('#notices_primary .notice').removeClass('mark-top');
57 $('#notices_primary .notice:first').addClass('mark-top');
59 RealtimeUpdate._updatecounter = 0;
60 document.title = RealtimeUpdate._documenttitle;
61 RealtimeUpdate._windowhasfocus = false;
67 receive: function(data)
69 if (RealtimeUpdate._paused === false) {
70 RealtimeUpdate.purgeLastNoticeItem();
72 RealtimeUpdate.insertNoticeItem(data);
75 RealtimeUpdate._queuedNotices.push(data);
77 RealtimeUpdate.updateQueuedCounter();
80 RealtimeUpdate.updateWindowCounter();
83 insertNoticeItem: function(data) {
84 // Don't add it if it already exists
85 if ($("#notice-"+data.id).length > 0) {
89 var noticeItem = RealtimeUpdate.makeNoticeItem(data);
90 var noticeItemID = $(noticeItem).attr('id');
92 $("#notices_primary .notices").prepend(noticeItem);
93 $("#notices_primary .notice:first").css({display:"none"});
94 $("#notices_primary .notice:first").fadeIn(1000);
96 SN.U.FormXHR($('#'+noticeItemID+' .form_favor'));
100 purgeLastNoticeItem: function() {
101 if ($('#notices_primary .notice').length > RealtimeUpdate._maxnotices) {
102 $("#notices_primary .notice:last").remove();
106 updateWindowCounter: function() {
107 if (RealtimeUpdate._windowhasfocus === false) {
108 RealtimeUpdate._updatecounter += 1;
109 document.title = '('+RealtimeUpdate._updatecounter+') ' + RealtimeUpdate._documenttitle;
113 makeNoticeItem: function(data)
116 html = data['html'].replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
117 source = data['source'].replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
119 ni = "<li class=\"hentry notice\" id=\"notice-"+data['id']+"\">"+
120 "<div class=\"entry-title\">"+
121 "<span class=\"vcard author\">"+
122 "<a href=\""+user['profile_url']+"\" class=\"url\">"+
123 "<img src=\""+user['profile_image_url']+"\" class=\"avatar photo\" width=\"48\" height=\"48\" alt=\""+user['screen_name']+"\"/>"+
124 "<span class=\"nickname fn\">"+user['screen_name']+"</span>"+
127 "<p class=\"entry-content\">"+html+"</p>"+
129 "<div class=\"entry-content\">"+
130 "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+
131 "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+
133 "<span class=\"source\">"+
135 "<span class=\"device\">"+source+"</span>"+ // may have a link
137 if (data['in_reply_to_status_id']) {
138 ni = ni+" <a class=\"response\" href=\""+data['in_reply_to_status_url']+"\">in context</a>";
142 "<div class=\"notice-options\">";
144 if (RealtimeUpdate._userid != 0) {
145 var input = $("form#form_notice fieldset input#token");
146 var session_key = input.val();
147 ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
148 ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
149 if (RealtimeUpdate._userid == data['user']['id']) {
150 ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
159 makeFavoriteForm: function(id, session_key)
163 ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
165 "<legend>Favor this notice</legend>"+
166 "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
167 "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
168 "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
174 makeReplyLink: function(id, nickname)
177 rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
181 makeDeleteLink: function(id)
184 delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
186 dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
191 initActions: function(url, timeline, path)
193 var NP = $('#notices_primary');
194 NP.prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
196 RealtimeUpdate._pluginPath = path;
198 RealtimeUpdate.initPlayPause();
199 RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
202 initPlayPause: function()
204 RealtimeUpdate.showPause();
207 showPause: function()
209 RT_PP = $('#realtime_playpause');
211 RT_PP.append('<button id="realtime_pause" class="pause" title="Pause">Pause</button>');
213 RT_P = $('#realtime_pause');
214 RT_P.bind('click', function() {
215 RealtimeUpdate._paused = true;
217 RealtimeUpdate.showPlay();
224 RT_PP = $('#realtime_playpause');
226 RT_PP.append('<span id="queued_counter"></span> <button id="realtime_play" class="play" title="Play">Play</button>');
228 RT_P = $('#realtime_play');
229 RT_P.bind('click', function() {
230 RealtimeUpdate._paused = false;
232 RealtimeUpdate.showPause();
234 RealtimeUpdate.showQueuedNotices();
240 showQueuedNotices: function()
242 $.each(RealtimeUpdate._queuedNotices, function(i, n) {
243 RealtimeUpdate.insertNoticeItem(n);
246 RealtimeUpdate._queuedNotices = [];
248 RealtimeUpdate.removeQueuedCounter();
251 updateQueuedCounter: function()
253 $('#realtime_playpause #queued_counter').html('('+RealtimeUpdate._queuedNotices.length+')');
256 removeQueuedCounter: function()
258 $('#realtime_playpause #queued_counter').empty();
261 initAddPopup: function(url, timeline, path)
263 var NP = $('#realtime_timeline');
264 NP.append('<button id="realtime_popup" title="Pop up in a window">Pop up</button>');
266 var PP = $('#realtime_popup');
267 PP.bind('click', function() {
270 'toolbar=no,resizable=yes,scrollbars=yes,status=no,menubar=no,personalbar=no,location=no,width=500,height=550');
276 initPopupWindow: function()
278 $('.notices .entry-title a, .notices .entry-content a').bind('click', function() {
279 window.open(this.href, '');