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