2 var jotcache = ''; //The jot cache. We use it as cache to restore old/original jot content
4 $(document).ready(function(){
5 //fade in/out based on scrollTop value
8 $(window).scroll(function () {
9 let currentScroll = $(this).scrollTop();
11 // Top of the page or going down = hide the button
12 if (!scrollStart || !currentScroll || currentScroll > scrollStart) {
13 $("#back-to-top").fadeOut();
14 scrollStart = currentScroll;
17 // Going up enough = show the button
18 if (scrollStart - currentScroll > 100) {
19 $("#back-to-top").fadeIn();
20 scrollStart = currentScroll;
24 // scroll body to 0px on click
25 $("#back-to-top").click(function () {
26 $("body,html").animate({
32 // add the class "selected" to group widges li if li > a does have the class group-selected
33 if( $("#sidebar-group-ul li a").hasClass("group-selected")) {
34 $("#sidebar-group-ul li a.group-selected").parent("li").addClass("selected");
37 // add the class "selected" to forums widges li if li > a does have the class forum-selected
38 if( $("#forumlist-sidbar-ul li a").hasClass("forum-selected")) {
39 $("#forumlist-sidbar-ul li a.forum-selected").parent("li").addClass("selected");
42 // add the class "active" to tabmenuli if li > a does have the class active
43 if( $("#tabmenu ul li a").hasClass("active")) {
44 $("#tabmenu ul li a.active").parent("li").addClass("active");
47 // give select fields an boostrap classes
48 // @todo: this needs to be changed in friendica core
49 $(".field.select, .field.custom").addClass("form-group");
50 $(".field.select > select, .field.custom > select").addClass("form-control");
52 // move the tabbar to the second nav bar
53 $("section .tabbar-wrapper").first().appendTo("#topbar-second > .container > #tabmenu");
55 // add mask css url to the logo-img container
57 // This is for firefox - we use a mask which looks like the friendica logo to apply user collers
58 // to the friendica logo (the mask is in nav.tpl at the botom). To make it work we need to apply the
59 // correct url. The only way which comes to my mind was to do this with js
60 // So we apply the correct url (with the link to the id of the mask) after the page is loaded.
61 if($("#logo-img").length ) {
62 var pageurl = "url('" + window.location.href + "#logo-mask')";
63 $("#logo-img").css({"mask": pageurl});
66 // make responsive tabmenu with flexmenu.js
67 // the menupoints which doesn't fit in the second nav bar will moved to a
68 // dropdown menu. Look at common_tabs.tpl
69 $("ul.tabs.flex-nav").flexMenu({
71 'popupClass': "dropdown-menu pull-right",
72 'popupAbsolute': false,
73 'target': ".flex-target"
76 // add Jot botton to the scecond navbar
77 let $jotButton = $("#jotOpen");
78 let $composeButton = $("#composeOpen");
81 if ($composeButton.length) {
82 $composeButton.appendTo("#topbar-second > .container > #navbar-button");
83 if($("#jot-popup").is(":hidden")) {
84 $composeButton.hide();
88 $composeButton.hide();
89 if ($jotButton.length) {
90 $jotButton.appendTo("#topbar-second > .container > #navbar-button");
91 if($("#jot-popup").is(":hidden")) {
97 // show bulk deletion button at network page if checkbox is checked
98 $("body").change("input.item-select", function(){
101 // We need to get all checked items, so it would close the delete button
102 // if we uncheck one item and others are still checked.
103 // So return checked = true if there is any checked item
104 $('input.item-select').each( function() {
105 if($(this).is(':checked')) {
112 $("#item-delete-selected").fadeTo(400, 1);
113 $("#item-delete-selected").show();
115 $("#item-delete-selected").fadeTo(400, 0, function(){
116 $("#item-delete-selected").hide();
121 //$('ul.flex-nav').flexMenu();
123 // initialize the bootstrap tooltips
125 selector: '[data-toggle="tooltip"]',
135 sanitizeFn: function (content) {
136 return DOMPurify.sanitize(content)
140 // initialize the bootstrap-select
141 $('.selectpicker').selectpicker();
143 // add search-heading to the seccond navbar
144 if( $(".search-heading").length) {
145 $(".search-heading").appendTo("#topbar-second > .container > #tabmenu");
148 // add search results heading to the second navbar
149 // and insert the search value to the top nav search input
150 if( $(".search-content-wrapper").length ) {
151 // get the text of the heading (we catch the plain text because we don't
152 // want to have a h4 heading in the navbar
153 var searchText = $(".section-title-wrapper > h2").text();
154 // insert the plain text in a <h4> heading and give it a class
155 var newText = '<h4 class="search-heading">'+searchText+'</h4>';
156 // append the new heading to the navbar
157 $("#topbar-second > .container > #tabmenu").append(newText);
159 // try to get the value of the original search input to insert it
160 // as value in the nav-search-input
161 var searchValue = $("#search-wrapper .form-group-search input").val();
163 // if the orignal search value isn't available use the location path as value
164 if( typeof searchValue === "undefined") {
165 // get the location path
166 var urlPath = window.location.search
167 // and split it up in its parts
168 var splitPath = urlPath.split(/(\?search?=)(.*$)/);
170 if(typeof splitPath[2] !== 'undefined') {
171 // decode the path (e.g to decode %40 to the character @)
172 var searchValue = decodeURIComponent(splitPath[2]);
176 if( typeof searchValue !== "undefined") {
177 $("#nav-search-input-field").val(searchValue);
181 // move the "Save the search" button to the second navbar
182 $(".search-content-wrapper #search-save").appendTo("#topbar-second > .container > #navbar-button");
184 // append the vcard-short-info to the second nav after passing the element
185 // with .fn (vcard username). Use scrollspy to get the scroll position.
186 if( $("aside .vcard .fn").length) {
187 $(".vcard .fn").scrollspy({
188 min: $(".vcard .fn").position().top - 50,
189 onLeaveTop: function onLeave(element) {
190 $("#vcard-short-info").fadeOut(500, function () {
191 $("#vcard-short-info").appendTo("#vcard-short-info-wrapper");
194 onEnter: function(element) {
195 $("#vcard-short-info").appendTo("#nav-short-info");
196 $("#vcard-short-info").fadeIn(500);
201 // move the forum contact information of the network page into the second navbar
202 if( $(".network-content-wrapper > #viewcontact_wrapper-network").length) {
203 // get the contact-wrapper element and append it to the second nav bar
204 // Note: We need the first() element with this class since at the present time we
205 // store also the js template information in the html code and thats why
206 // there are two elements with this class but we don't want the js template
207 $(".network-content-wrapper > #viewcontact_wrapper-network .contact-wrapper").first().appendTo("#nav-short-info");
210 // move heading from network stream to the second navbar nav-short-info section
211 if( $(".network-content-wrapper > .section-title-wrapper").length) {
212 // get the heading element
213 var heading = $(".network-content-wrapper > .section-title-wrapper > h2");
214 // get the text of the heading
215 var headingContent = heading.text();
216 // create a new element with the content of the heading
217 var newText = '<h4 class="heading" data-toggle="tooltip" title="'+headingContent+'">'+headingContent+'</h4>';
218 // remove the old heading element
220 // put the new element to the second nav bar
221 $("#topbar-second #nav-short-info").append(newText);
224 if( $(".community-content-wrapper").length) {
225 // get the heading element
226 var heading = $(".community-content-wrapper > h3").first();
227 // get the text of the heading
228 var headingContent = heading.text();
229 // create a new element with the content of the heading
230 var newText = '<h4 class="heading">'+headingContent+'</h4>';
231 // remove the old heading element
233 // put the new element to the second nav bar
234 $("#topbar-second > .container > #tabmenu").append(newText);
237 // Dropdown menus with the class "dropdown-head" will display the active tab
239 $("body").on('click', '.dropdown-head .dropdown-menu li a, .dropdown-head .dropdown-menu li button', function(){
240 toggleDropdownText(this);
243 // Change the css class while clicking on the switcher elements
244 $(".toggle label, .toggle .toggle-handle").click(function(event){
245 event.preventDefault();
246 // Get the value of the input element
247 var input = $(this).siblings("input");
248 var val = 1-input.val();
249 var id = input.attr("id");
251 // The css classes for "on" and "off"
252 var onstyle = "btn-primary";
253 var offstyle = "btn-default off";
255 // According to the value of the input element we need to decide
256 // which class need to be added and removed when changing the switch
257 var removedclass = (val == 0 ? onstyle : offstyle);
258 var addedclass = (val == 0 ? offstyle : onstyle)
259 $("#"+id+"_onoff").addClass(addedclass).removeClass(removedclass);
261 // After changing the switch the input element is getting
266 // Set the padding for input elements with inline buttons
268 // In Frio we use some input elements where the submit button is visually
269 // inside the the input field (through css). We need to set a padding-right
270 // to the input element where the padding value would be at least the width
271 // of the button. Otherwise long user input would be invisible because it is
272 // behind the button.
273 $("body").on('click', '.form-group-search > input', function() {
274 // Get the width of the button (if the button isn't available
275 // buttonWidth will be null
276 var buttonWidth = $(this).next('.form-button-search').outerWidth();
279 // Take the width of the button and ad 5px
280 var newWidth = buttonWidth + 5;
281 // Set the padding of the input element according
282 // to the width of the button
283 $(this).css('padding-right', newWidth);
289 * This event handler hides all comment UI when the user clicks anywhere on the page
290 * It ensures that we aren't closing the current comment box
292 * We are making an exception for buttons because of a race condition with the
293 * comment opening button that results in an already closed comment UI.
295 $(document).on('mousedown', function(event) {
296 if (event.target.type === 'button') {
300 var $dontclosethis = $(event.target).closest('.wall-item-comment-wrapper').find('.comment-edit-form');
301 $('.wall-item-comment-wrapper .comment-edit-submit-wrapper:visible').each(function() {
302 var $parent = $(this).parent('.comment-edit-form');
303 var itemId = $parent.data('itemId');
305 if ($dontclosethis[0] != $parent[0]) {
306 var textarea = $parent.find('textarea').get(0)
308 commentCloseUI(textarea, itemId);
313 // Customize some elements when the app is used in standalone mode on Android
314 if (window.matchMedia('(display-mode: standalone)').matches) {
315 // Open links to source outside of the webview
316 $('body').on('click', '.plink', function (e) {
317 $(e.target).attr('target', '_blank');
322 * This event listeners ensures that the textarea size is updated event if the
323 * value is changed externally (textcomplete, insertFormatting, fbrowser...)
325 $(document).on('change', 'textarea', function(event) {
326 autosize.update(event.target);
330 * Sticky aside on page scroll
331 * We enable the sticky aside only when window is wider than
332 * 976px - which is the maximum width where the aside is shown in
333 * mobile style - because on chrome-based browsers (desktop and
334 * android) the sticky plugin in mobile style causes the browser to
335 * scroll back to top the main content, making it impossible
337 * A side effect is that the sitky aside isn't really responsive,
338 * since is enabled or not at page loading time.
340 if ($(window).width() > 976) {
341 $("aside").stick_in_parent({
342 offset_top: 100, // px, header + tab bar + spacing
345 // recalculate sticky aside on clicks on <a> elements
346 // this handle height changes on expanding submenus
347 $("aside").on("click", "a", function(){
348 $(document.body).trigger("sticky_kit:recalc");
353 * Add or remove "aside-out" class to body tag
354 * when the mobile aside is shown or hidden.
355 * The class is used in css to disable scroll in page when the aside
359 .on("shown.bs.offcanvas", function() {
360 $("body").addClass("aside-out");
362 .on("hidden.bs.offcanvas", function() {
363 $("body").removeClass("aside-out");
366 // Event listener for 'Show & hide event map' button in the network stream.
367 $("body").on("click", ".event-map-btn", function() {
368 showHideEventMap(this);
373 function openClose(theID) {
374 var elem = document.getElementById(theID);
376 if( $(elem).is(':visible')) {
377 $(elem).slideUp(200);
380 $(elem).slideDown(200);
384 function showHide(theID) {
385 var elem = document.getElementById(theID);
386 var edit = document.getElementById("comment-edit-submit-wrapper-" + theID.match('[0-9$]+'));
388 if ($(elem).is(':visible')) {
389 if (!$(edit).is(':visible')) {
390 edit.style.display = "block";
393 elem.style.display = "none";
397 elem.style.display = "block";
401 // Show & hide event map in the network stream by button click.
402 function showHideEventMap(elm) {
403 // Get the id of the map element - it should be provided through
404 // the atribute "data-map-id".
405 var mapID = elm.getAttribute('data-map-id');
407 // Get translation labels.
408 var mapshow = elm.getAttribute('data-show-label');
409 var maphide = elm.getAttribute('data-hide-label');
411 // Change the button labels.
412 if (elm.innerText == mapshow) {
413 $('#' + elm.id).text(maphide);
415 $('#' + elm.id).text(mapshow);
417 // Because maps are iframe elements, we cant hide it through css (display: none).
418 // We solve this issue by putting the map outside the screen with css.
419 // So the first time the 'Show map' button is pressed we move the map
420 // element into the screen area.
421 var mappos = $('#' + mapID).css('position');
423 if (mappos === 'absolute') {
424 $('#' + mapID).hide();
425 $('#' + mapID).css({position: 'relative', left: 'auto', top: 'auto'});
433 function justifyPhotos() {
434 justifiedGalleryActive = true;
435 $('#photo-album-contents').justifiedGallery({
446 }).on('jg.complete', function(e){ justifiedGalleryActive = false; });
449 // Load a js script to the html head.
450 function loadScript(url, callback) {
451 // Check if the script is already in the html head.
452 var oscript = $('head script[src="' + url + '"]');
454 // Delete the old script from head.
455 if (oscript.length > 0) {
458 // Adding the script tag to the head as suggested before.
459 var head = document.getElementsByTagName('head')[0];
460 var script = document.createElement('script');
461 script.type = 'text/javascript';
464 // Then bind the event to the callback function.
465 // There are several events for cross browser compatibility.
466 script.onreadystatechange = callback;
467 script.onload = callback;
470 head.appendChild(script);
473 // Does we need a ? or a & to append values to a url
474 function qOrAmp(url) {
475 if(url.search('\\?') < 0) {
482 String.prototype.normalizeLink = function () {
483 var ret = this.replace('https:', 'http:');
484 var ret = ret.replace('//www', '//');
488 function cleanContactUrl(url) {
489 var parts = parseUrl(url);
491 if(! ("scheme" in parts) || ! ("host" in parts)) {
495 var newUrl =parts["scheme"] + "://" + parts["host"];
497 if("port" in parts) {
498 newUrl += ":" + parts["port"];
501 if("path" in parts) {
502 newUrl += parts["path"];
505 // if(url != newUrl) {
506 // console.log("Cleaned contact url " + url + " to " + newUrl);
512 function parseUrl (str, component) { // eslint-disable-line camelcase
513 // discuss at: http://locutusjs.io/php/parse_url/
514 // original by: Steven Levithan (http://blog.stevenlevithan.com)
515 // reimplemented by: Brett Zamir (http://brett-zamir.me)
516 // input by: Lorenzo Pisani
518 // improved by: Brett Zamir (http://brett-zamir.me)
519 // note 1: original by http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
520 // note 1: blog post at http://blog.stevenlevithan.com/archives/parseuri
521 // note 1: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
522 // note 1: Does not replace invalid characters with '_' as in PHP,
523 // note 1: nor does it return false with
524 // note 1: a seriously malformed URL.
525 // note 1: Besides function name, is essentially the same as parseUri as
526 // note 1: well as our allowing
527 // note 1: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
528 // example 1: parse_url('http://user:pass@host/path?a=v#a')
529 // returns 1: {scheme: 'http', host: 'host', user: 'user', pass: 'pass', path: '/path', query: 'a=v', fragment: 'a'}
530 // example 2: parse_url('http://en.wikipedia.org/wiki/%22@%22_%28album%29')
531 // returns 2: {scheme: 'http', host: 'en.wikipedia.org', path: '/wiki/%22@%22_%28album%29'}
532 // example 3: parse_url('https://host.domain.tld/a@b.c/folder')
533 // returns 3: {scheme: 'https', host: 'host.domain.tld', path: '/a@b.c/folder'}
534 // example 4: parse_url('https://gooduser:secretpassword@www.example.com/a@b.c/folder?foo=bar')
535 // returns 4: { scheme: 'https', host: 'www.example.com', path: '/a@b.c/folder', query: 'foo=bar', user: 'gooduser', pass: 'secretpassword' }
539 var mode = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.mode') : undefined) || 'php'
558 // For loose we added one optional slash to post-scheme to catch file:/// (should restrict this)
561 '(?:([^:\\/?#]+):)?',
562 '(?:\\/\\/()(?:(?:()(?:([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
564 '(?:(()(?:(?:[^?#\\/]*\\/)*)()(?:[^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
567 '(?:([^:\\/?#]+):)?',
568 '(?:\\/\\/((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
569 '((((?:[^?#\\/]*\\/)*)([^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
572 '(?:(?![^:@]+:[^:@\\/]*@)([^:\\/?#.]+):)?',
574 '((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?)',
575 '(((\\/(?:[^?#](?![^?#\\/]*\\.[^?#\\/.]+(?:[?#]|$)))*\\/?)?([^?#\\/]*))',
576 '(?:\\?([^#]*))?(?:#(.*))?)'
580 var m = parser[mode].exec(str)
591 return uri[component.replace('PHP_URL_', '').toLowerCase()]
594 if (mode !== 'php') {
595 var name = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.queryKey') : undefined) || 'queryKey'
596 parser = /(?:^|&)([^&=]*)=?([^&]*)/g
598 query = uri[key[12]] || ''
599 query.replace(parser, function ($0, $1, $2) {
610 // trim function to replace whithespace after the string
611 String.prototype.rtrim = function() {
612 var trimmed = this.replace(/\s+$/g, '');
617 * Scroll the screen to the item element whose id is provided, then highlights it
619 * Note: jquery.color.js is required
621 * @param {string} elementId The item element id
622 * @returns {undefined}
624 function scrollToItem(elementId) {
625 if (typeof elementId === "undefined") {
629 var $el = $('#' + elementId + ' > .media');
630 // Test if the Item exists
635 // Define the colors which are used for highlighting
636 var colWhite = {backgroundColor:'#F5F5F5'};
637 var colShiny = {backgroundColor:'#FFF176'};
639 // Get the Item Position (we need to substract 100 to match correct position
640 var itemPos = $el.offset().top - 100;
642 // Scroll to the DIV with the ID (GUID)
643 $('html, body').animate({
645 }, 400).promise().done( function() {
646 // Highlight post/commenent with ID (GUID)
647 $el.animate(colWhite, 1000).animate(colShiny).animate(colWhite, 600);
651 // format a html string to pure text
652 function htmlToText(htmlString) {
653 // Replace line breaks with spaces
654 var text = htmlString.replace(/<br>/g, ' ');
655 // Strip the text out of the html string
656 text = text.replace(/<[^>]*>/g, '');
662 * Sends a /like API call and updates the display of the relevant action button
663 * before the update reloads the item.
665 * @param {string} ident The id of the relevant item
666 * @param {string} verb The verb of the action
667 * @returns {undefined}
669 function doLikeAction(ident, verb) {
672 if (verb.indexOf('attend') === 0) {
673 $('.item-' + ident + ' .button-event:not(#' + verb + '-' + ident + ')').removeClass('active');
675 $('#' + verb + '-' + ident).toggleClass('active');
676 $('#like-rotator-' + ident.toString()).show();
677 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate );
680 update_item = ident.toString();
683 // Decodes a hexadecimally encoded binary string
684 function hex2bin (s) {
685 // discuss at: http://locutus.io/php/hex2bin/
686 // original by: Dumitru Uzun (http://duzun.me)
687 // example 1: hex2bin('44696d61')
689 // example 2: hex2bin('00')
691 // example 3: hex2bin('2f1q')
698 for (l = s.length; i < l; i += 2) {
699 var c = parseInt(s.substr(i, 1), 16);
700 var k = parseInt(s.substr(i + 1, 1), 16);
701 if (isNaN(c) || isNaN(k)) {
704 ret.push((c << 4) | k);
706 return String.fromCharCode.apply(String, ret);
709 // Convert binary data into hexadecimal representation
710 function bin2hex (s) {
711 // From: http://phpjs.org/functions
712 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
713 // + bugfixed by: Onno Marsman
714 // + bugfixed by: Linuxworld
715 // + improved by: ntoniazzi (http://phpjs.org/functions/bin2hex:361#comment_177616)
716 // * example 1: bin2hex('Kev');
717 // * returns 1: '4b6576'
718 // * example 2: bin2hex(String.fromCharCode(0x00));
725 for (i = 0, l = s.length; i < l; i++) {
726 n = s.charCodeAt(i).toString(16);
727 o += n.length < 2 ? "0" + n : n;
733 // Dropdown menus with the class "dropdown-head" will display the active tab
735 function toggleDropdownText(elm) {
736 $(elm).closest(".dropdown").find('.btn').html($(elm).text() + ' <span class="caret"></span>');
737 $(elm).closest(".dropdown").find('.btn').val($(elm).data('value'));
738 $(elm).closest("ul").children("li").show();
739 $(elm).parent("li").hide();
742 // Check if element does have a specific class
743 function hasClass(elem, cls) {
744 return (" " + elem.className + " " ).indexOf( " "+cls+" " ) > -1;