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