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