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',
54 NoticeGeoName: 'notice_data-geo_name',
55 NoticeDataGeo: 'notice_data-geo',
56 NoticeDataGeoSelected: 'notice_data-geo_selected'
61 FormNoticeEnhancements: function(form) {
62 form_id = form.attr('id');
64 if (jQuery.data(form[0], 'ElementData') === undefined) {
65 MaxLength = $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text();
66 if (typeof(MaxLength) == 'undefined') {
67 MaxLength = SN.C.I.MaxLength;
69 jQuery.data(form[0], 'ElementData', {MaxLength:MaxLength});
73 NDT = $('#'+form_id+' #'+SN.C.S.NoticeDataText);
75 NDT.bind('keyup', function(e) {
79 NDT.bind('keydown', function(e) {
80 SN.U.SubmitOnReturn(e, form);
84 $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text(jQuery.data(form[0], 'ElementData').MaxLength);
87 if ($('body')[0].id != 'conversation') {
88 $('#'+form_id+' textarea').focus();
92 SubmitOnReturn: function(event, el) {
93 if (event.keyCode == 13 || event.keyCode == 10) {
95 event.preventDefault();
96 event.stopPropagation();
97 $('#'+el[0].id+' #'+SN.C.S.NoticeDataText).blur();
104 Counter: function(form) {
105 SN.C.I.FormNoticeCurrent = form;
106 form_id = form.attr('id');
108 var MaxLength = jQuery.data(form[0], 'ElementData').MaxLength;
110 if (MaxLength <= 0) {
114 var remaining = MaxLength - $('#'+form_id+' #'+SN.C.S.NoticeDataText).val().length;
115 var counter = $('#'+form_id+' #'+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 form.addClass(SN.C.S.Warning);
125 form.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 SN.C.I.FormNoticeCurrent = form;
132 window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
138 ClearCounterBlackout: function(form) {
139 // Allow keyup events to poke the counter again
140 SN.C.I.CounterBlackout = false;
141 // Check if the string changed since we last looked
145 FormXHR: function(f) {
146 if (jQuery.data(f[0], "ElementData") === undefined) {
147 jQuery.data(f[0], "ElementData", {Bind:'submit'});
148 f.bind('submit', function(e) {
149 form_id = $(this)[0].id;
153 url: $(this)[0].action,
154 data: $(this).serialize() + '&ajax=1',
155 beforeSend: function(xhr) {
156 $('#'+form_id).addClass(SN.C.S.Processing);
157 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
158 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
160 error: function (xhr, textStatus, errorThrown) {
161 alert(errorThrown || textStatus);
163 success: function(data, textStatus) {
164 if (typeof($('form', data)[0]) != 'undefined') {
165 form_new = document._importNode($('form', data)[0], true);
166 $('#'+form_id).replaceWith(form_new);
167 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
170 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
179 FormNoticeXHR: function(form) {
180 var NDG, NLat, NLon, NLNS, NLID;
181 form_id = form.attr('id');
182 form.append('<input type="hidden" name="ajax" value="1"/>');
186 beforeSend: function(formData) {
187 if ($('#'+form_id+' #'+SN.C.S.NoticeDataText)[0].value.length === 0) {
188 form.addClass(SN.C.S.Warning);
191 form.addClass(SN.C.S.Processing);
192 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
193 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
195 NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked');
196 NLat = $('#'+SN.C.S.NoticeLat).val();
197 NLon = $('#'+SN.C.S.NoticeLon).val();
198 NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
199 NLID = $('#'+SN.C.S.NoticeLocationId).val();
203 error: function (xhr, textStatus, errorThrown) {
204 form.removeClass(SN.C.S.Processing);
205 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
206 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
207 $('#'+form_id+' .form_response').remove();
208 if (textStatus == 'timeout') {
209 form.append('<p class="form_response 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>');
212 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
213 form.append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
216 if (parseInt(xhr.status) === 0 || jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) >= 0) {
217 $('#'+form_id).resetForm();
218 $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
219 SN.U.FormNoticeEnhancements($('#'+form_id));
222 form.append('<p class="form_response 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>');
227 success: function(data, textStatus) {
228 $('#'+form_id+' .form_response').remove();
230 if ($('#'+SN.C.S.Error, data).length > 0) {
231 result = document._importNode($('p', data)[0], true);
232 result = result.textContent || result.innerHTML;
233 form.append('<p class="form_response error">'+result+'</p>');
236 if($('body')[0].id == 'bookmarklet') {
240 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
241 result = document._importNode($('p', data)[0], true);
242 result = result.textContent || result.innerHTML;
243 form.append('<p class="form_response success">'+result+'</p>');
246 var notices = $('#notices_primary .notices');
247 if (notices.length > 0) {
248 var notice = document._importNode($('li', data)[0], true);
249 if ($('#'+notice.id).length === 0) {
250 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
251 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
252 if($('body')[0].id == 'conversation') {
253 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
254 $(notice_irt).append('<ul class="notices"></ul>');
256 $($(notice_irt+' .notices')[0]).append(notice);
259 notices.prepend(notice);
261 $('#'+notice.id).css({display:'none'});
262 $('#'+notice.id).fadeIn(2500);
263 SN.U.NoticeWithAttachment($('#'+notice.id));
264 SN.U.NoticeReplyTo($('#'+notice.id));
265 SN.U.FormXHR($('#'+notice.id+' .form_favor'));
269 result = document._importNode($('title', data)[0], true);
270 result_title = result.textContent || result.innerHTML;
271 form.append('<p class="form_response success">'+result_title+'</p>');
274 $('#'+form_id).resetForm();
275 $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
276 SN.U.FormNoticeEnhancements($('#'+form_id));
279 complete: function(xhr, textStatus) {
280 form.removeClass(SN.C.S.Processing);
281 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
282 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
284 $('#'+SN.C.S.NoticeDataGeo).attr('checked', NDG);
285 $('#'+SN.C.S.NoticeLat).val(NLat);
286 $('#'+SN.C.S.NoticeLon).val(NLon);
287 $('#'+SN.C.S.NoticeLocationNs).val(NLNS);
288 $('#'+SN.C.S.NoticeLocationId).val(NLID);
293 NoticeReply: function() {
294 if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
295 $('#content .notice').each(function() { SN.U.NoticeReplyTo($(this)); });
299 NoticeReplyTo: function(notice_item) {
300 var notice = notice_item[0];
301 var notice_reply = $('.notice_reply', notice)[0];
303 if (jQuery.data(notice_reply, "ElementData") === undefined) {
304 jQuery.data(notice_reply, "ElementData", {Bind:'submit'});
305 $(notice_reply).bind('click', function() {
306 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
307 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
313 NoticeReplySet: function(nick,id) {
314 if (nick.match(SN.C.I.PatternUsername)) {
315 var text = $('#'+SN.C.S.NoticeDataText);
316 if (text.length > 0) {
317 replyto = '@' + nick + ' ';
318 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
319 $('#'+SN.C.S.FormNotice+' #'+SN.C.S.NoticeInReplyTo).val(id);
322 if (text[0].setSelectionRange) {
323 var len = text.val().length;
324 text[0].setSelectionRange(len,len);
330 NoticeFavor: function() {
331 $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
332 $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
335 NoticeRepeat: function() {
336 $('.form_repeat').each(function() {
337 SN.U.FormXHR($(this));
338 SN.U.NoticeRepeatConfirmation($(this));
342 NoticeRepeatConfirmation: function(form) {
344 form.closest('.notice-options').addClass('opaque');
345 form.addClass('dialogbox');
347 form.append('<button class="close">×</button>');
348 form.find('button.close').click(function(){
351 form.closest('.notice-options').removeClass('opaque');
352 form.removeClass('dialogbox');
353 form.find('.submit_dialogbox').remove();
354 form.find('.submit').show();
360 form.find('.submit').bind('click', function(e) {
363 var submit = form.find('.submit').clone();
364 submit.addClass('submit_dialogbox');
365 submit.removeClass('submit');
374 NoticeAttachments: function() {
375 $('.notice a.attachment').each(function() {
376 SN.U.NoticeWithAttachment($(this).closest('.notice'));
380 NoticeWithAttachment: function(notice) {
381 if ($('.attachment', notice).length === 0) {
385 var notice_id = notice.attr('id');
387 $.fn.jOverlay.options = {
395 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
396 bgClickToClose : true,
397 success : function() {
398 $('#jOverlayContent').append('<button class="close">×</button>');
399 $('#jOverlayContent button').click($.closeOverlay);
403 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
406 $('#'+notice_id+' a.attachment').click(function() {
407 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
412 $("body:not(#shownotice) #"+notice_id+" a.thumbnail").hover(
414 var anchor = $(this);
415 $("a.thumbnail").children('img').hide();
416 anchor.closest(".entry-title").addClass('ov');
418 if (anchor.children('img').length === 0) {
419 t = setTimeout(function() {
420 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
426 anchor.children('img').show();
431 $("a.thumbnail").children('img').hide();
432 $(this).closest(".entry-title").removeClass('ov');
437 NoticeDataAttach: function() {
438 NDA = $('#'+SN.C.S.NoticeDataAttach);
439 NDA.change(function() {
440 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">×</button></div>';
441 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
442 if (NDAS.length > 0) {
446 $('#'+SN.C.S.FormNotice).append(S);
448 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
449 $('#'+SN.C.S.NoticeDataAttachSelected).remove();
457 NoticeLocationAttach: function() {
458 var NLat = $('#'+SN.C.S.NoticeLat).val();
459 var NLon = $('#'+SN.C.S.NoticeLon).val();
460 var NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
461 var NLID = $('#'+SN.C.S.NoticeLocationId).val();
462 var NLN = $('#'+SN.C.S.NoticeGeoName).text();
463 var NDGe = $('#'+SN.C.S.NoticeDataGeo);
465 function removeNoticeDataGeo() {
466 $('label[for='+SN.C.S.NoticeDataGeo+']').removeClass('checked');
467 $('#'+SN.C.S.NoticeDataGeoSelected).hide();
469 $('#'+SN.C.S.NoticeLat).val('');
470 $('#'+SN.C.S.NoticeLon).val('');
471 $('#'+SN.C.S.NoticeLocationNs).val('');
472 $('#'+SN.C.S.NoticeLocationId).val('');
473 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
475 $.cookie(SN.C.S.NoticeDataGeo, 'disabled');
478 function getJSONgeocodeURL(geocodeURL, data) {
479 $.getJSON(geocodeURL, data, function(location) {
482 if (typeof(location.location_ns) != 'undefined') {
483 $('#'+SN.C.S.NoticeLocationNs).val(location.location_ns);
484 lns = location.location_ns;
487 if (typeof(location.location_id) != 'undefined') {
488 $('#'+SN.C.S.NoticeLocationId).val(location.location_id);
489 lid = location.location_id;
492 if (typeof(location.name) == 'undefined') {
493 NLN_text = position.coords.latitude + ';' + position.coords.longitude;
496 NLN_text = location.name;
499 $('#'+SN.C.S.NoticeGeoName)
500 .replaceWith('<a id="notice_data-geo_name"/>');
502 $('#'+SN.C.S.NoticeGeoName)
503 .attr('href', location.url)
506 window.open(location.url);
511 $('#'+SN.C.S.NoticeLat).val(data.lat);
512 $('#'+SN.C.S.NoticeLon).val(data.lon);
513 $('#'+SN.C.S.NoticeLocationNs).val(lns);
514 $('#'+SN.C.S.NoticeLocationId).val(lid);
515 $('#'+SN.C.S.NoticeDataGeo).attr('checked', true);
523 'NLNU': location.url,
526 $.cookie(SN.C.S.NoticeDataGeo, JSON.stringify(cookieValue));
530 if (NDGe.length > 0) {
531 if ($.cookie(SN.C.S.NoticeDataGeo) == 'disabled') {
532 NDGe.attr('checked', false);
535 NDGe.attr('checked', true);
538 var NGW = $('#notice_data-geo_wrap');
539 var geocodeURL = NGW.attr('title');
540 NGW.removeAttr('title');
542 $('label[for='+SN.C.S.NoticeDataGeo+']').attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()));
544 NDGe.change(function() {
545 var NLN = $('#'+SN.C.S.NoticeGeoName);
546 if (NLN.length > 0) {
550 if ($('#'+SN.C.S.NoticeDataGeo).attr('checked') === true || $.cookie(SN.C.S.NoticeDataGeo) === null) {
551 $('label[for='+SN.C.S.NoticeDataGeo+']').addClass('checked');
553 var S = '<div id="'+SN.C.S.NoticeDataGeoSelected+'" class="'+SN.C.S.Success+'"/>';
554 var NDGS = $('#'+SN.C.S.NoticeDataGeoSelected);
556 if (NDGS.length > 0) {
560 $('#'+SN.C.S.FormNotice).append(S);
563 NDGS = $('#'+SN.C.S.NoticeDataGeoSelected);
564 NDGS.prepend('<span id="'+SN.C.S.NoticeGeoName+'">Geo</span> <button class="minimize">_</button> <button class="close">×</button>');
566 var NLN = $('#'+SN.C.S.NoticeGeoName);
567 NLN.addClass('processing');
569 $('#'+SN.C.S.NoticeDataGeoSelected+' button.close').click(function(){
570 removeNoticeDataGeo();
572 $('#'+SN.C.S.NoticeDataGeoSelected).remove();
577 $('#'+SN.C.S.NoticeDataGeoSelected+' button.minimize').click(function(){
578 $('#'+SN.C.S.NoticeDataGeoSelected).hide();
583 if ($.cookie(SN.C.S.NoticeDataGeo) === null || $.cookie(SN.C.S.NoticeDataGeo) == 'disabled') {
584 if (navigator.geolocation) {
585 navigator.geolocation.getCurrentPosition(
587 $('#'+SN.C.S.NoticeLat).val(position.coords.latitude);
588 $('#'+SN.C.S.NoticeLon).val(position.coords.longitude);
591 'lat': position.coords.latitude,
592 'lon': position.coords.longitude,
593 'token': $('#token').val()
596 getJSONgeocodeURL(geocodeURL, data);
600 if (error.PERMISSION_DENIED == 1) {
601 removeNoticeDataGeo();
607 if (NLat.length > 0 && NLon.length > 0) {
611 'token': $('#token').val()
614 getJSONgeocodeURL(geocodeURL, data);
617 removeNoticeDataGeo();
618 $('#'+SN.C.S.NoticeDataGeo).remove();
619 $('label[for='+SN.C.S.NoticeDataGeo+']').remove();
624 var cookieValue = JSON.parse($.cookie(SN.C.S.NoticeDataGeo));
625 $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat);
626 $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon);
627 $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS);
628 $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID);
629 $('#'+SN.C.S.NoticeDataGeo).attr('checked', cookieValue.NDG);
631 $('#'+SN.C.S.NoticeGeoName)
632 .replaceWith('<a id="notice_data-geo_name"/>');
634 $('#'+SN.C.S.NoticeGeoName)
635 .attr('href', cookieValue.NLNU)
636 .text(cookieValue.NLN)
638 window.open($(this).attr('href'));
645 removeNoticeDataGeo();
651 NewDirectMessage: function() {
652 NDM = $('.entity_send-a-message a');
653 NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
654 NDM.bind('click', function() {
655 var NDMF = $('.entity_send-a-message form');
656 if (NDMF.length === 0) {
657 $(this).addClass('processing');
658 $.get(NDM.attr('href'), null, function(data) {
659 $('.entity_send-a-message').append(document._importNode($('form', data)[0], true));
660 NDMF = $('.entity_send-a-message .form_notice');
661 SN.U.FormNoticeXHR(NDMF);
662 SN.U.FormNoticeEnhancements(NDMF);
663 NDMF.append('<button class="close">×</button>');
664 $('.entity_send-a-message button').click(function(){
668 NDM.removeClass('processing');
673 $('.entity_send-a-message textarea').focus();
681 NoticeForm: function() {
682 if ($('body.user_in').length > 0) {
683 SN.U.NoticeLocationAttach();
685 $('.'+SN.C.S.FormNotice).each(function() {
686 SN.U.FormNoticeXHR($(this));
687 SN.U.FormNoticeEnhancements($(this));
690 SN.U.NoticeDataAttach();
694 Notices: function() {
695 if ($('body.user_in').length > 0) {
701 SN.U.NoticeAttachments();
704 EntityActions: function() {
705 if ($('body.user_in').length > 0) {
706 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
707 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
708 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
709 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
710 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
712 SN.U.NewDirectMessage();
718 $(document).ready(function(){
719 if ($('.'+SN.C.S.FormNotice).length > 0) {
720 SN.Init.NoticeForm();
722 if ($('#content .notices').length > 0) {
725 if ($('#content .entity_actions').length > 0) {
726 SN.Init.EntityActions();