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 NoticeDataGeoCookie: 'notice_data-geo_cookie',
57 NoticeDataGeoSelected: 'notice_data-geo_selected',
58 StatusNetInstance:'StatusNetInstance'
63 FormNoticeEnhancements: function(form) {
64 form_id = form.attr('id');
66 if (jQuery.data(form[0], 'ElementData') === undefined) {
67 MaxLength = $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text();
68 if (typeof(MaxLength) == 'undefined') {
69 MaxLength = SN.C.I.MaxLength;
71 jQuery.data(form[0], 'ElementData', {MaxLength:MaxLength});
75 NDT = $('#'+form_id+' #'+SN.C.S.NoticeDataText);
77 NDT.bind('keyup', function(e) {
81 NDT.bind('keydown', function(e) {
82 SN.U.SubmitOnReturn(e, form);
86 $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text(jQuery.data(form[0], 'ElementData').MaxLength);
89 if ($('body')[0].id != 'conversation') {
90 $('#'+form_id+' textarea').focus();
94 SubmitOnReturn: function(event, el) {
95 if (event.keyCode == 13 || event.keyCode == 10) {
97 event.preventDefault();
98 event.stopPropagation();
99 $('#'+el[0].id+' #'+SN.C.S.NoticeDataText).blur();
106 Counter: function(form) {
107 SN.C.I.FormNoticeCurrent = form;
108 form_id = form.attr('id');
110 var MaxLength = jQuery.data(form[0], 'ElementData').MaxLength;
112 if (MaxLength <= 0) {
116 var remaining = MaxLength - $('#'+form_id+' #'+SN.C.S.NoticeDataText).val().length;
117 var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount);
119 if (remaining.toString() != counter.text()) {
120 if (!SN.C.I.CounterBlackout || remaining === 0) {
121 if (counter.text() != String(remaining)) {
122 counter.text(remaining);
125 form.addClass(SN.C.S.Warning);
127 form.removeClass(SN.C.S.Warning);
129 // Skip updates for the next 500ms.
130 // On slower hardware, updating on every keypress is unpleasant.
131 if (!SN.C.I.CounterBlackout) {
132 SN.C.I.CounterBlackout = true;
133 SN.C.I.FormNoticeCurrent = form;
134 window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
140 ClearCounterBlackout: function(form) {
141 // Allow keyup events to poke the counter again
142 SN.C.I.CounterBlackout = false;
143 // Check if the string changed since we last looked
147 FormXHR: function(form) {
151 url: form.attr('action'),
152 data: form.serialize() + '&ajax=1',
153 beforeSend: function(xhr) {
155 .addClass(SN.C.S.Processing)
157 .addClass(SN.C.S.Disabled)
158 .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.replaceWith(form_new);
169 form.replaceWith(document._importNode($('p', data)[0], true));
175 FormNoticeXHR: function(form) {
176 SN.C.I.NoticeDataGeo = {};
177 form_id = form.attr('id');
178 form.append('<input type="hidden" name="ajax" value="1"/>');
182 beforeSend: function(formData) {
183 if (form.find('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
184 form.addClass(SN.C.S.Warning);
188 .addClass(SN.C.S.Processing)
189 .find('#'+SN.C.S.NoticeActionSubmit)
190 .addClass(SN.C.S.Disabled)
191 .attr(SN.C.S.Disabled, SN.C.S.Disabled);
193 SN.C.I.NoticeDataGeo.NLat = $('#'+SN.C.S.NoticeLat).val();
194 SN.C.I.NoticeDataGeo.NLon = $('#'+SN.C.S.NoticeLon).val();
195 SN.C.I.NoticeDataGeo.NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
196 SN.C.I.NoticeDataGeo.NLID = $('#'+SN.C.S.NoticeLocationId).val();
197 SN.C.I.NoticeDataGeo.NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked');
199 cookieValue = $.cookie(SN.C.S.NoticeDataGeoCookie);
201 if (cookieValue !== null && cookieValue != 'disabled') {
202 cookieValue = JSON.parse(cookieValue);
203 SN.C.I.NoticeDataGeo.NLat = $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat).val();
204 SN.C.I.NoticeDataGeo.NLon = $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon).val();
205 if ($('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS)) {
206 SN.C.I.NoticeDataGeo.NLNS = $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS).val();
207 SN.C.I.NoticeDataGeo.NLID = $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID).val();
210 if (cookieValue == 'disabled') {
211 SN.C.I.NoticeDataGeo.NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked', false).attr('checked');
214 SN.C.I.NoticeDataGeo.NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked', true).attr('checked');
219 error: function (xhr, textStatus, errorThrown) {
221 .removeClass(SN.C.S.Processing)
222 .find('#'+SN.C.S.NoticeActionSubmit)
223 .removeClass(SN.C.S.Disabled)
224 .removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
225 form.find('.form_response').remove();
226 if (textStatus == 'timeout') {
227 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>');
230 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
231 form.append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
234 if (parseInt(xhr.status) === 0 || jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) >= 0) {
237 .find('#'+SN.C.S.NoticeDataAttachSelected).remove();
238 SN.U.FormNoticeEnhancements(form);
241 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>');
246 success: function(data, textStatus) {
247 form.find('.form_response').remove();
249 if ($('#'+SN.C.S.Error, data).length > 0) {
250 result = document._importNode($('p', data)[0], true);
251 result = result.textContent || result.innerHTML;
252 form.append('<p class="form_response error">'+result+'</p>');
255 if($('body')[0].id == 'bookmarklet') {
259 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
260 result = document._importNode($('p', data)[0], true);
261 result = result.textContent || result.innerHTML;
262 form.append('<p class="form_response success">'+result+'</p>');
265 var notices = $('#notices_primary .notices');
266 if (notices.length > 0) {
267 var notice = document._importNode($('li', data)[0], true);
268 if ($('#'+notice.id).length === 0) {
269 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
270 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
271 if($('body')[0].id == 'conversation') {
272 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
273 $(notice_irt).append('<ul class="notices"></ul>');
275 $($(notice_irt+' .notices')[0]).append(notice);
278 notices.prepend(notice);
281 .css({display:'none'})
283 SN.U.NoticeWithAttachment($('#'+notice.id));
284 SN.U.NoticeReplyTo($('#'+notice.id));
288 result = document._importNode($('title', data)[0], true);
289 result_title = result.textContent || result.innerHTML;
290 form.append('<p class="form_response success">'+result_title+'</p>');
294 form.find('#'+SN.C.S.NoticeInReplyTo).val('');
295 form.find('#'+SN.C.S.NoticeDataAttachSelected).remove();
296 SN.U.FormNoticeEnhancements(form);
299 complete: function(xhr, textStatus) {
301 .removeClass(SN.C.S.Processing)
302 .find('#'+SN.C.S.NoticeActionSubmit)
303 .removeAttr(SN.C.S.Disabled)
304 .removeClass(SN.C.S.Disabled);
306 $('#'+SN.C.S.NoticeLat).val(SN.C.I.NoticeDataGeo.NLat);
307 $('#'+SN.C.S.NoticeLon).val(SN.C.I.NoticeDataGeo.NLon);
308 if ($('#'+SN.C.S.NoticeLocationNs)) {
309 $('#'+SN.C.S.NoticeLocationNs).val(SN.C.I.NoticeDataGeo.NLNS);
310 $('#'+SN.C.S.NoticeLocationId).val(SN.C.I.NoticeDataGeo.NLID);
312 $('#'+SN.C.S.NoticeDataGeo).attr('checked', SN.C.I.NoticeDataGeo.NDG);
317 NoticeReply: function() {
318 if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
319 $('#content .notice').each(function() { SN.U.NoticeReplyTo($(this)); });
323 NoticeReplyTo: function(notice_item) {
324 var notice = notice_item[0];
325 var notice_reply = $('.notice_reply', notice)[0];
327 if (jQuery.data(notice_reply, "ElementData") === undefined) {
328 jQuery.data(notice_reply, "ElementData", {Bind:'submit'});
329 $(notice_reply).bind('click', function() {
330 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
331 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
337 NoticeReplySet: function(nick,id) {
338 if (nick.match(SN.C.I.PatternUsername)) {
339 var text = $('#'+SN.C.S.NoticeDataText);
340 if (text.length > 0) {
341 replyto = '@' + nick + ' ';
342 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
343 $('#'+SN.C.S.FormNotice+' #'+SN.C.S.NoticeInReplyTo).val(id);
346 if (text[0].setSelectionRange) {
347 var len = text.val().length;
348 text[0].setSelectionRange(len,len);
354 NoticeFavor: function() {
355 $('.form_favor').live('click', function() { SN.U.FormXHR($(this)); return false; });
356 $('.form_disfavor').live('click', function() { SN.U.FormXHR($(this)); return false; });
359 NoticeRepeat: function() {
360 $('.form_repeat').live('click', function(e) {
363 SN.U.NoticeRepeatConfirmation($(this));
368 NoticeRepeatConfirmation: function(form) {
369 var submit_i = form.find('.submit');
371 var submit = submit_i.clone();
373 .addClass('submit_dialogbox')
374 .removeClass('submit');
376 submit.bind('click', function() { SN.U.FormXHR(form); return false; });
381 .addClass('dialogbox')
382 .append('<button class="close">×</button>')
383 .closest('.notice-options')
386 form.find('button.close').click(function(){
390 .removeClass('dialogbox')
391 .closest('.notice-options')
392 .removeClass('opaque');
394 form.find('.submit_dialogbox').remove();
395 form.find('.submit').show();
401 NoticeAttachments: function() {
402 $('.notice a.attachment').each(function() {
403 SN.U.NoticeWithAttachment($(this).closest('.notice'));
407 NoticeWithAttachment: function(notice) {
408 if (notice.find('.attachment').length === 0) {
412 $.fn.jOverlay.options = {
420 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
421 bgClickToClose : true,
422 success : function() {
423 $('#jOverlayContent').append('<button class="close">×</button>');
424 $('#jOverlayContent button').click($.closeOverlay);
428 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
431 notice.find('a.attachment').click(function() {
432 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
436 if ($('#shownotice').length == 0) {
438 notice.find('a.thumbnail').hover(
440 var anchor = $(this);
441 $('a.thumbnail').children('img').hide();
442 anchor.closest(".entry-title").addClass('ov');
444 if (anchor.children('img').length === 0) {
445 t = setTimeout(function() {
446 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
452 anchor.children('img').show();
457 $('a.thumbnail').children('img').hide();
458 $(this).closest('.entry-title').removeClass('ov');
464 NoticeDataAttach: function() {
465 NDA = $('#'+SN.C.S.NoticeDataAttach);
466 NDA.change(function() {
467 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">×</button></div>';
468 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
469 if (NDAS.length > 0) {
473 $('#'+SN.C.S.FormNotice).append(S);
475 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
476 $('#'+SN.C.S.NoticeDataAttachSelected).remove();
484 NoticeLocationAttach: function() {
485 var NLat = $('#'+SN.C.S.NoticeLat).val();
486 var NLon = $('#'+SN.C.S.NoticeLon).val();
487 var NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
488 var NLID = $('#'+SN.C.S.NoticeLocationId).val();
489 var NLN = $('#'+SN.C.S.NoticeGeoName).text();
490 var NDGe = $('#'+SN.C.S.NoticeDataGeo);
492 function removeNoticeDataGeo() {
493 $('label[for='+SN.C.S.NoticeDataGeo+']')
494 .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()))
495 .removeClass('checked');
497 $('#'+SN.C.S.NoticeLat).val('');
498 $('#'+SN.C.S.NoticeLon).val('');
499 $('#'+SN.C.S.NoticeLocationNs).val('');
500 $('#'+SN.C.S.NoticeLocationId).val('');
501 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
503 $.cookie(SN.C.S.NoticeDataGeoCookie, 'disabled', { path: '/', expires: SN.U.GetFullYear(2029, 0, 1) });
506 function getJSONgeocodeURL(geocodeURL, data) {
507 $.getJSON(geocodeURL, data, function(location) {
510 if (typeof(location.location_ns) != 'undefined') {
511 $('#'+SN.C.S.NoticeLocationNs).val(location.location_ns);
512 lns = location.location_ns;
515 if (typeof(location.location_id) != 'undefined') {
516 $('#'+SN.C.S.NoticeLocationId).val(location.location_id);
517 lid = location.location_id;
520 if (typeof(location.name) == 'undefined') {
521 NLN_text = data.lat + ';' + data.lon;
524 NLN_text = location.name;
527 $('label[for='+SN.C.S.NoticeDataGeo+']')
528 .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + NLN_text + ')');
530 $('#'+SN.C.S.NoticeLat).val(data.lat);
531 $('#'+SN.C.S.NoticeLon).val(data.lon);
532 $('#'+SN.C.S.NoticeLocationNs).val(lns);
533 $('#'+SN.C.S.NoticeLocationId).val(lid);
534 $('#'+SN.C.S.NoticeDataGeo).attr('checked', true);
546 $.cookie(SN.C.S.NoticeDataGeoCookie, JSON.stringify(cookieValue), { path: '/', expires: SN.U.GetFullYear(2029, 0, 1) });
550 if (NDGe.length > 0) {
551 if ($.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
552 NDGe.attr('checked', false);
555 NDGe.attr('checked', true);
558 var NGW = $('#notice_data-geo_wrap');
559 var geocodeURL = NGW.attr('title');
560 NGW.removeAttr('title');
562 $('label[for='+SN.C.S.NoticeDataGeo+']')
563 .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()));
565 NDGe.change(function() {
566 if ($('#'+SN.C.S.NoticeDataGeo).attr('checked') === true || $.cookie(SN.C.S.NoticeDataGeoCookie) === null) {
567 $('label[for='+SN.C.S.NoticeDataGeo+']')
568 .attr('title', NoticeDataGeo_text.ShareDisable)
569 .addClass('checked');
571 if ($.cookie(SN.C.S.NoticeDataGeoCookie) === null || $.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
572 if (navigator.geolocation) {
573 navigator.geolocation.getCurrentPosition(
575 $('#'+SN.C.S.NoticeLat).val(position.coords.latitude);
576 $('#'+SN.C.S.NoticeLon).val(position.coords.longitude);
579 lat: position.coords.latitude,
580 lon: position.coords.longitude,
581 token: $('#token').val()
584 getJSONgeocodeURL(geocodeURL, data);
589 case error.PERMISSION_DENIED:
590 removeNoticeDataGeo();
593 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
604 if (NLat.length > 0 && NLon.length > 0) {
608 token: $('#token').val()
611 getJSONgeocodeURL(geocodeURL, data);
614 removeNoticeDataGeo();
615 $('#'+SN.C.S.NoticeDataGeo).remove();
616 $('label[for='+SN.C.S.NoticeDataGeo+']').remove();
621 var cookieValue = JSON.parse($.cookie(SN.C.S.NoticeDataGeoCookie));
623 $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat);
624 $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon);
625 $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS);
626 $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID);
627 $('#'+SN.C.S.NoticeDataGeo).attr('checked', cookieValue.NDG);
629 $('label[for='+SN.C.S.NoticeDataGeo+']')
630 .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + cookieValue.NLN + ')')
631 .addClass('checked');
635 removeNoticeDataGeo();
641 NewDirectMessage: function() {
642 NDM = $('.entity_send-a-message a');
643 NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
644 NDM.bind('click', function() {
645 var NDMF = $('.entity_send-a-message form');
646 if (NDMF.length === 0) {
647 $(this).addClass(SN.C.S.Processing);
648 $.get(NDM.attr('href'), null, function(data) {
649 $('.entity_send-a-message').append(document._importNode($('form', data)[0], true));
650 NDMF = $('.entity_send-a-message .form_notice');
651 SN.U.FormNoticeXHR(NDMF);
652 SN.U.FormNoticeEnhancements(NDMF);
653 NDMF.append('<button class="close">×</button>');
654 $('.entity_send-a-message button').click(function(){
658 NDM.removeClass(SN.C.S.Processing);
663 $('.entity_send-a-message textarea').focus();
669 GetFullYear: function(year, month, day) {
670 var date = new Date();
671 date.setFullYear(year, month, day);
677 Set: function(value) {
678 var SNI = SN.U.StatusNetInstance.Get();
680 value = $.extend(SNI, value);
684 SN.C.S.StatusNetInstance,
685 JSON.stringify(value),
688 expires: SN.U.GetFullYear(2029, 0, 1)
693 var cookieValue = $.cookie(SN.C.S.StatusNetInstance);
694 if (cookieValue !== null) {
695 return JSON.parse(cookieValue);
701 $.cookie(SN.C.S.StatusNetInstance, null);
707 NoticeForm: function() {
708 if ($('body.user_in').length > 0) {
709 SN.U.NoticeLocationAttach();
711 $('.'+SN.C.S.FormNotice).each(function() {
712 SN.U.FormNoticeXHR($(this));
713 SN.U.FormNoticeEnhancements($(this));
716 SN.U.NoticeDataAttach();
720 Notices: function() {
721 if ($('body.user_in').length > 0) {
727 SN.U.NoticeAttachments();
730 EntityActions: function() {
731 if ($('body.user_in').length > 0) {
732 $('.form_user_subscribe').live('click', function() { SN.U.FormXHR($(this)); return false; });
733 $('.form_user_unsubscribe').live('click', function() { SN.U.FormXHR($(this)); return false; });
734 $('.form_group_join').live('click', function() { SN.U.FormXHR($(this)); return false; });
735 $('.form_group_leave').live('click', function() { SN.U.FormXHR($(this)); return false; });
736 $('.form_user_nudge').live('click', function() { SN.U.FormXHR($(this)); return false; });
738 SN.U.NewDirectMessage();
744 $(document).ready(function(){
745 if ($('.'+SN.C.S.FormNotice).length > 0) {
746 SN.Init.NoticeForm();
748 if ($('#content .notices').length > 0) {
751 if ($('#content .entity_actions').length > 0) {
752 SN.Init.EntityActions();