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