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'
62 FormNoticeEnhancements: function(form) {
63 form_id = form.attr('id');
65 if (jQuery.data(form[0], 'ElementData') === undefined) {
66 MaxLength = $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text();
67 if (typeof(MaxLength) == 'undefined') {
68 MaxLength = SN.C.I.MaxLength;
70 jQuery.data(form[0], 'ElementData', {MaxLength:MaxLength});
74 NDT = $('#'+form_id+' #'+SN.C.S.NoticeDataText);
76 NDT.bind('keyup', function(e) {
80 NDT.bind('keydown', function(e) {
81 SN.U.SubmitOnReturn(e, form);
85 $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text(jQuery.data(form[0], 'ElementData').MaxLength);
88 if ($('body')[0].id != 'conversation') {
89 $('#'+form_id+' textarea').focus();
93 SubmitOnReturn: function(event, el) {
94 if (event.keyCode == 13 || event.keyCode == 10) {
96 event.preventDefault();
97 event.stopPropagation();
98 $('#'+el[0].id+' #'+SN.C.S.NoticeDataText).blur();
105 Counter: function(form) {
106 SN.C.I.FormNoticeCurrent = form;
107 form_id = form.attr('id');
109 var MaxLength = jQuery.data(form[0], 'ElementData').MaxLength;
111 if (MaxLength <= 0) {
115 var remaining = MaxLength - $('#'+form_id+' #'+SN.C.S.NoticeDataText).val().length;
116 var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount);
118 if (remaining.toString() != counter.text()) {
119 if (!SN.C.I.CounterBlackout || remaining === 0) {
120 if (counter.text() != String(remaining)) {
121 counter.text(remaining);
124 form.addClass(SN.C.S.Warning);
126 form.removeClass(SN.C.S.Warning);
128 // Skip updates for the next 500ms.
129 // On slower hardware, updating on every keypress is unpleasant.
130 if (!SN.C.I.CounterBlackout) {
131 SN.C.I.CounterBlackout = true;
132 SN.C.I.FormNoticeCurrent = form;
133 window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
139 ClearCounterBlackout: function(form) {
140 // Allow keyup events to poke the counter again
141 SN.C.I.CounterBlackout = false;
142 // Check if the string changed since we last looked
146 FormXHR: function(f) {
147 if (jQuery.data(f[0], "ElementData") === undefined) {
148 jQuery.data(f[0], "ElementData", {Bind:'submit'});
149 f.bind('submit', function(e) {
150 form_id = $(this)[0].id;
154 url: $(this)[0].action,
155 data: $(this).serialize() + '&ajax=1',
156 beforeSend: function(xhr) {
157 $('#'+form_id).addClass(SN.C.S.Processing);
158 $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
159 $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
161 error: function (xhr, textStatus, errorThrown) {
162 alert(errorThrown || textStatus);
164 success: function(data, textStatus) {
165 if (typeof($('form', data)[0]) != 'undefined') {
166 form_new = document._importNode($('form', data)[0], true);
167 $('#'+form_id).replaceWith(form_new);
168 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
171 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
180 FormNoticeXHR: function(form) {
181 var NDG, NLat, NLon, NLNS, NLID;
182 form_id = form.attr('id');
183 form.append('<input type="hidden" name="ajax" value="1"/>');
187 beforeSend: function(formData) {
188 if ($('#'+form_id+' #'+SN.C.S.NoticeDataText)[0].value.length === 0) {
189 form.addClass(SN.C.S.Warning);
192 form.addClass(SN.C.S.Processing);
193 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
194 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
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();
200 NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked');
202 cookieValue = $.cookie(SN.C.S.NoticeDataGeoCookie);
204 if (cookieValue !== null && cookieValue != 'disabled') {
205 cookieValue = JSON.parse(cookieValue);
206 NLat = $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat).val();
207 NLon = $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon).val();
208 NLNS = $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS).val();
209 NLID = $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID).val();
211 if (cookieValue == 'disabled') {
212 NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked', false).attr('checked');
215 NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked', true).attr('checked');
220 error: function (xhr, textStatus, errorThrown) {
221 form.removeClass(SN.C.S.Processing);
222 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
223 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
224 $('#'+form_id+' .form_response').remove();
225 if (textStatus == 'timeout') {
226 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>');
229 if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
230 form.append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
233 if (parseInt(xhr.status) === 0 || jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) >= 0) {
234 $('#'+form_id).resetForm();
235 $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
236 SN.U.FormNoticeEnhancements($('#'+form_id));
239 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>');
244 success: function(data, textStatus) {
245 $('#'+form_id+' .form_response').remove();
247 if ($('#'+SN.C.S.Error, data).length > 0) {
248 result = document._importNode($('p', data)[0], true);
249 result = result.textContent || result.innerHTML;
250 form.append('<p class="form_response error">'+result+'</p>');
253 if($('body')[0].id == 'bookmarklet') {
257 if ($('#'+SN.C.S.CommandResult, data).length > 0) {
258 result = document._importNode($('p', data)[0], true);
259 result = result.textContent || result.innerHTML;
260 form.append('<p class="form_response success">'+result+'</p>');
263 var notices = $('#notices_primary .notices');
264 if (notices.length > 0) {
265 var notice = document._importNode($('li', data)[0], true);
266 if ($('#'+notice.id).length === 0) {
267 var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
268 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
269 if($('body')[0].id == 'conversation') {
270 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
271 $(notice_irt).append('<ul class="notices"></ul>');
273 $($(notice_irt+' .notices')[0]).append(notice);
276 notices.prepend(notice);
278 $('#'+notice.id).css({display:'none'});
279 $('#'+notice.id).fadeIn(2500);
280 SN.U.NoticeWithAttachment($('#'+notice.id));
281 SN.U.NoticeReplyTo($('#'+notice.id));
282 SN.U.FormXHR($('#'+notice.id+' .form_favor'));
286 result = document._importNode($('title', data)[0], true);
287 result_title = result.textContent || result.innerHTML;
288 form.append('<p class="form_response success">'+result_title+'</p>');
291 $('#'+form_id).resetForm();
292 $('#'+form_id+' #'+SN.C.S.NoticeInReplyTo).val('');
293 $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
294 SN.U.FormNoticeEnhancements($('#'+form_id));
297 complete: function(xhr, textStatus) {
298 form.removeClass(SN.C.S.Processing);
299 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
300 $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
302 $('#'+SN.C.S.NoticeLat).val(NLat);
303 $('#'+SN.C.S.NoticeLon).val(NLon);
304 $('#'+SN.C.S.NoticeLocationNs).val(NLNS);
305 $('#'+SN.C.S.NoticeLocationId).val(NLID);
306 $('#'+SN.C.S.NoticeDataGeo).attr('checked', NDG);
311 NoticeReply: function() {
312 if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
313 $('#content .notice').each(function() { SN.U.NoticeReplyTo($(this)); });
317 NoticeReplyTo: function(notice_item) {
318 var notice = notice_item[0];
319 var notice_reply = $('.notice_reply', notice)[0];
321 if (jQuery.data(notice_reply, "ElementData") === undefined) {
322 jQuery.data(notice_reply, "ElementData", {Bind:'submit'});
323 $(notice_reply).bind('click', function() {
324 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
325 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
331 NoticeReplySet: function(nick,id) {
332 if (nick.match(SN.C.I.PatternUsername)) {
333 var text = $('#'+SN.C.S.NoticeDataText);
334 if (text.length > 0) {
335 replyto = '@' + nick + ' ';
336 text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
337 $('#'+SN.C.S.FormNotice+' #'+SN.C.S.NoticeInReplyTo).val(id);
340 if (text[0].setSelectionRange) {
341 var len = text.val().length;
342 text[0].setSelectionRange(len,len);
348 NoticeFavor: function() {
349 $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
350 $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
353 NoticeRepeat: function() {
354 $('.form_repeat').each(function() {
355 SN.U.FormXHR($(this));
356 SN.U.NoticeRepeatConfirmation($(this));
360 NoticeRepeatConfirmation: function(form) {
362 form.closest('.notice-options').addClass('opaque');
363 form.addClass('dialogbox');
365 form.append('<button class="close">×</button>');
366 form.find('button.close').click(function(){
369 form.closest('.notice-options').removeClass('opaque');
370 form.removeClass('dialogbox');
371 form.find('.submit_dialogbox').remove();
372 form.find('.submit').show();
378 form.find('.submit').bind('click', function(e) {
381 var submit = form.find('.submit').clone();
382 submit.addClass('submit_dialogbox');
383 submit.removeClass('submit');
392 NoticeAttachments: function() {
393 $('.notice a.attachment').each(function() {
394 SN.U.NoticeWithAttachment($(this).closest('.notice'));
398 NoticeWithAttachment: function(notice) {
399 if ($('.attachment', notice).length === 0) {
403 var notice_id = notice.attr('id');
405 $.fn.jOverlay.options = {
413 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
414 bgClickToClose : true,
415 success : function() {
416 $('#jOverlayContent').append('<button class="close">×</button>');
417 $('#jOverlayContent button').click($.closeOverlay);
421 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
424 $('#'+notice_id+' a.attachment').click(function() {
425 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
430 $("body:not(#shownotice) #"+notice_id+" a.thumbnail").hover(
432 var anchor = $(this);
433 $("a.thumbnail").children('img').hide();
434 anchor.closest(".entry-title").addClass('ov');
436 if (anchor.children('img').length === 0) {
437 t = setTimeout(function() {
438 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
444 anchor.children('img').show();
449 $("a.thumbnail").children('img').hide();
450 $(this).closest(".entry-title").removeClass('ov');
455 NoticeDataAttach: function() {
456 NDA = $('#'+SN.C.S.NoticeDataAttach);
457 NDA.change(function() {
458 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">×</button></div>';
459 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
460 if (NDAS.length > 0) {
464 $('#'+SN.C.S.FormNotice).append(S);
466 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
467 $('#'+SN.C.S.NoticeDataAttachSelected).remove();
475 NoticeLocationAttach: function() {
476 var NLat = $('#'+SN.C.S.NoticeLat).val();
477 var NLon = $('#'+SN.C.S.NoticeLon).val();
478 var NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
479 var NLID = $('#'+SN.C.S.NoticeLocationId).val();
480 var NLN = $('#'+SN.C.S.NoticeGeoName).text();
481 var NDGe = $('#'+SN.C.S.NoticeDataGeo);
483 function removeNoticeDataGeo() {
484 $('label[for='+SN.C.S.NoticeDataGeo+']')
485 .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()))
486 .removeClass('checked');
488 $('#'+SN.C.S.NoticeLat).val('');
489 $('#'+SN.C.S.NoticeLon).val('');
490 $('#'+SN.C.S.NoticeLocationNs).val('');
491 $('#'+SN.C.S.NoticeLocationId).val('');
492 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
494 $.cookie(SN.C.S.NoticeDataGeoCookie, 'disabled');
497 function getJSONgeocodeURL(geocodeURL, data) {
498 $.getJSON(geocodeURL, data, function(location) {
501 if (typeof(location.location_ns) != 'undefined') {
502 $('#'+SN.C.S.NoticeLocationNs).val(location.location_ns);
503 lns = location.location_ns;
506 if (typeof(location.location_id) != 'undefined') {
507 $('#'+SN.C.S.NoticeLocationId).val(location.location_id);
508 lid = location.location_id;
511 if (typeof(location.name) == 'undefined') {
512 NLN_text = position.coords.latitude + ';' + position.coords.longitude;
515 NLN_text = location.name;
518 $('label[for='+SN.C.S.NoticeDataGeo+']')
519 .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + NLN_text + ')');
521 $('#'+SN.C.S.NoticeLat).val(data.lat);
522 $('#'+SN.C.S.NoticeLon).val(data.lon);
523 $('#'+SN.C.S.NoticeLocationNs).val(lns);
524 $('#'+SN.C.S.NoticeLocationId).val(lid);
525 $('#'+SN.C.S.NoticeDataGeo).attr('checked', true);
533 'NLNU': location.url,
536 $.cookie(SN.C.S.NoticeDataGeoCookie, JSON.stringify(cookieValue));
540 if (NDGe.length > 0) {
541 if ($.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
542 NDGe.attr('checked', false);
545 NDGe.attr('checked', true);
548 var NGW = $('#notice_data-geo_wrap');
549 var geocodeURL = NGW.attr('title');
550 NGW.removeAttr('title');
552 $('label[for='+SN.C.S.NoticeDataGeo+']')
553 .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()));
555 NDGe.change(function() {
556 if ($('#'+SN.C.S.NoticeDataGeo).attr('checked') === true || $.cookie(SN.C.S.NoticeDataGeoCookie) === null) {
557 $('label[for='+SN.C.S.NoticeDataGeo+']')
558 .attr('title', NoticeDataGeo_text.ShareDisable)
559 .addClass('checked');
561 if ($.cookie(SN.C.S.NoticeDataGeoCookie) === null || $.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
562 if (navigator.geolocation) {
563 navigator.geolocation.getCurrentPosition(
565 $('#'+SN.C.S.NoticeLat).val(position.coords.latitude);
566 $('#'+SN.C.S.NoticeLon).val(position.coords.longitude);
569 'lat': position.coords.latitude,
570 'lon': position.coords.longitude,
571 'token': $('#token').val()
574 getJSONgeocodeURL(geocodeURL, data);
579 case error.PERMISSION_DENIED:
580 removeNoticeDataGeo();
583 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
594 if (NLat.length > 0 && NLon.length > 0) {
598 'token': $('#token').val()
601 getJSONgeocodeURL(geocodeURL, data);
604 removeNoticeDataGeo();
605 $('#'+SN.C.S.NoticeDataGeo).remove();
606 $('label[for='+SN.C.S.NoticeDataGeo+']').remove();
611 var cookieValue = JSON.parse($.cookie(SN.C.S.NoticeDataGeoCookie));
613 $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat);
614 $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon);
615 $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS);
616 $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID);
617 $('#'+SN.C.S.NoticeDataGeo).attr('checked', cookieValue.NDG);
619 $('label[for='+SN.C.S.NoticeDataGeo+']')
620 .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + cookieValue.NLN + ')')
621 .addClass('checked');
625 removeNoticeDataGeo();
628 $('#'+SN.C.S.NoticeDataText).focus();
633 NewDirectMessage: function() {
634 NDM = $('.entity_send-a-message a');
635 NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
636 NDM.bind('click', function() {
637 var NDMF = $('.entity_send-a-message form');
638 if (NDMF.length === 0) {
639 $(this).addClass('processing');
640 $.get(NDM.attr('href'), null, function(data) {
641 $('.entity_send-a-message').append(document._importNode($('form', data)[0], true));
642 NDMF = $('.entity_send-a-message .form_notice');
643 SN.U.FormNoticeXHR(NDMF);
644 SN.U.FormNoticeEnhancements(NDMF);
645 NDMF.append('<button class="close">×</button>');
646 $('.entity_send-a-message button').click(function(){
650 NDM.removeClass('processing');
655 $('.entity_send-a-message textarea').focus();
663 NoticeForm: function() {
664 if ($('body.user_in').length > 0) {
665 SN.U.NoticeLocationAttach();
667 $('.'+SN.C.S.FormNotice).each(function() {
668 SN.U.FormNoticeXHR($(this));
669 SN.U.FormNoticeEnhancements($(this));
672 SN.U.NoticeDataAttach();
676 Notices: function() {
677 if ($('body.user_in').length > 0) {
683 SN.U.NoticeAttachments();
686 EntityActions: function() {
687 if ($('body.user_in').length > 0) {
688 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
689 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
690 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
691 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
692 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
694 SN.U.NewDirectMessage();
700 $(document).ready(function(){
701 if ($('.'+SN.C.S.FormNotice).length > 0) {
702 SN.Init.NoticeForm();
704 if ($('#content .notices').length > 0) {
707 if ($('#content .entity_actions').length > 0) {
708 SN.Init.EntityActions();