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