]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/realtimeupdate.js
da2f9ed3aa30fc7463a2b2eafce3969dbd1c5627
[quix0rs-gnu-social.git] / plugins / Realtime / realtimeupdate.js
1 $(document).ready(function() {
2     if (!$(document).getUrlParam('realtime')) {
3         $('#site_nav_local_views .current a').append('<button id="realtime_timeline" title="Pop this tab">&#8599;</button>');
4
5         $('#realtime_timeline').css({
6             'margin':'2px 0 0 11px',
7             'background':'transparent url('+$('address .url')[0].href+'/plugins/Realtime/icon_external.gif) no-repeat 45% 45%',
8             'text-indent':'-9999px',
9             'width':'16px',
10             'height':'16px',
11             'padding':'0',
12             'display':'block',
13             'float':'right',
14             'border':'none',
15             'cursor':'pointer'
16         });
17
18         $('#realtime_timeline').click(function() {
19             window.open($(this).parent('a').attr('href')+'?realtime=1',
20                         $(this).parent('a').attr('title'),
21                         'toolbar=no,resizable=yes,scrollbars=yes,status=yes');
22
23             return false;
24         });
25     }
26     else {
27         window.resizeTo(575, 640);
28         address = $('address');
29         content = $('#content');
30         $('body').html(address);
31         $('address').hide();
32         $('body').append(content);
33         $('#content').css({'width':'92%'});
34     }
35
36
37     // add a notice encoded as JSON into the current timeline
38     //
39     // TODO: i18n
40
41     RealtimeUpdate = {
42         _userid: 0,
43         _replyurl: '',
44         _favorurl: '',
45         _deleteurl: '',
46
47         init: function(userid, replyurl, favorurl, deleteurl)
48         {
49             RealtimeUpdate._userid = userid;
50             RealtimeUpdate._replyurl = replyurl;
51             RealtimeUpdate._favorurl = favorurl;
52             RealtimeUpdate._deleteurl = deleteurl;
53         },
54
55         receive: function(data)
56         {
57             id = data.id;
58
59             // Don't add it if it already exists
60
61             if ($("#notice-"+id).length > 0) {
62                 return;
63             }
64
65             var noticeItem = RealtimeUpdate.makeNoticeItem(data);
66             $("#notices_primary .notices").prepend(noticeItem, true);
67             $("#notices_primary .notice:first").css({display:"none"});
68             $("#notices_primary .notice:first").fadeIn(1000);
69             NoticeReply();
70         },
71
72         makeNoticeItem: function(data)
73         {
74             user = data['user'];
75             html = data['html'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
76             source = data['source'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
77
78             ni = "<li class=\"hentry notice\" id=\"notice-"+data['id']+"\">"+
79                 "<div class=\"entry-title\">"+
80                 "<span class=\"vcard author\">"+
81                 "<a href=\""+user['profile_url']+"\" class=\"url\">"+
82                 "<img src=\""+user['profile_image_url']+"\" class=\"avatar photo\" width=\"48\" height=\"48\" alt=\""+user['screen_name']+"\"/>"+
83                 "<span class=\"nickname fn\">"+user['screen_name']+"</span>"+
84                 "</a>"+
85                 "</span>"+
86                 "<p class=\"entry-content\">"+html+"</p>"+
87                 "</div>"+
88                 "<div class=\"entry-content\">"+
89                 "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+
90                 "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+
91                 "</a> "+
92                 "<span class=\"source\">"+
93                 "from "+
94                 "<span class=\"device\">"+source+"</span>"+ // may have a link
95                 "</span>";
96             if (data['in_reply_to_status_id']) {
97                 ni = ni+" <a class=\"response\" href=\""+data['in_reply_to_status_url']+"\">in context</a>";
98             }
99
100             ni = ni+"</div>"+
101             "<div class=\"notice-options\">";
102
103             if (RealtimeUpdate._userid != 0) {
104                 var input = $("form#form_notice fieldset input#token");
105                 var session_key = input.val();
106                 ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
107                 ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
108                 if (RealtimeUpdate._userid == data['user']['id']) {
109                     ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
110                 }
111              }
112
113             ni = ni+"</div>"+
114             "</li>";
115             return ni;
116         },
117
118         makeFavoriteForm: function(id, session_key)
119         {
120             var ff;
121
122             ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
123                 "<fieldset>"+
124                 "<legend>Favor this notice</legend>"+
125                 "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
126                 "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
127                 "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
128                 "</fieldset>"+
129                 "</form>";
130             return ff;
131         },
132
133         makeReplyLink: function(id, nickname)
134         {
135             var rl;
136             rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>";
137             return rl;
138         },
139
140         makeDeleteLink: function(id)
141         {
142             var dl, delurl;
143             delurl = RealtimeUpdate._deleteurl.replace("0000000000", id);
144
145             dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>";
146
147             return dl;
148         }
149     }
150
151 });
152