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
59 function submitonreturn(event) {
60 if (event.keyCode == 13 || event.keyCode == 10) {
61 // iPhone sends \n not \r for 'return'
62 $("#form_notice").submit();
63 event.preventDefault();
64 event.stopPropagation();
65 $("#notice_data-text").blur();
72 // define maxLength if it wasn't defined already
74 if (typeof(maxLength) == "undefined") {
78 if ($("#notice_data-text").length) {
80 $("#notice_data-text").bind("keyup", counter);
81 // run once in case there's something in there
85 $("#notice_data-text").bind("keydown", submitonreturn);
87 if($('body')[0].id != 'conversation') {
88 $("#notice_data-text").focus();
92 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
93 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
94 $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
95 $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
96 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
97 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
98 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
100 SN.U.FormNoticeXHR();
107 function NoticeReply() {
108 if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) {
109 $('#content .notice').each(function() {
110 var notice = $(this)[0];
111 $($('.notice_reply', notice)[0]).click(function() {
112 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
113 NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
120 function NoticeReplySet(nick,id) {
121 rgx_username = /^[0-9a-zA-Z\-_.]*$/;
122 if (nick.match(rgx_username)) {
123 var text = $("#notice_data-text");
125 replyto = "@" + nick + " ";
126 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
127 $("#form_notice input#notice_in-reply-to").val(id);
128 if (text.get(0).setSelectionRange) {
129 var len = text.val().length;
130 text.get(0).setSelectionRange(len,len);
139 function NoticeAttachments() {
140 $.fn.jOverlay.options = {
148 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
149 bgClickToClose : true,
150 success : function() {
151 $('#jOverlayContent').append('<button>×</button>');
152 $('#jOverlayContent button').click($.closeOverlay);
156 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
159 $('#content .notice a.attachment').click(function() {
160 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
165 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
167 var anchor = $(this);
168 $("a.thumbnail").children('img').hide();
169 anchor.closest(".entry-title").addClass('ov');
171 if (anchor.children('img').length == 0) {
172 t = setTimeout(function() {
173 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
179 anchor.children('img').show();
184 $("a.thumbnail").children('img').hide();
185 $(this).closest(".entry-title").removeClass('ov');
190 function NoticeDataAttach() {
191 NDA = $('#notice_data-attach');
192 NDA.change(function() {
193 S = '<div id="notice_data-attach_selected" class="success"><code>'+$(this).val()+'</code> <button>×</button></div>';
194 NDAS = $('#notice_data-attach_selected');
195 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#form_notice').append(S);
196 $('#notice_data-attach_selected button').click(function(){
197 $('#notice_data-attach_selected').remove();
203 var SN = { // StatusNet
206 NoticeTextCharMax: 140,
207 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
208 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
211 Disabled: 'disabled',
214 Processing: 'processing',
215 CommendResult: 'command_result',
216 FormNotice: 'form_notice',
217 NoticeDataText: 'notice_data-text',
218 NoticeTextCount: 'notice_text-count',
219 NoticeInReplyTo: 'notice_in-reply-to',
220 NoticeDataAttach: 'notice_data-attach',
221 NoticeActionSubmit: 'notice_action-submit'
226 FormXHR: function(f) {
227 f.bind('submit', function(e) {
228 form_id = $(this)[0].id;
231 url: $(this)[0].action,
232 data: $(this).serialize() + '&ajax=1',
233 beforeSend: function(xhr) {
234 $('#'+form_id).addClass(SN.C.S.Processing);
235 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
236 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
238 error: function (xhr, textStatus, errorThrown) {
239 alert(errorThrown || textStatus);
241 success: function(data, textStatus) {
242 if ($('form', data)[0].length > 0) {
243 form_new = $('form', data)[0];
244 $('#'+form_id).replaceWith(document._importNode(form_new, true));
245 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
248 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
256 FormNoticeXHR: function() {
257 $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
258 $('#'+SN.C.S.FormNotice).ajaxForm({
260 beforeSend: function(xhr) {
261 if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
262 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
265 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
266 $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
267 $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
270 error: function (xhr, textStatus, errorThrown) {
271 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
272 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
273 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
274 if (textStatus == 'timeout') {
275 alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
278 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
279 $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
282 if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
283 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
286 SN.C.I.NoticeDataText.val('');
287 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
292 success: function(data, textStatus) {
293 if ($('#'+SN.C.S.Error, data).length > 0) {
294 var result = document._importNode($('p', data)[0], true);
295 alert(result.textContent || result.innerHTML);
298 if($('body')[0].id == 'bookmarklet') {
301 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
302 var result = document._importNode($('p', data)[0], true);
303 alert(result.textContent || result.innerHTML);
306 notice = $('li', data)[0];
307 if ($('#'+notice.id).length === 0) {
308 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
309 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
310 if($('body')[0].id == 'conversation') {
311 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
312 $(notice_irt).append('<ul class="notices"></ul>');
314 $($(notice_irt+' .notices')[0]).append(document._importNode(notice, true));
317 $("#notices_primary .notices").prepend(document._importNode(notice, true));
319 $('#'+notice.id).css({display:'none'});
320 $('#'+notice.id).fadeIn(2500);
321 // SN.U.NoticeAttachments();
322 // SN.U.NoticeReply();
325 $('#'+SN.C.S.NoticeDataText).val('');
326 $('#'+SN.C.S.NoticeDataAttach).val('');
327 $('#'+SN.C.S.NoticeInReplyTo).val('');
328 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
331 complete: function(xhr, textStatus) {
332 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
333 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
334 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);