1 function resizeIframe(obj) {
2 //obj.style.height = 0;
6 function _resizeIframe(obj, desth) {
7 var h = obj.style.height;
8 var ch = obj.contentWindow.document.body.scrollHeight;
9 if (h == (ch + 'px')) {
12 if (desth == ch && ch>0) {
13 obj.style.height = ch + 'px';
15 setTimeout(_resizeIframe, 100, obj, ch);
18 function openClose(theID) {
19 if(document.getElementById(theID).style.display == "block") {
20 document.getElementById(theID).style.display = "none"
23 document.getElementById(theID).style.display = "block"
27 function openMenu(theID) {
28 var el = document.getElementById(theID)
30 el.style.display = "block";
34 function closeMenu(theID) {
35 var el = document.getElementById(theID)
37 el.style.display = "none";
41 function decodeHtml(html) {
42 var txt = document.createElement("textarea");
51 var force_update = false;
53 var totStopped = false;
57 var in_progress = false;
58 var langSelect = false;
59 var commentBusy = false;
60 var last_popup_menu = null;
61 var last_popup_button = null;
62 var lockLoadContent = false;
65 $.ajaxSetup({cache: false});
67 /* setup comment textarea buttons */
68 /* comment textarea buttons needs some "data-*" attributes to work:
69 * data-role="insert-formatting" : to mark the element as a formatting button
70 * data-bbcode="<string>" : name of the bbcode element to insert. insertFormatting() will insert it as "[name][/name]"
71 * data-id="<string>" : id of the comment, used to find other comment-related element, like the textarea
73 $('body').on('click','[data-role="insert-formatting"]', function(e) {
76 var bbcode = o.data('bbcode');
77 var id = o.data('id');
79 Dialog.doImageBrowser("comment", id);
82 insertFormatting(bbcode, id);
85 /* event from comment textarea button popups */
86 /* insert returned bbcode at cursor position or replace selected text */
87 $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
89 var textarea = document.getElementById("comment-edit-text-" +id);
90 var start = textarea.selectionStart;
91 var end = textarea.selectionEnd;
92 textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
93 $(textarea).trigger('change');
98 /* setup onoff widgets */
99 $(".onoff input").each(function(){
101 id = $(this).attr("id");
102 $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
105 $(".onoff > a").click(function(event){
106 event.preventDefault();
107 var input = $(this).siblings("input");
108 var val = 1-input.val();
109 var id = input.attr("id");
110 $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
111 $("#"+id+"_onoff ."+ (val==1?"on":"off")).removeClass("hidden");
116 function close_last_popup_menu() {
117 if(last_popup_menu) {
118 last_popup_menu.hide();
119 last_popup_menu.off('click', function(e) {e.stopPropagation()});
120 last_popup_button.removeClass("selected");
121 last_popup_menu = null;
122 last_popup_button = null;
125 $('a[rel^="#"]').click(function(e){
127 var parent = $(this).parent();
128 var isSelected = (last_popup_button && parent.attr('id') == last_popup_button.attr('id'));
129 close_last_popup_menu();
130 if(isSelected) return false;
131 menu = $( $(this).attr('rel') );
134 if (menu.attr('popup')=="false") return false;
135 parent.toggleClass("selected");
137 if (menu.css("display") == "none") {
138 last_popup_menu = null;
139 last_popup_button = null;
141 last_popup_menu = menu;
142 last_popup_menu.on('click', function(e) {e.stopPropagation()});
143 last_popup_button = parent;
144 $('#nav-notifications-menu').perfectScrollbar('update');
148 $('html').click(function() {
149 close_last_popup_menu();
153 $("a.popupbox").colorbox({
155 'transition' : 'elastic',
158 $("a.ajax-popupbox").colorbox({
159 'transition' : 'elastic',
163 /* notifications template */
164 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
165 var notifications_all = unescape($('<div>').append( $("#nav-notifications-see-all").clone() ).html()); //outerHtml hack
166 var notifications_mark = unescape($('<div>').append( $("#nav-notifications-mark-all").clone() ).html()); //outerHtml hack
167 var notifications_empty = unescape($("#nav-notifications-menu").html());
169 /* enable perfect-scrollbars for different elements */
170 $('#nav-notifications-menu, aside').perfectScrollbar();
172 /* nav update event */
173 $('nav').bind('nav-update', function(e, data){
174 var invalid = data.invalid || 0;
175 if(invalid == 1) { window.location.href=window.location.href }
177 ['net', 'home', 'intro', 'mail', 'events', 'birthdays', 'notify'].forEach(function(type) {
178 var number = data[type];
181 $('#' + type + '-update').removeClass('show');
183 $('#' + type + '-update').addClass('show');
185 $('#' + type + '-update').text(number);
188 var intro = data['intro'];
189 if(intro == 0) { intro = ''; $('#intro-update-li').removeClass('show') } else { $('#intro-update-li').addClass('show') }
190 $('#intro-update-li').html(intro);
192 var mail = data['mail'];
193 if(mail == 0) { mail = ''; $('#mail-update-li').removeClass('show') } else { $('#mail-update-li').addClass('show') }
194 $('#mail-update-li').html(mail);
196 $(".sidebar-group-li .notify").removeClass("show");
197 $(data.groups).each(function(key, group) {
199 var gcount = group.count;
200 $(".group-"+gid+" .notify").addClass("show").text(gcount);
203 $(".forum-widget-entry .notify").removeClass("show");
204 $(data.forums).each(function(key, forum) {
206 var fcount = forum.count;
207 $(".forum-"+fid+" .notify").addClass("show").text(fcount);
210 if (data.notifications.length == 0) {
211 $("#nav-notifications-menu").html(notifications_empty);
213 var nnm = $("#nav-notifications-menu");
214 nnm.html(notifications_all + notifications_mark);
216 var lastItemStorageKey = "notification-lastitem:" + localUser;
217 var notification_lastitem = parseInt(localStorage.getItem(lastItemStorageKey));
218 var notification_id = 0;
220 // Insert notifs into the notifications-menu
221 $(data.notifications).each(function(key, notif){
222 var text = notif.message.format('<span class="contactname">' + notif.name + '</span>');
223 var contact = ('<a href="' + notif.url + '"><span class="contactname">' + notif.name + '</span></a>');
224 var seenclass = (notif.seen == 1) ? "notify-seen" : "notify-unseen";
225 var html = notifications_tpl.format(
226 notif.href, // {0} // link to the source
227 notif.photo, // {1} // photo of the contact
228 text, // {2} // preformatted text (autor + text)
229 notif.date, // {3} // date of notification (time ago)
230 seenclass, // {4} // visited status of the notification
231 new Date(notif.timestamp*1000), // {5} // date of notification
232 notif.url, // {6} // profile url of the contact
233 notif.message.format(contact), // {7} // preformatted html (text including author profile url)
234 '' // {8} // Deprecated
239 // Desktop Notifications
240 $(data.notifications.reverse()).each(function(key, e){
241 notification_id = parseInt(e.timestamp);
242 if (notification_lastitem !== null && notification_id > notification_lastitem && Number(e.seen) === 0) {
243 if (getNotificationPermission() === "granted") {
244 var notification = new Notification(document.title, {
245 body: decodeHtml(e.message.replace('→ ', '').format(e.name)),
248 notification['url'] = e.href;
249 notification.addEventListener("click", function(ev){
250 window.location = ev.target.url;
256 notification_lastitem = notification_id;
257 localStorage.setItem(lastItemStorageKey, notification_lastitem)
259 $("img[data-src]", nnm).each(function(i, el){
260 // Add src attribute for images with a data-src attribute
261 // However, don't bother if the data-src attribute is empty, because
262 // an empty "src" tag for an image will cause some browsers
263 // to prefetch the root page of the Friendica hub, which will
264 // unnecessarily load an entire profile/ or network/ page
265 if($(el).data("src") != '') $(el).attr('src', $(el).data("src"));
269 var notif = data['notify'];
271 $("#nav-notifications-linkmenu").addClass("on");
273 $("#nav-notifications-linkmenu").removeClass("on");
276 $(data.sysmsgs.notice).each(function(key, message){
277 $.jGrowl(message, {sticky: true, theme: 'notice'});
279 $(data.sysmsgs.info).each(function(key, message){
280 $.jGrowl(message, {sticky: false, theme: 'info', life: 5000});
283 // Update the js scrollbars
284 $('#nav-notifications-menu').perfectScrollbar('update');
289 // Allow folks to stop the ajax page updates with the pause/break key
290 $(document).keydown(function(event) {
291 if(event.keyCode == '8') {
292 var target = event.target || event.srcElement;
293 if (!/input|textarea/i.test(target.nodeName)) {
297 if(event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
298 event.preventDefault();
299 if(stopped == false) {
304 $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
315 // Scroll to the next/previous thread when pressing J and K
316 $(document).keydown(function (event) {
317 var threads = $('.thread_level_1');
318 if ((event.keyCode === 74 || event.keyCode === 75) && !$(event.target).is('textarea, input')) {
319 var scrollTop = $(window).scrollTop();
320 if (event.keyCode === 75) {
321 threads = $(threads.get().reverse());
323 threads.each(function(key, item) {
325 var top = $(item).offset().top - 100;
326 if (event.keyCode === 74) {
327 comparison = top > scrollTop + 1;
328 } else if (event.keyCode === 75) {
329 comparison = top < scrollTop - 1;
332 $('html, body').animate({ scrollTop: top }, 200);
339 // Set an event listener for infinite scroll
340 if(typeof infinite_scroll !== 'undefined') {
341 $(window).scroll(function(e){
342 if ($(document).height() != $(window).height()) {
343 // First method that is expected to work - but has problems with Chrome
344 if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
347 // This method works with Chrome - but seems to be much slower in Firefox
348 if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5))
357 function NavUpdate() {
360 var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&f=&uid=' + localUser : '');
361 $.get(pingCmd, function(data) {
363 // send nav-update event
364 $('nav').trigger('nav-update', data.result);
367 ['network', 'profile', 'community', 'notes', 'display'].forEach(function (src) {
368 if ($('#live-' + src).length) {
372 if ($('#live-photos').length) {
375 window.location.href = window.location.href;
381 timer = setTimeout(NavUpdate, updateInterval);
384 function liveUpdate(src) {
385 if((src == null) || (stopped) || (! profile_uid)) { $('.like-rotator').hide(); return; }
386 if(($('.comment-edit-text-full').length) || (in_progress)) {
388 clearTimeout(livetime);
390 livetime = setTimeout(function() {liveUpdate(src)}, 5000);
396 prev = 'live-' + src;
400 if ($(document).scrollTop() == 0)
403 var udargs = ((netargs.length) ? '/' + netargs : '');
404 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&force=' + ((force_update) ? 1 : 0);
406 $.get(update_url,function(data) {
408 force_update = false;
409 // $('.collapsed-comments',data).each(function() {
410 // var ident = $(this).attr('id');
411 // var is_hidden = $('#' + ident).is(':hidden');
412 // if($('#' + ident).length) {
413 // $('#' + ident).replaceWith($(this));
415 // $('#' + ident).hide();
420 $('.toplevel_item',data).each(function() {
421 var ident = $(this).attr('id');
423 if($('#' + ident).length == 0 && profile_page == 1) {
424 $('img',this).each(function() {
425 $(this).attr('src',$(this).attr('dst'));
427 $('#' + prev).after($(this));
430 // Find out if the hidden comments are open, so we can keep it that way
431 // if a new comment has been posted
432 var id = $('.hide-comments-total', this).attr('id');
433 if(typeof id != 'undefined') {
434 id = id.split('-')[3];
435 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
438 $('img',this).each(function() {
439 $(this).attr('src',$(this).attr('dst'));
441 //vScroll = $(document).scrollTop();
442 $('html').height($('html').height());
443 $('#' + ident).replaceWith($(this));
445 if(typeof id != 'undefined') {
446 if(commentsOpen) showHideComments(id);
448 $('html').height('auto');
449 //$(document).scrollTop(vScroll);
454 // reset vars for inserting individual items
456 /* prev = 'live-' + src;
458 $('.wall-item-outside-wrapper',data).each(function() {
459 var ident = $(this).attr('id');
461 if($('#' + ident).length == 0 && prev != 'live-' + src) {
462 $('img',this).each(function() {
463 $(this).attr('src',$(this).attr('dst'));
465 $('#' + prev).after($(this));
468 $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago'));
469 if($('#' + ident + ' ' + '.comment-edit-text-empty').length)
470 $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
471 $('#' + ident + ' ' + '.hide-comments-total').replaceWith($(this).find('.hide-comments-total'));
472 $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
473 $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
474 $('#' + ident + ' ' + '.my-comment-photo').each(function() {
475 $(this).attr('src',$(this).attr('dst'));
481 $('.like-rotator').hide();
484 $('body').css('cursor', 'auto');
486 /* autocomplete @nicknames */
487 $(".comment-edit-form textarea").editor_autocomplete(baseurl+"/acl");
488 /* autocomplete bbcode */
489 $(".comment-edit-form textarea").bbco_autocomplete('bbcode');
493 function imgbright(node) {
494 $(node).removeClass("drophide").addClass("drop");
497 function imgdull(node) {
498 $(node).removeClass("drop").addClass("drophide");
501 // Since our ajax calls are asynchronous, we will give a few
502 // seconds for the first ajax call (setting like/dislike), then
503 // run the updater to pick up any changes and display on the page.
504 // The updater will turn any rotators off when it's done.
505 // This function will have returned long before any of these
506 // events have completed and therefore there won't be any
507 // visible feedback that anything changed without all this
508 // trickery. This still could cause confusion if the "like" ajax call
509 // is delayed and NavUpdate runs before it completes.
511 function dolike(ident,verb) {
513 $('#like-rotator-' + ident.toString()).show();
514 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate );
519 function dosubthread(ident) {
521 $('#like-rotator-' + ident.toString()).show();
522 $.get('subthread/' + ident.toString(), NavUpdate );
527 function dostar(ident) {
528 ident = ident.toString();
529 $('#like-rotator-' + ident).show();
530 $.get('starred/' + ident, function(data) {
531 if(data.match(/1/)) {
532 $('#starred-' + ident).addClass('starred');
533 $('#starred-' + ident).removeClass('unstarred');
534 $('#star-' + ident).addClass('hidden');
535 $('#unstar-' + ident).removeClass('hidden');
538 $('#starred-' + ident).addClass('unstarred');
539 $('#starred-' + ident).removeClass('starred');
540 $('#star-' + ident).removeClass('hidden');
541 $('#unstar-' + ident).addClass('hidden');
543 $('#like-rotator-' + ident).hide();
547 function doignore(ident) {
548 ident = ident.toString();
549 $('#like-rotator-' + ident).show();
550 $.get('ignored/' + ident, function(data) {
551 if(data.match(/1/)) {
552 $('#ignored-' + ident).addClass('ignored');
553 $('#ignored-' + ident).removeClass('unignored');
554 $('#ignore-' + ident).addClass('hidden');
555 $('#unignore-' + ident).removeClass('hidden');
558 $('#ignored-' + ident).addClass('unignored');
559 $('#ignored-' + ident).removeClass('ignored');
560 $('#ignore-' + ident).removeClass('hidden');
561 $('#unignore-' + ident).addClass('hidden');
563 $('#like-rotator-' + ident).hide();
567 function getPosition(e) {
568 var cursor = {x:0, y:0};
569 if ( e.pageX || e.pageY ) {
574 if( e.clientX || e.clientY ) {
575 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
576 cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
588 var lockvisible = false;
590 function lockview(event,id) {
591 event = event || window.event;
592 cursor = getPosition(event);
598 $.get('lockview/' + id, function(data) {
599 $('#panel').html(data);
600 $('#panel').css({ 'left': cursor.x + 5 , 'top': cursor.y + 5});
606 function lockviewhide() {
611 function post_comment(id) {
614 $('body').css('cursor', 'wait');
615 $("#comment-preview-inp-" + id).val("0");
618 $("#comment-edit-form-" + id).serialize(),
621 $("#comment-edit-wrapper-" + id).hide();
622 $("#comment-edit-text-" + id).val('');
623 var tarea = document.getElementById("comment-edit-text-" + id);
625 commentClose(tarea,id);
626 if(timer) clearTimeout(timer);
627 timer = setTimeout(NavUpdate,10);
631 window.location.href=data.reload;
640 function preview_comment(id) {
641 $("#comment-preview-inp-" + id).val("1");
642 $("#comment-edit-preview-" + id).show();
645 $("#comment-edit-form-" + id).serialize(),
648 $("#comment-edit-preview-" + id).html(data.preview);
649 $("#comment-edit-preview-" + id + " a").click(function() { return false; });
659 function showHideComments(id) {
660 if( $("#collapsed-comments-" + id).is(":visible")) {
661 $("#collapsed-comments-" + id).hide();
662 $("#hide-comments-" + id).html(window.showMore);
665 $("#collapsed-comments-" + id).show();
666 $("#hide-comments-" + id).html(window.showFewer);
672 function preview_post() {
673 $("#jot-preview").val("1");
674 $("#jot-preview-content").show();
677 $("#profile-jot-form").serialize(),
680 $("#jot-preview-content").html(data.preview);
681 $("#jot-preview-content" + " a").click(function() { return false; });
686 $("#jot-preview").val("0");
692 // unpause auto reloads if they are currently stopped
695 $('#pause').html('');
698 // load more network content (used for infinite scroll)
699 function loadScrollContent() {
700 if (lockLoadContent) return;
701 lockLoadContent = true;
703 $("#scroll-loader").fadeIn('normal');
705 // the page number to load is one higher than the actual
707 infinite_scroll.pageno+=1;
709 match = $("span.received").last();
710 if (match.length > 0) {
711 received = match[0].innerHTML;
713 received = "0000-00-00 00:00:00";
716 match = $("span.created").last();
717 if (match.length > 0) {
718 created = match[0].innerHTML;
720 created = "0000-00-00 00:00:00";
723 match = $("span.commented").last();
724 if (match.length > 0) {
725 commented = match[0].innerHTML;
727 commented = "0000-00-00 00:00:00";
730 match = $("span.id").last();
731 if (match.length > 0) {
732 id = match[0].innerHTML;
736 // console.log("Received: " + received + " - Commented: " + commented+ " - Created: " + created + " - ID: " + id);
738 // get the raw content from the next page and insert this content
739 // right before "#conversation-end"
740 $.get('network?mode=raw' + infinite_scroll.reload_uri + '&last_received=' + received + '&last_commented=' + commented + '&last_created=' + created + '&last_id=' + id + '&page=' + infinite_scroll.pageno, function(data) {
741 $("#scroll-loader").hide();
742 if ($(data).length > 0) {
744 $(data).insertBefore('#conversation-end');
745 lockLoadContent = false;
747 $("#scroll-end").fadeIn('normal');
753 // Converts the binary representation of data to hex
756 // discuss at: http://phpjs.org/functions/bin2hex
757 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
758 // + bugfixed by: Onno Marsman
759 // + bugfixed by: Linuxworld
760 // * example 1: bin2hex('Kev');
761 // * returns 1: '4b6576'
762 // * example 2: bin2hex(String.fromCharCode(0x00));
764 var v,i, f = 0, a = [];
768 for (i = 0; i<f; i++) {
769 a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
775 function groupChangeMember(gid, cid, sec_token) {
776 $('body .fakelink').css('cursor', 'wait');
777 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
778 $('#group-update-wrapper').html(data);
779 $('body .fakelink').css('cursor', 'auto');
783 function profChangeMember(gid,cid) {
784 $('body .fakelink').css('cursor', 'wait');
785 $.get('profperm/' + gid + '/' + cid, function(data) {
786 $('#prof-update-wrapper').html(data);
787 $('body .fakelink').css('cursor', 'auto');
791 function contactgroupChangeMember(gid,cid) {
792 $('body').css('cursor', 'wait');
793 $.get('contactgroup/' + gid + '/' + cid, function(data) {
794 $('body').css('cursor', 'auto');
799 function checkboxhighlight(box) {
800 if($(box).is(':checked')) {
801 $(box).addClass('checkeditem');
804 $(box).removeClass('checkeditem');
808 function notifyMarkAll() {
809 $.get('notify/mark/all', function(data) {
810 if(timer) clearTimeout(timer);
811 timer = setTimeout(NavUpdate,1000);
817 * sprintf in javascript
818 * "{0} and {1}".format('zero','uno');
820 String.prototype.format = function() {
821 var formatted = this;
822 for (var i = 0; i < arguments.length; i++) {
823 var regexp = new RegExp('\\{'+i+'\\}', 'gi');
824 formatted = formatted.replace(regexp, arguments[i]);
829 Array.prototype.remove = function(item) {
830 to=undefined; from=this.indexOf(item);
831 var rest = this.slice((to || from) + 1 || this.length);
832 this.length = from < 0 ? this.length + from : from;
833 return this.push.apply(this, rest);
836 function previewTheme(elm) {
837 theme = $(elm).val();
838 $.getJSON('pretheme?f=&theme=' + theme,function(data) {
839 $('#theme-preview').html('<div id="theme-desc">' + data.desc + '</div><div id="theme-version">' + data.version + '</div><div id="theme-credits">' + data.credits + '</div><a href="' + data.img + '"><img src="' + data.img + '" width="320" height="240" alt="' + theme + '" /></a>');
844 // notification permission settings in localstorage
845 // set by settings page
846 function getNotificationPermission() {
847 if (window["Notification"] === undefined) {
850 if (Notification.permission === 'granted') {
851 var val = localStorage.getItem('notification-permissions');
852 if (val === null) return 'denied';
855 return Notification.permission;
860 * Show a dialog loaded from an url
861 * By defaults this load the url in an iframe in colorbox
862 * Themes can overwrite `show()` function to personalize it
869 * @return object colorbox
871 show : function (url) {
872 var size = Dialog._get_size();
873 return $.colorbox({href: url, iframe:true,innerWidth: size.width+'px',innerHeight: size.height+'px'})
877 * Show the Image browser dialog
880 * @param string id (optional)
883 * The name will be used to build the event name
884 * fired by image browser dialog when the user select
885 * an image. The optional id will be passed as argument
886 * to the event handler
888 doImageBrowser : function (name, id) {
889 var url = Dialog._get_url("image",name,id);
890 return Dialog.show(url);
894 * Show the File browser dialog
897 * @param string id (optional)
900 * The name will be used to build the event name
901 * fired by file browser dialog when the user select
902 * a file. The optional id will be passed as argument
903 * to the event handler
905 doFileBrowser : function (name, id) {
906 var url = Dialog._get_url("file",name,id);
907 return Dialog.show(url);
910 _get_url : function(type, name, id) {
912 if (id !== undefined) hash = hash + "-" + id;
913 return baseurl + "/fbrowser/"+type+"/?mode=minimal#"+hash;
916 _get_size: function() {
918 width: window.innerWidth-50,
919 height: window.innerHeight-100