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