1 function resizeIframe(obj) {
5 function _resizeIframe(obj, desth) {
6 var h = obj.style.height;
7 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"
22 document.getElementById(theID).style.display = "block"
26 function openMenu(theID) {
27 var el = document.getElementById(theID)
30 el.style.display = "block";
34 function closeMenu(theID) {
35 var el = document.getElementById(theID)
38 el.style.display = "none";
42 function decodeHtml(html) {
43 var txt = document.createElement("textarea");
52 var force_update = false;
55 var totStopped = false;
59 var in_progress = false;
60 var langSelect = false;
61 var commentBusy = false;
62 var last_popup_menu = null;
63 var last_popup_button = null;
64 var lockLoadContent = false;
67 $.ajaxSetup({cache: false});
69 /* setup comment textarea buttons */
70 /* comment textarea buttons needs some "data-*" attributes to work:
71 * data-role="insert-formatting" : to mark the element as a formatting button
72 * data-bbcode="<string>" : name of the bbcode element to insert. insertFormatting() will insert it as "[name][/name]"
73 * data-id="<string>" : id of the comment, used to find other comment-related element, like the textarea
75 $('body').on('click','[data-role="insert-formatting"]', function(e) {
78 var bbcode = o.data('bbcode');
79 var id = o.data('id');
80 if (bbcode == "img") {
81 Dialog.doImageBrowser("comment", id);
84 insertFormatting(bbcode, id);
87 /* event from comment textarea button popups */
88 /* insert returned bbcode at cursor position or replace selected text */
89 $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
91 var textarea = document.getElementById("comment-edit-text-" +id);
92 var start = textarea.selectionStart;
93 var end = textarea.selectionEnd;
94 textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
95 $(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();
133 menu = $($(this).attr('rel'));
136 if (menu.attr('popup') == "false") {
139 parent.toggleClass("selected");
141 if (menu.css("display") == "none") {
142 last_popup_menu = null;
143 last_popup_button = null;
145 last_popup_menu = menu;
146 last_popup_menu.on('click', function(e) {e.stopPropagation()});
147 last_popup_button = parent;
148 $('#nav-notifications-menu').perfectScrollbar('update');
152 $('html').click(function() {
153 close_last_popup_menu();
157 $("a.popupbox").colorbox({
159 'transition' : 'elastic',
162 $("a.ajax-popupbox").colorbox({
163 'transition' : 'elastic',
167 /* notifications template */
168 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
169 var notifications_all = unescape($('<div>').append($("#nav-notifications-see-all").clone()).html()); //outerHtml hack
170 var notifications_mark = unescape($('<div>').append($("#nav-notifications-mark-all").clone()).html()); //outerHtml hack
171 var notifications_empty = unescape($("#nav-notifications-menu").html());
173 /* enable perfect-scrollbars for different elements */
174 $('#nav-notifications-menu, aside').perfectScrollbar();
176 /* nav update event */
177 $('nav').bind('nav-update', function(e, data) {
178 var invalid = data.invalid || 0;
180 window.location.href=window.location.href
183 ['net', 'home', 'intro', 'mail', 'events', 'birthdays', 'notify'].forEach(function(type) {
184 var number = data[type];
187 $('#' + type + '-update').removeClass('show');
189 $('#' + type + '-update').addClass('show');
191 $('#' + type + '-update').text(number);
194 var intro = data['intro'];
196 intro = ''; $('#intro-update-li').removeClass('show')
198 $('#intro-update-li').addClass('show')
201 $('#intro-update-li').html(intro);
203 var mail = data['mail'];
205 mail = ''; $('#mail-update-li').removeClass('show')
207 $('#mail-update-li').addClass('show')
210 $('#mail-update-li').html(mail);
212 $(".sidebar-group-li .notify").removeClass("show");
213 $(data.groups).each(function(key, group) {
215 var gcount = group.count;
216 $(".group-"+gid+" .notify").addClass("show").text(gcount);
219 $(".forum-widget-entry .notify").removeClass("show");
220 $(data.forums).each(function(key, forum) {
222 var fcount = forum.count;
223 $(".forum-"+fid+" .notify").addClass("show").text(fcount);
226 if (data.notifications.length == 0) {
227 $("#nav-notifications-menu").html(notifications_empty);
229 var nnm = $("#nav-notifications-menu");
230 nnm.html(notifications_all + notifications_mark);
232 var lastItemStorageKey = "notification-lastitem:" + localUser;
233 var notification_lastitem = parseInt(localStorage.getItem(lastItemStorageKey));
234 var notification_id = 0;
236 // Insert notifs into the notifications-menu
237 $(data.notifications).each(function(key, notif) {
238 var text = notif.message.format('<span class="contactname">' + notif.name + '</span>');
239 var contact = ('<a href="' + notif.url + '"><span class="contactname">' + notif.name + '</span></a>');
240 var seenclass = (notif.seen == 1) ? "notify-seen" : "notify-unseen";
241 var html = notifications_tpl.format(
242 notif.href, // {0} // link to the source
243 notif.photo, // {1} // photo of the contact
244 text, // {2} // preformatted text (autor + text)
245 notif.date, // {3} // date of notification (time ago)
246 seenclass, // {4} // visited status of the notification
247 new Date(notif.timestamp*1000), // {5} // date of notification
248 notif.url, // {6} // profile url of the contact
249 notif.message.format(contact), // {7} // preformatted html (text including author profile url)
250 '' // {8} // Deprecated
255 // Desktop Notifications
256 $(data.notifications.reverse()).each(function(key, e) {
257 notification_id = parseInt(e.timestamp);
258 if (notification_lastitem !== null && notification_id > notification_lastitem && Number(e.seen) === 0) {
259 if (getNotificationPermission() === "granted") {
260 var notification = new Notification(document.title, {
261 body: decodeHtml(e.message.replace('→ ', '').format(e.name)),
264 notification['url'] = e.href;
265 notification.addEventListener("click", function(ev) {
266 window.location = ev.target.url;
272 notification_lastitem = notification_id;
273 localStorage.setItem(lastItemStorageKey, notification_lastitem)
275 $("img[data-src]", nnm).each(function(i, el) {
276 // Add src attribute for images with a data-src attribute
277 // However, don't bother if the data-src attribute is empty, because
278 // an empty "src" tag for an image will cause some browsers
279 // to prefetch the root page of the Friendica hub, which will
280 // unnecessarily load an entire profile/ or network/ page
281 if ($(el).data("src") != '') {
282 $(el).attr('src', $(el).data("src"));
287 var notif = data['notify'];
289 $("#nav-notifications-linkmenu").addClass("on");
291 $("#nav-notifications-linkmenu").removeClass("on");
294 $(data.sysmsgs.notice).each(function(key, message) {
295 $.jGrowl(message, {sticky: true, theme: 'notice'});
297 $(data.sysmsgs.info).each(function(key, message) {
298 $.jGrowl(message, {sticky: false, theme: 'info', life: 5000});
301 // Update the js scrollbars
302 $('#nav-notifications-menu').perfectScrollbar('update');
305 // Asynchronous calls are deferred until the very end of the page load to ease on slower connections
306 window.addEventListener("load", function(){
311 // Allow folks to stop the ajax page updates with the pause/break key
312 $(document).keydown(function(event) {
313 if (event.keyCode == '8') {
314 var target = event.target || event.srcElement;
315 if (!/input|textarea/i.test(target.nodeName)) {
320 if (event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
321 event.preventDefault();
322 if (stopped == false) {
327 $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
331 } else if (!totStopped) {
336 // Scroll to the next/previous thread when pressing J and K
337 $(document).keydown(function (event) {
338 var threads = $('.thread_level_1');
339 if ((event.keyCode === 74 || event.keyCode === 75) && !$(event.target).is('textarea, input')) {
340 var scrollTop = $(window).scrollTop();
341 if (event.keyCode === 75) {
342 threads = $(threads.get().reverse());
344 threads.each(function(key, item) {
346 var top = $(item).offset().top - 100;
347 if (event.keyCode === 74) {
348 comparison = top > scrollTop + 1;
349 } else if (event.keyCode === 75) {
350 comparison = top < scrollTop - 1;
353 $('html, body').animate({scrollTop: top}, 200);
360 // Set an event listener for infinite scroll
361 if (typeof infinite_scroll !== 'undefined') {
362 $(window).scroll(function(e) {
363 if ($(document).height() != $(window).height()) {
364 // First method that is expected to work - but has problems with Chrome
365 if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
368 // This method works with Chrome - but seems to be much slower in Firefox
369 if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5)) {
377 function NavUpdate() {
379 var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&f=&uid=' + localUser : '');
380 $.get(pingCmd, function(data) {
382 // send nav-update event
383 $('nav').trigger('nav-update', data.result);
386 ['network', 'profile', 'community', 'notes', 'display', 'contacts'].forEach(function (src) {
387 if ($('#live-' + src).length) {
391 if ($('#live-photos').length) {
394 window.location.href = window.location.href;
400 timer = setTimeout(NavUpdate, updateInterval);
403 function updateConvItems(data) {
405 $('.toplevel_item',data).each(function() {
406 var ident = $(this).attr('id');
408 // Add new top-level item.
409 if ($('#' + ident).length == 0 && profile_page == 1) {
410 $('#' + prev).after($(this));
412 // Replace already existing thread.
414 // Find out if the hidden comments are open, so we can keep it that way
415 // if a new comment has been posted
416 var id = $('.hide-comments-total', this).attr('id');
417 if (typeof id != 'undefined') {
418 id = id.split('-')[3];
419 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
422 $('#' + ident).replaceWith($(this));
424 if (typeof id != 'undefined') {
426 showHideComments(id);
433 $('.like-rotator').hide();
436 $('body').css('cursor', 'auto');
438 /* autocomplete @nicknames */
439 $(".comment-edit-form textarea").editor_autocomplete(baseurl+"/acl");
440 /* autocomplete bbcode */
441 $(".comment-edit-form textarea").bbco_autocomplete('bbcode');
444 function liveUpdate(src) {
445 if ((src == null) || stopped || !profile_uid) {
446 $('.like-rotator').hide(); return;
449 if (($('.comment-edit-text-full').length) || in_progress) {
451 clearTimeout(livetime);
453 livetime = setTimeout(function() {liveUpdate(src)}, 5000);
457 if (livetime != null) {
460 prev = 'live-' + src;
464 if ($(document).scrollTop() == 0) {
468 var orgHeight = $("section").height();
470 var udargs = ((netargs.length) ? '/' + netargs : '');
471 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&force=' + ((force_update) ? 1 : 0) + '&item=' + update_item;
473 $.get(update_url,function(data) {
475 force_update = false;
478 $('.wall-item-body', data).imagesLoaded(function() {
479 updateConvItems(data);
481 document.dispatchEvent(new Event('postprocess_liveupdate'));
483 // Update the scroll position.
484 $(window).scrollTop($(window).scrollTop() + $("section").height() - orgHeight);
489 function imgbright(node) {
490 $(node).removeClass("drophide").addClass("drop");
493 function imgdull(node) {
494 $(node).removeClass("drop").addClass("drophide");
497 // Since our ajax calls are asynchronous, we will give a few
498 // seconds for the first ajax call (setting like/dislike), then
499 // run the updater to pick up any changes and display on the page.
500 // The updater will turn any rotators off when it's done.
501 // This function will have returned long before any of these
502 // events have completed and therefore there won't be any
503 // visible feedback that anything changed without all this
504 // trickery. This still could cause confusion if the "like" ajax call
505 // is delayed and NavUpdate runs before it completes.
507 function dolike(ident,verb) {
509 $('#like-rotator-' + ident.toString()).show();
510 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate);
513 update_item = ident.toString();
516 function dosubthread(ident) {
518 $('#like-rotator-' + ident.toString()).show();
519 $.get('subthread/' + ident.toString(), NavUpdate);
523 function dostar(ident) {
524 ident = ident.toString();
525 $('#like-rotator-' + ident).show();
526 $.get('starred/' + ident, function(data) {
527 if (data.match(/1/)) {
528 $('#starred-' + ident).addClass('starred');
529 $('#starred-' + ident).removeClass('unstarred');
530 $('#star-' + ident).addClass('hidden');
531 $('#unstar-' + ident).removeClass('hidden');
533 $('#starred-' + ident).addClass('unstarred');
534 $('#starred-' + ident).removeClass('starred');
535 $('#star-' + ident).removeClass('hidden');
536 $('#unstar-' + ident).addClass('hidden');
538 $('#like-rotator-' + ident).hide();
542 function doignore(ident) {
543 ident = ident.toString();
544 $('#like-rotator-' + ident).show();
545 $.get('ignored/' + ident, function(data) {
546 if (data.match(/1/)) {
547 $('#ignored-' + ident).addClass('ignored');
548 $('#ignored-' + ident).removeClass('unignored');
549 $('#ignore-' + ident).addClass('hidden');
550 $('#unignore-' + ident).removeClass('hidden');
552 $('#ignored-' + ident).addClass('unignored');
553 $('#ignored-' + ident).removeClass('ignored');
554 $('#ignore-' + ident).removeClass('hidden');
555 $('#unignore-' + ident).addClass('hidden');
557 $('#like-rotator-' + ident).hide();
561 function getPosition(e) {
562 var cursor = {x:0, y:0};
564 if (e.pageX || e.pageY) {
568 if (e.clientX || e.clientY) {
569 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
570 cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
571 } else if (e.x || e.y) {
579 var lockvisible = false;
581 function lockview(event,id) {
582 event = event || window.event;
583 cursor = getPosition(event);
588 $.get('lockview/' + id, function(data) {
589 $('#panel').html(data);
590 $('#panel').css({'left': cursor.x + 5 , 'top': cursor.y + 5});
596 function lockviewhide() {
601 function post_comment(id) {
604 $('body').css('cursor', 'wait');
605 $("#comment-preview-inp-" + id).val("0");
608 $("#comment-edit-form-" + id).serialize(),
611 $("#comment-edit-wrapper-" + id).hide();
612 $("#comment-edit-text-" + id).val('');
613 var tarea = document.getElementById("comment-edit-text-" + id);
615 commentClose(tarea,id);
620 timer = setTimeout(NavUpdate,10);
625 window.location.href=data.reload;
633 function preview_comment(id) {
634 $("#comment-preview-inp-" + id).val("1");
635 $("#comment-edit-preview-" + id).show();
638 $("#comment-edit-form-" + id).serialize(),
641 $("#comment-edit-preview-" + id).html(data.preview);
642 $("#comment-edit-preview-" + id + " a").click(function() {return false;});
650 function showHideComments(id) {
651 if ($("#collapsed-comments-" + id).is(":visible")) {
652 $("#collapsed-comments-" + id).hide();
653 $("#hide-comments-" + id).html(window.showMore);
655 $("#collapsed-comments-" + id).show();
656 $("#hide-comments-" + id).html(window.showFewer);
660 function preview_post() {
661 $("#jot-preview").val("1");
662 $("#jot-preview-content").show();
665 $("#profile-jot-form").serialize(),
668 $("#jot-preview-content").html(data.preview);
669 $("#jot-preview-content" + " a").click(function() {return false;});
670 document.dispatchEvent(new Event('postprocess_liveupdate'));
675 $("#jot-preview").val("0");
680 // unpause auto reloads if they are currently stopped
683 $('#pause').html('');
686 // load more network content (used for infinite scroll)
687 function loadScrollContent() {
688 if (lockLoadContent) {
691 lockLoadContent = true;
693 $("#scroll-loader").fadeIn('normal');
695 // the page number to load is one higher than the actual
697 infinite_scroll.pageno+=1;
699 match = $("span.received").last();
700 if (match.length > 0) {
701 received = match[0].innerHTML;
703 received = "0000-00-00 00:00:00";
706 match = $("span.created").last();
707 if (match.length > 0) {
708 created = match[0].innerHTML;
710 created = "0000-00-00 00:00:00";
713 match = $("span.commented").last();
714 if (match.length > 0) {
715 commented = match[0].innerHTML;
717 commented = "0000-00-00 00:00:00";
720 match = $("span.id").last();
721 if (match.length > 0) {
722 id = match[0].innerHTML;
727 // get the raw content from the next page and insert this content
728 // right before "#conversation-end"
729 $.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) {
730 $("#scroll-loader").hide();
731 if ($(data).length > 0) {
732 $(data).insertBefore('#conversation-end');
733 lockLoadContent = false;
735 $("#scroll-end").fadeIn('normal');
738 document.dispatchEvent(new Event('postprocess_liveupdate'));
742 function bin2hex(s) {
743 // Converts the binary representation of data to hex
746 // discuss at: http://phpjs.org/functions/bin2hex
747 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
748 // + bugfixed by: Onno Marsman
749 // + bugfixed by: Linuxworld
750 // * example 1: bin2hex('Kev');
751 // * returns 1: '4b6576'
752 // * example 2: bin2hex(String.fromCharCode(0x00));
754 var v,i, f = 0, a = [];
758 for (i = 0; i<f; i++) {
759 a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
765 function groupChangeMember(gid, cid, sec_token) {
766 $('body .fakelink').css('cursor', 'wait');
767 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
768 $('#group-update-wrapper').html(data);
769 $('body .fakelink').css('cursor', 'auto');
773 function profChangeMember(gid,cid) {
774 $('body .fakelink').css('cursor', 'wait');
775 $.get('profperm/' + gid + '/' + cid, function(data) {
776 $('#prof-update-wrapper').html(data);
777 $('body .fakelink').css('cursor', 'auto');
781 function contactgroupChangeMember(gid,cid) {
782 $('body').css('cursor', 'wait');
783 $.get('contactgroup/' + gid + '/' + cid, function(data) {
784 $('body').css('cursor', 'auto');
788 function checkboxhighlight(box) {
789 if ($(box).is(':checked')) {
790 $(box).addClass('checkeditem');
792 $(box).removeClass('checkeditem');
796 function notifyMarkAll() {
797 $.get('notify/mark/all', function(data) {
801 timer = setTimeout(NavUpdate,1000);
807 * sprintf in javascript
808 * "{0} and {1}".format('zero','uno');
810 String.prototype.format = function() {
811 var formatted = this;
812 for (var i = 0; i < arguments.length; i++) {
813 var regexp = new RegExp('\\{'+i+'\\}', 'gi');
814 formatted = formatted.replace(regexp, arguments[i]);
819 Array.prototype.remove = function(item) {
820 to=undefined; from=this.indexOf(item);
821 var rest = this.slice((to || from) + 1 || this.length);
822 this.length = from < 0 ? this.length + from : from;
823 return this.push.apply(this, rest);
826 function previewTheme(elm) {
827 theme = $(elm).val();
828 $.getJSON('pretheme?f=&theme=' + theme,function(data) {
829 $('#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>');
834 // notification permission settings in localstorage
835 // set by settings page
836 function getNotificationPermission() {
837 if (window["Notification"] === undefined) {
841 if (Notification.permission === 'granted') {
842 var val = localStorage.getItem('notification-permissions');
848 return Notification.permission;
853 * Show a dialog loaded from an url
854 * By defaults this load the url in an iframe in colorbox
855 * Themes can overwrite `show()` function to personalize it
862 * @return object colorbox
864 show : function (url) {
865 var size = Dialog._get_size();
866 return $.colorbox({href: url, iframe:true,innerWidth: size.width+'px',innerHeight: size.height+'px'})
870 * Show the Image browser dialog
873 * @param string id (optional)
876 * The name will be used to build the event name
877 * fired by image browser dialog when the user select
878 * an image. The optional id will be passed as argument
879 * to the event handler
881 doImageBrowser : function (name, id) {
882 var url = Dialog._get_url("image",name,id);
883 return Dialog.show(url);
887 * Show the File browser dialog
890 * @param string id (optional)
893 * The name will be used to build the event name
894 * fired by file browser dialog when the user select
895 * a file. The optional id will be passed as argument
896 * to the event handler
898 doFileBrowser : function (name, id) {
899 var url = Dialog._get_url("file",name,id);
900 return Dialog.show(url);
903 _get_url : function(type, name, id) {
905 if (id !== undefined) {
906 hash = hash + "-" + id;
908 return baseurl + "/fbrowser/"+type+"/?mode=minimal#"+hash;
911 _get_size: function() {
913 width: window.innerWidth-50,
914 height: window.innerHeight-100