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/>.
18 * @category UI interaction
20 * @author Sarven Capadisli <csarven@status.net>
21 * @author Evan Prodromou <evan@status.net>
22 * @copyright 2009 StatusNet, Inc.
23 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
24 * @link http://status.net/
27 $(document).ready(function(){
28 if ($('body.user_in').length > 0) {
29 if ($('#'+SN.C.S.NoticeDataText).length) {
31 $('#'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
34 // run once in case there's something in there
38 $('#'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
39 SN.U.SubmitOnReturn(e, $('#'+SN.C.S.FormNotice));
42 if($('body')[0].id != 'conversation') {
43 $('#'+SN.C.S.NoticeDataText).focus();
47 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
48 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
49 $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
50 $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
51 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
52 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
53 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
59 SN.U.NoticeDataAttach();
62 SN.U.NoticeAttachments();
66 var SN = { // StatusNet
69 CounterBlackout: false,
71 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
72 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
80 Processing: 'processing',
81 CommendResult: 'command_result',
82 FormNotice: 'form_notice',
83 NoticeDataText: 'notice_data-text',
84 NoticeTextCount: 'notice_text-count',
85 NoticeInReplyTo: 'notice_in-reply-to',
86 NoticeDataAttach: 'notice_data-attach',
87 NoticeDataAttachSelected: 'notice_data-attach_selected',
88 NoticeActionSubmit: 'notice_action-submit',
93 SubmitOnReturn: function(event, el) {
94 if (event.keyCode == 13 || event.keyCode == 10) {
96 event.preventDefault();
97 event.stopPropagation();
98 $('#'+SN.U.NoticeDataText).blur();
105 Counter: function() {
106 if (typeof(maxLength) == "undefined") {
107 maxLength = SN.C.I.MaxLength;
110 if (maxLength <= 0) {
114 var remaining = maxLength - $('#'+SN.C.S.NoticeDataText).val().length;
115 var counter = $('#'+SN.C.S.NoticeTextCount);
117 if (remaining.toString() != counter.text()) {
118 if (!SN.C.I.CounterBlackout || remaining == 0) {
119 if (counter.text() != String(remaining)) {
120 counter.text(remaining);
123 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
125 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Warning);
127 // Skip updates for the next 500ms.
128 // On slower hardware, updating on every keypress is unpleasant.
129 if (!SN.C.I.CounterBlackout) {
130 SN.C.I.CounterBlackout = true;
131 window.setTimeout(SN.U.ClearCounterBlackout, 500);
137 ClearCounterBlackout: function() {
138 // Allow keyup events to poke the counter again
139 SN.C.I.CounterBlackout = false;
140 // Check if the string changed since we last looked
144 FormXHR: function(f) {
145 f.bind('submit', function(e) {
146 form_id = $(this)[0].id;
149 url: $(this)[0].action,
150 data: $(this).serialize() + '&ajax=1',
151 beforeSend: function(xhr) {
152 $('#'+form_id).addClass(SN.C.S.Processing);
153 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
154 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
156 error: function (xhr, textStatus, errorThrown) {
157 alert(errorThrown || textStatus);
159 success: function(data, textStatus) {
160 if ($('form', data)[0].length > 0) {
161 form_new = $('form', data)[0];
162 $('#'+form_id).replaceWith(document._importNode(form_new, true));
163 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
166 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
174 FormNoticeXHR: function() {
175 $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
176 $('#'+SN.C.S.FormNotice).ajaxForm({
179 beforeSend: function(xhr) {
180 if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
181 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
184 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
185 $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
186 $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
189 error: function (xhr, textStatus, errorThrown) {
190 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
191 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
192 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
193 if (textStatus == 'timeout') {
194 alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
197 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
198 $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
201 if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
202 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
205 SN.C.I.NoticeDataText.val('');
211 success: function(data, textStatus) {
212 if ($('#'+SN.C.S.Error, data).length > 0) {
213 var result = document._importNode($('p', data)[0], true);
214 alert(result.textContent || result.innerHTML);
217 if($('body')[0].id == 'bookmarklet') {
220 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
221 var result = document._importNode($('p', data)[0], true);
222 alert(result.textContent || result.innerHTML);
225 notice = $('li', data)[0];
226 if ($('#'+notice.id).length == 0) {
227 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
228 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
229 if($('body')[0].id == 'conversation') {
230 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
231 $(notice_irt).append('<ul class="notices"></ul>');
233 $($(notice_irt+' .notices')[0]).append(document._importNode(notice, true));
236 $("#notices_primary .notices").prepend(document._importNode(notice, true));
238 $('#'+notice.id).css({display:'none'});
239 $('#'+notice.id).fadeIn(2500);
240 SN.U.NoticeAttachments();
244 $('#'+SN.C.S.NoticeDataText).val('');
245 $('#'+SN.C.S.NoticeDataAttach).val('');
246 $('#'+SN.C.S.NoticeInReplyTo).val('');
247 $('#'+SN.C.S.NoticeDataAttachSelected).remove();
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');
341 NoticeDataAttach: function() {
342 NDA = $('#'+SN.C.S.NoticeDataAttach);
343 NDA.change(function() {
344 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button>×</button></div>';
345 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
346 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#'+SN.C.S.FormNotice).append(S);
347 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
348 $('#'+SN.C.S.NoticeDataAttachSelected).remove();