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