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