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 if ($('#'+SN.C.S.NoticeDataText).length) {
31 $('#'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
34 // run once in case there's something in there
38 $('#'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
39 SN.U.SubmitOnReturn(e, $('#'+SN.C.S.FormNotice));
42 if($('body')[0].id != 'conversation') {
43 $('#'+SN.C.S.NoticeDataText).focus();
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)); });
59 SN.U.NoticeDataAttach();
61 SN.U.NewDirectMessage();
64 SN.U.NoticeAttachments();
68 var SN = { // StatusNet
71 CounterBlackout: false,
73 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
74 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
82 Processing: 'processing',
83 CommendResult: 'command_result',
84 FormNotice: 'form_notice',
85 NoticeDataText: 'notice_data-text',
86 NoticeTextCount: 'notice_text-count',
87 NoticeInReplyTo: 'notice_in-reply-to',
88 NoticeDataAttach: 'notice_data-attach',
89 NoticeDataAttachSelected: 'notice_data-attach_selected',
90 NoticeActionSubmit: 'notice_action-submit'
95 SubmitOnReturn: function(event, el) {
96 if (event.keyCode == 13 || event.keyCode == 10) {
98 event.preventDefault();
99 event.stopPropagation();
100 $('#'+SN.U.NoticeDataText).blur();
107 Counter: function() {
108 if (typeof(maxLength) == "undefined") {
109 maxLength = SN.C.I.MaxLength;
112 if (maxLength <= 0) {
116 var remaining = maxLength - $('#'+SN.C.S.NoticeDataText).val().length;
117 var counter = $('#'+SN.C.S.NoticeTextCount);
119 if (remaining.toString() != counter.text()) {
120 if (!SN.C.I.CounterBlackout || remaining == 0) {
121 if (counter.text() != String(remaining)) {
122 counter.text(remaining);
125 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
127 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Warning);
129 // Skip updates for the next 500ms.
130 // On slower hardware, updating on every keypress is unpleasant.
131 if (!SN.C.I.CounterBlackout) {
132 SN.C.I.CounterBlackout = true;
133 window.setTimeout(SN.U.ClearCounterBlackout, 500);
139 ClearCounterBlackout: function() {
140 // Allow keyup events to poke the counter again
141 SN.C.I.CounterBlackout = false;
142 // Check if the string changed since we last looked
146 FormXHR: function(f) {
147 f.bind('submit', function(e) {
148 form_id = $(this)[0].id;
152 url: $(this)[0].action,
153 data: $(this).serialize() + '&ajax=1',
154 beforeSend: function(xhr) {
155 $('#'+form_id).addClass(SN.C.S.Processing);
156 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
157 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
159 error: function (xhr, textStatus, errorThrown) {
160 alert(errorThrown || textStatus);
162 success: function(data, textStatus) {
163 if (typeof($('form', data)[0]) != 'undefined') {
164 form_new = document._importNode($('form', data)[0], true);
165 $('#'+form_id).replaceWith(form_new);
166 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
169 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
177 FormNoticeXHR: function() {
178 $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
179 $('#'+SN.C.S.FormNotice).ajaxForm({
182 beforeSend: function(xhr) {
183 if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
184 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
187 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
188 $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
189 $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
192 error: function (xhr, textStatus, errorThrown) {
193 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
194 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
195 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
196 if (textStatus == 'timeout') {
197 alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
200 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
201 $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
204 if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
205 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
208 SN.C.I.NoticeDataText.val('');
214 success: function(data, textStatus) {
215 if ($('#'+SN.C.S.Error, data).length > 0) {
216 var result = document._importNode($('p', data)[0], true);
217 alert(result.textContent || result.innerHTML);
220 if($('body')[0].id == 'bookmarklet') {
223 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
224 var result = document._importNode($('p', data)[0], true);
225 alert(result.textContent || result.innerHTML);
228 notice = document._importNode($('li', data)[0], true);
229 if ($('#'+notice.id).length == 0) {
230 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
231 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
232 if($('body')[0].id == 'conversation') {
233 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
234 $(notice_irt).append('<ul class="notices"></ul>');
236 $($(notice_irt+' .notices')[0]).append(notice);
239 $("#notices_primary .notices").prepend(notice);
241 $('#'+notice.id).css({display:'none'});
242 $('#'+notice.id).fadeIn(2500);
243 SN.U.NoticeAttachments();
247 $('#'+SN.C.S.NoticeDataText).val('');
248 $('#'+SN.C.S.NoticeDataAttach).val('');
249 $('#'+SN.C.S.NoticeInReplyTo).val('');
250 $('#'+SN.C.S.NoticeDataAttachSelected).remove();
254 complete: function(xhr, textStatus) {
255 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
256 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
257 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
262 NoticeReply: function() {
263 if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
264 $('#content .notice').each(function() {
265 var notice = $(this)[0];
266 $($('.notice_reply', notice)[0]).click(function() {
267 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
268 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
275 NoticeReplySet: function(nick,id) {
276 if (nick.match(SN.C.I.PatternUsername)) {
277 var text = $('#'+SN.C.S.NoticeDataText);
279 replyto = '@' + nick + ' ';
280 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
281 $('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id);
282 if (text.get(0).setSelectionRange) {
283 var len = text.val().length;
284 text.get(0).setSelectionRange(len,len);
293 NoticeAttachments: function() {
294 $.fn.jOverlay.options = {
302 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
303 bgClickToClose : true,
304 success : function() {
305 $('#jOverlayContent').append('<button>×</button>');
306 $('#jOverlayContent button').click($.closeOverlay);
310 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
313 $('#content .notice a.attachment').click(function() {
314 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
319 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
321 var anchor = $(this);
322 $("a.thumbnail").children('img').hide();
323 anchor.closest(".entry-title").addClass('ov');
325 if (anchor.children('img').length == 0) {
326 t = setTimeout(function() {
327 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
333 anchor.children('img').show();
338 $("a.thumbnail").children('img').hide();
339 $(this).closest(".entry-title").removeClass('ov');
344 NoticeDataAttach: function() {
345 NDA = $('#'+SN.C.S.NoticeDataAttach);
346 NDA.change(function() {
347 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button>×</button></div>';
348 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
349 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#'+SN.C.S.FormNotice).append(S);
350 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
351 $('#'+SN.C.S.NoticeDataAttachSelected).remove();
357 NewDirectMessage: function() {
358 NDM = $('.entity_send-a-message a');
359 NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
360 NDM.click(function() {
361 var NDMF = $('.entity_send-a-message form');
362 if (NDMF.length == 0) {
363 $.get(NDM.attr('href'), null, function(data) {
364 $('.entity_send-a-message').append(document._importNode($('form', data).get(0), true));
365 $('.entity_send-a-message textarea').focus();
367 NDMF = $('.entity_send-a-message form');
368 NDMF.append('<button>×</button>');
369 $('.entity_send-a-message button').click(function(){
377 $('.entity_send-a-message textarea').focus();