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 // XXX: refactor this code
94 var favoptions = { dataType: 'xml',
95 beforeSubmit: function(data, target, options) {
96 $(target).addClass('processing');
99 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
100 var dis = new_form.id;
101 var fav = dis.replace('disfavor', 'favor');
102 $('form#'+fav).replaceWith(new_form);
103 $('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden);
107 var disoptions = { dataType: 'xml',
108 beforeSubmit: function(data, target, options) {
109 $(target).addClass('processing');
112 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
113 var fav = new_form.id;
114 var dis = fav.replace('favor', 'disfavor');
115 $('form#'+dis).replaceWith(new_form);
116 $('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden);
120 var joinoptions = { dataType: 'xml',
121 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
122 var leave = new_form.id;
123 var join = leave.replace('leave', 'join');
124 $('form#'+join).replaceWith(new_form);
125 $('form#'+leave).ajaxForm(leaveoptions).each(addAjaxHidden);
129 var leaveoptions = { dataType: 'xml',
130 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
131 var join = new_form.id;
132 var leave = join.replace('join', 'leave');
133 $('form#'+leave).replaceWith(new_form);
134 $('form#'+join).ajaxForm(joinoptions).each(addAjaxHidden);
138 function addAjaxHidden() {
139 var ajax = document.createElement('input');
140 ajax.setAttribute('type', 'hidden');
141 ajax.setAttribute('name', 'ajax');
142 ajax.setAttribute('value', 1);
143 this.appendChild(ajax);
146 $("form.form_favor").ajaxForm(favoptions);
147 $("form.form_disfavor").ajaxForm(disoptions);
148 $("form.form_group_join").ajaxForm(joinoptions);
149 $("form.form_group_leave").ajaxForm(leaveoptions);
150 $("form.form_favor").each(addAjaxHidden);
151 $("form.form_disfavor").each(addAjaxHidden);
152 $("form.form_group_join").each(addAjaxHidden);
153 $("form.form_group_leave").each(addAjaxHidden);
155 $("#form_user_nudge").ajaxForm ({ dataType: 'xml',
156 beforeSubmit: function(xml) { $("#form_user_nudge input[type=submit]").attr("disabled", "disabled");
157 $("#form_user_nudge input[type=submit]").addClass("disabled");
159 success: function(xml) { $("#form_user_nudge").replaceWith(document._importNode($("#nudge_response", xml).get(0),true));
160 $("#form_user_nudge input[type=submit]").removeAttr("disabled");
161 $("#form_user_nudge input[type=submit]").removeClass("disabled");
164 $("#form_user_nudge").each(addAjaxHidden);
166 var Subscribe = { dataType: 'xml',
167 beforeSubmit: function(formData, jqForm, options) { $(".form_user_subscribe input[type=submit]").attr("disabled", "disabled");
168 $(".form_user_subscribe input[type=submit]").addClass("disabled");
170 success: function(xml) { var form_unsubscribe = document._importNode($('form', xml).get(0), true);
171 var form_unsubscribe_id = form_unsubscribe.id;
172 var form_subscribe_id = form_unsubscribe_id.replace('unsubscribe', 'subscribe');
173 $("form#"+form_subscribe_id).replaceWith(form_unsubscribe);
174 $("form#"+form_unsubscribe_id).ajaxForm(UnSubscribe).each(addAjaxHidden);
175 $("dd.subscribers").text(parseInt($("dd.subscribers").text())+1);
176 $(".form_user_subscribe input[type=submit]").removeAttr("disabled");
177 $(".form_user_subscribe input[type=submit]").removeClass("disabled");
181 var UnSubscribe = { dataType: 'xml',
182 beforeSubmit: function(formData, jqForm, options) { $(".form_user_unsubscribe input[type=submit]").attr("disabled", "disabled");
183 $(".form_user_unsubscribe input[type=submit]").addClass("disabled");
185 success: function(xml) { var form_subscribe = document._importNode($('form', xml).get(0), true);
186 var form_subscribe_id = form_subscribe.id;
187 var form_unsubscribe_id = form_subscribe_id.replace('subscribe', 'unsubscribe');
188 $("form#"+form_unsubscribe_id).replaceWith(form_subscribe);
189 $("form#"+form_subscribe_id).ajaxForm(Subscribe).each(addAjaxHidden);
190 $("#profile_send_a_new_message").remove();
191 $("#profile_nudge").remove();
192 $("dd.subscribers").text(parseInt($("dd.subscribers").text())-1);
193 $(".form_user_unsubscribe input[type=submit]").removeAttr("disabled");
194 $(".form_user_unsubscribe input[type=submit]").removeClass("disabled");
198 $(".form_user_subscribe").ajaxForm(Subscribe);
199 $(".form_user_unsubscribe").ajaxForm(UnSubscribe);
200 $(".form_user_subscribe").each(addAjaxHidden);
201 $(".form_user_unsubscribe").each(addAjaxHidden);
203 var PostNotice = { dataType: 'xml',
204 beforeSubmit: function(formData, jqForm, options) { if ($("#notice_data-text").get(0).value.length == 0) {
205 $("#form_notice").addClass("warning");
208 $("#form_notice").addClass("processing");
209 $("#notice_action-submit").attr("disabled", "disabled");
210 $("#notice_action-submit").addClass("disabled");
214 error: function (xhr, textStatus, errorThrown) { $("#form_notice").removeClass("processing");
215 $("#notice_action-submit").removeAttr("disabled");
216 $("#notice_action-submit").removeClass("disabled");
217 if (textStatus == "timeout") {
218 alert ("Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists");
221 if ($(".error", xhr.responseXML).length > 0) {
222 $('#form_notice').append(document._importNode($(".error", xhr.responseXML).get(0), true));
225 var HTTP20x30x = [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307];
226 if(jQuery.inArray(parseInt(xhr.status), HTTP20x30x) < 0) {
227 alert("Sorry! We had trouble sending your notice ("+xhr.status+" "+xhr.statusText+"). Please report the problem to the site administrator if this happens again.");
230 $("#notice_data-text").val("");
238 success: function(xml) { if ($("#error", xml).length > 0) {
239 var result = document._importNode($("p", xml).get(0), true);
240 result = result.textContent || result.innerHTML;
244 if ($("#command_result", xml).length > 0) {
245 var result = document._importNode($("p", xml).get(0), true);
246 result = result.textContent || result.innerHTML;
250 li = $("li", xml).get(0);
251 if ($("#"+li.id).length == 0) {
252 var notice_irt_value = $('#notice_in-reply-to').val();
253 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
254 if($('body')[0].id == 'conversation') {
255 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
256 $(notice_irt).append('<ul class="notices"></ul>');
258 $($(notice_irt+' .notices')[0]).append(document._importNode(li, true));
261 $("#notices_primary .notices").prepend(document._importNode(li, true));
263 $('#'+li.id).css({display:'none'});
264 $('#'+li.id).fadeIn(2500);
269 $("#notice_data-text").val("");
270 $("#notice_data-attach").val("");
271 $("#notice_in-reply-to").val("");
272 $('#notice_data-attach_selected').remove();
277 $("#form_notice").removeClass("processing");
278 $("#notice_action-submit").removeAttr("disabled");
279 $("#notice_action-submit").removeClass("disabled");
282 $("#form_notice").ajaxForm(PostNotice);
283 $("#form_notice").each(addAjaxHidden);
289 function NoticeReply() {
290 if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) {
291 $('#content .notice').each(function() {
292 var notice = $(this)[0];
293 $($('.notice_reply', notice)[0]).click(function() {
294 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
295 NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
302 function NoticeReplySet(nick,id) {
303 rgx_username = /^[0-9a-zA-Z\-_.]*$/;
304 if (nick.match(rgx_username)) {
305 var text = $("#notice_data-text");
307 replyto = "@" + nick + " ";
308 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
309 $("#form_notice input#notice_in-reply-to").val(id);
310 if (text.get(0).setSelectionRange) {
311 var len = text.val().length;
312 text.get(0).setSelectionRange(len,len);
321 function NoticeAttachments() {
322 $.fn.jOverlay.options = {
330 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
331 bgClickToClose : true,
332 success : function() {
333 $('#jOverlayContent').append('<button>×</button>');
334 $('#jOverlayContent button').click($.closeOverlay);
338 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
341 $('#content .notice a.attachment').click(function() {
342 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
347 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
349 var anchor = $(this);
350 $("a.thumbnail").children('img').hide();
351 anchor.closest(".entry-title").addClass('ov');
353 if (anchor.children('img').length == 0) {
354 t = setTimeout(function() {
355 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
361 anchor.children('img').show();
366 $("a.thumbnail").children('img').hide();
367 $(this).closest(".entry-title").removeClass('ov');
372 function NoticeDataAttach() {
373 NDA = $('#notice_data-attach');
374 NDA.change(function() {
375 S = '<div id="notice_data-attach_selected" class="success"><code>'+$(this).val()+'</code> <button>×</button></div>';
376 NDAS = $('#notice_data-attach_selected');
377 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#form_notice').append(S);
378 $('#notice_data-attach_selected button').click(function(){
379 $('#notice_data-attach_selected').remove();