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