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 ($('#'+SN.C.S.NoticeDataText).length) {
22 $('#'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
25 // run once in case there's something in there
29 $('#'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
30 SN.U.SubmitOnReturn(e, $('#'+SN.C.S.FormNotice));
33 if($('body')[0].id != 'conversation') {
34 $('#'+SN.C.S.NoticeDataText).focus();
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)); });
49 SN.U.NoticeAttachments();
50 SN.U.NoticeDataAttach();
54 var SN = { // StatusNet
57 CounterBlackout: false,
59 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
60 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
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',
80 SubmitOnReturn: function(event, el) {
81 if (event.keyCode == 13 || event.keyCode == 10) {
83 event.preventDefault();
84 event.stopPropagation();
85 $('#'+SN.U.NoticeDataText).blur();
93 if (typeof(maxLength) == "undefined") {
94 maxLength = SN.C.I.MaxLength;
101 var remaining = maxLength - $('#'+SN.C.S.NoticeDataText).val().length;
102 var counter = $('#'+SN.C.S.NoticeTextCount);
104 if (remaining.toString() != counter.text()) {
105 if (!SN.C.I.CounterBlackout || remaining == 0) {
106 if (counter.text() != String(remaining)) {
107 counter.text(remaining);
110 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
112 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Warning);
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);
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
131 FormXHR: function(f) {
132 f.bind('submit', function(e) {
133 form_id = $(this)[0].id;
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);
143 error: function (xhr, textStatus, errorThrown) {
144 alert(errorThrown || textStatus);
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)); });
153 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
161 FormNoticeXHR: function() {
162 $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
163 $('#'+SN.C.S.FormNotice).ajaxForm({
165 beforeSend: function(xhr) {
166 if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
167 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
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);
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');
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));
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.');
191 SN.C.I.NoticeDataText.val('');
192 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
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);
203 if($('body')[0].id == 'bookmarklet') {
206 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
207 var result = document._importNode($('p', data)[0], true);
208 alert(result.textContent || result.innerHTML);
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>');
219 $($(notice_irt+' .notices')[0]).append(document._importNode(notice, true));
222 $("#notices_primary .notices").prepend(document._importNode(notice, true));
224 $('#'+notice.id).css({display:'none'});
225 $('#'+notice.id).fadeIn(2500);
226 SN.U.NoticeAttachments();
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);
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);
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());
257 NoticeReplySet: function(nick,id) {
258 if (nick.match(SN.C.I.PatternUsername)) {
259 var text = $('#'+SN.C.S.NoticeDataText);
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);
275 NoticeAttachments: function() {
276 $.fn.jOverlay.options = {
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>×</button>');
288 $('#jOverlayContent button').click($.closeOverlay);
292 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
295 $('#content .notice a.attachment').click(function() {
296 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
301 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
303 var anchor = $(this);
304 $("a.thumbnail").children('img').hide();
305 anchor.closest(".entry-title").addClass('ov');
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) {
315 anchor.children('img').show();
320 $("a.thumbnail").children('img').hide();
321 $(this).closest(".entry-title").removeClass('ov');
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>×</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();