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