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]
79 Processing: 'processing',
80 CommendResult: 'command_result',
81 FormNotice: 'form_notice',
82 NoticeDataText: 'notice_data-text',
83 NoticeTextCount: 'notice_text-count',
84 NoticeInReplyTo: 'notice_in-reply-to',
85 NoticeDataAttach: 'notice_data-attach',
86 NoticeDataAttachSelected: 'notice_data-attach_selected',
87 NoticeActionSubmit: 'notice_action-submit',
92 SubmitOnReturn: function(event, el) {
93 if (event.keyCode == 13 || event.keyCode == 10) {
95 event.preventDefault();
96 event.stopPropagation();
97 $('#'+SN.U.NoticeDataText).blur();
104 Counter: function() {
105 if (typeof(maxLength) == "undefined") {
106 maxLength = SN.C.I.MaxLength;
109 if (maxLength <= 0) {
113 var remaining = maxLength - $('#'+SN.C.S.NoticeDataText).val().length;
114 var counter = $('#'+SN.C.S.NoticeTextCount);
116 if (remaining.toString() != counter.text()) {
117 if (!SN.C.I.CounterBlackout || remaining == 0) {
118 if (counter.text() != String(remaining)) {
119 counter.text(remaining);
122 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
124 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Warning);
126 // Skip updates for the next 500ms.
127 // On slower hardware, updating on every keypress is unpleasant.
128 if (!SN.C.I.CounterBlackout) {
129 SN.C.I.CounterBlackout = true;
130 window.setTimeout(SN.U.ClearCounterBlackout, 500);
136 ClearCounterBlackout: function() {
137 // Allow keyup events to poke the counter again
138 SN.C.I.CounterBlackout = false;
139 // Check if the string changed since we last looked
143 FormXHR: function(f) {
144 f.bind('submit', function(e) {
145 form_id = $(this)[0].id;
148 url: $(this)[0].action,
149 data: $(this).serialize() + '&ajax=1',
150 beforeSend: function(xhr) {
151 $('#'+form_id).addClass(SN.C.S.Processing);
152 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
153 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
155 error: function (xhr, textStatus, errorThrown) {
156 alert(errorThrown || textStatus);
158 success: function(data, textStatus) {
159 if ($('form', data)[0].length > 0) {
160 form_new = $('form', data)[0];
161 $('#'+form_id).replaceWith(document._importNode(form_new, true));
162 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
165 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
173 FormNoticeXHR: function() {
174 $('#'+SN.C.S.FormNotice).append('<input type="hidden" name="ajax" value="1"/>');
175 $('#'+SN.C.S.FormNotice).ajaxForm({
177 beforeSend: function(xhr) {
178 if ($('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
179 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
182 $('#'+SN.C.S.FormNotice).addClass(SN.C.S.Processing);
183 $('#'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
184 $('#'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
187 error: function (xhr, textStatus, errorThrown) {
188 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
189 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
190 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
191 if (textStatus == 'timeout') {
192 alert ('Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists');
195 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
196 $('#'+SN.C.S.FormNotice).append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
199 if(jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) < 0) {
200 alert('Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
203 SN.C.I.NoticeDataText.val('');
204 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
209 success: function(data, textStatus) {
210 if ($('#'+SN.C.S.Error, data).length > 0) {
211 var result = document._importNode($('p', data)[0], true);
212 alert(result.textContent || result.innerHTML);
215 if($('body')[0].id == 'bookmarklet') {
218 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
219 var result = document._importNode($('p', data)[0], true);
220 alert(result.textContent || result.innerHTML);
223 notice = $('li', data)[0];
224 if ($('#'+notice.id).length === 0) {
225 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
226 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
227 if($('body')[0].id == 'conversation') {
228 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
229 $(notice_irt).append('<ul class="notices"></ul>');
231 $($(notice_irt+' .notices')[0]).append(document._importNode(notice, true));
234 $("#notices_primary .notices").prepend(document._importNode(notice, true));
236 $('#'+notice.id).css({display:'none'});
237 $('#'+notice.id).fadeIn(2500);
238 SN.U.NoticeAttachments();
242 $('#'+SN.C.S.NoticeDataText).val('');
243 $('#'+SN.C.S.NoticeDataAttach).val('');
244 $('#'+SN.C.S.NoticeInReplyTo).val('');
245 // SN.U.NoticeTextCounter($('#'+SN.C.S.NoticeDataText), $('#'+SN.C.S.NoticeTextCount), SN.C.I.NoticeTextCharMax);
248 complete: function(xhr, textStatus) {
249 $('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Processing);
250 $('#'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
251 $('#'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
256 NoticeReply: function() {
257 if ($('#'+SN.C.S.NoticeDataText).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.uid');
262 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
269 NoticeReplySet: function(nick,id) {
270 if (nick.match(SN.C.I.PatternUsername)) {
271 var text = $('#'+SN.C.S.NoticeDataText);
273 replyto = '@' + nick + ' ';
274 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
275 $('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id);
276 if (text.get(0).setSelectionRange) {
277 var len = text.val().length;
278 text.get(0).setSelectionRange(len,len);
287 NoticeAttachments: function() {
288 $.fn.jOverlay.options = {
296 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
297 bgClickToClose : true,
298 success : function() {
299 $('#jOverlayContent').append('<button>×</button>');
300 $('#jOverlayContent button').click($.closeOverlay);
304 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
307 $('#content .notice a.attachment').click(function() {
308 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
313 $("body:not(#shownotice) #content .notice a.thumbnail").hover(
315 var anchor = $(this);
316 $("a.thumbnail").children('img').hide();
317 anchor.closest(".entry-title").addClass('ov');
319 if (anchor.children('img').length == 0) {
320 t = setTimeout(function() {
321 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
327 anchor.children('img').show();
332 $("a.thumbnail").children('img').hide();
333 $(this).closest(".entry-title").removeClass('ov');
338 NoticeDataAttach: function() {
339 NDA = $('#'+SN.C.S.NoticeDataAttach);
340 NDA.change(function() {
341 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button>×</button></div>';
342 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
343 (NDAS.length > 0) ? NDAS.replaceWith(S) : $('#'+SN.C.S.FormNotice).append(S);
344 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
345 $('#'+SN.C.S.NoticeDataAttachSelected).remove();