]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/realtimeupdate.js
ca6ea891a14f4be7b4c4c1351796fa3c3410c325
[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
38      init: function(userid, replyurl, favorurl, deleteurl)
39      {
40         RealtimeUpdate._userid = userid;
41         RealtimeUpdate._replyurl = replyurl;
42         RealtimeUpdate._favorurl = favorurl;
43         RealtimeUpdate._deleteurl = deleteurl;
44
45         DT = document.title;
46
47         $(window).blur(function() {
48           $('#notices_primary .notice').css({
49             'border-top-color':$('#notices_primary .notice:last').css('border-top-color'),
50             'border-top-style':'dotted'
51           });
52
53           $('#notices_primary .notice:first').css({
54             'border-top-color':'#AAAAAA',
55             'border-top-style':'solid'
56           });
57
58           RealtimeUpdate._updatecounter = 0;
59           document.title = DT;
60
61           return false;
62         });
63      },
64
65      receive: function(data)
66      {
67           setTimeout(function() {
68               id = data.id;
69
70               // Don't add it if it already exists
71               if ($("#notice-"+id).length > 0) {
72                    return;
73               }
74
75               var noticeItem = RealtimeUpdate.makeNoticeItem(data);
76               $("#notices_primary .notices").prepend(noticeItem);
77               $("#notices_primary .notice:first").css({display:"none"});
78               $("#notices_primary .notice:first").fadeIn(1000);
79               SN.U.NoticeReply();
80
81               RealtimeUpdate._updatecounter += 1;
82               document.title = '('+RealtimeUpdate._updatecounter+') ' + DT;
83           }, 500);
84      },
85
86      makeNoticeItem: function(data)
87      {
88           user = data['user'];
89           html = data['html'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
90           source = data['source'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
91
92           ni = "<li class=\"hentry notice\" id=\"notice-"+data['id']+"\">"+
93                "<div class=\"entry-title\">"+
94                "<span class=\"vcard author\">"+
95                "<a href=\""+user['profile_url']+"\" class=\"url\">"+
96                "<img src=\""+user['profile_image_url']+"\" class=\"avatar photo\" width=\"48\" height=\"48\" alt=\""+user['screen_name']+"\"/>"+
97                "<span class=\"nickname fn\">"+user['screen_name']+"</span>"+
98                "</a>"+
99                "</span>"+
100                "<p class=\"entry-content\">"+html+"</p>"+
101                "</div>"+
102                "<div class=\"entry-content\">"+
103                "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+
104                "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+
105                "</a> "+
106                "<span class=\"source\">"+
107                "from "+
108                 "<span class=\"device\">"+source+"</span>"+ // may have a link
109                "</span>";
110           if (data['in_reply_to_status_id']) {
111                ni = ni+" <a class=\"response\" href=\""+data['in_reply_to_status_url']+"\">in context</a>";
112           }
113
114           ni = ni+"</div>"+
115             "<div class=\"notice-options\">";
116
117           if (RealtimeUpdate._userid != 0) {
118                var input = $("form#form_notice fieldset input#token");
119                var session_key = input.val();
120                ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
121                ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
122                if (RealtimeUpdate._userid == data['user']['id']) {
123                     ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
124                }
125           }
126
127           ni = ni+"</div>"+
128                "</li>";
129           return ni;
130      },
131
132      makeFavoriteForm: function(id, session_key)
133      {
134           var ff;
135
136           ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
137                 "<fieldset>"+
138                "<legend>Favor this notice</legend>"+
139                "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
140                "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
141                "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
142                 "</fieldset>"+
143                "</form>";
144           return ff;
145      },
146
147      makeReplyLink: function(id, nickname)
148      {
149           var rl;
150           rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
151           return rl;
152         },
153
154      makeDeleteLink: function(id)
155      {
156           var dl, delurl;
157           delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
158
159           dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
160
161           return dl;
162      },
163
164      addPopup: function(url, timeline, iconurl)
165      {
166          $('#notices_primary').css({'position':'relative'});
167          $('#notices_primary').prepend('<button id="realtime_timeline" title="Pop up in a window">Pop up</button>');
168
169          $('#realtime_timeline').css({
170              'margin':'0 0 11px 0',
171              'background':'transparent url('+ iconurl + ') no-repeat 0% 30%',
172              'padding':'0 0 0 20px',
173              'display':'block',
174              'position':'absolute',
175              'top':'-20px',
176              'right':'0',
177              'border':'none',
178              'cursor':'pointer',
179              'color':$("a").css("color"),
180              'font-weight':'bold',
181              'font-size':'1em'
182          });
183
184          $('#realtime_timeline').click(function() {
185              window.open(url,
186                          timeline,
187                          'toolbar=no,resizable=yes,scrollbars=yes,status=yes');
188
189              return false;
190          });
191      },
192
193      initPopupWindow: function()
194      {
195          window.resizeTo(500, 550);
196          $('address').hide();
197          $('#content').css({'width':'93.5%'});
198
199          $('#form_notice').css({
200             'margin':'18px 0 18px 1.795%',
201             'width':'93%',
202             'max-width':'451px'
203          });
204
205          $('#form_notice label[for=notice_data-text], h1').css({'display': 'none'});
206
207          $('.notices li:first-child').css({'border-top-color':'transparent'});
208
209          $('#form_notice label[for="notice_data-attach"], #form_notice #notice_data-attach').css({'top':'0'});
210
211          $('#form_notice #notice_data-attach').css({
212             'left':'auto',
213             'right':'0'
214          });
215      }
216 }
217