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