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