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