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