]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/util.js
c7a3e8f8895928357aca0748cc0303d7746f8264
[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  * @category  UI interaction
19  * @package   StatusNet
20  * @author    Sarven Capadisli <csarven@status.net>
21  * @author    Evan Prodromou <evan@status.net>
22  * @copyright 2009 StatusNet, Inc.
23  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
24  * @link      http://status.net/
25  */
26
27 $(document).ready(function(){
28     if ($('body.user_in').length > 0) {
29         if ($('#'+SN.C.S.NoticeDataText).length) {
30             if (maxLength > 0) {
31                 $('#'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
32                     SN.U.Counter();
33                 });
34                 // run once in case there's something in there
35                 SN.U.Counter();
36             }
37
38             $('#'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
39                 SN.U.SubmitOnReturn(e, $('#'+SN.C.S.FormNotice));
40             });
41
42             if($('body')[0].id != 'conversation') {
43                 $('#'+SN.C.S.NoticeDataText).focus();
44             }
45         }
46
47         $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
48         $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
49         $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
50         $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
51         $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
52         $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
53         $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
54
55         SN.U.FormNoticeXHR();
56
57         SN.U.NoticeReply();
58
59         SN.U.NoticeDataAttach();
60
61         SN.U.NewDirectMessage();
62     }
63
64     SN.U.NoticeAttachments();
65 });
66
67
68 var SN = { // StatusNet
69     C: { // Config
70         I: { // Init
71             CounterBlackout: false,
72             MaxLength: 140,
73             PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
74             HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
75         },
76
77         S: { // Selector
78             Disabled: 'disabled',
79             Warning: 'warning',
80             Error: 'error',
81             Success: 'success',
82             Processing: 'processing',
83             CommendResult: 'command_result',
84             FormNotice: 'form_notice',
85             NoticeDataText: 'notice_data-text',
86             NoticeTextCount: 'notice_text-count',
87             NoticeInReplyTo: 'notice_in-reply-to',
88             NoticeDataAttach: 'notice_data-attach',
89             NoticeDataAttachSelected: 'notice_data-attach_selected',
90             NoticeActionSubmit: 'notice_action-submit'
91         }
92     },
93
94     U: { // Utils
95         SubmitOnReturn: function(event, el) {
96             if (event.keyCode == 13 || event.keyCode == 10) {
97                 el.submit();
98                 event.preventDefault();
99                 event.stopPropagation();
100                 $('#'+SN.U.NoticeDataText).blur();
101                 $('body').focus();
102                 return false;
103             }
104             return true;
105         },
106
107         Counter: function() {
108             if (typeof(maxLength) == "undefined") {
109                  maxLength = SN.C.I.MaxLength;
110             }
111
112             if (maxLength <= 0) {
113                 return;
114             }
115
116             var remaining = maxLength - $('#'+SN.C.S.NoticeDataText).val().length;
117             var counter = $('#'+SN.C.S.NoticeTextCount);
118
119             if (remaining.toString() != counter.text()) {
120                 if (!SN.C.I.CounterBlackout || remaining == 0) {
121                     if (counter.text() != String(remaining)) {
122                         counter.text(remaining);
123                     }
124                     if (remaining < 0) {
125                         $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
126                     } else {
127                         $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Warning);
128                     }
129                     // Skip updates for the next 500ms.
130                     // On slower hardware, updating on every keypress is unpleasant.
131                     if (!SN.C.I.CounterBlackout) {
132                         SN.C.I.CounterBlackout = true;
133                         window.setTimeout(SN.U.ClearCounterBlackout, 500);
134                     }
135                 }
136             }
137         },
138
139         ClearCounterBlackout: function() {
140             // Allow keyup events to poke the counter again
141             SN.C.I.CounterBlackout = false;
142             // Check if the string changed since we last looked
143             SN.U.Counter(null);
144         },
145
146         FormXHR: function(f) {
147             f.bind('submit', function(e) {
148                 form_id = $(this)[0].id;
149                 $.ajax({
150                     type: 'POST',
151                     dataType: 'xml',
152                     url: $(this)[0].action,
153                     data: $(this).serialize() + '&ajax=1',
154                     beforeSend: function(xhr) {
155                         $('#'+form_id).addClass(SN.C.S.Processing);
156                         $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
157                         $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
158                     },
159                     error: function (xhr, textStatus, errorThrown) {
160                         alert(errorThrown || textStatus);
161                     },
162                     success: function(data, textStatus) {
163                         if (typeof($('form', data)[0]) != 'undefined') {
164                             form_new = document._importNode($('form', data)[0], true);
165                             $('#'+form_id).replaceWith(form_new);
166                             $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
167                         }
168                         else {
169                             $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
170                         }
171                     }
172                 });
173                 return false;
174             });
175         },
176
177         FormNoticeXHR: function() {
178             $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
179             $('#'+SN.C.S.FormNotice).ajaxForm({
180                 dataType: 'xml',
181                 timeout: '60000',
182                 beforeSend: function(xhr) {
183                     if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
184                         $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
185                         return false;
186                     }
187                     $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
188                     $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
189                     $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
190                     return true;
191                 },
192                 error: function (xhr, textStatus, errorThrown) {
193                     $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
194                     $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
195                     $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
196                     if (textStatus == 'timeout') {
197                         alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
198                     }
199                     else {
200                         if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
201                             $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
202                         }
203                         else {
204                             if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
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                             else {
208                                 SN.C.I.NoticeDataText.val('');
209                                 SN.U.Counter();
210                             }
211                         }
212                     }
213                 },
214                 success: function(data, textStatus) {
215                     if ($('#'+SN.C.S.Error, data).length > 0) {
216                         var result = document._importNode($('p', data)[0], true);
217                         alert(result.textContent || result.innerHTML);
218                     }
219                     else {
220                         if($('body')[0].id == 'bookmarklet') {
221                             self.close();
222                         }
223                         if ($('#'+SN.C.S.CommandResult, data).length > 0) {
224                             var result = document._importNode($('p', data)[0], true);
225                             alert(result.textContent || result.innerHTML);
226                         }
227                         else {
228                              notice = document._importNode($('li', data)[0], true);
229                              if ($('#'+notice.id).length == 0) {
230                                 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
231                                 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
232                                 if($('body')[0].id == 'conversation') {
233                                     if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
234                                         $(notice_irt).append('<ul class="notices"></ul>');
235                                     }
236                                     $($(notice_irt+' .notices')[0]).append(notice);
237                                 }
238                                 else {
239                                     $("#notices_primary .notices").prepend(notice);
240                                 }
241                                 $('#'+notice.id).css({display:'none'});
242                                 $('#'+notice.id).fadeIn(2500);
243                                 SN.U.NoticeAttachments();
244                                 SN.U.NoticeReply();
245                              }
246                         }
247                         $('#'+SN.C.S.NoticeDataText).val('');
248                         $('#'+SN.C.S.NoticeDataAttach).val('');
249                         $('#'+SN.C.S.NoticeInReplyTo).val('');
250                         $('#'+SN.C.S.NoticeDataAttachSelected).remove();
251                         SN.U.Counter();
252                     }
253                 },
254                 complete: function(xhr, textStatus) {
255                     $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
256                     $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
257                     $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
258                 }
259             });
260         },
261
262         NoticeReply: function() {
263             if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
264                 $('#content .notice').each(function() {
265                     var notice = $(this)[0];
266                     $($('.notice_reply', notice)[0]).click(function() {
267                         var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
268                         SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
269                         return false;
270                     });
271                 });
272             }
273         },
274
275         NoticeReplySet: function(nick,id) {
276             if (nick.match(SN.C.I.PatternUsername)) {
277                 var text = $('#'+SN.C.S.NoticeDataText);
278                 if (text.length) {
279                     replyto = '@' + nick + ' ';
280                     text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
281                     $('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id);
282                     if (text.get(0).setSelectionRange) {
283                         var len = text.val().length;
284                         text.get(0).setSelectionRange(len,len);
285                         text.get(0).focus();
286                     }
287                     return false;
288                 }
289             }
290             return true;
291         },
292
293         NoticeAttachments: function() {
294             $.fn.jOverlay.options = {
295                 method : 'GET',
296                 data : '',
297                 url : '',
298                 color : '#000',
299                 opacity : '0.6',
300                 zIndex : 99,
301                 center : false,
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                 autoHide : true,
310                 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
311             };
312
313             $('#content .notice a.attachment').click(function() {
314                 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
315                 return false;
316             });
317
318             var t;
319             $("body:not(#shownotice) #content .notice a.thumbnail").hover(
320                 function() {
321                     var anchor = $(this);
322                     $("a.thumbnail").children('img').hide();
323                     anchor.closest(".entry-title").addClass('ov');
324
325                     if (anchor.children('img').length == 0) {
326                         t = setTimeout(function() {
327                             $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
328                                 anchor.append(data);
329                             });
330                         }, 500);
331                     }
332                     else {
333                         anchor.children('img').show();
334                     }
335                 },
336                 function() {
337                     clearTimeout(t);
338                     $("a.thumbnail").children('img').hide();
339                     $(this).closest(".entry-title").removeClass('ov');
340                 }
341             );
342         },
343
344         NoticeDataAttach: function() {
345             NDA = $('#'+SN.C.S.NoticeDataAttach);
346             NDA.change(function() {
347                 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button>&#215;</button></div>';
348                 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
349                 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#'+SN.C.S.FormNotice).append(S);
350                 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
351                     $('#'+SN.C.S.NoticeDataAttachSelected).remove();
352                     NDA.val('');
353                 });
354             });
355         },
356
357         NewDirectMessage: function() {
358             NDM = $('.entity_send-a-message a');
359             NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
360             NDM.click(function() {
361                 var NDMF = $('.entity_send-a-message form');
362                 if (NDMF.length == 0) {
363                     $.get(NDM.attr('href'), null, function(data) {
364                         $('.entity_send-a-message').append(document._importNode($('form', data).get(0), true));
365                         $('.entity_send-a-message textarea').focus();
366
367                         NDMF = $('.entity_send-a-message form');
368                         NDMF.append('<button>&#215;</button>');
369                         $('.entity_send-a-message button').click(function(){
370                             NDMF.hide();
371                             return false;
372                         });
373                     });
374                 }
375                 else {
376                     NDMF.show();
377                     $('.entity_send-a-message textarea').focus();
378                 }
379                 return false;
380             });
381         }
382     }
383 }