]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/util.js
Updated form_notice
[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     $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
93     $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
94     $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
95     $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
96     $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
97     $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
98     $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
99
100     SN.U.FormNoticeXHR();
101
102     NoticeReply();
103     NoticeAttachments();
104     NoticeDataAttach();
105 });
106
107 function NoticeReply() {
108     if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) {
109         $('#content .notice').each(function() {
110             var notice = $(this)[0];
111             $($('.notice_reply', notice)[0]).click(function() {
112                 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
113                 NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
114                 return false;
115             });
116         });
117     }
118 }
119
120 function NoticeReplySet(nick,id) {
121         rgx_username = /^[0-9a-zA-Z\-_.]*$/;
122         if (nick.match(rgx_username)) {
123                 var text = $("#notice_data-text");
124                 if (text.length) {
125                         replyto = "@" + nick + " ";
126                         text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
127                         $("#form_notice input#notice_in-reply-to").val(id);
128                         if (text.get(0).setSelectionRange) {
129                                 var len = text.val().length;
130                                 text.get(0).setSelectionRange(len,len);
131                                 text.get(0).focus();
132                         }
133                         return false;
134                 }
135         }
136         return true;
137 }
138
139 function NoticeAttachments() {
140     $.fn.jOverlay.options = {
141         method : 'GET',
142         data : '',
143         url : '',
144         color : '#000',
145         opacity : '0.6',
146         zIndex : 99,
147         center : false,
148         imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
149         bgClickToClose : true,
150         success : function() {
151             $('#jOverlayContent').append('<button>&#215;</button>');
152             $('#jOverlayContent button').click($.closeOverlay);
153         },
154         timeout : 0,
155         autoHide : true,
156         css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
157     };
158
159     $('#content .notice a.attachment').click(function() {
160         $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
161         return false;
162     });
163
164     var t;
165     $("body:not(#shownotice) #content .notice a.thumbnail").hover(
166         function() {
167             var anchor = $(this);
168             $("a.thumbnail").children('img').hide();
169             anchor.closest(".entry-title").addClass('ov');
170
171             if (anchor.children('img').length == 0) {
172                 t = setTimeout(function() {
173                     $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
174                         anchor.append(data);
175                     });
176                 }, 500);
177             }
178             else {
179                 anchor.children('img').show();
180             }
181         },
182         function() {
183             clearTimeout(t);
184             $("a.thumbnail").children('img').hide();
185             $(this).closest(".entry-title").removeClass('ov');
186         }
187     );
188 }
189
190 function NoticeDataAttach() {
191     NDA = $('#notice_data-attach');
192     NDA.change(function() {
193         S = '<div id="notice_data-attach_selected" class="success"><code>'+$(this).val()+'</code> <button>&#215;</button></div>';
194         NDAS = $('#notice_data-attach_selected');
195         (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#form_notice').append(S);
196         $('#notice_data-attach_selected button').click(function(){
197             $('#notice_data-attach_selected').remove();
198             NDA.val('');
199         });
200     });
201 }
202
203 var SN = { // StatusNet
204     C: { // Config
205         I: {
206             NoticeTextCharMax: 140,
207             PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
208             HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
209         },
210         S: { // Selector
211             Disabled: 'disabled',
212             Warning: 'warning',
213             Error: 'error',
214             Processing: 'processing',
215             CommendResult: 'command_result',
216             FormNotice: 'form_notice',
217             NoticeDataText: 'notice_data-text',
218             NoticeTextCount: 'notice_text-count',
219             NoticeInReplyTo: 'notice_in-reply-to',
220             NoticeDataAttach: 'notice_data-attach',
221             NoticeActionSubmit: 'notice_action-submit'
222         }
223     },
224
225     U: { // Utils
226         FormXHR: function(f) {
227             f.bind('submit', function(e) {
228                 form_id = $(this)[0].id;
229                 $.ajax({
230                     type: 'POST',
231                     url: $(this)[0].action,
232                     data: $(this).serialize() + '&ajax=1',
233                     beforeSend: function(xhr) {
234                         $('#'+form_id).addClass(SN.C.S.Processing);
235                         $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
236                         $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
237                     },
238                     error: function (xhr, textStatus, errorThrown) {
239                         alert(errorThrown || textStatus);
240                     },
241                     success: function(data, textStatus) {
242                         if ($('form', data)[0].length > 0) {
243                             form_new = $('form', data)[0];
244                             $('#'+form_id).replaceWith(document._importNode(form_new, true));
245                             $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
246                         }
247                         else {
248                             $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
249                         }
250                     }
251                 });
252                 return false;
253             });
254         },
255
256         FormNoticeXHR: function() {
257             $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
258             $('#'+SN.C.S.FormNotice).ajaxForm({
259                 timeout: '60000',
260                 beforeSend: function(xhr) {
261                     if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
262                         $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
263                         return false;
264                     }
265                     $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
266                     $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
267                     $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
268                     return true;
269                 },
270                 error: function (xhr, textStatus, errorThrown) {
271                     $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
272                     $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
273                     $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
274                     if (textStatus == 'timeout') {
275                         alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
276                     }
277                     else {
278                         if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
279                             $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
280                         }
281                         else {
282                             if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
283                                 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
284                             }
285                             else {
286                                 SN.C.I.NoticeDataText.val('');
287 //                                SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
288                             }
289                         }
290                     }
291                 },
292                 success: function(data, textStatus) {
293                     if ($('#'+SN.C.S.Error, data).length > 0) {
294                         var result = document._importNode($('p', data)[0], true);
295                         alert(result.textContent || result.innerHTML);
296                     }
297                     else {
298                         if($('body')[0].id == 'bookmarklet') {
299                             self.close();
300                         }
301                         if ($('#'+SN.C.S.CommandResult, data).length > 0) {
302                             var result = document._importNode($('p', data)[0], true);
303                             alert(result.textContent || result.innerHTML);
304                         }
305                         else {
306                              notice = $('li', data)[0];
307                              if ($('#'+notice.id).length === 0) {
308                                 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
309                                 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
310                                 if($('body')[0].id == 'conversation') {
311                                     if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
312                                         $(notice_irt).append('<ul class="notices"></ul>');
313                                     }
314                                     $($(notice_irt+' .notices')[0]).append(document._importNode(notice, true));
315                                 }
316                                 else {
317                                     $("#notices_primary .notices").prepend(document._importNode(notice, true));
318                                 }
319                                 $('#'+notice.id).css({display:'none'});
320                                 $('#'+notice.id).fadeIn(2500);
321 //                                SN.U.NoticeAttachments();
322 //                                SN.U.NoticeReply();
323                              }
324                         }
325                         $('#'+SN.C.S.NoticeDataText).val('');
326                         $('#'+SN.C.S.NoticeDataAttach).val('');
327                         $('#'+SN.C.S.NoticeInReplyTo).val('');
328 //                        SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
329                     }
330                 },
331                 complete: function(xhr, textStatus) {
332                     $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
333                     $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
334                     $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
335                 }
336             });
337         }
338     }
339 }