2 * Laconica - a distributed open-source microblogging tool
3 * Copyright (C) 2008, Controlez-Vous, 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 // count character on keyup
21 function counter(event){
25 var currentLength = $("#notice_data-text").val().length;
26 var remaining = maxLength - currentLength;
27 var counter = $("#notice_text-count");
28 counter.text(remaining);
31 $("#form_notice").addClass("warning");
33 $("#form_notice").removeClass("warning");
37 function submitonreturn(event) {
38 if (event.keyCode == 13) {
39 $("#form_notice").submit();
40 event.preventDefault();
41 event.stopPropagation();
47 // define maxLength if it wasn't defined already
49 if (typeof(maxLength) == "undefined") {
53 if ($("#notice_data-text").length) {
55 $("#notice_data-text").bind("keyup", counter);
56 // run once in case there's something in there
60 $("#notice_data-text").bind("keydown", submitonreturn);
62 if($('body')[0].id != 'conversation') {
63 $("#notice_data-text").focus();
67 // XXX: refactor this code
69 var favoptions = { dataType: 'xml',
70 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
71 var dis = new_form.id;
72 var fav = dis.replace('disfavor', 'favor');
73 $('form#'+fav).replaceWith(new_form);
74 $('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden);
78 var disoptions = { dataType: 'xml',
79 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
80 var fav = new_form.id;
81 var dis = fav.replace('favor', 'disfavor');
82 $('form#'+dis).replaceWith(new_form);
83 $('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden);
87 var joinoptions = { dataType: 'xml',
88 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
89 var leave = new_form.id;
90 var join = leave.replace('leave', 'join');
91 $('form#'+join).replaceWith(new_form);
92 $('form#'+leave).ajaxForm(leaveoptions).each(addAjaxHidden);
96 var leaveoptions = { dataType: 'xml',
97 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
98 var join = new_form.id;
99 var leave = join.replace('join', 'leave');
100 $('form#'+leave).replaceWith(new_form);
101 $('form#'+join).ajaxForm(joinoptions).each(addAjaxHidden);
105 function addAjaxHidden() {
106 var ajax = document.createElement('input');
107 ajax.setAttribute('type', 'hidden');
108 ajax.setAttribute('name', 'ajax');
109 ajax.setAttribute('value', 1);
110 this.appendChild(ajax);
113 $("form.form_favor").ajaxForm(favoptions);
114 $("form.form_disfavor").ajaxForm(disoptions);
115 $("form.form_group_join").ajaxForm(joinoptions);
116 $("form.form_group_leave").ajaxForm(leaveoptions);
117 $("form.form_favor").each(addAjaxHidden);
118 $("form.form_disfavor").each(addAjaxHidden);
119 $("form.form_group_join").each(addAjaxHidden);
120 $("form.form_group_leave").each(addAjaxHidden);
122 $("#form_user_nudge").ajaxForm ({ dataType: 'xml',
123 beforeSubmit: function(xml) { $("#form_user_nudge input[type=submit]").attr("disabled", "disabled");
124 $("#form_user_nudge input[type=submit]").addClass("disabled");
126 success: function(xml) { $("#form_user_nudge").replaceWith(document._importNode($("#nudge_response", xml).get(0),true));
127 $("#form_user_nudge input[type=submit]").removeAttr("disabled");
128 $("#form_user_nudge input[type=submit]").removeClass("disabled");
131 $("#form_user_nudge").each(addAjaxHidden);
133 var Subscribe = { dataType: 'xml',
134 beforeSubmit: function(formData, jqForm, options) { $(".form_user_subscribe input[type=submit]").attr("disabled", "disabled");
135 $(".form_user_subscribe input[type=submit]").addClass("disabled");
137 success: function(xml) { var form_unsubscribe = document._importNode($('form', xml).get(0), true);
138 var form_unsubscribe_id = form_unsubscribe.id;
139 var form_subscribe_id = form_unsubscribe_id.replace('unsubscribe', 'subscribe');
140 $("form#"+form_subscribe_id).replaceWith(form_unsubscribe);
141 $("form#"+form_unsubscribe_id).ajaxForm(UnSubscribe).each(addAjaxHidden);
142 $("dd.subscribers").text(parseInt($("dd.subscribers").text())+1);
143 $(".form_user_subscribe input[type=submit]").removeAttr("disabled");
144 $(".form_user_subscribe input[type=submit]").removeClass("disabled");
148 var UnSubscribe = { dataType: 'xml',
149 beforeSubmit: function(formData, jqForm, options) { $(".form_user_unsubscribe input[type=submit]").attr("disabled", "disabled");
150 $(".form_user_unsubscribe input[type=submit]").addClass("disabled");
152 success: function(xml) { var form_subscribe = document._importNode($('form', xml).get(0), true);
153 var form_subscribe_id = form_subscribe.id;
154 var form_unsubscribe_id = form_subscribe_id.replace('subscribe', 'unsubscribe');
155 $("form#"+form_unsubscribe_id).replaceWith(form_subscribe);
156 $("form#"+form_subscribe_id).ajaxForm(Subscribe).each(addAjaxHidden);
157 $("#profile_send_a_new_message").remove();
158 $("#profile_nudge").remove();
159 $("dd.subscribers").text(parseInt($("dd.subscribers").text())-1);
160 $(".form_user_unsubscribe input[type=submit]").removeAttr("disabled");
161 $(".form_user_unsubscribe input[type=submit]").removeClass("disabled");
165 $(".form_user_subscribe").ajaxForm(Subscribe);
166 $(".form_user_unsubscribe").ajaxForm(UnSubscribe);
167 $(".form_user_subscribe").each(addAjaxHidden);
168 $(".form_user_unsubscribe").each(addAjaxHidden);
170 var PostNotice = { dataType: 'xml',
171 beforeSubmit: function(formData, jqForm, options) { if ($("#notice_data-text").get(0).value.length == 0) {
172 $("#form_notice").addClass("warning");
175 $("#form_notice").addClass("processing");
176 $("#notice_action-submit").attr("disabled", "disabled");
177 $("#notice_action-submit").addClass("disabled");
181 error: function (xhr, textStatus, errorThrown) { $("#form_notice").removeClass("processing");
182 $("#notice_action-submit").removeAttr("disabled");
183 $("#notice_action-submit").removeClass("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 ($(".error", xhr.responseXML).length > 0) {
189 $('#form_notice').append(document._importNode($(".error", xhr.responseXML).get(0), true));
192 var HTTP20x30x = [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307];
193 if(jQuery.inArray(parseInt(xhr.status), HTTP20x30x) < 0) {
194 alert("Sorry! We had trouble sending your notice ("+xhr.status+" "+xhr.statusText+"). Please report the problem to the site administrator if this happens again.");
197 $("#notice_data-text").val("");
205 success: function(xml) { if ($("#error", xml).length > 0) {
206 var result = document._importNode($("p", xml).get(0), true);
207 result = result.textContent || result.innerHTML;
211 if ($("#command_result", xml).length > 0) {
212 var result = document._importNode($("p", xml).get(0), true);
213 result = result.textContent || result.innerHTML;
217 li = $("li", xml).get(0);
218 if ($("#"+li.id).length == 0) {
219 var notice_irt_value = $('#notice_in-reply-to').val();
220 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
221 if($('body')[0].id == 'conversation') {
222 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
223 $(notice_irt).append('<ul class="notices"></ul>');
225 $($(notice_irt+' .notices')[0]).append(document._importNode(li, true));
228 $("#notices_primary .notices").prepend(document._importNode(li, true));
230 $('#'+li.id).css({display:'none'});
231 $('#'+li.id).fadeIn(2500);
236 $("#notice_data-text").val("");
237 $("#notice_data-attach").val("");
238 $("#notice_in-reply-to").val("");
239 $('#notice_data-attach_selected').remove();
244 $("#form_notice").removeClass("processing");
245 $("#notice_action-submit").removeAttr("disabled");
246 $("#notice_action-submit").removeClass("disabled");
249 $("#form_notice").ajaxForm(PostNotice);
250 $("#form_notice").each(addAjaxHidden);
256 function NoticeReply() {
257 if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) {
258 $('#content .notice').each(function() {
259 var notice = $(this)[0];
260 $($('.notice_reply', notice)[0]).click(function() {
261 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname');
262 NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
269 function NoticeReplySet(nick,id) {
270 rgx_username = /^[0-9a-zA-Z\-_.]*$/;
271 if (nick.match(rgx_username)) {
272 replyto = "@" + nick + " ";
273 if ($("#notice_data-text").length) {
274 $("#notice_data-text").val(replyto);
275 $("#form_notice input#notice_in-reply-to").val(id);
276 $("#notice_data-text").focus();
283 function NoticeAttachments() {
284 $.fn.jOverlay.options = {
292 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
293 bgClickToClose : true,
294 success : function() {
295 $('#jOverlayContent').append('<button>×</button>');
296 $('#jOverlayContent button').click($.closeOverlay);
300 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
303 $('#content .notice a.attachment').click(function() {
304 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
309 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
311 var anchor = $(this);
312 $("a.thumbnail").children('img').hide();
313 anchor.closest(".entry-title").addClass('ov');
315 if (anchor.children('img').length == 0) {
316 t = setTimeout(function() {
317 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
323 anchor.children('img').show();
328 $("a.thumbnail").children('img').hide();
329 $(this).closest(".entry-title").removeClass('ov');
334 function NoticeDataAttach() {
335 NDA = $('#notice_data-attach');
336 NDA.change(function() {
337 S = '<div id="notice_data-attach_selected" class="success"><code>'+$(this).val()+'</code> <button>×</button></div>';
338 NDAS = $('#notice_data-attach_selected');
339 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#form_notice').append(S);
340 $('#notice_data-attach_selected button').click(function(){
341 $('#notice_data-attach_selected').remove();