]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/util.js
3e8d244b72a1925a31f198959f1e9bd5b51d1938
[quix0rs-gnu-social.git] / js / util.js
1 /*
2  * StatusNet - a distributed open-source microblogging tool
3  * Copyright (C) 2008, StatusNet, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 $(document).ready(function(){
20     if ($('body.user_in').length > 0) {
21         if ($('#'+SN.C.S.NoticeDataText).length) {
22             if (maxLength > 0) {
23                 $('#'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
24                     SN.U.Counter();
25                 });
26                 // run once in case there's something in there
27                 SN.U.Counter();
28             }
29
30             $('#'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
31                 SN.U.SubmitOnReturn(e, $('#'+SN.C.S.FormNotice));
32             });
33
34             if($('body')[0].id != 'conversation') {
35                 $('#'+SN.C.S.NoticeDataText).focus();
36             }
37         }
38
39         $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
40         $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
41         $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
42         $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
43         $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
44         $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
45         $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
46
47         SN.U.FormNoticeXHR();
48
49         SN.U.NoticeReply();
50
51         SN.U.NoticeDataAttach();
52     }
53
54     SN.U.NoticeAttachments();
55 });
56
57
58 var SN = { // StatusNet
59     C: { // Config
60         I: {
61             CounterBlackout: false,
62             MaxLength: 140,
63             PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
64             HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
65         },
66         S: { // Selector
67             Disabled: 'disabled',
68             Warning: 'warning',
69             Error: 'error',
70             Success: 'success',
71             Processing: 'processing',
72             CommendResult: 'command_result',
73             FormNotice: 'form_notice',
74             NoticeDataText: 'notice_data-text',
75             NoticeTextCount: 'notice_text-count',
76             NoticeInReplyTo: 'notice_in-reply-to',
77             NoticeDataAttach: 'notice_data-attach',
78             NoticeDataAttachSelected: 'notice_data-attach_selected',
79             NoticeActionSubmit: 'notice_action-submit',
80         }
81     },
82
83     U: { // Utils
84         SubmitOnReturn: function(event, el) {
85             if (event.keyCode == 13 || event.keyCode == 10) {
86                 el.submit();
87                 event.preventDefault();
88                 event.stopPropagation();
89                 $('#'+SN.U.NoticeDataText).blur();
90                 $('body').focus();
91                 return false;
92             }
93             return true;
94         },
95
96         Counter: function() {
97             if (typeof(maxLength) == "undefined") {
98                  maxLength = SN.C.I.MaxLength;
99             }
100
101             if (maxLength <= 0) {
102                 return;
103             }
104
105             var remaining = maxLength - $('#'+SN.C.S.NoticeDataText).val().length;
106             var counter = $('#'+SN.C.S.NoticeTextCount);
107
108             if (remaining.toString() != counter.text()) {
109                 if (!SN.C.I.CounterBlackout || remaining == 0) {
110                     if (counter.text() != String(remaining)) {
111                         counter.text(remaining);
112                     }
113                     if (remaining < 0) {
114                         $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
115                     } else {
116                         $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Warning);
117                     }
118                     // Skip updates for the next 500ms.
119                     // On slower hardware, updating on every keypress is unpleasant.
120                     if (!SN.C.I.CounterBlackout) {
121                         SN.C.I.CounterBlackout = true;
122                         window.setTimeout(SN.U.ClearCounterBlackout, 500);
123                     }
124                 }
125             }
126         },
127
128         ClearCounterBlackout: function() {
129             // Allow keyup events to poke the counter again
130             SN.C.I.CounterBlackout = false;
131             // Check if the string changed since we last looked
132             SN.U.Counter(null);
133         },
134
135         FormXHR: function(f) {
136             f.bind('submit', function(e) {
137                 form_id = $(this)[0].id;
138                 $.ajax({
139                     type: 'POST',
140                     url: $(this)[0].action,
141                     data: $(this).serialize() + '&ajax=1',
142                     beforeSend: function(xhr) {
143                         $('#'+form_id).addClass(SN.C.S.Processing);
144                         $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
145                         $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
146                     },
147                     error: function (xhr, textStatus, errorThrown) {
148                         alert(errorThrown || textStatus);
149                     },
150                     success: function(data, textStatus) {
151                         if ($('form', data)[0].length > 0) {
152                             form_new = $('form', data)[0];
153                             $('#'+form_id).replaceWith(document._importNode(form_new, true));
154                             $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
155                         }
156                         else {
157                             $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
158                         }
159                     }
160                 });
161                 return false;
162             });
163         },
164
165         FormNoticeXHR: function() {
166             $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
167             $('#'+SN.C.S.FormNotice).ajaxForm({
168                 timeout: '60000',
169                 beforeSend: function(xhr) {
170                     if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
171                         $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
172                         return false;
173                     }
174                     $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
175                     $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
176                     $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
177                     return true;
178                 },
179                 error: function (xhr, textStatus, errorThrown) {
180                     $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
181                     $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
182                     $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
183                     if (textStatus == 'timeout') {
184                         alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
185                     }
186                     else {
187                         if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
188                             $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
189                         }
190                         else {
191                             if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
192                                 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
193                             }
194                             else {
195                                 SN.C.I.NoticeDataText.val('');
196 //                                SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
197                             }
198                         }
199                     }
200                 },
201                 success: function(data, textStatus) {
202                     if ($('#'+SN.C.S.Error, data).length > 0) {
203                         var result = document._importNode($('p', data)[0], true);
204                         alert(result.textContent || result.innerHTML);
205                     }
206                     else {
207                         if($('body')[0].id == 'bookmarklet') {
208                             self.close();
209                         }
210                         if ($('#'+SN.C.S.CommandResult, data).length > 0) {
211                             var result = document._importNode($('p', data)[0], true);
212                             alert(result.textContent || result.innerHTML);
213                         }
214                         else {
215                              notice = $('li', data)[0];
216                              if ($('#'+notice.id).length === 0) {
217                                 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
218                                 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
219                                 if($('body')[0].id == 'conversation') {
220                                     if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
221                                         $(notice_irt).append('<ul class="notices"></ul>');
222                                     }
223                                     $($(notice_irt+' .notices')[0]).append(document._importNode(notice, true));
224                                 }
225                                 else {
226                                     $("#notices_primary .notices").prepend(document._importNode(notice, true));
227                                 }
228                                 $('#'+notice.id).css({display:'none'});
229                                 $('#'+notice.id).fadeIn(2500);
230                                 SN.U.NoticeAttachments();
231                                 SN.U.NoticeReply();
232                              }
233                         }
234                         $('#'+SN.C.S.NoticeDataText).val('');
235                         $('#'+SN.C.S.NoticeDataAttach).val('');
236                         $('#'+SN.C.S.NoticeInReplyTo).val('');
237 //                        SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
238                     }
239                 },
240                 complete: function(xhr, textStatus) {
241                     $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
242                     $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
243                     $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
244                 }
245             });
246         },
247
248         NoticeReply: function() {
249             if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
250                 $('#content .notice').each(function() {
251                     var notice = $(this)[0];
252                     $($('.notice_reply', notice)[0]).click(function() {
253                         var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
254                         SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
255                         return false;
256                     });
257                 });
258             }
259         },
260
261         NoticeReplySet: function(nick,id) {
262             if (nick.match(SN.C.I.PatternUsername)) {
263                 var text = $('#'+SN.C.S.NoticeDataText);
264                 if (text.length) {
265                     replyto = '@' + nick + ' ';
266                     text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
267                     $('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id);
268                     if (text.get(0).setSelectionRange) {
269                         var len = text.val().length;
270                         text.get(0).setSelectionRange(len,len);
271                         text.get(0).focus();
272                     }
273                     return false;
274                 }
275             }
276             return true;
277         },
278
279         NoticeAttachments: function() {
280             $.fn.jOverlay.options = {
281                 method : 'GET',
282                 data : '',
283                 url : '',
284                 color : '#000',
285                 opacity : '0.6',
286                 zIndex : 99,
287                 center : false,
288                 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
289                 bgClickToClose : true,
290                 success : function() {
291                     $('#jOverlayContent').append('<button>&#215;</button>');
292                     $('#jOverlayContent button').click($.closeOverlay);
293                 },
294                 timeout : 0,
295                 autoHide : true,
296                 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
297             };
298
299             $('#content .notice a.attachment').click(function() {
300                 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
301                 return false;
302             });
303
304             var t;
305             $("body:not(#shownotice) #content .notice a.thumbnail").hover(
306                 function() {
307                     var anchor = $(this);
308                     $("a.thumbnail").children('img').hide();
309                     anchor.closest(".entry-title").addClass('ov');
310
311                     if (anchor.children('img').length == 0) {
312                         t = setTimeout(function() {
313                             $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
314                                 anchor.append(data);
315                             });
316                         }, 500);
317                     }
318                     else {
319                         anchor.children('img').show();
320                     }
321                 },
322                 function() {
323                     clearTimeout(t);
324                     $("a.thumbnail").children('img').hide();
325                     $(this).closest(".entry-title").removeClass('ov');
326                 }
327             );
328         },
329
330         NoticeDataAttach: function() {
331             NDA = $('#'+SN.C.S.NoticeDataAttach);
332             NDA.change(function() {
333                 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button>&#215;</button></div>';
334                 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
335                 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#'+SN.C.S.FormNotice).append(S);
336                 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
337                     $('#'+SN.C.S.NoticeDataAttachSelected).remove();
338                     NDA.val('');
339                 });
340             });
341         }
342     }
343 }