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