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