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