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();
103 SN.U.NoticeAttachments();
110 function NoticeDataAttach() {
111 NDA = $('#notice_data-attach');
112 NDA.change(function() {
113 S = '<div id="notice_data-attach_selected" class="success"><code>'+$(this).val()+'</code> <button>×</button></div>';
114 NDAS = $('#notice_data-attach_selected');
115 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#form_notice').append(S);
116 $('#notice_data-attach_selected button').click(function(){
117 $('#notice_data-attach_selected').remove();
123 var SN = { // StatusNet
126 NoticeTextCharMax: 140,
127 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
128 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
131 Disabled: 'disabled',
134 Processing: 'processing',
135 CommendResult: 'command_result',
136 FormNotice: 'form_notice',
137 NoticeDataText: 'notice_data-text',
138 NoticeTextCount: 'notice_text-count',
139 NoticeInReplyTo: 'notice_in-reply-to',
140 NoticeDataAttach: 'notice_data-attach',
141 NoticeActionSubmit: 'notice_action-submit'
146 FormXHR: function(f) {
147 f.bind('submit', function(e) {
148 form_id = $(this)[0].id;
151 url: $(this)[0].action,
152 data: $(this).serialize() + '&ajax=1',
153 beforeSend: function(xhr) {
154 $('#'+form_id).addClass(SN.C.S.Processing);
155 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
156 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
158 error: function (xhr, textStatus, errorThrown) {
159 alert(errorThrown || textStatus);
161 success: function(data, textStatus) {
162 if ($('form', data)[0].length > 0) {
163 form_new = $('form', data)[0];
164 $('#'+form_id).replaceWith(document._importNode(form_new, true));
165 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
168 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
176 FormNoticeXHR: function() {
177 $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
178 $('#'+SN.C.S.FormNotice).ajaxForm({
180 beforeSend: function(xhr) {
181 if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
182 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
185 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
186 $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
187 $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
190 error: function (xhr, textStatus, errorThrown) {
191 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
192 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
193 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
194 if (textStatus == 'timeout') {
195 alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
198 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
199 $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
202 if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
203 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
206 SN.C.I.NoticeDataText.val('');
207 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
212 success: function(data, textStatus) {
213 if ($('#'+SN.C.S.Error, data).length > 0) {
214 var result = document._importNode($('p', data)[0], true);
215 alert(result.textContent || result.innerHTML);
218 if($('body')[0].id == 'bookmarklet') {
221 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
222 var result = document._importNode($('p', data)[0], true);
223 alert(result.textContent || result.innerHTML);
226 notice = $('li', data)[0];
227 if ($('#'+notice.id).length === 0) {
228 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
229 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
230 if($('body')[0].id == 'conversation') {
231 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
232 $(notice_irt).append('<ul class="notices"></ul>');
234 $($(notice_irt+' .notices')[0]).append(document._importNode(notice, true));
237 $("#notices_primary .notices").prepend(document._importNode(notice, true));
239 $('#'+notice.id).css({display:'none'});
240 $('#'+notice.id).fadeIn(2500);
241 // SN.U.NoticeAttachments();
245 $('#'+SN.C.S.NoticeDataText).val('');
246 $('#'+SN.C.S.NoticeDataAttach).val('');
247 $('#'+SN.C.S.NoticeInReplyTo).val('');
248 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
251 complete: function(xhr, textStatus) {
252 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
253 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
254 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
259 NoticeReply: function() {
260 if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
261 $('#content .notice').each(function() {
262 var notice = $(this)[0];
263 $($('.notice_reply', notice)[0]).click(function() {
264 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
265 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
272 NoticeReplySet: function(nick,id) {
273 if (nick.match(SN.C.I.PatternUsername)) {
274 var text = $('#'+SN.C.S.NoticeDataText);
276 replyto = '@' + nick + ' ';
277 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
278 $('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id);
279 if (text.get(0).setSelectionRange) {
280 var len = text.val().length;
281 text.get(0).setSelectionRange(len,len);
290 NoticeAttachments: function() {
291 $.fn.jOverlay.options = {
299 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
300 bgClickToClose : true,
301 success : function() {
302 $('#jOverlayContent').append('<button>×</button>');
303 $('#jOverlayContent button').click($.closeOverlay);
307 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
310 $('#content .notice a.attachment').click(function() {
311 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
316 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
318 var anchor = $(this);
319 $("a.thumbnail").children('img').hide();
320 anchor.closest(".entry-title").addClass('ov');
322 if (anchor.children('img').length == 0) {
323 t = setTimeout(function() {
324 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
330 anchor.children('img').show();
335 $("a.thumbnail").children('img').hide();
336 $(this).closest(".entry-title").removeClass('ov');