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