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 $(document).ready(function(){
28 if ($('body.user_in').length > 0) {
29 $('.'+SN.C.S.FormNotice).each(function() { SN.U.FormNoticeEnhancements($(this)); });
31 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
32 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
33 $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
34 $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
35 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
36 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
37 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
41 SN.U.NoticeDataAttach();
43 SN.U.NewDirectMessage();
46 SN.U.NoticeAttachments();
50 var SN = { // StatusNet
53 CounterBlackout: false,
55 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
56 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
64 Processing: 'processing',
65 CommandResult: 'command_result',
66 FormNotice: 'form_notice',
67 NoticeDataText: 'notice_data-text',
68 NoticeTextCount: 'notice_text-count',
69 NoticeInReplyTo: 'notice_in-reply-to',
70 NoticeDataAttach: 'notice_data-attach',
71 NoticeDataAttachSelected: 'notice_data-attach_selected',
72 NoticeActionSubmit: 'notice_action-submit'
77 FormNoticeEnhancements: function(form) {
78 form_id = form.attr('id');
80 $('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
83 // run once in case there's something in there
87 $('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
88 SN.U.SubmitOnReturn(e, form);
91 if($('body')[0].id != 'conversation') {
92 $('#'+form_id+' textarea').focus();
95 SN.U.FormNoticeXHR(form);
98 SubmitOnReturn: function(event, el) {
99 if (event.keyCode == 13 || event.keyCode == 10) {
101 event.preventDefault();
102 event.stopPropagation();
103 $('#'+el[0].id+' #'+SN.U.NoticeDataText).blur();
110 Counter: function(form) {
111 SN.C.I.FormNoticeCurrent = form;
112 form_id = form.attr('id');
113 if (typeof(maxLength) == "undefined") {
114 maxLength = SN.C.I.MaxLength;
117 if (maxLength <= 0) {
121 var remaining = maxLength - $('#'+form_id+' #'+SN.C.S.NoticeDataText).val().length;
122 var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount);
124 if (remaining.toString() != counter.text()) {
125 if (!SN.C.I.CounterBlackout || remaining == 0) {
126 if (counter.text() != String(remaining)) {
127 counter.text(remaining);
130 form.addClass(SN.C.S.Warning);
132 form.removeClass(SN.C.S.Warning);
134 // Skip updates for the next 500ms.
135 // On slower hardware, updating on every keypress is unpleasant.
136 if (!SN.C.I.CounterBlackout) {
137 SN.C.I.CounterBlackout = true;
138 SN.C.I.FormNoticeCurrent = form;
139 window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
145 ClearCounterBlackout: function(form) {
146 // Allow keyup events to poke the counter again
147 SN.C.I.CounterBlackout = false;
148 // Check if the string changed since we last looked
152 FormXHR: function(f) {
153 f.bind('submit', function(e) {
154 form_id = $(this)[0].id;
158 url: $(this)[0].action,
159 data: $(this).serialize() + '&ajax=1',
160 beforeSend: function(xhr) {
161 $('#'+form_id).addClass(SN.C.S.Processing);
162 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
163 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
165 error: function (xhr, textStatus, errorThrown) {
166 alert(errorThrown || textStatus);
168 success: function(data, textStatus) {
169 if (typeof($('form', data)[0]) != 'undefined') {
170 form_new = document._importNode($('form', data)[0], true);
171 $('#'+form_id).replaceWith(form_new);
172 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
175 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
183 FormNoticeXHR: function(form) {
184 form_id = form.attr('id');
185 form.append('<input type="hidden" name="ajax" value="1"/>');
189 beforeSend: function(xhr) {
190 if ($('#'+form_id+' #'+SN.C.S.NoticeDataText)[0].value.length === 0) {
191 form.addClass(SN.C.S.Warning);
194 form.addClass(SN.C.S.Processing);
195 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
196 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
199 error: function (xhr, textStatus, errorThrown) {
200 form.removeClass(SN.C.S.Processing);
201 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
202 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
203 if (textStatus == 'timeout') {
204 alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
207 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
208 form.append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
211 if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
212 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
215 $('#'+form_id+' #'+SN.C.S.NoticeDataText).val('');
216 SN.U.NoticeFormEnhancements($('#'+SN.C.S.FormNotice));
221 success: function(data, textStatus) {
222 if ($('#'+SN.C.S.Error, data).length > 0) {
223 var result = document._importNode($('p', data)[0], true);
224 alert(result.textContent || result.innerHTML);
227 if($('body')[0].id == 'bookmarklet') {
231 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
232 var result = document._importNode($('p', data)[0], true);
233 alert(result.textContent || result.innerHTML);
236 notice = document._importNode($('li', data)[0], true);
237 if ($('#'+notice.id).length == 0) {
238 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
239 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
240 if($('body')[0].id == 'conversation') {
241 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
242 $(notice_irt).append('<ul class="notices"></ul>');
244 $($(notice_irt+' .notices')[0]).append(notice);
247 $("#notices_primary .notices").prepend(notice);
249 $('#'+notice.id).css({display:'none'});
250 $('#'+notice.id).fadeIn(2500);
251 SN.U.NoticeAttachments();
255 $('#'+form_id+' #'+SN.C.S.NoticeDataText).val('');
256 $('#'+form_id+' #'+SN.C.S.NoticeDataAttach).val('');
257 $('#'+form_id+' #'+SN.C.S.NoticeInReplyTo).val('');
258 $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
259 SN.U.FormNoticeEnhancements(form);
262 complete: function(xhr, textStatus) {
263 form.removeClass(SN.C.S.Processing);
264 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
265 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
270 NoticeReply: function() {
271 if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
272 $('#content .notice').each(function() {
273 var notice = $(this)[0];
274 $($('.notice_reply', notice)[0]).click(function() {
275 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
276 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
283 NoticeReplySet: function(nick,id) {
284 if (nick.match(SN.C.I.PatternUsername)) {
285 var text = $('#'+SN.C.S.NoticeDataText);
287 replyto = '@' + nick + ' ';
288 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
289 $('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id);
290 if (text.get(0).setSelectionRange) {
291 var len = text.val().length;
292 text.get(0).setSelectionRange(len,len);
301 NoticeAttachments: function() {
302 $.fn.jOverlay.options = {
310 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
311 bgClickToClose : true,
312 success : function() {
313 $('#jOverlayContent').append('<button class="close">×</button>');
314 $('#jOverlayContent button').click($.closeOverlay);
318 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
321 $('#content .notice a.attachment').click(function() {
322 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
327 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
329 var anchor = $(this);
330 $("a.thumbnail").children('img').hide();
331 anchor.closest(".entry-title").addClass('ov');
333 if (anchor.children('img').length == 0) {
334 t = setTimeout(function() {
335 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
341 anchor.children('img').show();
346 $("a.thumbnail").children('img').hide();
347 $(this).closest(".entry-title").removeClass('ov');
352 NoticeDataAttach: function() {
353 NDA = $('#'+SN.C.S.NoticeDataAttach);
354 NDA.change(function() {
355 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">×</button></div>';
356 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
357 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#'+SN.C.S.FormNotice).append(S);
358 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
359 $('#'+SN.C.S.NoticeDataAttachSelected).remove();
365 NewDirectMessage: function() {
366 NDM = $('.entity_send-a-message a');
367 NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
368 NDM.click(function() {
369 var NDMF = $('.entity_send-a-message form');
370 if (NDMF.length == 0) {
371 $.get(NDM.attr('href'), null, function(data) {
372 $('.entity_send-a-message').append(document._importNode($('form', data).get(0), true));
373 NDMF = $('.entity_send-a-message .form_notice');
374 SN.U.FormNoticeEnhancements(NDMF);
375 NDMF.append('<button class="close">×</button>');
376 $('.entity_send-a-message button').click(function(){
384 $('.entity_send-a-message textarea').focus();