1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
3 // https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill
4 if (!Element.prototype.matches) {
5 Element.prototype.matches =
6 Element.prototype.matchesSelector ||
7 Element.prototype.mozMatchesSelector ||
8 Element.prototype.msMatchesSelector ||
9 Element.prototype.oMatchesSelector ||
10 Element.prototype.webkitMatchesSelector ||
12 var matches = (this.document || this.ownerDocument).querySelectorAll(s),
14 while (--i >= 0 && matches.item(i) !== this) {}
19 function resizeIframe(obj) {
20 _resizeIframe(obj, 0);
23 function _resizeIframe(obj, desth) {
24 var h = obj.style.height;
25 var ch = obj.contentWindow.document.body.scrollHeight;
27 if (h == (ch + 'px')) {
30 if (desth == ch && ch > 0) {
31 obj.style.height = ch + 'px';
33 setTimeout(_resizeIframe, 100, obj, ch);
36 function initWidget(inflated, deflated) {
37 var elInf = document.getElementById(inflated);
38 var elDef = document.getElementById(deflated);
40 if (!elInf || !elDef) {
43 if (localStorage.getItem(window.location.pathname.split("/")[1] + ":" + inflated) != "none") {
44 elInf.style.display = "block";
45 elDef.style.display = "none";
47 elInf.style.display = "none";
48 elDef.style.display = "block";
52 function openCloseWidget(inflated, deflated) {
53 var elInf = document.getElementById(inflated);
54 var elDef = document.getElementById(deflated);
56 if (!elInf || !elDef) {
60 if (window.getComputedStyle(elInf).display === "none") {
61 elInf.style.display = "block";
62 elDef.style.display = "none";
63 localStorage.setItem(window.location.pathname.split("/")[1] + ":" + inflated, "block");
65 elInf.style.display = "none";
66 elDef.style.display = "block";
67 localStorage.setItem(window.location.pathname.split("/")[1] + ":" + inflated, "none");
71 function openClose(theID) {
72 var el = document.getElementById(theID);
74 if (window.getComputedStyle(el).display === "none") {
82 function openMenu(theID) {
83 var el = document.getElementById(theID);
85 if (!el.dataset.display) {
86 el.dataset.display = 'block';
88 el.style.display = el.dataset.display;
92 function closeMenu(theID) {
93 var el = document.getElementById(theID);
95 el.dataset.display = window.getComputedStyle(el).display;
96 el.style.display = "none";
100 function decodeHtml(html) {
101 var txt = document.createElement("textarea");
103 txt.innerHTML = html;
108 * Retrieves a single named query string parameter
110 * @param {string} name
112 * @see https://davidwalsh.name/query-string-javascript
114 function getUrlParameter(name) {
115 name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
116 var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
117 var results = regex.exec(location.search);
118 return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
124 var force_update = false;
127 var totStopped = false;
131 var in_progress = false;
132 var langSelect = false;
133 var commentBusy = false;
134 var last_popup_menu = null;
135 var last_popup_button = null;
136 var lockLoadContent = false;
137 var originalTitle = document.title;
139 const urlRegex = /^(?:https?:\/\/|\s)[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})(?:\/+[a-z0-9_.:;-]*)*(?:\?[&%|+a-z0-9_=,.:;-]*)?(?:[&%|+&a-z0-9_=,:;.-]*)(?:[!#\/&%|+a-z0-9_=,:;.-]*)}*$/i;
142 $.ajaxSetup({cache: false});
144 /* setup comment textarea buttons */
145 /* comment textarea buttons needs some "data-*" attributes to work:
146 * data-role="insert-formatting" : to mark the element as a formatting button
147 * data-bbcode="<string>" : name of the bbcode element to insert. insertFormatting() will insert it as "[name][/name]"
148 * data-id="<string>" : id of the comment, used to find other comment-related element, like the textarea
150 $('body').on('click','[data-role="insert-formatting"]', function(e) {
153 var bbcode = o.data('bbcode');
154 var id = o.data('id');
155 if (bbcode == "img") {
156 Dialog.doImageBrowser("comment", id);
160 if (bbcode == "imgprv") {
164 insertFormatting(bbcode, id);
167 /* event from comment textarea button popups */
168 /* insert returned bbcode at cursor position or replace selected text */
169 $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
171 var textarea = document.getElementById("comment-edit-text-" +id);
172 var start = textarea.selectionStart;
173 var end = textarea.selectionEnd;
174 textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
175 $(textarea).trigger('change');
179 function close_last_popup_menu() {
180 if (last_popup_menu) {
181 last_popup_menu.hide();
182 last_popup_menu.off('click', function(e) {e.stopPropagation()});
183 last_popup_button.removeClass("selected");
184 last_popup_menu = null;
185 last_popup_button = null;
188 $('a[rel^="#"]').click(function(e) {
190 var parent = $(this).parent();
191 var isSelected = (last_popup_button && parent.attr('id') == last_popup_button.attr('id'));
192 close_last_popup_menu();
196 menu = $($(this).attr('rel'));
199 if (menu.attr('popup') == "false") {
202 parent.toggleClass("selected");
204 if (menu.css("display") == "none") {
205 last_popup_menu = null;
206 last_popup_button = null;
208 last_popup_menu = menu;
209 last_popup_menu.on('click', function(e) {e.stopPropagation()});
210 last_popup_button = parent;
211 $('#nav-notifications-menu').perfectScrollbar('update');
215 $('html').click(function() {
216 close_last_popup_menu();
220 $("a.popupbox").colorbox({
222 'transition' : 'elastic',
225 $("a.ajax-popupbox").colorbox({
226 'transition' : 'elastic',
230 /* notifications template */
231 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
232 var notifications_all = unescape($('<div>').append($("#nav-notifications-see-all").clone()).html()); //outerHtml hack
233 var notifications_mark = unescape($('<div>').append($("#nav-notifications-mark-all").clone()).html()); //outerHtml hack
234 var notifications_empty = unescape($("#nav-notifications-menu").html());
236 /* enable perfect-scrollbars for different elements */
237 $('#nav-notifications-menu, aside').perfectScrollbar();
239 /* nav update event */
240 $('nav').bind('nav-update', function(e, data) {
241 var invalid = data.invalid || 0;
243 window.location.href=window.location.href
246 let tabNotifications = data.mail + data.notification;
247 if (tabNotifications > 0) {
248 document.title = '(' + tabNotifications + ') ' + originalTitle;
250 document.title = originalTitle;
253 ['net', 'home', 'intro', 'mail', 'events', 'birthdays', 'notification'].forEach(function(type) {
254 var number = data[type];
257 $('#' + type + '-update').removeClass('show');
259 $('#' + type + '-update').addClass('show');
261 $('#' + type + '-update').text(number);
264 var intro = data['intro'];
266 intro = ''; $('#intro-update-li').removeClass('show')
268 $('#intro-update-li').addClass('show')
271 $('#intro-update-li').html(intro);
273 var mail = data['mail'];
275 mail = ''; $('#mail-update-li').removeClass('show')
277 $('#mail-update-li').addClass('show')
280 $('#mail-update-li').html(mail);
282 $(".sidebar-group-li .notify").removeClass("show");
283 $(data.groups).each(function(key, group) {
285 var gcount = group.count;
286 $(".group-"+gid+" .notify").addClass("show").text(gcount);
289 $(".forum-widget-entry .notify").removeClass("show");
290 $(data.forums).each(function(key, forum) {
292 var fcount = forum.count;
293 $(".forum-"+fid+" .notify").addClass("show").text(fcount);
296 if (data.notifications.length == 0) {
297 $("#nav-notifications-menu").html(notifications_empty);
299 var nnm = $("#nav-notifications-menu");
300 nnm.html(notifications_all + notifications_mark);
302 var lastItemStorageKey = "notification-lastitem:" + localUser;
303 var notification_lastitem = parseInt(localStorage.getItem(lastItemStorageKey));
304 var notification_id = 0;
306 // Insert notifs into the notifications-menu
307 $(data.notifications).each(function(key, notification) {
308 var text = notification.message.format('<span class="contactname">' + notification.name + '</span>');
309 var contact = ('<a href="' + notification.url + '"><span class="contactname">' + notification.name + '</span></a>');
310 var seenclass = (notification.seen == 1) ? "notification-seen" : "notification-unseen";
311 var html = notifications_tpl.format(
312 notification.href, // {0} // link to the source
313 notification.photo, // {1} // photo of the contact
314 text, // {2} // preformatted text (autor + text)
315 notification.date, // {3} // date of notification (time ago)
316 seenclass, // {4} // visited status of the notification
317 new Date(notification.timestamp*1000), // {5} // date of notification
318 notification.url, // {6} // profile url of the contact
319 notification.message.format(contact), // {7} // preformatted html (text including author profile url)
320 '' // {8} // Deprecated
325 // Desktop Notifications
326 $(data.notifications.reverse()).each(function(key, e) {
327 notification_id = parseInt(e.timestamp);
328 if (notification_lastitem !== null && notification_id > notification_lastitem && Number(e.seen) === 0) {
329 if (getNotificationPermission() === "granted") {
330 var notification = new Notification(document.title, {
331 body: decodeHtml(e.message.replace('→ ', '').format(e.name)),
334 notification['url'] = e.href;
335 notification.addEventListener("click", function(ev) {
336 window.location = ev.target.url;
342 notification_lastitem = notification_id;
343 localStorage.setItem(lastItemStorageKey, notification_lastitem)
345 $("img[data-src]", nnm).each(function(i, el) {
346 // Add src attribute for images with a data-src attribute
347 // However, don't bother if the data-src attribute is empty, because
348 // an empty "src" tag for an image will cause some browsers
349 // to prefetch the root page of the Friendica hub, which will
350 // unnecessarily load an entire profile/ or network/ page
351 if ($(el).data("src") != '') {
352 $(el).attr('src', $(el).data("src"));
357 var notif = data['notification'];
359 $("#nav-notifications-linkmenu").addClass("on");
361 $("#nav-notifications-linkmenu").removeClass("on");
364 $(data.sysmsgs.notice).each(function(key, message) {
365 $.jGrowl(message, {sticky: true, theme: 'notice'});
367 $(data.sysmsgs.info).each(function(key, message) {
368 $.jGrowl(message, {sticky: false, theme: 'info', life: 5000});
371 // Update the js scrollbars
372 $('#nav-notifications-menu').perfectScrollbar('update');
375 // Asynchronous calls are deferred until the very end of the page load to ease on slower connections
376 window.addEventListener("load", function(){
378 if (typeof acl !== 'undefined') {
383 // Allow folks to stop the ajax page updates with the pause/break key
384 $(document).keydown(function(event) {
385 if (event.keyCode == '8') {
386 var target = event.target || event.srcElement;
387 if (!/input|textarea/i.test(target.nodeName)) {
392 if (event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
393 event.preventDefault();
394 if (stopped == false) {
399 $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
403 } else if (!totStopped) {
408 // Scroll to the next/previous thread when pressing J and K
409 $(document).keydown(function (event) {
410 var threads = $('.thread_level_1');
411 if ((event.keyCode === 74 || event.keyCode === 75) && !$(event.target).is('textarea, input')) {
412 var scrollTop = $(window).scrollTop();
413 if (event.keyCode === 75) {
414 threads = $(threads.get().reverse());
416 threads.each(function(key, item) {
418 var top = $(item).offset().top - 100;
419 if (event.keyCode === 74) {
420 comparison = top > scrollTop + 1;
421 } else if (event.keyCode === 75) {
422 comparison = top < scrollTop - 1;
425 $('html, body').animate({scrollTop: top}, 200);
432 // Set an event listener for infinite scroll
433 if (typeof infinite_scroll !== 'undefined') {
434 $(window).scroll(function(e) {
435 if ($(document).height() != $(window).height()) {
436 // First method that is expected to work - but has problems with Chrome
437 if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
440 // This method works with Chrome - but seems to be much slower in Firefox
441 if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5)) {
450 * Inserts a BBCode tag in the comment textarea identified by id
452 * @param {string} BBCode
456 function insertFormatting(BBCode, id) {
457 let textarea = document.getElementById('comment-edit-text-' + id);
459 if (textarea.value === '') {
461 .addClass("comment-edit-text-full")
462 .removeClass("comment-edit-text-empty");
463 closeMenu("comment-fake-form-" + id);
464 openMenu("item-comments-" + id);
467 insertBBCodeInTextarea(BBCode, textarea);
473 * Inserts a BBCode tag in the provided textarea element, wrapping the currently selected text.
474 * For URL BBCode, it discriminates between link text and non-link text to determine where to insert the selected text.
476 * @param {string} BBCode
477 * @param {HTMLTextAreaElement} textarea
479 function insertBBCodeInTextarea(BBCode, textarea) {
480 let selectionStart = textarea.selectionStart;
481 let selectionEnd = textarea.selectionEnd;
482 let selectedText = textarea.value.substring(selectionStart, selectionEnd);
483 let openingTag = '[' + BBCode + ']';
484 let closingTag = '[/' + BBCode + ']';
485 let cursorPosition = selectionStart + openingTag.length + selectedText.length;
487 if (BBCode === 'url') {
488 if (urlRegex.test(selectedText)) {
489 openingTag = '[' + BBCode + '=' + selectedText + ']';
491 cursorPosition = selectionStart + openingTag.length;
493 openingTag = '[' + BBCode + '=]';
494 cursorPosition = selectionStart + openingTag.length - 1;
498 textarea.value = textarea.value.substring(0, selectionStart) + openingTag + selectedText + closingTag + textarea.value.substring(selectionEnd, textarea.value.length);
499 textarea.setSelectionRange(cursorPosition, cursorPosition);
500 textarea.dispatchEvent(new Event('change'));
504 function NavUpdate() {
506 var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&uid=' + localUser : '');
507 $.get(pingCmd, function(data) {
509 // send nav-update event
510 $('nav').trigger('nav-update', data.result);
513 ['network', 'profile', 'community', 'notes', 'display', 'contact'].forEach(function (src) {
514 if ($('#live-' + src).length) {
518 if ($('#live-photos').length) {
521 window.location.href = window.location.href;
527 timer = setTimeout(NavUpdate, updateInterval);
530 function updateConvItems(data) {
532 $('.toplevel_item',data).each(function() {
533 var ident = $(this).attr('id');
535 // Add new top-level item.
536 if ($('#' + ident).length === 0
537 && (!getUrlParameter('page')
538 && !getUrlParameter('max_id')
539 && !getUrlParameter('since_id')
540 || getUrlParameter('page') === '1'
543 $('#' + prev).after($(this));
545 // Replace already existing thread.
547 // Find out if the hidden comments are open, so we can keep it that way
548 // if a new comment has been posted
549 var id = $('.hide-comments-total', this).attr('id');
550 if (typeof id != 'undefined') {
551 id = id.split('-')[3];
552 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
555 $('#' + ident).replaceWith($(this));
557 if (typeof id != 'undefined') {
559 showHideComments(id);
566 $('.like-rotator').hide();
569 $('body').css('cursor', 'auto');
571 /* autocomplete @nicknames */
572 $(".comment-edit-form textarea").editor_autocomplete(baseurl + '/search/acl');
573 /* autocomplete bbcode */
574 $(".comment-edit-form textarea").bbco_autocomplete('bbcode');
577 function liveUpdate(src) {
578 if ((src == null) || stopped || !profile_uid) {
579 $('.like-rotator').hide(); return;
582 if (($('.comment-edit-text-full').length) || in_progress) {
584 clearTimeout(livetime);
586 livetime = setTimeout(function() {liveUpdate(src)}, 5000);
590 if (livetime != null) {
593 prev = 'live-' + src;
597 let force = force_update || $(document).scrollTop() === 0;
599 var orgHeight = $("section").height();
601 var udargs = ((netargs.length) ? '/' + netargs : '');
603 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&force=' + (force ? 1 : 0) + '&item=' + update_item;
606 force_update = false;
609 if (getUrlParameter('page')) {
610 update_url += '&page=' + getUrlParameter('page');
612 if (getUrlParameter('since_id')) {
613 update_url += '&since_id=' + getUrlParameter('since_id');
615 if (getUrlParameter('max_id')) {
616 update_url += '&max_id=' + getUrlParameter('max_id');
619 $.get(update_url, function(data) {
623 $('.wall-item-body', data).imagesLoaded(function() {
624 updateConvItems(data);
626 document.dispatchEvent(new Event('postprocess_liveupdate'));
628 // Update the scroll position.
629 $(window).scrollTop($(window).scrollTop() + $("section").height() - orgHeight);
634 function imgbright(node) {
635 $(node).removeClass("drophide").addClass("drop");
638 function imgdull(node) {
639 $(node).removeClass("drop").addClass("drophide");
642 // Since our ajax calls are asynchronous, we will give a few
643 // seconds for the first ajax call (setting like/dislike), then
644 // run the updater to pick up any changes and display on the page.
645 // The updater will turn any rotators off when it's done.
646 // This function will have returned long before any of these
647 // events have completed and therefore there won't be any
648 // visible feedback that anything changed without all this
649 // trickery. This still could cause confusion if the "like" ajax call
650 // is delayed and NavUpdate runs before it completes.
653 * @param {int} ident The id of the relevant item
654 * @param {string} verb The verb of the action
655 * @param {boolean} un Whether to perform an activity removal instead of creation
657 function dolike(ident, verb, un) {
659 $('#like-rotator-' + ident.toString()).show();
660 verb = un ? 'un' + verb : verb;
661 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate);
664 update_item = ident.toString();
667 function dosubthread(ident) {
669 $('#like-rotator-' + ident.toString()).show();
670 $.get('subthread/' + ident.toString(), NavUpdate);
674 function dostar(ident) {
675 ident = ident.toString();
676 $('#like-rotator-' + ident).show();
677 $.get('starred/' + ident, function(data) {
678 if (data.match(/1/)) {
679 $('#starred-' + ident).addClass('starred');
680 $('#starred-' + ident).removeClass('unstarred');
681 $('#star-' + ident).addClass('hidden');
682 $('#unstar-' + ident).removeClass('hidden');
684 $('#starred-' + ident).addClass('unstarred');
685 $('#starred-' + ident).removeClass('starred');
686 $('#star-' + ident).removeClass('hidden');
687 $('#unstar-' + ident).addClass('hidden');
689 $('#like-rotator-' + ident).hide();
693 function dopin(ident) {
694 ident = ident.toString();
695 $('#like-rotator-' + ident).show();
696 $.get('pinned/' + ident, function(data) {
697 if (data.match(/1/)) {
698 $('#pinned-' + ident).addClass('pinned');
699 $('#pinned-' + ident).removeClass('unpinned');
700 $('#pin-' + ident).addClass('hidden');
701 $('#unpin-' + ident).removeClass('hidden');
703 $('#pinned-' + ident).addClass('unpinned');
704 $('#pinned-' + ident).removeClass('pinned');
705 $('#pin-' + ident).removeClass('hidden');
706 $('#unpin-' + ident).addClass('hidden');
708 $('#like-rotator-' + ident).hide();
712 function doignore(ident) {
713 ident = ident.toString();
714 $('#like-rotator-' + ident).show();
715 $.get('item/ignore/' + ident, function(data) {
717 $('#ignored-' + ident)
719 .removeClass('unignored');
720 $('#ignore-' + ident).addClass('hidden');
721 $('#unignore-' + ident).removeClass('hidden');
723 $('#ignored-' + ident)
724 .addClass('unignored')
725 .removeClass('ignored');
726 $('#ignore-' + ident).removeClass('hidden');
727 $('#unignore-' + ident).addClass('hidden');
729 $('#like-rotator-' + ident).hide();
733 function getPosition(e) {
734 var cursor = {x:0, y:0};
736 if (e.pageX || e.pageY) {
740 if (e.clientX || e.clientY) {
741 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
742 cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
743 } else if (e.x || e.y) {
751 var lockvisible = false;
753 function lockview(event, type, id) {
754 event = event || window.event;
755 cursor = getPosition(event);
761 $.get('permission/tooltip/' + type + '/' + id, function(data) {
764 .css({'left': cursor.x + 5 , 'top': cursor.y + 5})
770 function post_comment(id) {
773 $('body').css('cursor', 'wait');
776 $("#comment-edit-form-" + id).serialize(),
779 $("#comment-edit-wrapper-" + id).hide();
780 $("#comment-edit-text-" + id).val('');
781 var tarea = document.getElementById("comment-edit-text-" + id);
783 commentClose(tarea,id);
788 timer = setTimeout(NavUpdate,10);
793 window.location.href=data.reload;
801 function preview_comment(id) {
802 $("#comment-edit-preview-" + id).show();
805 $("#comment-edit-form-" + id).serialize() + '&preview=1',
808 $("#comment-edit-preview-" + id).html(data.preview);
809 $("#comment-edit-preview-" + id + " a").click(function() {return false;});
817 function showHideComments(id) {
818 if ($('#collapsed-comments-' + id).is(':visible')) {
819 $('#collapsed-comments-' + id).slideUp();
820 $('#hide-comments-' + id).hide();
821 $('#hide-comments-total-' + id).show();
823 $('#collapsed-comments-' + id).slideDown();
824 $('#hide-comments-' + id).show();
825 $('#hide-comments-total-' + id).hide();
829 function preview_post() {
830 $("#jot-preview-content").show();
833 $("#profile-jot-form").serialize() + '&preview=1',
836 $("#jot-preview-content").html(data.preview);
837 $("#jot-preview-content" + " a").click(function() {return false;});
838 document.dispatchEvent(new Event('postprocess_liveupdate'));
847 // unpause auto reloads if they are currently stopped
850 $('#pause').html('');
853 // load more network content (used for infinite scroll)
854 function loadScrollContent() {
855 if (lockLoadContent) {
858 lockLoadContent = true;
860 $("#scroll-loader").fadeIn('normal');
862 match = $("span.received").last();
863 if (match.length > 0) {
864 received = match[0].innerHTML;
866 received = "0000-00-00 00:00:00";
869 match = $("span.created").last();
870 if (match.length > 0) {
871 created = match[0].innerHTML;
873 created = "0000-00-00 00:00:00";
876 match = $("span.commented").last();
877 if (match.length > 0) {
878 commented = match[0].innerHTML;
880 commented = "0000-00-00 00:00:00";
883 match = $("span.uriid").last();
884 if (match.length > 0) {
885 uriid = match[0].innerHTML;
890 // get the raw content from the next page and insert this content
891 // right before "#conversation-end"
892 $.get(infinite_scroll.reload_uri + '&mode=raw&last_received=' + received + '&last_commented=' + commented + '&last_created=' + created + '&last_uriid=' + uriid, function(data) {
893 $("#scroll-loader").hide();
894 if ($(data).length > 0) {
895 $(data).insertBefore('#conversation-end');
896 lockLoadContent = false;
898 $("#scroll-end").fadeIn('normal');
901 document.dispatchEvent(new Event('postprocess_liveupdate'));
905 function bin2hex(s) {
906 // Converts the binary representation of data to hex
909 // discuss at: http://phpjs.org/functions/bin2hex
910 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
911 // + bugfixed by: Onno Marsman
912 // + bugfixed by: Linuxworld
913 // * example 1: bin2hex('Kev');
914 // * returns 1: '4b6576'
915 // * example 2: bin2hex(String.fromCharCode(0x00));
917 var v,i, f = 0, a = [];
921 for (i = 0; i<f; i++) {
922 a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
928 function groupChangeMember(gid, cid, sec_token) {
929 $('body .fakelink').css('cursor', 'wait');
930 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
931 $('#group-update-wrapper').html(data);
932 $('body .fakelink').css('cursor', 'auto');
936 function contactgroupChangeMember(checkbox, gid, cid) {
938 // checkbox.checked is the checkbox state after the click
939 if (checkbox.checked) {
940 url = 'group/' + gid + '/add/' + cid;
942 url = 'group/' + gid + '/remove/' + cid;
944 $('body').css('cursor', 'wait');
947 // Restores previous state in case of error
948 checkbox.checked = !checkbox.checked;
951 $('body').css('cursor', 'auto');
957 function checkboxhighlight(box) {
958 if ($(box).is(':checked')) {
959 $(box).addClass('checkeditem');
961 $(box).removeClass('checkeditem');
965 function notificationMarkAll() {
966 $.get('notification/mark/all', function(data) {
970 timer = setTimeout(NavUpdate,1000);
976 * sprintf in javascript
977 * "{0} and {1}".format('zero','uno');
979 String.prototype.format = function() {
980 var formatted = this;
981 for (var i = 0; i < arguments.length; i++) {
982 var regexp = new RegExp('\\{'+i+'\\}', 'gi');
983 formatted = formatted.replace(regexp, arguments[i]);
988 Array.prototype.remove = function(item) {
989 to=undefined; from=this.indexOf(item);
990 var rest = this.slice((to || from) + 1 || this.length);
991 this.length = from < 0 ? this.length + from : from;
992 return this.push.apply(this, rest);
995 function previewTheme(elm) {
996 theme = $(elm).val();
997 $.getJSON('pretheme?theme=' + theme,function(data) {
998 $('#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>');
1003 // notification permission settings in localstorage
1004 // set by settings page
1005 function getNotificationPermission() {
1006 if (window["Notification"] === undefined) {
1010 if (Notification.permission === 'granted') {
1011 var val = localStorage.getItem('notification-permissions');
1017 return Notification.permission;
1022 * Show a dialog loaded from an url
1023 * By defaults this load the url in an iframe in colorbox
1024 * Themes can overwrite `show()` function to personalize it
1031 * @return object colorbox
1033 show : function (url) {
1034 var size = Dialog._get_size();
1035 return $.colorbox({href: url, iframe:true,innerWidth: size.width+'px',innerHeight: size.height+'px'})
1039 * Show the Image browser dialog
1041 * @param string name
1042 * @param string id (optional)
1045 * The name will be used to build the event name
1046 * fired by image browser dialog when the user select
1047 * an image. The optional id will be passed as argument
1048 * to the event handler
1050 doImageBrowser : function (name, id) {
1051 var url = Dialog._get_url("image",name,id);
1052 return Dialog.show(url);
1056 * Show the File browser dialog
1058 * @param string name
1059 * @param string id (optional)
1062 * The name will be used to build the event name
1063 * fired by file browser dialog when the user select
1064 * a file. The optional id will be passed as argument
1065 * to the event handler
1067 doFileBrowser : function (name, id) {
1068 var url = Dialog._get_url("file",name,id);
1069 return Dialog.show(url);
1072 _get_url : function(type, name, id) {
1074 if (id !== undefined) {
1075 hash = hash + "-" + id;
1077 return baseurl + "/fbrowser/"+type+"/?mode=minimal#"+hash;
1080 _get_size: function() {
1082 width: window.innerWidth-50,
1083 height: window.innerHeight-100