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){
25 var currentLength = $("#notice_data-text").val().length;
26 var remaining = maxLength - currentLength;
27 var counter = $("#notice_text-count");
29 if (remaining.toString() != counter.text()) {
30 if (!counterBlackout || remaining == 0) {
31 if (counter.text() != String(remaining)) {
32 counter.text(remaining);
36 $("#form_notice").addClass("warning");
38 $("#form_notice").removeClass("warning");
40 // Skip updates for the next 500ms.
41 // On slower hardware, updating on every keypress is unpleasant.
42 if (!counterBlackout) {
43 counterBlackout = true;
44 window.setTimeout(clearCounterBlackout, 500);
50 function clearCounterBlackout() {
51 // Allow keyup events to poke the counter again
52 counterBlackout = false;
53 // Check if the string changed since we last looked
57 function submitonreturn(event) {
58 if (event.keyCode == 13 || event.keyCode == 10) {
59 // iPhone sends \n not \r for 'return'
60 $("#form_notice").submit();
61 event.preventDefault();
62 event.stopPropagation();
63 $("#notice_data-text").blur();
70 if ($("#notice_data-text").length) {
71 $("#notice_data-text").bind("keyup", counter);
72 $("#notice_data-text").bind("keydown", submitonreturn);
74 // run once in case there's something in there
77 if($('body')[0].id != 'conversation') {
78 $("#notice_data-text").focus();
82 // XXX: refactor this code
84 var favoptions = { dataType: 'xml',
85 beforeSubmit: function(data, target, options) {
86 $(target).addClass('processing');
89 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
90 var dis = new_form.id;
91 var fav = dis.replace('disfavor', 'favor');
92 $('form#'+fav).replaceWith(new_form);
93 $('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden);
97 var disoptions = { dataType: 'xml',
98 beforeSubmit: function(data, target, options) {
99 $(target).addClass('processing');
102 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
103 var fav = new_form.id;
104 var dis = fav.replace('favor', 'disfavor');
105 $('form#'+dis).replaceWith(new_form);
106 $('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden);
110 var joinoptions = { dataType: 'xml',
111 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
112 var leave = new_form.id;
113 var join = leave.replace('leave', 'join');
114 $('form#'+join).replaceWith(new_form);
115 $('form#'+leave).ajaxForm(leaveoptions).each(addAjaxHidden);
119 var leaveoptions = { dataType: 'xml',
120 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
121 var join = new_form.id;
122 var leave = join.replace('join', 'leave');
123 $('form#'+leave).replaceWith(new_form);
124 $('form#'+join).ajaxForm(joinoptions).each(addAjaxHidden);
128 function addAjaxHidden() {
129 var ajax = document.createElement('input');
130 ajax.setAttribute('type', 'hidden');
131 ajax.setAttribute('name', 'ajax');
132 ajax.setAttribute('value', 1);
133 this.appendChild(ajax);
136 $("form.form_favor").ajaxForm(favoptions);
137 $("form.form_disfavor").ajaxForm(disoptions);
138 $("form.form_group_join").ajaxForm(joinoptions);
139 $("form.form_group_leave").ajaxForm(leaveoptions);
140 $("form.form_favor").each(addAjaxHidden);
141 $("form.form_disfavor").each(addAjaxHidden);
142 $("form.form_group_join").each(addAjaxHidden);
143 $("form.form_group_leave").each(addAjaxHidden);
145 $("#form_user_nudge").ajaxForm ({ dataType: 'xml',
146 beforeSubmit: function(xml) { $("#form_user_nudge input[type=submit]").attr("disabled", "disabled");
147 $("#form_user_nudge input[type=submit]").addClass("disabled");
149 success: function(xml) { $("#form_user_nudge").replaceWith(document._importNode($("#nudge_response", xml).get(0),true));
150 $("#form_user_nudge input[type=submit]").removeAttr("disabled");
151 $("#form_user_nudge input[type=submit]").removeClass("disabled");
154 $("#form_user_nudge").each(addAjaxHidden);
156 var Subscribe = { dataType: 'xml',
157 beforeSubmit: function(formData, jqForm, options) { $(".form_user_subscribe input[type=submit]").attr("disabled", "disabled");
158 $(".form_user_subscribe input[type=submit]").addClass("disabled");
160 success: function(xml) { var form_unsubscribe = document._importNode($('form', xml).get(0), true);
161 var form_unsubscribe_id = form_unsubscribe.id;
162 var form_subscribe_id = form_unsubscribe_id.replace('unsubscribe', 'subscribe');
163 $("form#"+form_subscribe_id).replaceWith(form_unsubscribe);
164 $("form#"+form_unsubscribe_id).ajaxForm(UnSubscribe).each(addAjaxHidden);
165 $("dd.subscribers").text(parseInt($("dd.subscribers").text())+1);
166 $(".form_user_subscribe input[type=submit]").removeAttr("disabled");
167 $(".form_user_subscribe input[type=submit]").removeClass("disabled");
171 var UnSubscribe = { dataType: 'xml',
172 beforeSubmit: function(formData, jqForm, options) { $(".form_user_unsubscribe input[type=submit]").attr("disabled", "disabled");
173 $(".form_user_unsubscribe input[type=submit]").addClass("disabled");
175 success: function(xml) { var form_subscribe = document._importNode($('form', xml).get(0), true);
176 var form_subscribe_id = form_subscribe.id;
177 var form_unsubscribe_id = form_subscribe_id.replace('subscribe', 'unsubscribe');
178 $("form#"+form_unsubscribe_id).replaceWith(form_subscribe);
179 $("form#"+form_subscribe_id).ajaxForm(Subscribe).each(addAjaxHidden);
180 $("#profile_send_a_new_message").remove();
181 $("#profile_nudge").remove();
182 $("dd.subscribers").text(parseInt($("dd.subscribers").text())-1);
183 $(".form_user_unsubscribe input[type=submit]").removeAttr("disabled");
184 $(".form_user_unsubscribe input[type=submit]").removeClass("disabled");
188 $(".form_user_subscribe").ajaxForm(Subscribe);
189 $(".form_user_unsubscribe").ajaxForm(UnSubscribe);
190 $(".form_user_subscribe").each(addAjaxHidden);
191 $(".form_user_unsubscribe").each(addAjaxHidden);
193 var PostNotice = { dataType: 'xml',
194 beforeSubmit: function(formData, jqForm, options) { if ($("#notice_data-text").get(0).value.length == 0) {
195 $("#form_notice").addClass("warning");
198 $("#form_notice").addClass("processing");
199 $("#notice_action-submit").attr("disabled", "disabled");
200 $("#notice_action-submit").addClass("disabled");
204 error: function (xhr, textStatus, errorThrown) { $("#form_notice").removeClass("processing");
205 $("#notice_action-submit").removeAttr("disabled");
206 $("#notice_action-submit").removeClass("disabled");
207 if (textStatus == "timeout") {
208 alert ("Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists");
211 if ($(".error", xhr.responseXML).length > 0) {
212 $('#form_notice').append(document._importNode($(".error", xhr.responseXML).get(0), true));
215 var HTTP20x30x = [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307];
216 if(jQuery.inArray(parseInt(xhr.status), HTTP20x30x) < 0) {
217 alert("Sorry! We had trouble sending your notice ("+xhr.status+" "+xhr.statusText+"). Please report the problem to the site administrator if this happens again.");
220 $("#notice_data-text").val("");
226 success: function(xml) { if ($("#error", xml).length > 0) {
227 var result = document._importNode($("p", xml).get(0), true);
228 result = result.textContent || result.innerHTML;
232 if ($("#command_result", xml).length > 0) {
233 var result = document._importNode($("p", xml).get(0), true);
234 result = result.textContent || result.innerHTML;
238 li = $("li", xml).get(0);
239 if ($("#"+li.id).length == 0) {
240 var notice_irt_value = $('#notice_in-reply-to').val();
241 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
242 if($('body')[0].id == 'conversation') {
243 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
244 $(notice_irt).append('<ul class="notices"></ul>');
246 $($(notice_irt+' .notices')[0]).append(document._importNode(li, true));
249 $("#notices_primary .notices").prepend(document._importNode(li, true));
251 $('#'+li.id).css({display:'none'});
252 $('#'+li.id).fadeIn(2500);
257 $("#notice_data-text").val("");
258 $("#notice_data-attach").val("");
259 $("#notice_in-reply-to").val("");
260 $('#notice_data-attach_selected').remove();
263 $("#form_notice").removeClass("processing");
264 $("#notice_action-submit").removeAttr("disabled");
265 $("#notice_action-submit").removeClass("disabled");
268 $("#form_notice").ajaxForm(PostNotice);
269 $("#form_notice").each(addAjaxHidden);
275 function NoticeReply() {
276 if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) {
277 $('#content .notice').each(function() {
278 var notice = $(this)[0];
279 $($('.notice_reply', notice)[0]).click(function() {
280 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
281 NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
288 function NoticeReplySet(nick,id) {
289 rgx_username = /^[0-9a-zA-Z\-_.]*$/;
290 if (nick.match(rgx_username)) {
291 var text = $("#notice_data-text");
293 replyto = "@" + nick + " ";
294 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
295 $("#form_notice input#notice_in-reply-to").val(id);
296 if (text.get(0).setSelectionRange) {
297 var len = text.val().length;
298 text.get(0).setSelectionRange(len,len);
307 function NoticeAttachments() {
308 $.fn.jOverlay.options = {
316 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
317 bgClickToClose : true,
318 success : function() {
319 $('#jOverlayContent').append('<button>×</button>');
320 $('#jOverlayContent button').click($.closeOverlay);
324 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
327 $('#content .notice a.attachment').click(function() {
328 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
333 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
335 var anchor = $(this);
336 $("a.thumbnail").children('img').hide();
337 anchor.closest(".entry-title").addClass('ov');
339 if (anchor.children('img').length == 0) {
340 t = setTimeout(function() {
341 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
347 anchor.children('img').show();
352 $("a.thumbnail").children('img').hide();
353 $(this).closest(".entry-title").removeClass('ov');
358 function NoticeDataAttach() {
359 NDA = $('#notice_data-attach');
360 NDA.change(function() {
361 S = '<div id="notice_data-attach_selected" class="success"><code>'+$(this).val()+'</code> <button>×</button></div>';
362 NDAS = $('#notice_data-attach_selected');
363 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#form_notice').append(S);
364 $('#notice_data-attach_selected button').click(function(){
365 $('#notice_data-attach_selected').remove();