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/>.
19 $(document).ready(function(){
20 if ($('body.user_in').length > 0) {
21 if ($('#'+SN.C.S.NoticeDataText).length) {
23 $('#'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
26 // run once in case there's something in there
30 $('#'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
31 SN.U.SubmitOnReturn(e, $('#'+SN.C.S.FormNotice));
34 if($('body')[0].id != 'conversation') {
35 $('#'+SN.C.S.NoticeDataText).focus();
39 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
40 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
41 $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
42 $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
43 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
44 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
45 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
51 SN.U.NoticeDataAttach();
54 SN.U.NoticeAttachments();
58 var SN = { // StatusNet
61 CounterBlackout: false,
63 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
64 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
71 Processing: 'processing',
72 CommendResult: 'command_result',
73 FormNotice: 'form_notice',
74 NoticeDataText: 'notice_data-text',
75 NoticeTextCount: 'notice_text-count',
76 NoticeInReplyTo: 'notice_in-reply-to',
77 NoticeDataAttach: 'notice_data-attach',
78 NoticeDataAttachSelected: 'notice_data-attach_selected',
79 NoticeActionSubmit: 'notice_action-submit',
84 SubmitOnReturn: function(event, el) {
85 if (event.keyCode == 13 || event.keyCode == 10) {
87 event.preventDefault();
88 event.stopPropagation();
89 $('#'+SN.U.NoticeDataText).blur();
97 if (typeof(maxLength) == "undefined") {
98 maxLength = SN.C.I.MaxLength;
101 if (maxLength <= 0) {
105 var remaining = maxLength - $('#'+SN.C.S.NoticeDataText).val().length;
106 var counter = $('#'+SN.C.S.NoticeTextCount);
108 if (remaining.toString() != counter.text()) {
109 if (!SN.C.I.CounterBlackout || remaining == 0) {
110 if (counter.text() != String(remaining)) {
111 counter.text(remaining);
114 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
116 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Warning);
118 // Skip updates for the next 500ms.
119 // On slower hardware, updating on every keypress is unpleasant.
120 if (!SN.C.I.CounterBlackout) {
121 SN.C.I.CounterBlackout = true;
122 window.setTimeout(SN.U.ClearCounterBlackout, 500);
128 ClearCounterBlackout: function() {
129 // Allow keyup events to poke the counter again
130 SN.C.I.CounterBlackout = false;
131 // Check if the string changed since we last looked
135 FormXHR: function(f) {
136 f.bind('submit', function(e) {
137 form_id = $(this)[0].id;
140 url: $(this)[0].action,
141 data: $(this).serialize() + '&ajax=1',
142 beforeSend: function(xhr) {
143 $('#'+form_id).addClass(SN.C.S.Processing);
144 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
145 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
147 error: function (xhr, textStatus, errorThrown) {
148 alert(errorThrown || textStatus);
150 success: function(data, textStatus) {
151 if ($('form', data)[0].length > 0) {
152 form_new = $('form', data)[0];
153 $('#'+form_id).replaceWith(document._importNode(form_new, true));
154 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
157 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
165 FormNoticeXHR: function() {
166 $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
167 $('#'+SN.C.S.FormNotice).ajaxForm({
169 beforeSend: function(xhr) {
170 if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
171 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
174 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
175 $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
176 $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
179 error: function (xhr, textStatus, errorThrown) {
180 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
181 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
182 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
183 if (textStatus == 'timeout') {
184 alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
187 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
188 $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
191 if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
192 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
195 SN.C.I.NoticeDataText.val('');
196 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
201 success: function(data, textStatus) {
202 if ($('#'+SN.C.S.Error, data).length > 0) {
203 var result = document._importNode($('p', data)[0], true);
204 alert(result.textContent || result.innerHTML);
207 if($('body')[0].id == 'bookmarklet') {
210 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
211 var result = document._importNode($('p', data)[0], true);
212 alert(result.textContent || result.innerHTML);
215 notice = $('li', data)[0];
216 if ($('#'+notice.id).length === 0) {
217 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
218 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
219 if($('body')[0].id == 'conversation') {
220 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
221 $(notice_irt).append('<ul class="notices"></ul>');
223 $($(notice_irt+' .notices')[0]).append(document._importNode(notice, true));
226 $("#notices_primary .notices").prepend(document._importNode(notice, true));
228 $('#'+notice.id).css({display:'none'});
229 $('#'+notice.id).fadeIn(2500);
230 SN.U.NoticeAttachments();
234 $('#'+SN.C.S.NoticeDataText).val('');
235 $('#'+SN.C.S.NoticeDataAttach).val('');
236 $('#'+SN.C.S.NoticeInReplyTo).val('');
237 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
240 complete: function(xhr, textStatus) {
241 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
242 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
243 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
248 NoticeReply: function() {
249 if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
250 $('#content .notice').each(function() {
251 var notice = $(this)[0];
252 $($('.notice_reply', notice)[0]).click(function() {
253 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
254 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
261 NoticeReplySet: function(nick,id) {
262 if (nick.match(SN.C.I.PatternUsername)) {
263 var text = $('#'+SN.C.S.NoticeDataText);
265 replyto = '@' + nick + ' ';
266 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
267 $('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id);
268 if (text.get(0).setSelectionRange) {
269 var len = text.val().length;
270 text.get(0).setSelectionRange(len,len);
279 NoticeAttachments: function() {
280 $.fn.jOverlay.options = {
288 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
289 bgClickToClose : true,
290 success : function() {
291 $('#jOverlayContent').append('<button>×</button>');
292 $('#jOverlayContent button').click($.closeOverlay);
296 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
299 $('#content .notice a.attachment').click(function() {
300 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
305 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
307 var anchor = $(this);
308 $("a.thumbnail").children('img').hide();
309 anchor.closest(".entry-title").addClass('ov');
311 if (anchor.children('img').length == 0) {
312 t = setTimeout(function() {
313 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
319 anchor.children('img').show();
324 $("a.thumbnail").children('img').hide();
325 $(this).closest(".entry-title").removeClass('ov');
330 NoticeDataAttach: function() {
331 NDA = $('#'+SN.C.S.NoticeDataAttach);
332 NDA.change(function() {
333 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button>×</button></div>';
334 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
335 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#'+SN.C.S.FormNotice).append(S);
336 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
337 $('#'+SN.C.S.NoticeDataAttachSelected).remove();