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