2 * StatusNet - a distributed open-source microblogging tool
3 * Copyright (C) 2008, StatusNet, Inc.
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.
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.
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/>.
18 * @category UI interaction
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/
27 var SN = { // StatusNet
30 CounterBlackout: false,
32 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
33 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
41 Processing: 'processing',
42 CommandResult: 'command_result',
43 FormNotice: 'form_notice',
44 NoticeDataText: 'notice_data-text',
45 NoticeTextCount: 'notice_text-count',
46 NoticeInReplyTo: 'notice_in-reply-to',
47 NoticeDataAttach: 'notice_data-attach',
48 NoticeDataAttachSelected: 'notice_data-attach_selected',
49 NoticeActionSubmit: 'notice_action-submit'
54 FormNoticeEnhancements: function(form) {
55 form_id = form.attr('id');
56 $('#'+form_id+' #'+SN.C.S.NoticeDataText).unbind('keyup');
57 $('#'+form_id+' #'+SN.C.S.NoticeDataText).unbind('keydown');
59 $('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
62 // run once in case there's something in there
66 $('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
67 SN.U.SubmitOnReturn(e, form);
70 if($('body')[0].id != 'conversation') {
71 $('#'+form_id+' textarea').focus();
75 SubmitOnReturn: function(event, el) {
76 if (event.keyCode == 13 || event.keyCode == 10) {
78 event.preventDefault();
79 event.stopPropagation();
80 $('#'+el[0].id+' #'+SN.C.S.NoticeDataText).blur();
87 Counter: function(form) {
88 SN.C.I.FormNoticeCurrent = form;
89 form_id = form.attr('id');
90 if (typeof(maxLength) == "undefined") {
91 maxLength = SN.C.I.MaxLength;
98 var remaining = maxLength - $('#'+form_id+' #'+SN.C.S.NoticeDataText).val().length;
99 var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount);
101 if (remaining.toString() != counter.text()) {
102 if (!SN.C.I.CounterBlackout || remaining === 0) {
103 if (counter.text() != String(remaining)) {
104 counter.text(remaining);
107 form.addClass(SN.C.S.Warning);
109 form.removeClass(SN.C.S.Warning);
111 // Skip updates for the next 500ms.
112 // On slower hardware, updating on every keypress is unpleasant.
113 if (!SN.C.I.CounterBlackout) {
114 SN.C.I.CounterBlackout = true;
115 SN.C.I.FormNoticeCurrent = form;
116 window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
122 ClearCounterBlackout: function(form) {
123 // Allow keyup events to poke the counter again
124 SN.C.I.CounterBlackout = false;
125 // Check if the string changed since we last looked
129 FormXHR: function(f) {
130 f.bind('submit', function(e) {
131 form_id = $(this)[0].id;
135 url: $(this)[0].action,
136 data: $(this).serialize() + '&ajax=1',
137 beforeSend: function(xhr) {
138 $('#'+form_id).addClass(SN.C.S.Processing);
139 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
140 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
142 error: function (xhr, textStatus, errorThrown) {
143 alert(errorThrown || textStatus);
145 success: function(data, textStatus) {
146 if (typeof($('form', data)[0]) != 'undefined') {
147 form_new = document._importNode($('form', data)[0], true);
148 $('#'+form_id).replaceWith(form_new);
149 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
152 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
160 FormNoticeXHR: function(form) {
161 form_id = form.attr('id');
162 form.append('<input type="hidden" name="ajax" value="1"/>');
166 beforeSend: function(xhr) {
167 if ($('#'+form_id+' #'+SN.C.S.NoticeDataText)[0].value.length === 0) {
168 form.addClass(SN.C.S.Warning);
171 form.addClass(SN.C.S.Processing);
172 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
173 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
176 error: function (xhr, textStatus, errorThrown) {
177 form.removeClass(SN.C.S.Processing);
178 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
179 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
180 if (textStatus == 'timeout') {
181 alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
184 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
185 form.append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
188 if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
189 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
192 $('#'+form_id+' #'+SN.C.S.NoticeDataText).val('');
193 SN.U.FormNoticeEnhancements($('#'+form_id));
198 success: function(data, textStatus) {
200 if ($('#'+SN.C.S.Error, data).length > 0) {
201 result = document._importNode($('p', data)[0], true);
202 alert(result.textContent || result.innerHTML);
205 if($('body')[0].id == 'bookmarklet') {
209 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
210 result = document._importNode($('p', data)[0], true);
211 alert(result.textContent || result.innerHTML);
214 notice = document._importNode($('li', data)[0], true);
215 if ($('#'+notice.id).length === 0) {
216 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
217 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
218 if($('body')[0].id == 'conversation') {
219 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
220 $(notice_irt).append('<ul class="notices"></ul>');
222 $($(notice_irt+' .notices')[0]).append(notice);
225 $("#notices_primary .notices").prepend(notice);
227 $('#'+notice.id).css({display:'none'});
228 $('#'+notice.id).fadeIn(2500);
229 SN.U.NoticeAttachments();
234 $('#'+form_id+' #'+SN.C.S.NoticeDataText).val('');
235 $('#'+form_id+' #'+SN.C.S.NoticeDataAttach).val('');
236 $('#'+form_id+' #'+SN.C.S.NoticeInReplyTo).val('');
237 $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
238 SN.U.FormNoticeEnhancements($('#'+form_id));
241 complete: function(xhr, textStatus) {
242 form.removeClass(SN.C.S.Processing);
243 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
244 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
249 NoticeReply: function() {
250 if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
251 $('#content .notice').each(function() {
252 var notice = $(this)[0];
253 $($('.notice_reply', notice)[0]).click(function() {
254 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
255 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
262 NoticeReplySet: function(nick,id) {
263 if (nick.match(SN.C.I.PatternUsername)) {
264 var text = $('#'+SN.C.S.NoticeDataText);
266 replyto = '@' + nick + ' ';
267 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
268 $('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id);
269 if (text[0].setSelectionRange) {
270 var len = text.val().length;
271 text[0].setSelectionRange(len,len);
280 NoticeFavor: function() {
281 $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
282 $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
285 NoticeAttachments: function() {
286 $.fn.jOverlay.options = {
294 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
295 bgClickToClose : true,
296 success : function() {
297 $('#jOverlayContent').append('<button class="close">×</button>');
298 $('#jOverlayContent button').click($.closeOverlay);
302 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
305 $('#content .notice a.attachment').click(function() {
306 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
311 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
313 var anchor = $(this);
314 $("a.thumbnail").children('img').hide();
315 anchor.closest(".entry-title").addClass('ov');
317 if (anchor.children('img').length === 0) {
318 t = setTimeout(function() {
319 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
325 anchor.children('img').show();
330 $("a.thumbnail").children('img').hide();
331 $(this).closest(".entry-title").removeClass('ov');
336 NoticeDataAttach: function() {
337 NDA = $('#'+SN.C.S.NoticeDataAttach);
338 NDA.change(function() {
339 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">×</button></div>';
340 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
341 if (NDAS.length > 0) {
345 $('#'+SN.C.S.FormNotice).append(S);
347 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
348 $('#'+SN.C.S.NoticeDataAttachSelected).remove();
354 NewDirectMessage: function() {
355 NDM = $('.entity_send-a-message a');
356 NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
357 NDM.click(function() {
358 var NDMF = $('.entity_send-a-message form');
359 if (NDMF.length === 0) {
360 $.get(NDM.attr('href'), null, function(data) {
361 $('.entity_send-a-message').append(document._importNode($('form', data)[0], true));
362 NDMF = $('.entity_send-a-message .form_notice');
363 SN.U.FormNoticeXHR(NDMF);
364 SN.U.FormNoticeEnhancements(NDMF);
365 NDMF.append('<button class="close">×</button>');
366 $('.entity_send-a-message button').click(function(){
374 $('.entity_send-a-message textarea').focus();
382 NoticeForm: function() {
383 if ($('body.user_in').length > 0) {
384 $('.'+SN.C.S.FormNotice).each(function() {
385 SN.U.FormNoticeXHR($(this));
386 SN.U.FormNoticeEnhancements($(this));
389 SN.U.NoticeDataAttach();
393 Notices: function() {
394 if ($('body.user_in').length > 0) {
400 SN.U.NoticeAttachments();
403 EntityActions: function() {
404 if ($('body.user_in').length > 0) {
405 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
406 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
407 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
408 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
409 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
411 SN.U.NewDirectMessage();
417 $(document).ready(function(){
418 if ($('.'+SN.C.S.FormNotice).length > 0) {
419 SN.Init.NoticeForm();
421 if ($('#content .notices').length > 0) {
424 if ($('#content .entity_actions').length > 0) {
425 SN.Init.EntityActions();