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 var counterBlackout = false;
22 // count character on keyup
23 function counter(event){
27 var currentLength = $("#notice_data-text").val().length;
28 var remaining = maxLength - currentLength;
29 var counter = $("#notice_text-count");
31 if (remaining.toString() != counter.text()) {
32 if (!counterBlackout || remaining == 0) {
33 if (counter.text() != String(remaining)) {
34 counter.text(remaining);
38 $("#form_notice").addClass("warning");
40 $("#form_notice").removeClass("warning");
42 // Skip updates for the next 500ms.
43 // On slower hardware, updating on every keypress is unpleasant.
44 if (!counterBlackout) {
45 counterBlackout = true;
46 window.setTimeout(clearCounterBlackout, 500);
52 function clearCounterBlackout() {
53 // Allow keyup events to poke the counter again
54 counterBlackout = false;
55 // Check if the string changed since we last looked
61 // define maxLength if it wasn't defined already
63 if (typeof(maxLength) == "undefined") {
67 if ($("#notice_data-text").length) {
69 $("#notice_data-text").bind("keyup", counter);
70 // run once in case there's something in there
74 $('#'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
75 SN.U.SubmitOnReturn(e, $('#'+SN.C.S.FormNotice));
78 if($('body')[0].id != 'conversation') {
79 $("#notice_data-text").focus();
83 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
84 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
85 $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
86 $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
87 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
88 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
89 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
94 SN.U.NoticeAttachments();
95 SN.U.NoticeDataAttach();
99 var SN = { // StatusNet
102 NoticeTextCharMax: 140,
103 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
104 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
107 Disabled: 'disabled',
111 Processing: 'processing',
112 CommendResult: 'command_result',
113 FormNotice: 'form_notice',
114 NoticeDataText: 'notice_data-text',
115 NoticeTextCount: 'notice_text-count',
116 NoticeInReplyTo: 'notice_in-reply-to',
117 NoticeDataAttach: 'notice_data-attach',
118 NoticeDataAttachSelected: 'notice_data-attach_selected',
119 NoticeActionSubmit: 'notice_action-submit'
124 SubmitOnReturn: function(event, el) {
125 if (event.keyCode == 13 || event.keyCode == 10) {
127 event.preventDefault();
128 event.stopPropagation();
129 $('#'+SN.U.NoticeDataText).blur();
136 FormXHR: function(f) {
137 f.bind('submit', function(e) {
138 form_id = $(this)[0].id;
141 url: $(this)[0].action,
142 data: $(this).serialize() + '&ajax=1',
143 beforeSend: function(xhr) {
144 $('#'+form_id).addClass(SN.C.S.Processing);
145 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
146 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
148 error: function (xhr, textStatus, errorThrown) {
149 alert(errorThrown || textStatus);
151 success: function(data, textStatus) {
152 if ($('form', data)[0].length > 0) {
153 form_new = $('form', data)[0];
154 $('#'+form_id).replaceWith(document._importNode(form_new, true));
155 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
158 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
166 FormNoticeXHR: function() {
167 $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
168 $('#'+SN.C.S.FormNotice).ajaxForm({
170 beforeSend: function(xhr) {
171 if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
172 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
175 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
176 $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
177 $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
180 error: function (xhr, textStatus, errorThrown) {
181 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
182 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
183 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
184 if (textStatus == 'timeout') {
185 alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
188 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
189 $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
192 if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
193 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
196 SN.C.I.NoticeDataText.val('');
197 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
202 success: function(data, textStatus) {
203 if ($('#'+SN.C.S.Error, data).length > 0) {
204 var result = document._importNode($('p', data)[0], true);
205 alert(result.textContent || result.innerHTML);
208 if($('body')[0].id == 'bookmarklet') {
211 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
212 var result = document._importNode($('p', data)[0], true);
213 alert(result.textContent || result.innerHTML);
216 notice = $('li', data)[0];
217 if ($('#'+notice.id).length === 0) {
218 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
219 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
220 if($('body')[0].id == 'conversation') {
221 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
222 $(notice_irt).append('<ul class="notices"></ul>');
224 $($(notice_irt+' .notices')[0]).append(document._importNode(notice, true));
227 $("#notices_primary .notices").prepend(document._importNode(notice, true));
229 $('#'+notice.id).css({display:'none'});
230 $('#'+notice.id).fadeIn(2500);
231 SN.U.NoticeAttachments();
235 $('#'+SN.C.S.NoticeDataText).val('');
236 $('#'+SN.C.S.NoticeDataAttach).val('');
237 $('#'+SN.C.S.NoticeInReplyTo).val('');
238 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
241 complete: function(xhr, textStatus) {
242 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
243 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
244 $('#'+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.get(0).setSelectionRange) {
270 var len = text.val().length;
271 text.get(0).setSelectionRange(len,len);
280 NoticeAttachments: function() {
281 $.fn.jOverlay.options = {
289 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
290 bgClickToClose : true,
291 success : function() {
292 $('#jOverlayContent').append('<button>×</button>');
293 $('#jOverlayContent button').click($.closeOverlay);
297 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
300 $('#content .notice a.attachment').click(function() {
301 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
306 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
308 var anchor = $(this);
309 $("a.thumbnail").children('img').hide();
310 anchor.closest(".entry-title").addClass('ov');
312 if (anchor.children('img').length == 0) {
313 t = setTimeout(function() {
314 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
320 anchor.children('img').show();
325 $("a.thumbnail").children('img').hide();
326 $(this).closest(".entry-title").removeClass('ov');
331 NoticeDataAttach: function() {
332 NDA = $('#'+SN.C.S.NoticeDataAttach);
333 NDA.change(function() {
334 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button>×</button></div>';
335 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
336 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#'+SN.C.S.FormNotice).append(S);
337 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
338 $('#'+SN.C.S.NoticeDataAttachSelected).remove();