]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/util.js
6511c0380da3215f0eff0972f431373896153cf4
[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 // attachments and attachment pages not used at the moment except for attachment_ajax version
21 //    $('.attachments').click(function() {$().jOverlay({zIndex:999, success:function(html) {$('.attachment').click(function() {$().jOverlay({url:$(this).attr('href') + '/ajax'}); return false; });
22 //        }, url:$(this).attr('href') + '/ajax'}); return false; });
23     $('.attachment').click(function() {$().jOverlay({url:'../attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'}); return false; });
24
25         // count character on keyup
26         function counter(event){
27                 var maxLength = 140;
28                 var currentLength = $("#notice_data-text").val().length;
29                 var remaining = maxLength - currentLength;
30                 var counter = $("#notice_text-count");
31                 counter.text(remaining);
32
33                 if (remaining <= 0) {
34                         $("#form_notice").addClass("warning");
35                 } else {
36                         $("#form_notice").removeClass("warning");
37                 }
38         }
39
40         function submitonreturn(event) {
41                 if (event.keyCode == 13) {
42                         $("#form_notice").submit();
43                         event.preventDefault();
44                         event.stopPropagation();
45                         return false;
46                 }
47                 return true;
48         }
49
50         if ($("#notice_data-text").length) {
51                 $("#notice_data-text").bind("keyup", counter);
52                 $("#notice_data-text").bind("keydown", submitonreturn);
53
54                 // run once in case there's something in there
55                 counter();
56
57                 // set the focus
58                 $("#notice_data-text").focus();
59         }
60
61         // XXX: refactor this code
62
63         var favoptions = { dataType: 'xml',
64                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
65                                                                                                 var dis = new_form.id;
66                                                                                                 var fav = dis.replace('disfavor', 'favor');
67                                                                                                 $('form#'+fav).replaceWith(new_form);
68                                                                                                 $('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden);
69                                                                                           }
70                                          };
71
72         var disoptions = { dataType: 'xml',
73                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
74                                                                                                 var fav = new_form.id;
75                                                                                                 var dis = fav.replace('favor', 'disfavor');
76                                                                                                 $('form#'+dis).replaceWith(new_form);
77                                                                                                 $('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden);
78                                                                                           }
79                                          };
80
81         var joinoptions = { dataType: 'xml',
82                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
83                                                                                                 var leave = new_form.id;
84                                                                                                 var join = leave.replace('leave', 'join');
85                                                                                                 $('form#'+join).replaceWith(new_form);
86                                                                                                 $('form#'+leave).ajaxForm(leaveoptions).each(addAjaxHidden);
87                                                                                           }
88                                          };
89
90         var leaveoptions = { dataType: 'xml',
91                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
92                                                                                                 var join = new_form.id;
93                                                                                                 var leave = join.replace('join', 'leave');
94                                                                                                 $('form#'+leave).replaceWith(new_form);
95                                                                                                 $('form#'+join).ajaxForm(joinoptions).each(addAjaxHidden);
96                                                                                           }
97                                          };
98
99         function addAjaxHidden() {
100                 var ajax = document.createElement('input');
101                 ajax.setAttribute('type', 'hidden');
102                 ajax.setAttribute('name', 'ajax');
103                 ajax.setAttribute('value', 1);
104                 this.appendChild(ajax);
105         }
106
107         $("form.form_favor").ajaxForm(favoptions);
108         $("form.form_disfavor").ajaxForm(disoptions);
109         $("form.form_group_join").ajaxForm(joinoptions);
110         $("form.form_group_leave").ajaxForm(leaveoptions);
111         $("form.form_favor").each(addAjaxHidden);
112         $("form.form_disfavor").each(addAjaxHidden);
113         $("form.form_group_join").each(addAjaxHidden);
114         $("form.form_group_leave").each(addAjaxHidden);
115
116         $("#form_user_nudge").ajaxForm ({ dataType: 'xml',
117                 beforeSubmit: function(xml) { $("#form_user_nudge input[type=submit]").attr("disabled", "disabled");
118                                                                           $("#form_user_nudge input[type=submit]").addClass("disabled");
119                                                                         },
120                 success: function(xml) { $("#form_user_nudge").replaceWith(document._importNode($("#nudge_response", xml).get(0),true));
121                                                              $("#form_user_nudge input[type=submit]").removeAttr("disabled");
122                                                              $("#form_user_nudge input[type=submit]").removeClass("disabled");
123                                                            }
124          });
125         $("#form_user_nudge").each(addAjaxHidden);
126
127         var Subscribe = { dataType: 'xml',
128                                           beforeSubmit: function(formData, jqForm, options) { $(".form_user_subscribe input[type=submit]").attr("disabled", "disabled");
129                                                                                                                                               $(".form_user_subscribe input[type=submit]").addClass("disabled");
130                                                                                                                                             },
131                                           success: function(xml) { var form_unsubscribe = document._importNode($('form', xml).get(0), true);
132                                                                                            var form_unsubscribe_id = form_unsubscribe.id;
133                                                                                            var form_subscribe_id = form_unsubscribe_id.replace('unsubscribe', 'subscribe');
134                                                                                            $("form#"+form_subscribe_id).replaceWith(form_unsubscribe);
135                                                                                            $("form#"+form_unsubscribe_id).ajaxForm(UnSubscribe).each(addAjaxHidden);
136                                                                                            $("dd.subscribers").text(parseInt($("dd.subscribers").text())+1);
137                                                                                            $(".form_user_subscribe input[type=submit]").removeAttr("disabled");
138                                                                                            $(".form_user_subscribe input[type=submit]").removeClass("disabled");
139                                                                                      }
140                                         };
141
142         var UnSubscribe = { dataType: 'xml',
143                                                 beforeSubmit: function(formData, jqForm, options) { $(".form_user_unsubscribe input[type=submit]").attr("disabled", "disabled");
144                                                                                                                                                     $(".form_user_unsubscribe input[type=submit]").addClass("disabled");
145                                                                                                                                                   },
146                                             success: function(xml) { var form_subscribe = document._importNode($('form', xml).get(0), true);
147                                                                                                  var form_subscribe_id = form_subscribe.id;
148                                                                                                  var form_unsubscribe_id = form_subscribe_id.replace('subscribe', 'unsubscribe');
149                                                                                                  $("form#"+form_unsubscribe_id).replaceWith(form_subscribe);
150                                                                                                  $("form#"+form_subscribe_id).ajaxForm(Subscribe).each(addAjaxHidden);
151                                                                                                  $("#profile_send_a_new_message").remove();
152                                                                                                  $("#profile_nudge").remove();
153                                                                                              $("dd.subscribers").text(parseInt($("dd.subscribers").text())-1);
154                                                                                                  $(".form_user_unsubscribe input[type=submit]").removeAttr("disabled");
155                                                                                                  $(".form_user_unsubscribe input[type=submit]").removeClass("disabled");
156                                                                                            }
157                                           };
158
159         $(".form_user_subscribe").ajaxForm(Subscribe);
160         $(".form_user_unsubscribe").ajaxForm(UnSubscribe);
161         $(".form_user_subscribe").each(addAjaxHidden);
162         $(".form_user_unsubscribe").each(addAjaxHidden);
163
164         var PostNotice = { dataType: 'xml',
165                                            beforeSubmit: function(formData, jqForm, options) { if ($("#notice_data-text").get(0).value.length == 0) {
166                                                                                                                                                                 $("#form_notice").addClass("warning");
167                                                                                                                                                                 return false;
168                                                                                                                                                    }
169                                                                                                                                                    $("#form_notice").addClass("processing");
170                                                                                                                                                    $("#notice_action-submit").attr("disabled", "disabled");
171                                                                                                                                                    $("#notice_action-submit").addClass("disabled");
172                                                                                                                                                    return true;
173                                                                                                                                                  },
174                                            timeout: '60000',
175                                            error: function (xhr, textStatus, errorThrown) {     $("#form_notice").removeClass("processing");
176                                                                                                                                                 $("#notice_action-submit").removeAttr("disabled");
177                                                                                                                                                 $("#notice_action-submit").removeClass("disabled");
178                                                                                                                                                 if (textStatus == "timeout") {
179                                                                                                                                                         alert ("Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists");
180                                                                                                                                                 }
181                                                                                                                                                 else {
182                                                                                                                                                         if ($(".error", xhr.responseXML).length > 0) {
183                                                                                                                                                                 $('#form_notice').append(document._importNode($(".error", xhr.responseXML).get(0), true));
184                                                                                                                                                         }
185                                                                                                                                                         else {
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                                                                                                                                                 }
189                                                                                                                                           },
190                                            success: function(xml) {     if ($("#error", xml).length > 0) {
191                                                                                                         var result = document._importNode($("p", xml).get(0), true);
192                                                                                                         result = result.textContent || result.innerHTML;
193                                                                                                         alert(result);
194                                                                                                 }
195                                                                                                 else {
196                                                                                                     if ($("#command_result", xml).length > 0) {
197                                                                                                             var result = document._importNode($("p", xml).get(0), true);
198                                                                                                             result = result.textContent || result.innerHTML;
199                                                                                                             alert(result);
200                                                     }
201                                                     else {
202                                                          li = $("li", xml).get(0);
203                                                          if ($("#"+li.id).length == 0) {
204                                                               $("#notices_primary .notices").prepend(document._importNode(li, true));
205                                                               $("#notices_primary .notice:first").css({display:"none"});
206                                                               $("#notices_primary .notice:first").fadeIn(2500);
207                                                               NoticeReply();
208                                                          }
209                                                                                                         }
210                                                                                                         $("#notice_data-text").val("");
211                                                     counter();
212                                                                                                 }
213                                                                                                 $("#form_notice").removeClass("processing");
214                                                                                                 $("#notice_action-submit").removeAttr("disabled");
215                                                                                                 $("#notice_action-submit").removeClass("disabled");
216                                                                                          }
217                                            };
218         $("#form_notice").ajaxForm(PostNotice);
219         $("#form_notice").each(addAjaxHidden);
220     NoticeHover();
221     NoticeReply();
222 });
223
224
225 function NoticeHover() {
226     function mouseHandler(e) {
227         $(e.target).closest('li.hentry')[(e.type === 'mouseover') ? 'addClass' : 'removeClass']('hover');
228     };
229     $('#content .notices').mouseover(mouseHandler);
230     $('#content .notices').mouseout(mouseHandler);
231 }
232
233
234 function NoticeReply() {
235     if ($('#notice_data-text').length > 0) {
236         $('#content .notice').each(function() {
237             var notice = $(this)[0];
238             $($('.notice_reply', notice)[0]).click(function() {
239                 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname');
240                 NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
241                 return false;
242             });
243         });
244     }
245 }
246
247 function NoticeReplySet(nick,id) {
248         rgx_username = /^[0-9a-zA-Z\-_.]*$/;
249         if (nick.match(rgx_username)) {
250                 replyto = "@" + nick + " ";
251                 if ($("#notice_data-text").length) {
252                         $("#notice_data-text").val(replyto);
253                         $("#form_notice input#notice_in-reply-to").val(id);
254                         $("#notice_data-text").focus();
255                         return false;
256                 }
257         }
258         return true;
259 }