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