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