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