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 tooltips *//*
68 $("a,.tt").each(function(){
71 if (e.hasClass("tttop")) pos="top";
72 if (e.hasClass("ttbottom")) pos="bottom";
73 if (e.hasClass("ttleft")) pos="left";
74 if (e.hasClass("ttright")) pos="right";
75 e.tipTip({defaultPosition: pos, edgeOffset: 8});
78 /* setup comment textarea buttons */
79 /* comment textarea buttons needs some "data-*" attributes to work:
80 * data-role="insert-formatting" : to mark the element as a formatting button
81 * data-bbcode="<string>" : name of the bbcode element to insert. insertFormatting() will insert it as "[name][/name]"
82 * data-id="<string>" : id of the comment, used to find other comment-related element, like the textarea
84 $('body').on('click','[data-role="insert-formatting"]', function(e) {
87 var bbcode = o.data('bbcode');
88 var id = o.data('id');
90 Dialog.doImageBrowser("comment", id);
93 insertFormatting(bbcode, id);
96 /* event from comment textarea button popups */
97 /* insert returned bbcode at cursor position or replace selected text */
98 $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
100 var textarea = document.getElementById("comment-edit-text-" +id);
101 var start = textarea.selectionStart;
102 var end = textarea.selectionEnd;
103 textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
108 /* setup onoff widgets */
109 $(".onoff input").each(function(){
111 id = $(this).attr("id");
112 $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
115 $(".onoff > a").click(function(event){
116 event.preventDefault();
117 var input = $(this).siblings("input");
118 var val = 1-input.val();
119 var id = input.attr("id");
120 $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
121 $("#"+id+"_onoff ."+ (val==1?"on":"off")).removeClass("hidden");
126 function close_last_popup_menu() {
127 if(last_popup_menu) {
128 last_popup_menu.hide();
129 last_popup_menu.off('click', function(e) {e.stopPropagation()});
130 last_popup_button.removeClass("selected");
131 last_popup_menu = null;
132 last_popup_button = null;
135 $('a[rel^=#]').click(function(e){
137 var parent = $(this).parent();
138 var isSelected = (last_popup_button && parent.attr('id') == last_popup_button.attr('id'));
139 close_last_popup_menu();
140 if(isSelected) return false;
141 menu = $( $(this).attr('rel') );
144 if (menu.attr('popup')=="false") return 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;
185 if(invalid == 1) { window.location.href=window.location.href }
187 ['net', 'home', 'intro', 'mail', 'events', 'birthdays', 'notify'].forEach(function(type) {
188 var number = data[type];
191 $('#' + type + '-update').removeClass('show');
193 $('#' + type + '-update').addClass('show');
195 $('#' + type + '-update').text(number);
198 var intro = data['intro'];
199 if(intro == 0) { intro = ''; $('#intro-update-li').removeClass('show') } else { $('#intro-update-li').addClass('show') }
200 $('#intro-update-li').html(intro);
202 var mail = data['mail'];
203 if(mail == 0) { mail = ''; $('#mail-update-li').removeClass('show') } else { $('#mail-update-li').addClass('show') }
204 $('#mail-update-li').html(mail);
206 $(".sidebar-group-li .notify").removeClass("show");
207 $(data.groups).each(function(key, group) {
209 var gcount = group.count;
210 $(".group-"+gid+" .notify").addClass("show").text(gcount);
213 $(".forum-widget-entry .notify").removeClass("show");
214 $(data.forums).each(function(key, forum) {
216 var fcount = forum.count;
217 $(".forum-"+fid+" .notify").addClass("show").text(fcount);
220 if (data.notifications.length == 0) {
221 $("#nav-notifications-menu").html(notifications_empty);
223 var nnm = $("#nav-notifications-menu");
224 nnm.html(notifications_all + notifications_mark);
226 var notification_lastitem = parseInt(localStorage.getItem("notification-lastitem"));
227 var notification_id = 0;
228 $(data.notifications).each(function(key, notif){
229 var text = notif.message.format('<span class="contactname">' + notif.name + '</span>');
230 var contact = ('<a href="' + notif.url + '"><span class="contactname">' + notif.name + '</span></a>');
231 var seenclass = (notif.seen == 1) ? "notify-seen" : "notify-unseen";
232 var html = notifications_tpl.format(
233 notif.href, // {0} // link to the source
234 notif.photo, // {1} // photo of the contact
235 text, // {2} // preformatted text (autor + text)
236 notif.date, // {3} // date of notification (time ago)
237 seenclass, // {4} // visited status of the notification
238 new Date(notif.timestamp*1000), // {5} // date of notification
239 notif.url, // {6} // profile url of the contact
240 notif.message.format(contact), // {7} // preformatted html (text including author profile url)
241 '' // {8} // Deprecated
245 $(data.notifications.reverse()).each(function(key, e){
246 notification_id = parseInt(e.timestamp);
247 if (notification_lastitem !== null && notification_id > notification_lastitem) {
248 if (getNotificationPermission() === "granted") {
249 var notification = new Notification(document.title, {
250 body: decodeHtml(e.message.replace('→ ', '').format(e.name)),
253 notification['url'] = e.href;
254 notification.addEventListener("click", function(ev){
255 window.location = ev.target.url;
261 notification_lastitem = notification_id;
262 localStorage.setItem("notification-lastitem", notification_lastitem)
264 $("img[data-src]", nnm).each(function(i, el){
265 // Add src attribute for images with a data-src attribute
266 // However, don't bother if the data-src attribute is empty, because
267 // an empty "src" tag for an image will cause some browsers
268 // to prefetch the root page of the Friendica hub, which will
269 // unnecessarily load an entire profile/ or network/ page
270 if($(el).data("src") != '') $(el).attr('src', $(el).data("src"));
274 var notif = data['notify'];
276 $("#nav-notifications-linkmenu").addClass("on");
278 $("#nav-notifications-linkmenu").removeClass("on");
281 $(data.sysmsgs.notice).each(function(key, message){
282 $.jGrowl(message, {sticky: true, theme: 'notice'});
284 $(data.sysmsgs.info).each(function(key, message){
285 $.jGrowl(message, {sticky: false, theme: 'info', life: 5000});
288 /* update the js scrollbars */
289 $('#nav-notifications-menu').perfectScrollbar('update');
294 // Allow folks to stop the ajax page updates with the pause/break key
295 $(document).keydown(function(event) {
296 if(event.keyCode == '8') {
297 var target = event.target || event.srcElement;
298 if (!/input|textarea/i.test(target.nodeName)) {
302 if(event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
303 event.preventDefault();
304 if(stopped == false) {
309 $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
320 // Set an event listener for infinite scroll
321 if(typeof infinite_scroll !== 'undefined') {
322 $(window).scroll(function(e){
323 if ($(document).height() != $(window).height()) {
324 // First method that is expected to work - but has problems with Chrome
325 if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
328 // This method works with Chrome - but seems to be much slower in Firefox
329 if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5))
338 function NavUpdate() {
341 var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&f=&uid=' + localUser : '');
342 $.get(pingCmd, function(data) {
344 // send nav-update event
345 $('nav').trigger('nav-update', data.result);
348 ['network', 'profile', 'community', 'notes', 'display'].forEach(function (src) {
349 if ($('#live-' + src).length) {
353 if ($('#live-photos').length) {
356 window.location.href = window.location.href;
362 timer = setTimeout(NavUpdate, updateInterval);
365 function liveUpdate(src) {
366 if((src == null) || (stopped) || (! profile_uid)) { $('.like-rotator').hide(); return; }
367 if(($('.comment-edit-text-full').length) || (in_progress)) {
369 clearTimeout(livetime);
371 livetime = setTimeout(function() {liveUpdate(src)}, 5000);
377 prev = 'live-' + src;
381 if ($(document).scrollTop() == 0)
384 var udargs = ((netargs.length) ? '/' + netargs : '');
385 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&force=' + ((force_update) ? 1 : 0);
387 $.get(update_url,function(data) {
389 force_update = false;
390 // $('.collapsed-comments',data).each(function() {
391 // var ident = $(this).attr('id');
392 // var is_hidden = $('#' + ident).is(':hidden');
393 // if($('#' + ident).length) {
394 // $('#' + ident).replaceWith($(this));
396 // $('#' + ident).hide();
401 $('.toplevel_item',data).each(function() {
402 var ident = $(this).attr('id');
404 if($('#' + ident).length == 0 && profile_page == 1) {
405 $('img',this).each(function() {
406 $(this).attr('src',$(this).attr('dst'));
408 $('#' + prev).after($(this));
411 // Find out if the hidden comments are open, so we can keep it that way
412 // if a new comment has been posted
413 var id = $('.hide-comments-total', this).attr('id');
414 if(typeof id != 'undefined') {
415 id = id.split('-')[3];
416 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
419 $('img',this).each(function() {
420 $(this).attr('src',$(this).attr('dst'));
422 //vScroll = $(document).scrollTop();
423 $('html').height($('html').height());
424 $('#' + ident).replaceWith($(this));
426 if(typeof id != 'undefined') {
427 if(commentsOpen) showHideComments(id);
429 $('html').height('auto');
430 //$(document).scrollTop(vScroll);
435 // reset vars for inserting individual items
437 /* prev = 'live-' + src;
439 $('.wall-item-outside-wrapper',data).each(function() {
440 var ident = $(this).attr('id');
442 if($('#' + ident).length == 0 && prev != 'live-' + src) {
443 $('img',this).each(function() {
444 $(this).attr('src',$(this).attr('dst'));
446 $('#' + prev).after($(this));
449 $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago'));
450 if($('#' + ident + ' ' + '.comment-edit-text-empty').length)
451 $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
452 $('#' + ident + ' ' + '.hide-comments-total').replaceWith($(this).find('.hide-comments-total'));
453 $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
454 $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
455 $('#' + ident + ' ' + '.my-comment-photo').each(function() {
456 $(this).attr('src',$(this).attr('dst'));
462 $('.like-rotator').hide();
465 $('body').css('cursor', 'auto');
467 /* autocomplete @nicknames */
468 $(".comment-edit-form textarea").editor_autocomplete(baseurl+"/acl");
469 /* autocomplete bbcode */
470 $(".comment-edit-form textarea").bbco_autocomplete('bbcode');
472 // setup videos, since VideoJS won't take care of any loaded via AJAX
473 if(typeof videojs != 'undefined') videojs.autoSetup();
477 function imgbright(node) {
478 $(node).removeClass("drophide").addClass("drop");
481 function imgdull(node) {
482 $(node).removeClass("drop").addClass("drophide");
485 // Since our ajax calls are asynchronous, we will give a few
486 // seconds for the first ajax call (setting like/dislike), then
487 // run the updater to pick up any changes and display on the page.
488 // The updater will turn any rotators off when it's done.
489 // This function will have returned long before any of these
490 // events have completed and therefore there won't be any
491 // visible feedback that anything changed without all this
492 // trickery. This still could cause confusion if the "like" ajax call
493 // is delayed and NavUpdate runs before it completes.
495 function dolike(ident,verb) {
497 $('#like-rotator-' + ident.toString()).show();
498 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate );
503 function dosubthread(ident) {
505 $('#like-rotator-' + ident.toString()).show();
506 $.get('subthread/' + ident.toString(), NavUpdate );
511 function dostar(ident) {
512 ident = ident.toString();
513 $('#like-rotator-' + ident).show();
514 $.get('starred/' + ident, function(data) {
515 if(data.match(/1/)) {
516 $('#starred-' + ident).addClass('starred');
517 $('#starred-' + ident).removeClass('unstarred');
518 $('#star-' + ident).addClass('hidden');
519 $('#unstar-' + ident).removeClass('hidden');
522 $('#starred-' + ident).addClass('unstarred');
523 $('#starred-' + ident).removeClass('starred');
524 $('#star-' + ident).removeClass('hidden');
525 $('#unstar-' + ident).addClass('hidden');
527 $('#like-rotator-' + ident).hide();
531 function doignore(ident) {
532 ident = ident.toString();
533 $('#like-rotator-' + ident).show();
534 $.get('ignored/' + ident, function(data) {
535 if(data.match(/1/)) {
536 $('#ignored-' + ident).addClass('ignored');
537 $('#ignored-' + ident).removeClass('unignored');
538 $('#ignore-' + ident).addClass('hidden');
539 $('#unignore-' + ident).removeClass('hidden');
542 $('#ignored-' + ident).addClass('unignored');
543 $('#ignored-' + ident).removeClass('ignored');
544 $('#ignore-' + ident).removeClass('hidden');
545 $('#unignore-' + ident).addClass('hidden');
547 $('#like-rotator-' + ident).hide();
551 function getPosition(e) {
552 var cursor = {x:0, y:0};
553 if ( e.pageX || e.pageY ) {
558 if( e.clientX || e.clientY ) {
559 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
560 cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
572 var lockvisible = false;
574 function lockview(event,id) {
575 event = event || window.event;
576 cursor = getPosition(event);
582 $.get('lockview/' + id, function(data) {
583 $('#panel').html(data);
584 $('#panel').css({ 'left': cursor.x + 5 , 'top': cursor.y + 5});
590 function lockviewhide() {
595 function post_comment(id) {
598 $('body').css('cursor', 'wait');
599 $("#comment-preview-inp-" + id).val("0");
602 $("#comment-edit-form-" + id).serialize(),
605 $("#comment-edit-wrapper-" + id).hide();
606 $("#comment-edit-text-" + id).val('');
607 var tarea = document.getElementById("comment-edit-text-" + id);
609 commentClose(tarea,id);
610 if(timer) clearTimeout(timer);
611 timer = setTimeout(NavUpdate,10);
615 window.location.href=data.reload;
624 function preview_comment(id) {
625 $("#comment-preview-inp-" + id).val("1");
626 $("#comment-edit-preview-" + id).show();
629 $("#comment-edit-form-" + id).serialize(),
632 $("#comment-edit-preview-" + id).html(data.preview);
633 $("#comment-edit-preview-" + id + " a").click(function() { return false; });
643 function showHideComments(id) {
644 if( $("#collapsed-comments-" + id).is(":visible")) {
645 $("#collapsed-comments-" + id).hide();
646 $("#hide-comments-" + id).html(window.showMore);
649 $("#collapsed-comments-" + id).show();
650 $("#hide-comments-" + id).html(window.showFewer);
656 function preview_post() {
657 $("#jot-preview").val("1");
658 $("#jot-preview-content").show();
661 $("#profile-jot-form").serialize(),
664 $("#jot-preview-content").html(data.preview);
665 $("#jot-preview-content" + " a").click(function() { return false; });
670 $("#jot-preview").val("0");
676 // unpause auto reloads if they are currently stopped
679 $('#pause').html('');
682 // load more network content (used for infinite scroll)
683 function loadScrollContent() {
684 if (lockLoadContent) return;
685 lockLoadContent = true;
687 $("#scroll-loader").fadeIn('normal');
689 // the page number to load is one higher than the actual
691 infinite_scroll.pageno+=1;
693 // get the raw content from the next page and insert this content
694 // right before "#conversation-end"
695 $.get('network?mode=raw' + infinite_scroll.reload_uri + '&page=' + infinite_scroll.pageno, function(data) {
696 $("#scroll-loader").hide();
697 if ($(data).length > 0) {
698 $(data).insertBefore('#conversation-end');
699 lockLoadContent = false;
701 $("#scroll-end").fadeIn('normal');
707 // Converts the binary representation of data to hex
710 // discuss at: http://phpjs.org/functions/bin2hex
711 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
712 // + bugfixed by: Onno Marsman
713 // + bugfixed by: Linuxworld
714 // * example 1: bin2hex('Kev');
715 // * returns 1: '4b6576'
716 // * example 2: bin2hex(String.fromCharCode(0x00));
718 var v,i, f = 0, a = [];
722 for (i = 0; i<f; i++) {
723 a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
729 function groupChangeMember(gid, cid, sec_token) {
730 $('body .fakelink').css('cursor', 'wait');
731 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
732 $('#group-update-wrapper').html(data);
733 $('body .fakelink').css('cursor', 'auto');
737 function profChangeMember(gid,cid) {
738 $('body .fakelink').css('cursor', 'wait');
739 $.get('profperm/' + gid + '/' + cid, function(data) {
740 $('#prof-update-wrapper').html(data);
741 $('body .fakelink').css('cursor', 'auto');
745 function contactgroupChangeMember(gid,cid) {
746 $('body').css('cursor', 'wait');
747 $.get('contactgroup/' + gid + '/' + cid, function(data) {
748 $('body').css('cursor', 'auto');
753 function checkboxhighlight(box) {
754 if($(box).is(':checked')) {
755 $(box).addClass('checkeditem');
758 $(box).removeClass('checkeditem');
762 function notifyMarkAll() {
763 $.get('notify/mark/all', function(data) {
764 if(timer) clearTimeout(timer);
765 timer = setTimeout(NavUpdate,1000);
771 * sprintf in javascript
772 * "{0} and {1}".format('zero','uno');
774 String.prototype.format = function() {
775 var formatted = this;
776 for (var i = 0; i < arguments.length; i++) {
777 var regexp = new RegExp('\\{'+i+'\\}', 'gi');
778 formatted = formatted.replace(regexp, arguments[i]);
783 Array.prototype.remove = function(item) {
784 to=undefined; from=this.indexOf(item);
785 var rest = this.slice((to || from) + 1 || this.length);
786 this.length = from < 0 ? this.length + from : from;
787 return this.push.apply(this, rest);
790 function previewTheme(elm) {
791 theme = $(elm).val();
792 $.getJSON('pretheme?f=&theme=' + theme,function(data) {
793 $('#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>');
798 // notification permission settings in localstorage
799 // set by settings page
800 function getNotificationPermission() {
801 if (window["Notification"] === undefined) {
804 if (Notification.permission === 'granted') {
805 var val = localStorage.getItem('notification-permissions');
806 if (val === null) return 'denied';
809 return Notification.permission;
814 * Show a dialog loaded from an url
815 * By defaults this load the url in an iframe in colorbox
816 * Themes can overwrite `show()` function to personalize it
823 * @return object colorbox
825 show : function (url) {
826 var size = Dialog._get_size();
827 return $.colorbox({href: url, iframe:true,innerWidth: size.width+'px',innerHeight: size.height+'px'})
831 * Show the Image browser dialog
834 * @param string id (optional)
837 * The name will be used to build the event name
838 * fired by image browser dialog when the user select
839 * an image. The optional id will be passed as argument
840 * to the event handler
842 doImageBrowser : function (name, id) {
843 var url = Dialog._get_url("image",name,id);
844 return Dialog.show(url);
848 * Show the File browser dialog
851 * @param string id (optional)
854 * The name will be used to build the event name
855 * fired by file browser dialog when the user select
856 * a file. The optional id will be passed as argument
857 * to the event handler
859 doFileBrowser : function (name, id) {
860 var url = Dialog._get_url("file",name,id);
861 return Dialog.show(url);
864 _get_url : function(type, name, id) {
866 if (id !== undefined) hash = hash + "-" + id;
867 return baseurl + "/fbrowser/"+type+"/?mode=minimal#"+hash;
870 _get_size: function() {
872 width: window.innerWidth-50,
873 height: window.innerHeight-100