]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/util.js
Using 'CSS sprites' for common icons for the identica theme. Default
[quix0rs-gnu-social.git] / js / util.js
1 /*
2  * StatusNet - a distributed open-source microblogging tool
3  * Copyright (C) 2008, StatusNet, 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         var counterBlackout = false;
21         
22         // count character on keyup
23         function counter(event){
24          if (maxLength <= 0) {
25               return;
26          }
27                 var currentLength = $("#notice_data-text").val().length;
28                 var remaining = maxLength - currentLength;
29                 var counter = $("#notice_text-count");
30                 
31                 if (remaining.toString() != counter.text()) {
32                     if (!counterBlackout || remaining == 0) {
33                         if (counter.text() != String(remaining)) {
34                             counter.text(remaining);
35                         }
36
37                         if (remaining < 0) {
38                             $("#form_notice").addClass("warning");
39                         } else {
40                             $("#form_notice").removeClass("warning");
41                         }
42                         // Skip updates for the next 500ms.
43                         // On slower hardware, updating on every keypress is unpleasant.
44                         if (!counterBlackout) {
45                             counterBlackout = true;
46                             window.setTimeout(clearCounterBlackout, 500);
47                         }
48                     }
49                 }
50         }
51         
52         function clearCounterBlackout() {
53                 // Allow keyup events to poke the counter again
54                 counterBlackout = false;
55                 // Check if the string changed since we last looked
56                 counter(null);
57         }
58
59         function submitonreturn(event) {
60                 if (event.keyCode == 13 || event.keyCode == 10) {
61                         // iPhone sends \n not \r for 'return'
62                         $("#form_notice").submit();
63                         event.preventDefault();
64                         event.stopPropagation();
65                         $("#notice_data-text").blur();
66                         $("body").focus();
67                         return false;
68                 }
69                 return true;
70         }
71
72      // define maxLength if it wasn't defined already
73
74     if (typeof(maxLength) == "undefined") {
75          maxLength = 140;
76     }
77
78         if ($("#notice_data-text").length) {
79          if (maxLength > 0) {
80               $("#notice_data-text").bind("keyup", counter);
81               // run once in case there's something in there
82               counter();
83          }
84
85                 $("#notice_data-text").bind("keydown", submitonreturn);
86
87         if($('body')[0].id != 'conversation') {
88             $("#notice_data-text").focus();
89         }
90         }
91
92         // XXX: refactor this code
93
94         var favoptions = { dataType: 'xml',
95                                            beforeSubmit: function(data, target, options) {
96                                                                                                 $(target).addClass('processing');
97                                                                                                 return true;
98                                                                                           },
99                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
100                                                                                                 var dis = new_form.id;
101                                                                                                 var fav = dis.replace('disfavor', 'favor');
102                                                                                                 $('form#'+fav).replaceWith(new_form);
103                                                                                                 $('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden);
104                                                                                           }
105                                          };
106
107         var disoptions = { dataType: 'xml',
108                                            beforeSubmit: function(data, target, options) {
109                                                                                                 $(target).addClass('processing');
110                                                                                                 return true;
111                                                                                           },
112                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
113                                                                                                 var fav = new_form.id;
114                                                                                                 var dis = fav.replace('favor', 'disfavor');
115                                                                                                 $('form#'+dis).replaceWith(new_form);
116                                                                                                 $('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden);
117                                                                                           }
118                                          };
119
120         var joinoptions = { dataType: 'xml',
121                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
122                                                                                                 var leave = new_form.id;
123                                                                                                 var join = leave.replace('leave', 'join');
124                                                                                                 $('form#'+join).replaceWith(new_form);
125                                                                                                 $('form#'+leave).ajaxForm(leaveoptions).each(addAjaxHidden);
126                                                                                           }
127                                          };
128
129         var leaveoptions = { dataType: 'xml',
130                                            success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
131                                                                                                 var join = new_form.id;
132                                                                                                 var leave = join.replace('join', 'leave');
133                                                                                                 $('form#'+leave).replaceWith(new_form);
134                                                                                                 $('form#'+join).ajaxForm(joinoptions).each(addAjaxHidden);
135                                                                                           }
136                                          };
137
138         function addAjaxHidden() {
139                 var ajax = document.createElement('input');
140                 ajax.setAttribute('type', 'hidden');
141                 ajax.setAttribute('name', 'ajax');
142                 ajax.setAttribute('value', 1);
143                 this.appendChild(ajax);
144         }
145
146         $("form.form_favor").ajaxForm(favoptions);
147         $("form.form_disfavor").ajaxForm(disoptions);
148         $("form.form_group_join").ajaxForm(joinoptions);
149         $("form.form_group_leave").ajaxForm(leaveoptions);
150         $("form.form_favor").each(addAjaxHidden);
151         $("form.form_disfavor").each(addAjaxHidden);
152         $("form.form_group_join").each(addAjaxHidden);
153         $("form.form_group_leave").each(addAjaxHidden);
154
155         $("#form_user_nudge").ajaxForm ({ dataType: 'xml',
156                 beforeSubmit: function(xml) { $("#form_user_nudge input[type=submit]").attr("disabled", "disabled");
157                                                                           $("#form_user_nudge input[type=submit]").addClass("disabled");
158                                                                         },
159                 success: function(xml) { $("#form_user_nudge").replaceWith(document._importNode($("#nudge_response", xml).get(0),true));
160                                                              $("#form_user_nudge input[type=submit]").removeAttr("disabled");
161                                                              $("#form_user_nudge input[type=submit]").removeClass("disabled");
162                                                            }
163          });
164         $("#form_user_nudge").each(addAjaxHidden);
165
166         var Subscribe = { dataType: 'xml',
167                                           beforeSubmit: function(formData, jqForm, options) { $(".form_user_subscribe input[type=submit]").attr("disabled", "disabled");
168                                                                                                                                               $(".form_user_subscribe input[type=submit]").addClass("disabled");
169                                                                                                                                             },
170                                           success: function(xml) { var form_unsubscribe = document._importNode($('form', xml).get(0), true);
171                                                                                            var form_unsubscribe_id = form_unsubscribe.id;
172                                                                                            var form_subscribe_id = form_unsubscribe_id.replace('unsubscribe', 'subscribe');
173                                                                                            $("form#"+form_subscribe_id).replaceWith(form_unsubscribe);
174                                                                                            $("form#"+form_unsubscribe_id).ajaxForm(UnSubscribe).each(addAjaxHidden);
175                                                                                            $("dd.subscribers").text(parseInt($("dd.subscribers").text())+1);
176                                                                                            $(".form_user_subscribe input[type=submit]").removeAttr("disabled");
177                                                                                            $(".form_user_subscribe input[type=submit]").removeClass("disabled");
178                                                                                      }
179                                         };
180
181         var UnSubscribe = { dataType: 'xml',
182                                                 beforeSubmit: function(formData, jqForm, options) { $(".form_user_unsubscribe input[type=submit]").attr("disabled", "disabled");
183                                                                                                                                                     $(".form_user_unsubscribe input[type=submit]").addClass("disabled");
184                                                                                                                                                   },
185                                             success: function(xml) { var form_subscribe = document._importNode($('form', xml).get(0), true);
186                                                                                                  var form_subscribe_id = form_subscribe.id;
187                                                                                                  var form_unsubscribe_id = form_subscribe_id.replace('subscribe', 'unsubscribe');
188                                                                                                  $("form#"+form_unsubscribe_id).replaceWith(form_subscribe);
189                                                                                                  $("form#"+form_subscribe_id).ajaxForm(Subscribe).each(addAjaxHidden);
190                                                                                                  $("#profile_send_a_new_message").remove();
191                                                                                                  $("#profile_nudge").remove();
192                                                                                              $("dd.subscribers").text(parseInt($("dd.subscribers").text())-1);
193                                                                                                  $(".form_user_unsubscribe input[type=submit]").removeAttr("disabled");
194                                                                                                  $(".form_user_unsubscribe input[type=submit]").removeClass("disabled");
195                                                                                            }
196                                           };
197
198         $(".form_user_subscribe").ajaxForm(Subscribe);
199         $(".form_user_unsubscribe").ajaxForm(UnSubscribe);
200         $(".form_user_subscribe").each(addAjaxHidden);
201         $(".form_user_unsubscribe").each(addAjaxHidden);
202
203         var PostNotice = { dataType: 'xml',
204                                            beforeSubmit: function(formData, jqForm, options) { if ($("#notice_data-text").get(0).value.length == 0) {
205                                                                                                                                                                 $("#form_notice").addClass("warning");
206                                                                                                                                                                 return false;
207                                                                                                                                                    }
208                                                                                                                                                    $("#form_notice").addClass("processing");
209                                                                                                                                                    $("#notice_action-submit").attr("disabled", "disabled");
210                                                                                                                                                    $("#notice_action-submit").addClass("disabled");
211                                                                                                                                                    return true;
212                                                                                                                                                  },
213                                            timeout: '60000',
214                                            error: function (xhr, textStatus, errorThrown) {     $("#form_notice").removeClass("processing");
215                                                                                                                                                 $("#notice_action-submit").removeAttr("disabled");
216                                                                                                                                                 $("#notice_action-submit").removeClass("disabled");
217                                                                                                                                                 if (textStatus == "timeout") {
218                                                                                                                                                         alert ("Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists");
219                                                                                                                                                 }
220                                                                                                                                                 else {
221                                                                                                                                                         if ($(".error", xhr.responseXML).length > 0) {
222                                                                                                                                                                 $('#form_notice').append(document._importNode($(".error", xhr.responseXML).get(0), true));
223                                                                                                                                                         }
224                                                                                                                                                         else {
225                                                                                                                                                                 var HTTP20x30x = [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307];
226                                                                                                                                                                 if(jQuery.inArray(parseInt(xhr.status), HTTP20x30x) < 0) {
227                                                                                                                                                                         alert("Sorry! We had trouble sending your notice ("+xhr.status+" "+xhr.statusText+"). Please report the problem to the site administrator if this happens again.");
228                                                                                                                                                                 }
229                                                                                                                                                                 else {
230                                                                                                                                                                         $("#notice_data-text").val("");
231                                                                                      if (maxLength > 0) {
232                                                                                           counter();
233                                                                                      }
234                                                                                                                                                                 }
235                                                                                                                                                         }
236                                                                                                                                                 }
237                                                                                                                                           },
238                                            success: function(xml) {     if ($("#error", xml).length > 0) {
239                                                                                                         var result = document._importNode($("p", xml).get(0), true);
240                                                                                                         result = result.textContent || result.innerHTML;
241                                                                                                         alert(result);
242                                                                                                 }
243                                                                                                 else {
244                                                                                                     if ($("#command_result", xml).length > 0) {
245                                                                                                             var result = document._importNode($("p", xml).get(0), true);
246                                                                                                             result = result.textContent || result.innerHTML;
247                                                                                                             alert(result);
248                                                     }
249                                                     else {
250                                                          li = $("li", xml).get(0);
251                                                          if ($("#"+li.id).length == 0) {
252                                                             var notice_irt_value = $('#notice_in-reply-to').val();
253                                                             var notice_irt = '#notices_primary #notice-'+notice_irt_value;
254                                                             if($('body')[0].id == 'conversation') {
255                                                                 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
256                                                                     $(notice_irt).append('<ul class="notices"></ul>');
257                                                                 }
258                                                                 $($(notice_irt+' .notices')[0]).append(document._importNode(li, true));
259                                                             }
260                                                             else {
261                                                                 $("#notices_primary .notices").prepend(document._importNode(li, true));
262                                                             }
263                                                             $('#'+li.id).css({display:'none'});
264                                                             $('#'+li.id).fadeIn(2500);
265                                                             NoticeReply();
266                                                             NoticeAttachments();
267                                                          }
268                                                                                                         }
269                                                                                                         $("#notice_data-text").val("");
270                                                                                                 $("#notice_data-attach").val("");
271                                                                                                 $("#notice_in-reply-to").val("");
272                                                     $('#notice_data-attach_selected').remove();
273                                                      if (maxLength > 0) {
274                                                           counter();
275                                                      }
276                                                                                                 }
277                                                                                                 $("#form_notice").removeClass("processing");
278                                                                                                 $("#notice_action-submit").removeAttr("disabled");
279                                                                                                 $("#notice_action-submit").removeClass("disabled");
280                                                                                          }
281                                            };
282         $("#form_notice").ajaxForm(PostNotice);
283         $("#form_notice").each(addAjaxHidden);
284     NoticeReply();
285     NoticeAttachments();
286     NoticeDataAttach();
287 });
288
289 function NoticeReply() {
290     if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) {
291         $('#content .notice').each(function() {
292             var notice = $(this)[0];
293             $($('.notice_reply', notice)[0]).click(function() {
294                 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
295                 NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
296                 return false;
297             });
298         });
299     }
300 }
301
302 function NoticeReplySet(nick,id) {
303         rgx_username = /^[0-9a-zA-Z\-_.]*$/;
304         if (nick.match(rgx_username)) {
305                 var text = $("#notice_data-text");
306                 if (text.length) {
307                         replyto = "@" + nick + " ";
308                         text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
309                         $("#form_notice input#notice_in-reply-to").val(id);
310                         if (text.get(0).setSelectionRange) {
311                                 var len = text.val().length;
312                                 text.get(0).setSelectionRange(len,len);
313                                 text.get(0).focus();
314                         }
315                         return false;
316                 }
317         }
318         return true;
319 }
320
321 function NoticeAttachments() {
322     $.fn.jOverlay.options = {
323         method : 'GET',
324         data : '',
325         url : '',
326         color : '#000',
327         opacity : '0.6',
328         zIndex : 99,
329         center : false,
330         imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
331         bgClickToClose : true,
332         success : function() {
333             $('#jOverlayContent').append('<button>&#215;</button>');
334             $('#jOverlayContent button').click($.closeOverlay);
335         },
336         timeout : 0,
337         autoHide : true,
338         css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
339     };
340
341     $('#content .notice a.attachment').click(function() {
342         $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
343         return false;
344     });
345
346     var t;
347     $("body:not(#shownotice) #content .notice a.thumbnail").hover(
348         function() {
349             var anchor = $(this);
350             $("a.thumbnail").children('img').hide();
351             anchor.closest(".entry-title").addClass('ov');
352
353             if (anchor.children('img').length == 0) {
354                 t = setTimeout(function() {
355                     $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
356                         anchor.append(data);
357                     });
358                 }, 500);
359             }
360             else {
361                 anchor.children('img').show();
362             }
363         },
364         function() {
365             clearTimeout(t);
366             $("a.thumbnail").children('img').hide();
367             $(this).closest(".entry-title").removeClass('ov');
368         }
369     );
370 }
371
372 function NoticeDataAttach() {
373     NDA = $('#notice_data-attach');
374     NDA.change(function() {
375         S = '<div id="notice_data-attach_selected" class="success"><code>'+$(this).val()+'</code> <button>&#215;</button></div>';
376         NDAS = $('#notice_data-attach_selected');
377         (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#form_notice').append(S);
378         $('#notice_data-attach_selected button').click(function(){
379             $('#notice_data-attach_selected').remove();
380             NDA.val('');
381         });
382     });
383 }