]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/util.js
e5f117df0feb40b7ae98b2c91763698551741ba6
[quix0rs-gnu-social.git] / js / util.js
1 /*
2  * Laconica - a distributed open-source microblogging tool
3  * Copyright (C) 2008, Controlez-Vous, 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 favoptions = { dataType: 'xml',
85                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
86                                                                                                 var dis = new_form.id;
87                                                                                                 var fav = dis.replace('disfavor', 'favor');
88                                                                                                 $('form#'+fav).replaceWith(new_form);
89                                                                                                 $('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden);
90                                                                                           }
91                                          };
92
93         var disoptions = { dataType: 'xml',
94                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
95                                                                                                 var fav = new_form.id;
96                                                                                                 var dis = fav.replace('favor', 'disfavor');
97                                                                                                 $('form#'+dis).replaceWith(new_form);
98                                                                                                 $('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden);
99                                                                                           }
100                                          };
101
102         var joinoptions = { dataType: 'xml',
103                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
104                                                                                                 var leave = new_form.id;
105                                                                                                 var join = leave.replace('leave', 'join');
106                                                                                                 $('form#'+join).replaceWith(new_form);
107                                                                                                 $('form#'+leave).ajaxForm(leaveoptions).each(addAjaxHidden);
108                                                                                           }
109                                          };
110
111         var leaveoptions = { dataType: 'xml',
112                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
113                                                                                                 var join = new_form.id;
114                                                                                                 var leave = join.replace('join', 'leave');
115                                                                                                 $('form#'+leave).replaceWith(new_form);
116                                                                                                 $('form#'+join).ajaxForm(joinoptions).each(addAjaxHidden);
117                                                                                           }
118                                          };
119
120         function addAjaxHidden() {
121                 var ajax = document.createElement('input');
122                 ajax.setAttribute('type', 'hidden');
123                 ajax.setAttribute('name', 'ajax');
124                 ajax.setAttribute('value', 1);
125                 this.appendChild(ajax);
126         }
127
128         $("form.form_favor").ajaxForm(favoptions);
129         $("form.form_disfavor").ajaxForm(disoptions);
130         $("form.form_group_join").ajaxForm(joinoptions);
131         $("form.form_group_leave").ajaxForm(leaveoptions);
132         $("form.form_favor").each(addAjaxHidden);
133         $("form.form_disfavor").each(addAjaxHidden);
134         $("form.form_group_join").each(addAjaxHidden);
135         $("form.form_group_leave").each(addAjaxHidden);
136
137         $("#form_user_nudge").ajaxForm ({ dataType: 'xml',
138                 beforeSubmit: function(xml) { $("#form_user_nudge input[type=submit]").attr("disabled", "disabled");
139                                                                           $("#form_user_nudge input[type=submit]").addClass("disabled");
140                                                                         },
141                 success: function(xml) { $("#form_user_nudge").replaceWith(document._importNode($("#nudge_response", xml).get(0),true));
142                                                              $("#form_user_nudge input[type=submit]").removeAttr("disabled");
143                                                              $("#form_user_nudge input[type=submit]").removeClass("disabled");
144                                                            }
145          });
146         $("#form_user_nudge").each(addAjaxHidden);
147
148         var Subscribe = { dataType: 'xml',
149                                           beforeSubmit: function(formData, jqForm, options) { $(".form_user_subscribe input[type=submit]").attr("disabled", "disabled");
150                                                                                                                                               $(".form_user_subscribe input[type=submit]").addClass("disabled");
151                                                                                                                                             },
152                                           success: function(xml) { var form_unsubscribe = document._importNode($('form', xml).get(0), true);
153                                                                                            var form_unsubscribe_id = form_unsubscribe.id;
154                                                                                            var form_subscribe_id = form_unsubscribe_id.replace('unsubscribe', 'subscribe');
155                                                                                            $("form#"+form_subscribe_id).replaceWith(form_unsubscribe);
156                                                                                            $("form#"+form_unsubscribe_id).ajaxForm(UnSubscribe).each(addAjaxHidden);
157                                                                                            $("dd.subscribers").text(parseInt($("dd.subscribers").text())+1);
158                                                                                            $(".form_user_subscribe input[type=submit]").removeAttr("disabled");
159                                                                                            $(".form_user_subscribe input[type=submit]").removeClass("disabled");
160                                                                                      }
161                                         };
162
163         var UnSubscribe = { dataType: 'xml',
164                                                 beforeSubmit: function(formData, jqForm, options) { $(".form_user_unsubscribe input[type=submit]").attr("disabled", "disabled");
165                                                                                                                                                     $(".form_user_unsubscribe input[type=submit]").addClass("disabled");
166                                                                                                                                                   },
167                                             success: function(xml) { var form_subscribe = document._importNode($('form', xml).get(0), true);
168                                                                                                  var form_subscribe_id = form_subscribe.id;
169                                                                                                  var form_unsubscribe_id = form_subscribe_id.replace('subscribe', 'unsubscribe');
170                                                                                                  $("form#"+form_unsubscribe_id).replaceWith(form_subscribe);
171                                                                                                  $("form#"+form_subscribe_id).ajaxForm(Subscribe).each(addAjaxHidden);
172                                                                                                  $("#profile_send_a_new_message").remove();
173                                                                                                  $("#profile_nudge").remove();
174                                                                                              $("dd.subscribers").text(parseInt($("dd.subscribers").text())-1);
175                                                                                                  $(".form_user_unsubscribe input[type=submit]").removeAttr("disabled");
176                                                                                                  $(".form_user_unsubscribe input[type=submit]").removeClass("disabled");
177                                                                                            }
178                                           };
179
180         $(".form_user_subscribe").ajaxForm(Subscribe);
181         $(".form_user_unsubscribe").ajaxForm(UnSubscribe);
182         $(".form_user_subscribe").each(addAjaxHidden);
183         $(".form_user_unsubscribe").each(addAjaxHidden);
184
185         var PostNotice = { dataType: 'xml',
186                                            beforeSubmit: function(formData, jqForm, options) { if ($("#notice_data-text").get(0).value.length == 0) {
187                                                                                                                                                                 $("#form_notice").addClass("warning");
188                                                                                                                                                                 return false;
189                                                                                                                                                    }
190                                                                                                                                                    $("#form_notice").addClass("processing");
191                                                                                                                                                    $("#notice_action-submit").attr("disabled", "disabled");
192                                                                                                                                                    $("#notice_action-submit").addClass("disabled");
193                                                                                                                                                    return true;
194                                                                                                                                                  },
195                                            timeout: '60000',
196                                            error: function (xhr, textStatus, errorThrown) {     $("#form_notice").removeClass("processing");
197                                                                                                                                                 $("#notice_action-submit").removeAttr("disabled");
198                                                                                                                                                 $("#notice_action-submit").removeClass("disabled");
199                                                                                                                                                 if (textStatus == "timeout") {
200                                                                                                                                                         alert ("Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists");
201                                                                                                                                                 }
202                                                                                                                                                 else {
203                                                                                                                                                         if ($(".error", xhr.responseXML).length > 0) {
204                                                                                                                                                                 $('#form_notice').append(document._importNode($(".error", xhr.responseXML).get(0), true));
205                                                                                                                                                         }
206                                                                                                                                                         else {
207                                                                                                                                                                 var HTTP20x30x = [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307];
208                                                                                                                                                                 if(jQuery.inArray(parseInt(xhr.status), HTTP20x30x) < 0) {
209                                                                                                                                                                         alert("Sorry! We had trouble sending your notice ("+xhr.status+" "+xhr.statusText+"). Please report the problem to the site administrator if this happens again.");
210                                                                                                                                                                 }
211                                                                                                                                                                 else {
212                                                                                                                                                                         $("#notice_data-text").val("");
213                                                                                                                                                                         counter();
214                                                                                                                                                                 }
215                                                                                                                                                         }
216                                                                                                                                                 }
217                                                                                                                                           },
218                                            success: function(xml) {     if ($("#error", xml).length > 0) {
219                                                                                                         var result = document._importNode($("p", xml).get(0), true);
220                                                                                                         result = result.textContent || result.innerHTML;
221                                                                                                         alert(result);
222                                                                                                 }
223                                                                                                 else {
224                                                                                                     if ($("#command_result", xml).length > 0) {
225                                                                                                             var result = document._importNode($("p", xml).get(0), true);
226                                                                                                             result = result.textContent || result.innerHTML;
227                                                                                                             alert(result);
228                                                     }
229                                                     else {
230                                                          li = $("li", xml).get(0);
231                                                          if ($("#"+li.id).length == 0) {
232                                                             var notice_irt_value = $('#notice_in-reply-to').val();
233                                                             var notice_irt = '#notices_primary #notice-'+notice_irt_value;
234                                                             if($('body')[0].id == 'conversation') {
235                                                                 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
236                                                                     $(notice_irt).append('<ul class="notices"></ul>');
237                                                                 }
238                                                                 $($(notice_irt+' .notices')[0]).append(document._importNode(li, true));
239                                                             }
240                                                             else {
241                                                                 $("#notices_primary .notices").prepend(document._importNode(li, true));
242                                                             }
243                                                             $('#'+li.id).css({display:'none'});
244                                                             $('#'+li.id).fadeIn(2500);
245                                                             NoticeReply();
246                                                             NoticeAttachments();
247                                                          }
248                                                                                                         }
249                                                                                                         $("#notice_data-text").val("");
250                                                                                                 $("#notice_data-attach").val("");
251                                                                                                 $("#notice_in-reply-to").val("");
252                                                     $('#notice_data-attach_selected').remove();
253                                                     counter();
254                                                                                                 }
255                                                                                                 $("#form_notice").removeClass("processing");
256                                                                                                 $("#notice_action-submit").removeAttr("disabled");
257                                                                                                 $("#notice_action-submit").removeClass("disabled");
258                                                                                          }
259                                            };
260         $("#form_notice").ajaxForm(PostNotice);
261         $("#form_notice").each(addAjaxHidden);
262     NoticeReply();
263     NoticeAttachments();
264     NoticeDataAttach();
265 });
266
267 function NoticeReply() {
268     if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) {
269         $('#content .notice').each(function() {
270             var notice = $(this)[0];
271             $($('.notice_reply', notice)[0]).click(function() {
272                 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
273                 NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
274                 return false;
275             });
276         });
277     }
278 }
279
280 function NoticeReplySet(nick,id) {
281         rgx_username = /^[0-9a-zA-Z\-_.]*$/;
282         if (nick.match(rgx_username)) {
283                 var text = $("#notice_data-text");
284                 if (text.length) {
285                         replyto = "@" + nick + " ";
286                         text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
287                         $("#form_notice input#notice_in-reply-to").val(id);
288                         if (text.get(0).setSelectionRange) {
289                                 var len = text.val().length;
290                                 text.get(0).setSelectionRange(len,len);
291                                 text.get(0).focus();
292                         }
293                         return false;
294                 }
295         }
296         return true;
297 }
298
299 function NoticeAttachments() {
300     $.fn.jOverlay.options = {
301         method : 'GET',
302         data : '',
303         url : '',
304         color : '#000',
305         opacity : '0.6',
306         zIndex : 99,
307         center : false,
308         imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
309         bgClickToClose : true,
310         success : function() {
311             $('#jOverlayContent').append('<button>&#215;</button>');
312             $('#jOverlayContent button').click($.closeOverlay);
313         },
314         timeout : 0,
315         autoHide : true,
316         css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
317     };
318
319     $('#content .notice a.attachment').click(function() {
320         $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
321         return false;
322     });
323
324     var t;
325     $("body:not(#shownotice) #content .notice a.thumbnail").hover(
326         function() {
327             var anchor = $(this);
328             $("a.thumbnail").children('img').hide();
329             anchor.closest(".entry-title").addClass('ov');
330
331             if (anchor.children('img').length == 0) {
332                 t = setTimeout(function() {
333                     $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
334                         anchor.append(data);
335                     });
336                 }, 500);
337             }
338             else {
339                 anchor.children('img').show();
340             }
341         },
342         function() {
343             clearTimeout(t);
344             $("a.thumbnail").children('img').hide();
345             $(this).closest(".entry-title").removeClass('ov');
346         }
347     );
348 }
349
350 function NoticeDataAttach() {
351     NDA = $('#notice_data-attach');
352     NDA.change(function() {
353         S = '<div id="notice_data-attach_selected" class="success"><code>'+$(this).val()+'</code> <button>&#215;</button></div>';
354         NDAS = $('#notice_data-attach_selected');
355         (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#form_notice').append(S);
356         $('#notice_data-attach_selected button').click(function(){
357             $('#notice_data-attach_selected').remove();
358             NDA.val('');
359         });
360     });
361 }