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){
23 var currentLength = $("#notice_data-text").val().length;
24 var remaining = maxLength - currentLength;
25 var counter = $("#notice_text-count");
26 counter.text(remaining);
29 $("#form_notice").addClass("warning");
31 $("#form_notice").removeClass("warning");
35 function submitonreturn(event) {
36 if (event.keyCode == 13) {
37 $("#form_notice").submit();
38 event.preventDefault();
39 event.stopPropagation();
45 if ($("#notice_data-text").length) {
46 $("#notice_data-text").bind("keyup", counter);
47 $("#notice_data-text").bind("keydown", submitonreturn);
49 // run once in case there's something in there
52 if($('body')[0].id != 'conversation') {
53 $("#notice_data-text").focus();
57 // XXX: refactor this code
59 var favoptions = { dataType: 'xml',
60 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
61 var dis = new_form.id;
62 var fav = dis.replace('disfavor', 'favor');
63 $('form#'+fav).replaceWith(new_form);
64 $('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden);
68 var disoptions = { dataType: 'xml',
69 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
70 var fav = new_form.id;
71 var dis = fav.replace('favor', 'disfavor');
72 $('form#'+dis).replaceWith(new_form);
73 $('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden);
77 var joinoptions = { dataType: 'xml',
78 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
79 var leave = new_form.id;
80 var join = leave.replace('leave', 'join');
81 $('form#'+join).replaceWith(new_form);
82 $('form#'+leave).ajaxForm(leaveoptions).each(addAjaxHidden);
86 var leaveoptions = { dataType: 'xml',
87 success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
88 var join = new_form.id;
89 var leave = join.replace('join', 'leave');
90 $('form#'+leave).replaceWith(new_form);
91 $('form#'+join).ajaxForm(joinoptions).each(addAjaxHidden);
95 function addAjaxHidden() {
96 var ajax = document.createElement('input');
97 ajax.setAttribute('type', 'hidden');
98 ajax.setAttribute('name', 'ajax');
99 ajax.setAttribute('value', 1);
100 this.appendChild(ajax);
103 $("form.form_favor").ajaxForm(favoptions);
104 $("form.form_disfavor").ajaxForm(disoptions);
105 $("form.form_group_join").ajaxForm(joinoptions);
106 $("form.form_group_leave").ajaxForm(leaveoptions);
107 $("form.form_favor").each(addAjaxHidden);
108 $("form.form_disfavor").each(addAjaxHidden);
109 $("form.form_group_join").each(addAjaxHidden);
110 $("form.form_group_leave").each(addAjaxHidden);
112 $("#form_user_nudge").ajaxForm ({ dataType: 'xml',
113 beforeSubmit: function(xml) { $("#form_user_nudge input[type=submit]").attr("disabled", "disabled");
114 $("#form_user_nudge input[type=submit]").addClass("disabled");
116 success: function(xml) { $("#form_user_nudge").replaceWith(document._importNode($("#nudge_response", xml).get(0),true));
117 $("#form_user_nudge input[type=submit]").removeAttr("disabled");
118 $("#form_user_nudge input[type=submit]").removeClass("disabled");
121 $("#form_user_nudge").each(addAjaxHidden);
123 var Subscribe = { dataType: 'xml',
124 beforeSubmit: function(formData, jqForm, options) { $(".form_user_subscribe input[type=submit]").attr("disabled", "disabled");
125 $(".form_user_subscribe input[type=submit]").addClass("disabled");
127 success: function(xml) { var form_unsubscribe = document._importNode($('form', xml).get(0), true);
128 var form_unsubscribe_id = form_unsubscribe.id;
129 var form_subscribe_id = form_unsubscribe_id.replace('unsubscribe', 'subscribe');
130 $("form#"+form_subscribe_id).replaceWith(form_unsubscribe);
131 $("form#"+form_unsubscribe_id).ajaxForm(UnSubscribe).each(addAjaxHidden);
132 $("dd.subscribers").text(parseInt($("dd.subscribers").text())+1);
133 $(".form_user_subscribe input[type=submit]").removeAttr("disabled");
134 $(".form_user_subscribe input[type=submit]").removeClass("disabled");
138 var UnSubscribe = { dataType: 'xml',
139 beforeSubmit: function(formData, jqForm, options) { $(".form_user_unsubscribe input[type=submit]").attr("disabled", "disabled");
140 $(".form_user_unsubscribe input[type=submit]").addClass("disabled");
142 success: function(xml) { var form_subscribe = document._importNode($('form', xml).get(0), true);
143 var form_subscribe_id = form_subscribe.id;
144 var form_unsubscribe_id = form_subscribe_id.replace('subscribe', 'unsubscribe');
145 $("form#"+form_unsubscribe_id).replaceWith(form_subscribe);
146 $("form#"+form_subscribe_id).ajaxForm(Subscribe).each(addAjaxHidden);
147 $("#profile_send_a_new_message").remove();
148 $("#profile_nudge").remove();
149 $("dd.subscribers").text(parseInt($("dd.subscribers").text())-1);
150 $(".form_user_unsubscribe input[type=submit]").removeAttr("disabled");
151 $(".form_user_unsubscribe input[type=submit]").removeClass("disabled");
155 $(".form_user_subscribe").ajaxForm(Subscribe);
156 $(".form_user_unsubscribe").ajaxForm(UnSubscribe);
157 $(".form_user_subscribe").each(addAjaxHidden);
158 $(".form_user_unsubscribe").each(addAjaxHidden);
160 var PostNotice = { dataType: 'xml',
161 beforeSubmit: function(formData, jqForm, options) { if ($("#notice_data-text").get(0).value.length == 0) {
162 $("#form_notice").addClass("warning");
165 $("#form_notice").addClass("processing");
166 $("#notice_action-submit").attr("disabled", "disabled");
167 $("#notice_action-submit").addClass("disabled");
171 error: function (xhr, textStatus, errorThrown) { $("#form_notice").removeClass("processing");
172 $("#notice_action-submit").removeAttr("disabled");
173 $("#notice_action-submit").removeClass("disabled");
174 if (textStatus == "timeout") {
175 alert ("Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists");
178 if ($(".error", xhr.responseXML).length > 0) {
179 $('#form_notice').append(document._importNode($(".error", xhr.responseXML).get(0), true));
182 var HTTP20x30x = [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307];
183 if(jQuery.inArray(parseInt(xhr.status), HTTP20x30x) < 0) {
184 alert("Sorry! We had trouble sending your notice ("+xhr.status+" "+xhr.statusText+"). Please report the problem to the site administrator if this happens again.");
187 $("#notice_data-text").val("");
193 success: function(xml) { if ($("#error", xml).length > 0) {
194 var result = document._importNode($("p", xml).get(0), true);
195 result = result.textContent || result.innerHTML;
199 if ($("#command_result", xml).length > 0) {
200 var result = document._importNode($("p", xml).get(0), true);
201 result = result.textContent || result.innerHTML;
205 li = $("li", xml).get(0);
206 if ($("#"+li.id).length == 0) {
207 var notice_irt_value = $('#notice_in-reply-to').val();
208 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
209 if($('body')[0].id == 'conversation') {
210 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
211 $(notice_irt).append('<ul class="notices"></ul>');
213 $($(notice_irt+' .notices')[0]).append(document._importNode(li, true));
216 $("#notices_primary .notices").prepend(document._importNode(li, true));
218 $('#'+li.id).css({display:'none'});
219 $('#'+li.id).fadeIn(2500);
224 $("#notice_data-text").val("");
225 $("#notice_data-attach").val("");
226 $("#notice_in-reply-to").val("");
227 $('#notice_data-attach_selected').remove();
230 $("#form_notice").removeClass("processing");
231 $("#notice_action-submit").removeAttr("disabled");
232 $("#notice_action-submit").removeClass("disabled");
235 $("#form_notice").ajaxForm(PostNotice);
236 $("#form_notice").each(addAjaxHidden);
242 function NoticeReply() {
243 if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) {
244 $('#content .notice').each(function() {
245 var notice = $(this)[0];
246 $($('.notice_reply', notice)[0]).click(function() {
247 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname');
248 NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
255 function NoticeReplySet(nick,id) {
256 rgx_username = /^[0-9a-zA-Z\-_.]*$/;
257 if (nick.match(rgx_username)) {
258 replyto = "@" + nick + " ";
259 if ($("#notice_data-text").length) {
260 $("#notice_data-text").val(replyto);
261 $("#form_notice input#notice_in-reply-to").val(id);
262 $("#notice_data-text").focus();
269 function NoticeAttachments() {
270 $.fn.jOverlay.options = {
278 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
279 bgClickToClose : true,
280 success : function() {
281 $('#jOverlayContent').append('<button>×</button>');
282 $('#jOverlayContent button').click($.closeOverlay);
286 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
289 $('#content .notice a.attachment').click(function() {
290 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
295 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
297 var anchor = $(this);
298 $("a.thumbnail").children('img').hide();
299 anchor.closest(".entry-title").addClass('ov');
301 if (anchor.children('img').length == 0) {
302 t = setTimeout(function() {
303 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
309 anchor.children('img').show();
314 $("a.thumbnail").children('img').hide();
315 $(this).closest(".entry-title").removeClass('ov');
320 function NoticeDataAttach() {
321 NDA = $('#notice_data-attach');
322 NDA.change(function() {
323 S = '<div id="notice_data-attach_selected" class="success"><code>'+$(this).val()+'</code> <button>×</button></div>';
324 NDAS = $('#notice_data-attach_selected');
325 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#form_notice').append(S);
326 $('#notice_data-attach_selected button').click(function(){
327 $('#notice_data-attach_selected').remove();