1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
2 function resizeIframe(obj) {
6 function _resizeIframe(obj, desth) {
7 var h = obj.style.height;
8 var ch = obj.contentWindow.document.body.scrollHeight;
10 if (h == (ch + 'px')) {
13 if (desth == ch && ch > 0) {
14 obj.style.height = ch + 'px';
16 setTimeout(_resizeIframe, 100, obj, ch);
19 function openClose(theID) {
20 var el = document.getElementById(theID);
22 if (window.getComputedStyle(el).display === "none") {
30 function openMenu(theID) {
31 var el = document.getElementById(theID);
33 if (!el.dataset.display) {
34 el.dataset.display = 'block';
36 el.style.display = el.dataset.display;
40 function closeMenu(theID) {
41 var el = document.getElementById(theID);
43 el.dataset.display = window.getComputedStyle(el).display;
44 el.style.display = "none";
48 function decodeHtml(html) {
49 var txt = document.createElement("textarea");
58 var force_update = false;
61 var totStopped = false;
65 var in_progress = false;
66 var langSelect = false;
67 var commentBusy = false;
68 var last_popup_menu = null;
69 var last_popup_button = null;
70 var lockLoadContent = false;
73 $.ajaxSetup({cache: false});
75 /* setup comment textarea buttons */
76 /* comment textarea buttons needs some "data-*" attributes to work:
77 * data-role="insert-formatting" : to mark the element as a formatting button
78 * data-bbcode="<string>" : name of the bbcode element to insert. insertFormatting() will insert it as "[name][/name]"
79 * data-id="<string>" : id of the comment, used to find other comment-related element, like the textarea
81 $('body').on('click','[data-role="insert-formatting"]', function(e) {
84 var bbcode = o.data('bbcode');
85 var id = o.data('id');
86 if (bbcode == "img") {
87 Dialog.doImageBrowser("comment", id);
91 if (bbcode == "imgprv") {
95 insertFormatting(bbcode, id);
98 /* event from comment textarea button popups */
99 /* insert returned bbcode at cursor position or replace selected text */
100 $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
102 var textarea = document.getElementById("comment-edit-text-" +id);
103 var start = textarea.selectionStart;
104 var end = textarea.selectionEnd;
105 textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
106 $(textarea).trigger('change');
109 /* setup onoff widgets */
110 $(".onoff input").each(function() {
112 id = $(this).attr("id");
113 $("#"+id+"_onoff ." + (val == 0 ? "on":"off")).addClass("hidden");
116 $(".onoff > a").click(function(event) {
117 event.preventDefault();
118 var input = $(this).siblings("input");
119 var val = 1-input.val();
120 var id = input.attr("id");
121 $("#"+id+"_onoff ." + (val == 0 ? "on":"off")).addClass("hidden");
122 $("#"+id+"_onoff ." + (val == 1 ? "on":"off")).removeClass("hidden");
127 function close_last_popup_menu() {
128 if (last_popup_menu) {
129 last_popup_menu.hide();
130 last_popup_menu.off('click', function(e) {e.stopPropagation()});
131 last_popup_button.removeClass("selected");
132 last_popup_menu = null;
133 last_popup_button = null;
136 $('a[rel^="#"]').click(function(e) {
138 var parent = $(this).parent();
139 var isSelected = (last_popup_button && parent.attr('id') == last_popup_button.attr('id'));
140 close_last_popup_menu();
144 menu = $($(this).attr('rel'));
147 if (menu.attr('popup') == "false") {
150 parent.toggleClass("selected");
152 if (menu.css("display") == "none") {
153 last_popup_menu = null;
154 last_popup_button = null;
156 last_popup_menu = menu;
157 last_popup_menu.on('click', function(e) {e.stopPropagation()});
158 last_popup_button = parent;
159 $('#nav-notifications-menu').perfectScrollbar('update');
163 $('html').click(function() {
164 close_last_popup_menu();
168 $("a.popupbox").colorbox({
170 'transition' : 'elastic',
173 $("a.ajax-popupbox").colorbox({
174 'transition' : 'elastic',
178 /* notifications template */
179 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
180 var notifications_all = unescape($('<div>').append($("#nav-notifications-see-all").clone()).html()); //outerHtml hack
181 var notifications_mark = unescape($('<div>').append($("#nav-notifications-mark-all").clone()).html()); //outerHtml hack
182 var notifications_empty = unescape($("#nav-notifications-menu").html());
184 /* enable perfect-scrollbars for different elements */
185 $('#nav-notifications-menu, aside').perfectScrollbar();
187 /* nav update event */
188 $('nav').bind('nav-update', function(e, data) {
189 var invalid = data.invalid || 0;
191 window.location.href=window.location.href
194 ['net', 'home', 'intro', 'mail', 'events', 'birthdays', 'notify'].forEach(function(type) {
195 var number = data[type];
198 $('#' + type + '-update').removeClass('show');
200 $('#' + type + '-update').addClass('show');
202 $('#' + type + '-update').text(number);
205 var intro = data['intro'];
207 intro = ''; $('#intro-update-li').removeClass('show')
209 $('#intro-update-li').addClass('show')
212 $('#intro-update-li').html(intro);
214 var mail = data['mail'];
216 mail = ''; $('#mail-update-li').removeClass('show')
218 $('#mail-update-li').addClass('show')
221 $('#mail-update-li').html(mail);
223 $(".sidebar-group-li .notify").removeClass("show");
224 $(data.groups).each(function(key, group) {
226 var gcount = group.count;
227 $(".group-"+gid+" .notify").addClass("show").text(gcount);
230 $(".forum-widget-entry .notify").removeClass("show");
231 $(data.forums).each(function(key, forum) {
233 var fcount = forum.count;
234 $(".forum-"+fid+" .notify").addClass("show").text(fcount);
237 if (data.notifications.length == 0) {
238 $("#nav-notifications-menu").html(notifications_empty);
240 var nnm = $("#nav-notifications-menu");
241 nnm.html(notifications_all + notifications_mark);
243 var lastItemStorageKey = "notification-lastitem:" + localUser;
244 var notification_lastitem = parseInt(localStorage.getItem(lastItemStorageKey));
245 var notification_id = 0;
247 // Insert notifs into the notifications-menu
248 $(data.notifications).each(function(key, notif) {
249 var text = notif.message.format('<span class="contactname">' + notif.name + '</span>');
250 var contact = ('<a href="' + notif.url + '"><span class="contactname">' + notif.name + '</span></a>');
251 var seenclass = (notif.seen == 1) ? "notify-seen" : "notify-unseen";
252 var html = notifications_tpl.format(
253 notif.href, // {0} // link to the source
254 notif.photo, // {1} // photo of the contact
255 text, // {2} // preformatted text (autor + text)
256 notif.date, // {3} // date of notification (time ago)
257 seenclass, // {4} // visited status of the notification
258 new Date(notif.timestamp*1000), // {5} // date of notification
259 notif.url, // {6} // profile url of the contact
260 notif.message.format(contact), // {7} // preformatted html (text including author profile url)
261 '' // {8} // Deprecated
266 // Desktop Notifications
267 $(data.notifications.reverse()).each(function(key, e) {
268 notification_id = parseInt(e.timestamp);
269 if (notification_lastitem !== null && notification_id > notification_lastitem && Number(e.seen) === 0) {
270 if (getNotificationPermission() === "granted") {
271 var notification = new Notification(document.title, {
272 body: decodeHtml(e.message.replace('→ ', '').format(e.name)),
275 notification['url'] = e.href;
276 notification.addEventListener("click", function(ev) {
277 window.location = ev.target.url;
283 notification_lastitem = notification_id;
284 localStorage.setItem(lastItemStorageKey, notification_lastitem)
286 $("img[data-src]", nnm).each(function(i, el) {
287 // Add src attribute for images with a data-src attribute
288 // However, don't bother if the data-src attribute is empty, because
289 // an empty "src" tag for an image will cause some browsers
290 // to prefetch the root page of the Friendica hub, which will
291 // unnecessarily load an entire profile/ or network/ page
292 if ($(el).data("src") != '') {
293 $(el).attr('src', $(el).data("src"));
298 var notif = data['notify'];
300 $("#nav-notifications-linkmenu").addClass("on");
302 $("#nav-notifications-linkmenu").removeClass("on");
305 $(data.sysmsgs.notice).each(function(key, message) {
306 $.jGrowl(message, {sticky: true, theme: 'notice'});
308 $(data.sysmsgs.info).each(function(key, message) {
309 $.jGrowl(message, {sticky: false, theme: 'info', life: 5000});
312 // Update the js scrollbars
313 $('#nav-notifications-menu').perfectScrollbar('update');
316 // Asynchronous calls are deferred until the very end of the page load to ease on slower connections
317 window.addEventListener("load", function(){
319 if (typeof acl !== 'undefined') {
324 // Allow folks to stop the ajax page updates with the pause/break key
325 $(document).keydown(function(event) {
326 if (event.keyCode == '8') {
327 var target = event.target || event.srcElement;
328 if (!/input|textarea/i.test(target.nodeName)) {
333 if (event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
334 event.preventDefault();
335 if (stopped == false) {
340 $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
344 } else if (!totStopped) {
349 // Scroll to the next/previous thread when pressing J and K
350 $(document).keydown(function (event) {
351 var threads = $('.thread_level_1');
352 if ((event.keyCode === 74 || event.keyCode === 75) && !$(event.target).is('textarea, input')) {
353 var scrollTop = $(window).scrollTop();
354 if (event.keyCode === 75) {
355 threads = $(threads.get().reverse());
357 threads.each(function(key, item) {
359 var top = $(item).offset().top - 100;
360 if (event.keyCode === 74) {
361 comparison = top > scrollTop + 1;
362 } else if (event.keyCode === 75) {
363 comparison = top < scrollTop - 1;
366 $('html, body').animate({scrollTop: top}, 200);
373 // Set an event listener for infinite scroll
374 if (typeof infinite_scroll !== 'undefined') {
375 $(window).scroll(function(e) {
376 if ($(document).height() != $(window).height()) {
377 // First method that is expected to work - but has problems with Chrome
378 if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
381 // This method works with Chrome - but seems to be much slower in Firefox
382 if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5)) {
390 function NavUpdate() {
392 var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&f=&uid=' + localUser : '');
393 $.get(pingCmd, function(data) {
395 // send nav-update event
396 $('nav').trigger('nav-update', data.result);
399 ['network', 'profile', 'community', 'notes', 'display', 'contact'].forEach(function (src) {
400 if ($('#live-' + src).length) {
404 if ($('#live-photos').length) {
407 window.location.href = window.location.href;
413 timer = setTimeout(NavUpdate, updateInterval);
416 function updateConvItems(data) {
418 $('.toplevel_item',data).each(function() {
419 var ident = $(this).attr('id');
421 // Add new top-level item.
422 if ($('#' + ident).length == 0 && profile_page == 1) {
423 $('#' + prev).after($(this));
425 // Replace already existing thread.
427 // Find out if the hidden comments are open, so we can keep it that way
428 // if a new comment has been posted
429 var id = $('.hide-comments-total', this).attr('id');
430 if (typeof id != 'undefined') {
431 id = id.split('-')[3];
432 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
435 $('#' + ident).replaceWith($(this));
437 if (typeof id != 'undefined') {
439 showHideComments(id);
446 $('.like-rotator').hide();
449 $('body').css('cursor', 'auto');
451 /* autocomplete @nicknames */
452 $(".comment-edit-form textarea").editor_autocomplete(baseurl+"/acl");
453 /* autocomplete bbcode */
454 $(".comment-edit-form textarea").bbco_autocomplete('bbcode');
457 function liveUpdate(src) {
458 if ((src == null) || stopped || !profile_uid) {
459 $('.like-rotator').hide(); return;
462 if (($('.comment-edit-text-full').length) || in_progress) {
464 clearTimeout(livetime);
466 livetime = setTimeout(function() {liveUpdate(src)}, 5000);
470 if (livetime != null) {
473 prev = 'live-' + src;
477 if ($(document).scrollTop() == 0) {
481 var orgHeight = $("section").height();
483 var udargs = ((netargs.length) ? '/' + netargs : '');
484 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&force=' + ((force_update) ? 1 : 0) + '&item=' + update_item;
486 $.get(update_url,function(data) {
488 force_update = false;
491 $('.wall-item-body', data).imagesLoaded(function() {
492 updateConvItems(data);
494 document.dispatchEvent(new Event('postprocess_liveupdate'));
496 // Update the scroll position.
497 $(window).scrollTop($(window).scrollTop() + $("section").height() - orgHeight);
502 function imgbright(node) {
503 $(node).removeClass("drophide").addClass("drop");
506 function imgdull(node) {
507 $(node).removeClass("drop").addClass("drophide");
510 // Since our ajax calls are asynchronous, we will give a few
511 // seconds for the first ajax call (setting like/dislike), then
512 // run the updater to pick up any changes and display on the page.
513 // The updater will turn any rotators off when it's done.
514 // This function will have returned long before any of these
515 // events have completed and therefore there won't be any
516 // visible feedback that anything changed without all this
517 // trickery. This still could cause confusion if the "like" ajax call
518 // is delayed and NavUpdate runs before it completes.
520 function dolike(ident,verb) {
522 $('#like-rotator-' + ident.toString()).show();
523 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate);
526 update_item = ident.toString();
529 function dosubthread(ident) {
531 $('#like-rotator-' + ident.toString()).show();
532 $.get('subthread/' + ident.toString(), NavUpdate);
536 function dostar(ident) {
537 ident = ident.toString();
538 $('#like-rotator-' + ident).show();
539 $.get('starred/' + ident, function(data) {
540 if (data.match(/1/)) {
541 $('#starred-' + ident).addClass('starred');
542 $('#starred-' + ident).removeClass('unstarred');
543 $('#star-' + ident).addClass('hidden');
544 $('#unstar-' + ident).removeClass('hidden');
546 $('#starred-' + ident).addClass('unstarred');
547 $('#starred-' + ident).removeClass('starred');
548 $('#star-' + ident).removeClass('hidden');
549 $('#unstar-' + ident).addClass('hidden');
551 $('#like-rotator-' + ident).hide();
555 function doignore(ident) {
556 ident = ident.toString();
557 $('#like-rotator-' + ident).show();
558 $.get('ignored/' + ident, function(data) {
559 if (data.match(/1/)) {
560 $('#ignored-' + ident).addClass('ignored');
561 $('#ignored-' + ident).removeClass('unignored');
562 $('#ignore-' + ident).addClass('hidden');
563 $('#unignore-' + ident).removeClass('hidden');
565 $('#ignored-' + ident).addClass('unignored');
566 $('#ignored-' + ident).removeClass('ignored');
567 $('#ignore-' + ident).removeClass('hidden');
568 $('#unignore-' + ident).addClass('hidden');
570 $('#like-rotator-' + ident).hide();
574 function getPosition(e) {
575 var cursor = {x:0, y:0};
577 if (e.pageX || e.pageY) {
581 if (e.clientX || e.clientY) {
582 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
583 cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
584 } else if (e.x || e.y) {
592 var lockvisible = false;
594 function lockview(event,id) {
595 event = event || window.event;
596 cursor = getPosition(event);
601 $.get('lockview/' + id, function(data) {
602 $('#panel').html(data);
603 $('#panel').css({'left': cursor.x + 5 , 'top': cursor.y + 5});
609 function lockviewhide() {
614 function post_comment(id) {
617 $('body').css('cursor', 'wait');
618 $("#comment-preview-inp-" + id).val("0");
621 $("#comment-edit-form-" + id).serialize(),
624 $("#comment-edit-wrapper-" + id).hide();
625 $("#comment-edit-text-" + id).val('');
626 var tarea = document.getElementById("comment-edit-text-" + id);
628 commentClose(tarea,id);
633 timer = setTimeout(NavUpdate,10);
638 window.location.href=data.reload;
646 function preview_comment(id) {
647 $("#comment-preview-inp-" + id).val("1");
648 $("#comment-edit-preview-" + id).show();
651 $("#comment-edit-form-" + id).serialize(),
654 $("#comment-edit-preview-" + id).html(data.preview);
655 $("#comment-edit-preview-" + id + " a").click(function() {return false;});
663 function showHideComments(id) {
664 if ($('#collapsed-comments-' + id).is(':visible')) {
665 $('#collapsed-comments-' + id).slideUp();
666 $('#hide-comments-' + id).hide();
667 $('#hide-comments-total-' + id).show();
669 $('#collapsed-comments-' + id).slideDown();
670 $('#hide-comments-' + id).show();
671 $('#hide-comments-total-' + id).hide();
675 function preview_post() {
676 $("#jot-preview").val("1");
677 $("#jot-preview-content").show();
680 $("#profile-jot-form").serialize(),
683 $("#jot-preview-content").html(data.preview);
684 $("#jot-preview-content" + " a").click(function() {return false;});
685 document.dispatchEvent(new Event('postprocess_liveupdate'));
690 $("#jot-preview").val("0");
695 // unpause auto reloads if they are currently stopped
698 $('#pause').html('');
701 // load more network content (used for infinite scroll)
702 function loadScrollContent() {
703 if (lockLoadContent) {
706 lockLoadContent = true;
708 $("#scroll-loader").fadeIn('normal');
710 // the page number to load is one higher than the actual
712 infinite_scroll.pageno+=1;
714 match = $("span.received").last();
715 if (match.length > 0) {
716 received = match[0].innerHTML;
718 received = "0000-00-00 00:00:00";
721 match = $("span.created").last();
722 if (match.length > 0) {
723 created = match[0].innerHTML;
725 created = "0000-00-00 00:00:00";
728 match = $("span.commented").last();
729 if (match.length > 0) {
730 commented = match[0].innerHTML;
732 commented = "0000-00-00 00:00:00";
735 match = $("span.id").last();
736 if (match.length > 0) {
737 id = match[0].innerHTML;
742 // get the raw content from the next page and insert this content
743 // right before "#conversation-end"
744 $.get(infinite_scroll.reload_uri + '&mode=raw&last_received=' + received + '&last_commented=' + commented + '&last_created=' + created + '&last_id=' + id + '&page=' + infinite_scroll.pageno, function(data) {
745 $("#scroll-loader").hide();
746 if ($(data).length > 0) {
747 $(data).insertBefore('#conversation-end');
748 lockLoadContent = false;
750 $("#scroll-end").fadeIn('normal');
753 document.dispatchEvent(new Event('postprocess_liveupdate'));
757 function bin2hex(s) {
758 // Converts the binary representation of data to hex
761 // discuss at: http://phpjs.org/functions/bin2hex
762 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
763 // + bugfixed by: Onno Marsman
764 // + bugfixed by: Linuxworld
765 // * example 1: bin2hex('Kev');
766 // * returns 1: '4b6576'
767 // * example 2: bin2hex(String.fromCharCode(0x00));
769 var v,i, f = 0, a = [];
773 for (i = 0; i<f; i++) {
774 a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
780 function groupChangeMember(gid, cid, sec_token) {
781 $('body .fakelink').css('cursor', 'wait');
782 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
783 $('#group-update-wrapper').html(data);
784 $('body .fakelink').css('cursor', 'auto');
788 function profChangeMember(gid,cid) {
789 $('body .fakelink').css('cursor', 'wait');
790 $.get('profperm/' + gid + '/' + cid, function(data) {
791 $('#prof-update-wrapper').html(data);
792 $('body .fakelink').css('cursor', 'auto');
796 function contactgroupChangeMember(checkbox, gid, cid) {
798 // checkbox.checked is the checkbox state after the click
799 if (checkbox.checked) {
800 url = 'group/' + gid + '/add/' + cid;
802 url = 'group/' + gid + '/remove/' + cid;
804 $('body').css('cursor', 'wait');
807 // Restores previous state in case of error
808 checkbox.checked = !checkbox.checked;
811 $('body').css('cursor', 'auto');
817 function checkboxhighlight(box) {
818 if ($(box).is(':checked')) {
819 $(box).addClass('checkeditem');
821 $(box).removeClass('checkeditem');
825 function notifyMarkAll() {
826 $.get('notify/mark/all', function(data) {
830 timer = setTimeout(NavUpdate,1000);
836 * sprintf in javascript
837 * "{0} and {1}".format('zero','uno');
839 String.prototype.format = function() {
840 var formatted = this;
841 for (var i = 0; i < arguments.length; i++) {
842 var regexp = new RegExp('\\{'+i+'\\}', 'gi');
843 formatted = formatted.replace(regexp, arguments[i]);
848 Array.prototype.remove = function(item) {
849 to=undefined; from=this.indexOf(item);
850 var rest = this.slice((to || from) + 1 || this.length);
851 this.length = from < 0 ? this.length + from : from;
852 return this.push.apply(this, rest);
855 function previewTheme(elm) {
856 theme = $(elm).val();
857 $.getJSON('pretheme?f=&theme=' + theme,function(data) {
858 $('#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>');
863 // notification permission settings in localstorage
864 // set by settings page
865 function getNotificationPermission() {
866 if (window["Notification"] === undefined) {
870 if (Notification.permission === 'granted') {
871 var val = localStorage.getItem('notification-permissions');
877 return Notification.permission;
882 * Show a dialog loaded from an url
883 * By defaults this load the url in an iframe in colorbox
884 * Themes can overwrite `show()` function to personalize it
891 * @return object colorbox
893 show : function (url) {
894 var size = Dialog._get_size();
895 return $.colorbox({href: url, iframe:true,innerWidth: size.width+'px',innerHeight: size.height+'px'})
899 * Show the Image browser dialog
902 * @param string id (optional)
905 * The name will be used to build the event name
906 * fired by image browser dialog when the user select
907 * an image. The optional id will be passed as argument
908 * to the event handler
910 doImageBrowser : function (name, id) {
911 var url = Dialog._get_url("image",name,id);
912 return Dialog.show(url);
916 * Show the File browser dialog
919 * @param string id (optional)
922 * The name will be used to build the event name
923 * fired by file browser dialog when the user select
924 * a file. The optional id will be passed as argument
925 * to the event handler
927 doFileBrowser : function (name, id) {
928 var url = Dialog._get_url("file",name,id);
929 return Dialog.show(url);
932 _get_url : function(type, name, id) {
934 if (id !== undefined) {
935 hash = hash + "-" + id;
937 return baseurl + "/fbrowser/"+type+"/?mode=minimal#"+hash;
940 _get_size: function() {
942 width: window.innerWidth-50,
943 height: window.innerHeight-100