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 if (document.getElementById(theID).style.display == "block") {
21 document.getElementById(theID).style.display = "none"
23 document.getElementById(theID).style.display = "block"
27 function openMenu(theID) {
28 var el = document.getElementById(theID);
31 el.style.display = "block";
35 function closeMenu(theID) {
36 var el = document.getElementById(theID)
39 el.style.display = "none";
43 function decodeHtml(html) {
44 var txt = document.createElement("textarea");
53 var force_update = false;
56 var totStopped = false;
60 var in_progress = false;
61 var langSelect = false;
62 var commentBusy = false;
63 var last_popup_menu = null;
64 var last_popup_button = null;
65 var lockLoadContent = false;
68 $.ajaxSetup({cache: false});
70 /* setup comment textarea buttons */
71 /* comment textarea buttons needs some "data-*" attributes to work:
72 * data-role="insert-formatting" : to mark the element as a formatting button
73 * data-bbcode="<string>" : name of the bbcode element to insert. insertFormatting() will insert it as "[name][/name]"
74 * data-id="<string>" : id of the comment, used to find other comment-related element, like the textarea
76 $('body').on('click','[data-role="insert-formatting"]', function(e) {
79 var bbcode = o.data('bbcode');
80 var id = o.data('id');
81 if (bbcode == "img") {
82 Dialog.doImageBrowser("comment", id);
86 if (bbcode == "imgprv") {
90 insertFormatting(bbcode, id);
93 /* event from comment textarea button popups */
94 /* insert returned bbcode at cursor position or replace selected text */
95 $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
97 var textarea = document.getElementById("comment-edit-text-" +id);
98 var start = textarea.selectionStart;
99 var end = textarea.selectionEnd;
100 textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
101 $(textarea).trigger('change');
104 /* setup onoff widgets */
105 $(".onoff input").each(function() {
107 id = $(this).attr("id");
108 $("#"+id+"_onoff ." + (val == 0 ? "on":"off")).addClass("hidden");
111 $(".onoff > a").click(function(event) {
112 event.preventDefault();
113 var input = $(this).siblings("input");
114 var val = 1-input.val();
115 var id = input.attr("id");
116 $("#"+id+"_onoff ." + (val == 0 ? "on":"off")).addClass("hidden");
117 $("#"+id+"_onoff ." + (val == 1 ? "on":"off")).removeClass("hidden");
122 function close_last_popup_menu() {
123 if (last_popup_menu) {
124 last_popup_menu.hide();
125 last_popup_menu.off('click', function(e) {e.stopPropagation()});
126 last_popup_button.removeClass("selected");
127 last_popup_menu = null;
128 last_popup_button = null;
131 $('a[rel^="#"]').click(function(e) {
133 var parent = $(this).parent();
134 var isSelected = (last_popup_button && parent.attr('id') == last_popup_button.attr('id'));
135 close_last_popup_menu();
139 menu = $($(this).attr('rel'));
142 if (menu.attr('popup') == "false") {
145 parent.toggleClass("selected");
147 if (menu.css("display") == "none") {
148 last_popup_menu = null;
149 last_popup_button = null;
151 last_popup_menu = menu;
152 last_popup_menu.on('click', function(e) {e.stopPropagation()});
153 last_popup_button = parent;
154 $('#nav-notifications-menu').perfectScrollbar('update');
158 $('html').click(function() {
159 close_last_popup_menu();
163 $("a.popupbox").colorbox({
165 'transition' : 'elastic',
168 $("a.ajax-popupbox").colorbox({
169 'transition' : 'elastic',
173 /* notifications template */
174 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
175 var notifications_all = unescape($('<div>').append($("#nav-notifications-see-all").clone()).html()); //outerHtml hack
176 var notifications_mark = unescape($('<div>').append($("#nav-notifications-mark-all").clone()).html()); //outerHtml hack
177 var notifications_empty = unescape($("#nav-notifications-menu").html());
179 /* enable perfect-scrollbars for different elements */
180 $('#nav-notifications-menu, aside').perfectScrollbar();
182 /* nav update event */
183 $('nav').bind('nav-update', function(e, data) {
184 var invalid = data.invalid || 0;
186 window.location.href=window.location.href
189 ['net', 'home', 'intro', 'mail', 'events', 'birthdays', 'notify'].forEach(function(type) {
190 var number = data[type];
193 $('#' + type + '-update').removeClass('show');
195 $('#' + type + '-update').addClass('show');
197 $('#' + type + '-update').text(number);
200 var intro = data['intro'];
202 intro = ''; $('#intro-update-li').removeClass('show')
204 $('#intro-update-li').addClass('show')
207 $('#intro-update-li').html(intro);
209 var mail = data['mail'];
211 mail = ''; $('#mail-update-li').removeClass('show')
213 $('#mail-update-li').addClass('show')
216 $('#mail-update-li').html(mail);
218 $(".sidebar-group-li .notify").removeClass("show");
219 $(data.groups).each(function(key, group) {
221 var gcount = group.count;
222 $(".group-"+gid+" .notify").addClass("show").text(gcount);
225 $(".forum-widget-entry .notify").removeClass("show");
226 $(data.forums).each(function(key, forum) {
228 var fcount = forum.count;
229 $(".forum-"+fid+" .notify").addClass("show").text(fcount);
232 if (data.notifications.length == 0) {
233 $("#nav-notifications-menu").html(notifications_empty);
235 var nnm = $("#nav-notifications-menu");
236 nnm.html(notifications_all + notifications_mark);
238 var lastItemStorageKey = "notification-lastitem:" + localUser;
239 var notification_lastitem = parseInt(localStorage.getItem(lastItemStorageKey));
240 var notification_id = 0;
242 // Insert notifs into the notifications-menu
243 $(data.notifications).each(function(key, notif) {
244 var text = notif.message.format('<span class="contactname">' + notif.name + '</span>');
245 var contact = ('<a href="' + notif.url + '"><span class="contactname">' + notif.name + '</span></a>');
246 var seenclass = (notif.seen == 1) ? "notify-seen" : "notify-unseen";
247 var html = notifications_tpl.format(
248 notif.href, // {0} // link to the source
249 notif.photo, // {1} // photo of the contact
250 text, // {2} // preformatted text (autor + text)
251 notif.date, // {3} // date of notification (time ago)
252 seenclass, // {4} // visited status of the notification
253 new Date(notif.timestamp*1000), // {5} // date of notification
254 notif.url, // {6} // profile url of the contact
255 notif.message.format(contact), // {7} // preformatted html (text including author profile url)
256 '' // {8} // Deprecated
261 // Desktop Notifications
262 $(data.notifications.reverse()).each(function(key, e) {
263 notification_id = parseInt(e.timestamp);
264 if (notification_lastitem !== null && notification_id > notification_lastitem && Number(e.seen) === 0) {
265 if (getNotificationPermission() === "granted") {
266 var notification = new Notification(document.title, {
267 body: decodeHtml(e.message.replace('→ ', '').format(e.name)),
270 notification['url'] = e.href;
271 notification.addEventListener("click", function(ev) {
272 window.location = ev.target.url;
278 notification_lastitem = notification_id;
279 localStorage.setItem(lastItemStorageKey, notification_lastitem)
281 $("img[data-src]", nnm).each(function(i, el) {
282 // Add src attribute for images with a data-src attribute
283 // However, don't bother if the data-src attribute is empty, because
284 // an empty "src" tag for an image will cause some browsers
285 // to prefetch the root page of the Friendica hub, which will
286 // unnecessarily load an entire profile/ or network/ page
287 if ($(el).data("src") != '') {
288 $(el).attr('src', $(el).data("src"));
293 var notif = data['notify'];
295 $("#nav-notifications-linkmenu").addClass("on");
297 $("#nav-notifications-linkmenu").removeClass("on");
300 $(data.sysmsgs.notice).each(function(key, message) {
301 $.jGrowl(message, {sticky: true, theme: 'notice'});
303 $(data.sysmsgs.info).each(function(key, message) {
304 $.jGrowl(message, {sticky: false, theme: 'info', life: 5000});
307 // Update the js scrollbars
308 $('#nav-notifications-menu').perfectScrollbar('update');
311 // Asynchronous calls are deferred until the very end of the page load to ease on slower connections
312 window.addEventListener("load", function(){
314 if (typeof acl !== 'undefined') {
319 // Allow folks to stop the ajax page updates with the pause/break key
320 $(document).keydown(function(event) {
321 if (event.keyCode == '8') {
322 var target = event.target || event.srcElement;
323 if (!/input|textarea/i.test(target.nodeName)) {
328 if (event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
329 event.preventDefault();
330 if (stopped == false) {
335 $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
339 } else if (!totStopped) {
344 // Scroll to the next/previous thread when pressing J and K
345 $(document).keydown(function (event) {
346 var threads = $('.thread_level_1');
347 if ((event.keyCode === 74 || event.keyCode === 75) && !$(event.target).is('textarea, input')) {
348 var scrollTop = $(window).scrollTop();
349 if (event.keyCode === 75) {
350 threads = $(threads.get().reverse());
352 threads.each(function(key, item) {
354 var top = $(item).offset().top - 100;
355 if (event.keyCode === 74) {
356 comparison = top > scrollTop + 1;
357 } else if (event.keyCode === 75) {
358 comparison = top < scrollTop - 1;
361 $('html, body').animate({scrollTop: top}, 200);
368 // Set an event listener for infinite scroll
369 if (typeof infinite_scroll !== 'undefined') {
370 $(window).scroll(function(e) {
371 if ($(document).height() != $(window).height()) {
372 // First method that is expected to work - but has problems with Chrome
373 if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
376 // This method works with Chrome - but seems to be much slower in Firefox
377 if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5)) {
385 function NavUpdate() {
387 var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&f=&uid=' + localUser : '');
388 $.get(pingCmd, function(data) {
390 // send nav-update event
391 $('nav').trigger('nav-update', data.result);
394 ['network', 'profile', 'community', 'notes', 'display', 'contact'].forEach(function (src) {
395 if ($('#live-' + src).length) {
399 if ($('#live-photos').length) {
402 window.location.href = window.location.href;
408 timer = setTimeout(NavUpdate, updateInterval);
411 function updateConvItems(data) {
413 $('.toplevel_item',data).each(function() {
414 var ident = $(this).attr('id');
416 // Add new top-level item.
417 if ($('#' + ident).length == 0 && profile_page == 1) {
418 $('#' + prev).after($(this));
420 // Replace already existing thread.
422 // Find out if the hidden comments are open, so we can keep it that way
423 // if a new comment has been posted
424 var id = $('.hide-comments-total', this).attr('id');
425 if (typeof id != 'undefined') {
426 id = id.split('-')[3];
427 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
430 $('#' + ident).replaceWith($(this));
432 if (typeof id != 'undefined') {
434 showHideComments(id);
441 $('.like-rotator').hide();
444 $('body').css('cursor', 'auto');
446 /* autocomplete @nicknames */
447 $(".comment-edit-form textarea").editor_autocomplete(baseurl+"/acl");
448 /* autocomplete bbcode */
449 $(".comment-edit-form textarea").bbco_autocomplete('bbcode');
452 function liveUpdate(src) {
453 if ((src == null) || stopped || !profile_uid) {
454 $('.like-rotator').hide(); return;
457 if (($('.comment-edit-text-full').length) || in_progress) {
459 clearTimeout(livetime);
461 livetime = setTimeout(function() {liveUpdate(src)}, 5000);
465 if (livetime != null) {
468 prev = 'live-' + src;
472 if ($(document).scrollTop() == 0) {
476 var orgHeight = $("section").height();
478 var udargs = ((netargs.length) ? '/' + netargs : '');
479 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&force=' + ((force_update) ? 1 : 0) + '&item=' + update_item;
481 $.get(update_url,function(data) {
483 force_update = false;
486 $('.wall-item-body', data).imagesLoaded(function() {
487 updateConvItems(data);
489 document.dispatchEvent(new Event('postprocess_liveupdate'));
491 // Update the scroll position.
492 $(window).scrollTop($(window).scrollTop() + $("section").height() - orgHeight);
497 function imgbright(node) {
498 $(node).removeClass("drophide").addClass("drop");
501 function imgdull(node) {
502 $(node).removeClass("drop").addClass("drophide");
505 // Since our ajax calls are asynchronous, we will give a few
506 // seconds for the first ajax call (setting like/dislike), then
507 // run the updater to pick up any changes and display on the page.
508 // The updater will turn any rotators off when it's done.
509 // This function will have returned long before any of these
510 // events have completed and therefore there won't be any
511 // visible feedback that anything changed without all this
512 // trickery. This still could cause confusion if the "like" ajax call
513 // is delayed and NavUpdate runs before it completes.
515 function dolike(ident,verb) {
517 $('#like-rotator-' + ident.toString()).show();
518 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate);
521 update_item = ident.toString();
524 function dosubthread(ident) {
526 $('#like-rotator-' + ident.toString()).show();
527 $.get('subthread/' + ident.toString(), NavUpdate);
531 function dostar(ident) {
532 ident = ident.toString();
533 $('#like-rotator-' + ident).show();
534 $.get('starred/' + ident, function(data) {
535 if (data.match(/1/)) {
536 $('#starred-' + ident).addClass('starred');
537 $('#starred-' + ident).removeClass('unstarred');
538 $('#star-' + ident).addClass('hidden');
539 $('#unstar-' + ident).removeClass('hidden');
541 $('#starred-' + ident).addClass('unstarred');
542 $('#starred-' + ident).removeClass('starred');
543 $('#star-' + ident).removeClass('hidden');
544 $('#unstar-' + ident).addClass('hidden');
546 $('#like-rotator-' + ident).hide();
550 function doignore(ident) {
551 ident = ident.toString();
552 $('#like-rotator-' + ident).show();
553 $.get('ignored/' + ident, function(data) {
554 if (data.match(/1/)) {
555 $('#ignored-' + ident).addClass('ignored');
556 $('#ignored-' + ident).removeClass('unignored');
557 $('#ignore-' + ident).addClass('hidden');
558 $('#unignore-' + ident).removeClass('hidden');
560 $('#ignored-' + ident).addClass('unignored');
561 $('#ignored-' + ident).removeClass('ignored');
562 $('#ignore-' + ident).removeClass('hidden');
563 $('#unignore-' + ident).addClass('hidden');
565 $('#like-rotator-' + ident).hide();
569 function getPosition(e) {
570 var cursor = {x:0, y:0};
572 if (e.pageX || e.pageY) {
576 if (e.clientX || e.clientY) {
577 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
578 cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
579 } else if (e.x || e.y) {
587 var lockvisible = false;
589 function lockview(event,id) {
590 event = event || window.event;
591 cursor = getPosition(event);
596 $.get('lockview/' + id, function(data) {
597 $('#panel').html(data);
598 $('#panel').css({'left': cursor.x + 5 , 'top': cursor.y + 5});
604 function lockviewhide() {
609 function post_comment(id) {
612 $('body').css('cursor', 'wait');
613 $("#comment-preview-inp-" + id).val("0");
616 $("#comment-edit-form-" + id).serialize(),
619 $("#comment-edit-wrapper-" + id).hide();
620 $("#comment-edit-text-" + id).val('');
621 var tarea = document.getElementById("comment-edit-text-" + id);
623 commentClose(tarea,id);
628 timer = setTimeout(NavUpdate,10);
633 window.location.href=data.reload;
641 function preview_comment(id) {
642 $("#comment-preview-inp-" + id).val("1");
643 $("#comment-edit-preview-" + id).show();
646 $("#comment-edit-form-" + id).serialize(),
649 $("#comment-edit-preview-" + id).html(data.preview);
650 $("#comment-edit-preview-" + id + " a").click(function() {return false;});
658 function showHideComments(id) {
659 if ($("#collapsed-comments-" + id).is(":visible")) {
660 $("#collapsed-comments-" + id).hide();
661 $("#hide-comments-" + id).html(window.showMore);
663 $("#collapsed-comments-" + id).show();
664 $("#hide-comments-" + id).html(window.showFewer);
668 function preview_post() {
669 $("#jot-preview").val("1");
670 $("#jot-preview-content").show();
673 $("#profile-jot-form").serialize(),
676 $("#jot-preview-content").html(data.preview);
677 $("#jot-preview-content" + " a").click(function() {return false;});
678 document.dispatchEvent(new Event('postprocess_liveupdate'));
683 $("#jot-preview").val("0");
688 // unpause auto reloads if they are currently stopped
691 $('#pause').html('');
694 // load more network content (used for infinite scroll)
695 function loadScrollContent() {
696 if (lockLoadContent) {
699 lockLoadContent = true;
701 $("#scroll-loader").fadeIn('normal');
703 // the page number to load is one higher than the actual
705 infinite_scroll.pageno+=1;
707 match = $("span.received").last();
708 if (match.length > 0) {
709 received = match[0].innerHTML;
711 received = "0000-00-00 00:00:00";
714 match = $("span.created").last();
715 if (match.length > 0) {
716 created = match[0].innerHTML;
718 created = "0000-00-00 00:00:00";
721 match = $("span.commented").last();
722 if (match.length > 0) {
723 commented = match[0].innerHTML;
725 commented = "0000-00-00 00:00:00";
728 match = $("span.id").last();
729 if (match.length > 0) {
730 id = match[0].innerHTML;
735 // get the raw content from the next page and insert this content
736 // right before "#conversation-end"
737 $.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) {
738 $("#scroll-loader").hide();
739 if ($(data).length > 0) {
740 $(data).insertBefore('#conversation-end');
741 lockLoadContent = false;
743 $("#scroll-end").fadeIn('normal');
746 document.dispatchEvent(new Event('postprocess_liveupdate'));
750 function bin2hex(s) {
751 // Converts the binary representation of data to hex
754 // discuss at: http://phpjs.org/functions/bin2hex
755 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
756 // + bugfixed by: Onno Marsman
757 // + bugfixed by: Linuxworld
758 // * example 1: bin2hex('Kev');
759 // * returns 1: '4b6576'
760 // * example 2: bin2hex(String.fromCharCode(0x00));
762 var v,i, f = 0, a = [];
766 for (i = 0; i<f; i++) {
767 a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
773 function groupChangeMember(gid, cid, sec_token) {
774 $('body .fakelink').css('cursor', 'wait');
775 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
776 $('#group-update-wrapper').html(data);
777 $('body .fakelink').css('cursor', 'auto');
781 function profChangeMember(gid,cid) {
782 $('body .fakelink').css('cursor', 'wait');
783 $.get('profperm/' + gid + '/' + cid, function(data) {
784 $('#prof-update-wrapper').html(data);
785 $('body .fakelink').css('cursor', 'auto');
789 function contactgroupChangeMember(gid,cid) {
790 $('body').css('cursor', 'wait');
791 $.get('contactgroup/' + gid + '/' + cid, function(data) {
792 $('body').css('cursor', 'auto');
796 function checkboxhighlight(box) {
797 if ($(box).is(':checked')) {
798 $(box).addClass('checkeditem');
800 $(box).removeClass('checkeditem');
804 function notifyMarkAll() {
805 $.get('notify/mark/all', function(data) {
809 timer = setTimeout(NavUpdate,1000);
815 * sprintf in javascript
816 * "{0} and {1}".format('zero','uno');
818 String.prototype.format = function() {
819 var formatted = this;
820 for (var i = 0; i < arguments.length; i++) {
821 var regexp = new RegExp('\\{'+i+'\\}', 'gi');
822 formatted = formatted.replace(regexp, arguments[i]);
827 Array.prototype.remove = function(item) {
828 to=undefined; from=this.indexOf(item);
829 var rest = this.slice((to || from) + 1 || this.length);
830 this.length = from < 0 ? this.length + from : from;
831 return this.push.apply(this, rest);
834 function previewTheme(elm) {
835 theme = $(elm).val();
836 $.getJSON('pretheme?f=&theme=' + theme,function(data) {
837 $('#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>');
842 // notification permission settings in localstorage
843 // set by settings page
844 function getNotificationPermission() {
845 if (window["Notification"] === undefined) {
849 if (Notification.permission === 'granted') {
850 var val = localStorage.getItem('notification-permissions');
856 return Notification.permission;
861 * Show a dialog loaded from an url
862 * By defaults this load the url in an iframe in colorbox
863 * Themes can overwrite `show()` function to personalize it
870 * @return object colorbox
872 show : function (url) {
873 var size = Dialog._get_size();
874 return $.colorbox({href: url, iframe:true,innerWidth: size.width+'px',innerHeight: size.height+'px'})
878 * Show the Image browser dialog
881 * @param string id (optional)
884 * The name will be used to build the event name
885 * fired by image browser dialog when the user select
886 * an image. The optional id will be passed as argument
887 * to the event handler
889 doImageBrowser : function (name, id) {
890 var url = Dialog._get_url("image",name,id);
891 return Dialog.show(url);
895 * Show the File browser dialog
898 * @param string id (optional)
901 * The name will be used to build the event name
902 * fired by file browser dialog when the user select
903 * a file. The optional id will be passed as argument
904 * to the event handler
906 doFileBrowser : function (name, id) {
907 var url = Dialog._get_url("file",name,id);
908 return Dialog.show(url);
911 _get_url : function(type, name, id) {
913 if (id !== undefined) {
914 hash = hash + "-" + id;
916 return baseurl + "/fbrowser/"+type+"/?mode=minimal#"+hash;
919 _get_size: function() {
921 width: window.innerWidth-50,
922 height: window.innerHeight-100