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 var SN = { // StatusNet
30 CounterBlackout: false,
32 PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
33 HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
41 Processing: 'processing',
42 CommandResult: 'command_result',
43 FormNotice: 'form_notice',
44 NoticeDataText: 'notice_data-text',
45 NoticeTextCount: 'notice_text-count',
46 NoticeInReplyTo: 'notice_in-reply-to',
47 NoticeDataAttach: 'notice_data-attach',
48 NoticeDataAttachSelected: 'notice_data-attach_selected',
49 NoticeActionSubmit: 'notice_action-submit',
50 NoticeLat: 'notice_data-lat',
51 NoticeLon: 'notice_data-lon',
52 NoticeLocationId: 'notice_data-location_id',
53 NoticeLocationNs: 'notice_data-location_ns'
58 FormNoticeEnhancements: function(form) {
59 form_id = form.attr('id');
60 $('#'+form_id+' #'+SN.C.S.NoticeDataText).unbind('keyup');
61 $('#'+form_id+' #'+SN.C.S.NoticeDataText).unbind('keydown');
63 $('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
66 // run once in case there's something in there
70 $('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
71 SN.U.SubmitOnReturn(e, form);
74 if($('body')[0].id != 'conversation') {
75 $('#'+form_id+' textarea').focus();
79 SubmitOnReturn: function(event, el) {
80 if (event.keyCode == 13 || event.keyCode == 10) {
82 event.preventDefault();
83 event.stopPropagation();
84 $('#'+el[0].id+' #'+SN.C.S.NoticeDataText).blur();
91 Counter: function(form) {
92 SN.C.I.FormNoticeCurrent = form;
93 form_id = form.attr('id');
94 if (typeof(maxLength) == "undefined") {
95 maxLength = SN.C.I.MaxLength;
102 var remaining = maxLength - $('#'+form_id+' #'+SN.C.S.NoticeDataText).val().length;
103 var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount);
105 if (remaining.toString() != counter.text()) {
106 if (!SN.C.I.CounterBlackout || remaining === 0) {
107 if (counter.text() != String(remaining)) {
108 counter.text(remaining);
111 form.addClass(SN.C.S.Warning);
113 form.removeClass(SN.C.S.Warning);
115 // Skip updates for the next 500ms.
116 // On slower hardware, updating on every keypress is unpleasant.
117 if (!SN.C.I.CounterBlackout) {
118 SN.C.I.CounterBlackout = true;
119 SN.C.I.FormNoticeCurrent = form;
120 window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
126 ClearCounterBlackout: function(form) {
127 // Allow keyup events to poke the counter again
128 SN.C.I.CounterBlackout = false;
129 // Check if the string changed since we last looked
133 FormXHR: function(f) {
134 if (jQuery.data(f[0], "ElementData") === undefined) {
135 jQuery.data(f[0], "ElementData", {Bind:'submit'});
136 f.bind('submit', function(e) {
137 form_id = $(this)[0].id;
141 url: $(this)[0].action,
142 data: $(this).serialize() + '&ajax=1',
143 beforeSend: function(xhr) {
144 $('#'+form_id).addClass(SN.C.S.Processing);
145 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
146 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
148 error: function (xhr, textStatus, errorThrown) {
149 alert(errorThrown || textStatus);
151 success: function(data, textStatus) {
152 if (typeof($('form', data)[0]) != 'undefined') {
153 form_new = document._importNode($('form', data)[0], true);
154 $('#'+form_id).replaceWith(form_new);
155 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
158 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
167 FormNoticeXHR: function(form) {
168 form_id = form.attr('id');
169 form.append('<input type="hidden" name="ajax" value="1"/>');
173 beforeSend: function(xhr) {
174 if ($('#'+form_id+' #'+SN.C.S.NoticeDataText)[0].value.length === 0) {
175 form.addClass(SN.C.S.Warning);
178 form.addClass(SN.C.S.Processing);
179 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
180 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
183 error: function (xhr, textStatus, errorThrown) {
184 form.removeClass(SN.C.S.Processing);
185 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
186 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
187 if (textStatus == 'timeout') {
188 form.append('<p class="error>Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists.</p>');
191 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
192 form.append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
195 if (parseInt(xhr.status) === 0 || jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) >= 0) {
196 $('#'+form_id).resetForm();
197 $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
198 SN.U.FormNoticeEnhancements($('#'+form_id));
201 form.append('<p class="error>(Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.</p>');
206 success: function(data, textStatus) {
208 if ($('#'+SN.C.S.Error, data).length > 0) {
209 result = document._importNode($('p', data)[0], true);
210 result = result.textContent || result.innerHTML;
211 form.append('<p class="error">'+result+'</p>');
214 if($('body')[0].id == 'bookmarklet') {
218 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
219 result = document._importNode($('p', data)[0], true);
220 result = result.textContent || result.innerHTML;
221 form.append('<p class="success">'+result+'</p>');
224 var notices = $('#notices_primary .notices');
225 if (notices.length > 0) {
226 var notice = document._importNode($('li', data)[0], true);
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(notice);
237 notices.prepend(notice);
239 $('#'+notice.id).css({display:'none'});
240 $('#'+notice.id).fadeIn(2500);
241 SN.U.NoticeWithAttachment($('#'+notice.id));
242 SN.U.NoticeReplyTo($('#'+notice.id));
243 SN.U.FormXHR($('#'+notice.id+' .form_favor'));
247 result = document._importNode($('title', data)[0], true);
248 result_title = result.textContent || result.innerHTML;
249 form.append('<p class="success">'+result_title+'</p>');
252 $('#'+form_id).resetForm();
253 $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
254 SN.U.FormNoticeEnhancements($('#'+form_id));
257 complete: function(xhr, textStatus) {
258 form.removeClass(SN.C.S.Processing);
259 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
260 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
265 NoticeReply: function() {
266 if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
267 $('#content .notice').each(function() { SN.U.NoticeReplyTo($(this)); });
271 NoticeReplyTo: function(notice_item) {
272 var notice = notice_item[0];
273 var notice_reply = $('.notice_reply', notice)[0];
275 if (jQuery.data(notice_reply, "ElementData") === undefined) {
276 jQuery.data(notice_reply, "ElementData", {Bind:'submit'});
277 $(notice_reply).bind('click', function() {
278 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
279 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
285 NoticeReplySet: function(nick,id) {
286 if (nick.match(SN.C.I.PatternUsername)) {
287 var text = $('#'+SN.C.S.NoticeDataText);
288 if (text.length > 0) {
289 replyto = '@' + nick + ' ';
290 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
291 $('#'+SN.C.S.FormNotice+' #'+SN.C.S.NoticeInReplyTo).val(id);
294 if (text[0].setSelectionRange) {
295 var len = text.val().length;
296 text[0].setSelectionRange(len,len);
302 NoticeFavor: function() {
303 $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
304 $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
307 NoticeAttachments: function() {
308 $('.notice a.attachment').each(function() {
309 SN.U.NoticeWithAttachment($(this).closest('.notice'));
313 NoticeWithAttachment: function(notice) {
314 if ($('.attachment', notice).length === 0) {
318 var notice_id = notice.attr('id');
320 $.fn.jOverlay.options = {
328 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
329 bgClickToClose : true,
330 success : function() {
331 $('#jOverlayContent').append('<button class="close">×</button>');
332 $('#jOverlayContent button').click($.closeOverlay);
336 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
339 $('#'+notice_id+' a.attachment').click(function() {
340 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
345 $("body:not(#shownotice) #"+notice_id+" a.thumbnail").hover(
347 var anchor = $(this);
348 $("a.thumbnail").children('img').hide();
349 anchor.closest(".entry-title").addClass('ov');
351 if (anchor.children('img').length === 0) {
352 t = setTimeout(function() {
353 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
359 anchor.children('img').show();
364 $("a.thumbnail").children('img').hide();
365 $(this).closest(".entry-title").removeClass('ov');
370 NoticeDataAttach: function() {
371 NDA = $('#'+SN.C.S.NoticeDataAttach);
372 NDA.change(function() {
373 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">×</button></div>';
374 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
375 if (NDAS.length > 0) {
379 $('#'+SN.C.S.FormNotice).append(S);
381 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
382 $('#'+SN.C.S.NoticeDataAttachSelected).remove();
388 NoticeLocationAttach: function() {
389 if(navigator.geolocation) navigator.geolocation.watchPosition(function(position) {
390 $('#'+SN.C.S.NoticeLat).val(position.coords.latitude);
391 $('#'+SN.C.S.NoticeLon).val(position.coords.longitude);
395 NewDirectMessage: function() {
396 NDM = $('.entity_send-a-message a');
397 NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
398 NDM.bind('click', function() {
399 var NDMF = $('.entity_send-a-message form');
400 if (NDMF.length === 0) {
401 $(this).addClass('processing');
402 $.get(NDM.attr('href'), null, function(data) {
403 $('.entity_send-a-message').append(document._importNode($('form', data)[0], true));
404 NDMF = $('.entity_send-a-message .form_notice');
405 SN.U.FormNoticeXHR(NDMF);
406 SN.U.FormNoticeEnhancements(NDMF);
407 NDMF.append('<button class="close">×</button>');
408 $('.entity_send-a-message button').click(function(){
412 NDM.removeClass('processing');
417 $('.entity_send-a-message textarea').focus();
425 NoticeForm: function() {
426 if ($('body.user_in').length > 0) {
427 $('.'+SN.C.S.FormNotice).each(function() {
428 SN.U.FormNoticeXHR($(this));
429 SN.U.FormNoticeEnhancements($(this));
432 SN.U.NoticeDataAttach();
433 SN.U.NoticeLocationAttach();
437 Notices: function() {
438 if ($('body.user_in').length > 0) {
444 SN.U.NoticeAttachments();
447 EntityActions: function() {
448 if ($('body.user_in').length > 0) {
449 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
450 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
451 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
452 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
453 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
459 $(document).ready(function(){
460 if ($('.'+SN.C.S.FormNotice).length > 0) {
461 SN.Init.NoticeForm();
463 if ($('#content .notices').length > 0) {
466 if ($('#content .entity_actions').length > 0) {
467 SN.Init.EntityActions();