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
6 $(window).scroll(function () {
7 if ($(this).scrollTop() > 1000) {
8 $("#back-to-top").fadeIn();
10 $("#back-to-top").fadeOut();
14 // scroll body to 0px on click
15 $("#back-to-top").click(function () {
16 $("body,html").animate({
22 // add the class "selected" to group widges li if li > a does have the class group-selected
23 if( $("#sidebar-group-ul li a").hasClass("group-selected")) {
24 $("#sidebar-group-ul li a.group-selected").parent("li").addClass("selected");
27 // add the class "selected" to forums widges li if li > a does have the class forum-selected
28 if( $("#forumlist-sidbar-ul li a").hasClass("forum-selected")) {
29 $("#forumlist-sidbar-ul li a.forum-selected").parent("li").addClass("selected");
32 // add the class "active" to tabmenuli if li > a does have the class active
33 if( $("#tabmenu ul li a").hasClass("active")) {
34 $("#tabmenu ul li a.active").parent("li").addClass("active");
37 // give select fields an boostrap classes
38 // @todo: this needs to be changed in friendica core
39 $(".field.select, .field.custom").addClass("form-group");
40 $(".field.select > select, .field.custom > select").addClass("form-control");
42 // move the tabbar to the second nav bar
43 $("section .tabbar-wrapper").first().appendTo("#topbar-second > .container > #tabmenu");
45 // add mask css url to the logo-img container
47 // This is for firefox - we use a mask which looks like the friendica logo to apply user collers
48 // to the friendica logo (the mask is in nav.tpl at the botom). To make it work we need to apply the
49 // correct url. The only way which comes to my mind was to do this with js
50 // So we apply the correct url (with the link to the id of the mask) after the page is loaded.
51 if($("#logo-img").length ) {
52 var pageurl = "url('" + window.location.href + "#logo-mask')";
53 $("#logo-img").css({"mask": pageurl});
56 // make responsive tabmenu with flexmenu.js
57 // the menupoints which doesn't fit in the second nav bar will moved to a
58 // dropdown menu. Look at common_tabs.tpl
59 $("ul.tabs.flex-nav").flexMenu({
61 'popupClass': "dropdown-menu pull-right",
62 'popupAbsolute': false,
63 'target': ".flex-target"
66 // add Jot botton to the scecond navbar
67 if( $("section #jotOpen").length ) {
68 $("section #jotOpen").appendTo("#topbar-second > .container > #navbar-button");
69 if( $("#jot-popup").is(":hidden")) $("#topbar-second > .container > #navbar-button #jotOpen").hide();
72 // show bulk deletion button at network page if checkbox is checked
73 $("body").change("input.item-select", function(){
76 // We need to get all checked items, so it would close the delete button
77 // if we uncheck one item and others are still checked.
78 // So return checked = true if there is any checked item
79 $('input.item-select').each( function() {
80 if($(this).is(':checked')) {
87 $("#item-delete-selected").fadeTo(400, 1);
88 $("#item-delete-selected").show();
90 $("#item-delete-selected").fadeTo(400, 0, function(){
91 $("#item-delete-selected").hide();
96 //$('ul.flex-nav').flexMenu();
98 // initialize the bootstrap tooltips
100 selector: '[data-toggle="tooltip"]',
112 // initialize the bootstrap-select
113 $('.selectpicker').selectpicker();
115 // add search-heading to the seccond navbar
116 if( $(".search-heading").length) {
117 $(".search-heading").appendTo("#topbar-second > .container > #tabmenu");
120 // add search results heading to the second navbar
121 // and insert the search value to the top nav search input
122 if( $(".search-content-wrapper").length ) {
123 // get the text of the heading (we catch the plain text because we don't
124 // want to have a h4 heading in the navbar
125 var searchText = $(".section-title-wrapper > h2").text();
126 // insert the plain text in a <h4> heading and give it a class
127 var newText = '<h4 class="search-heading">'+searchText+'</h4>';
128 // append the new heading to the navbar
129 $("#topbar-second > .container > #tabmenu").append(newText);
131 // try to get the value of the original search input to insert it
132 // as value in the nav-search-input
133 var searchValue = $("#search-wrapper .form-group-search input").val();
135 // if the orignal search value isn't available use the location path as value
136 if( typeof searchValue === "undefined") {
137 // get the location path
138 var urlPath = window.location.search
139 // and split it up in its parts
140 var splitPath = urlPath.split(/(\?search?=)(.*$)/);
142 if(typeof splitPath[2] !== 'undefined') {
143 // decode the path (e.g to decode %40 to the character @)
144 var searchValue = decodeURIComponent(splitPath[2]);
148 if( typeof searchValue !== "undefined") {
149 $("#nav-search-input-field").val(searchValue);
153 // move the "Save the search" button to the second navbar
154 $(".search-content-wrapper #search-save-form ").appendTo("#topbar-second > .container > #navbar-button");
156 // append the vcard-short-info to the second nav after passing the element
157 // with .fn (vcard username). Use scrollspy to get the scroll position.
158 if( $("aside .vcard .fn").length) {
159 $(".vcard .fn").scrollspy({
160 min: $(".vcard .fn").position().top - 50,
161 onLeaveTop: function onLeave(element) {
162 $("#vcard-short-info").fadeOut(500, function () {
163 $("#vcard-short-info").appendTo("#vcard-short-info-wrapper");
166 onEnter: function(element) {
167 $("#vcard-short-info").appendTo("#nav-short-info");
168 $("#vcard-short-info").fadeIn(500);
173 // move the forum contact information of the network page into the second navbar
174 if( $(".network-content-wrapper > #viewcontact_wrapper-network").length) {
175 // get the contact-wrapper element and append it to the second nav bar
176 // Note: We need the first() element with this class since at the present time we
177 // store also the js template information in the html code and thats why
178 // there are two elements with this class but we don't want the js template
179 $(".network-content-wrapper > #viewcontact_wrapper-network .contact-wrapper").first().appendTo("#nav-short-info");
182 // move heading from network stream to the second navbar nav-short-info section
183 if( $(".network-content-wrapper > .section-title-wrapper").length) {
184 // get the heading element
185 var heading = $(".network-content-wrapper > .section-title-wrapper > h2");
186 // get the text of the heading
187 var headingContent = heading.text();
188 // create a new element with the content of the heading
189 var newText = '<h4 class="heading" data-toggle="tooltip" title="'+headingContent+'">'+headingContent+'</h4>';
190 // remove the old heading element
192 // put the new element to the second nav bar
193 $("#topbar-second #nav-short-info").append(newText);
196 if( $(".community-content-wrapper").length) {
197 // get the heading element
198 var heading = $(".community-content-wrapper > h3").first();
199 // get the text of the heading
200 var headingContent = heading.text();
201 // create a new element with the content of the heading
202 var newText = '<h4 class="heading">'+headingContent+'</h4>';
203 // remove the old heading element
205 // put the new element to the second nav bar
206 $("#topbar-second > .container > #tabmenu").append(newText);
209 // Dropdown menus with the class "dropdown-head" will display the active tab
211 $("body").on('click', '.dropdown-head .dropdown-menu li a, .dropdown-head .dropdown-menu li button', function(){
212 toggleDropdownText(this);
215 /* setup onoff widgets */
216 // Add the correct class to the switcher according to the input
218 $(".toggle input").each(function(){
219 // Get the value of the input element
221 id = $(this).attr("id");
223 // The css classes for "on" and "off"
224 onstyle = "btn-primary";
225 offstyle = "btn-default off";
227 // Add the correct class in dependence of input value (On/Off)
228 toggleclass = (val == 0 ? offstyle : onstyle);
229 $("#"+id+"_onoff").addClass(toggleclass);
233 // Change the css class while clicking on the switcher elements
234 $(".toggle label, .toggle .toggle-handle").click(function(event){
235 event.preventDefault();
236 // Get the value of the input element
237 var input = $(this).siblings("input");
238 var val = 1-input.val();
239 var id = input.attr("id");
241 // The css classes for "on" and "off"
242 var onstyle = "btn-primary";
243 var offstyle = "btn-default off";
245 // According to the value of the input element we need to decide
246 // which class need to be added and removed when changing the switch
247 var removedclass = (val == 0 ? onstyle : offstyle);
248 var addedclass = (val == 0 ? offstyle : onstyle)
249 $("#"+id+"_onoff").addClass(addedclass).removeClass(removedclass);
251 // After changing the switch the input element is getting
256 // Set the padding for input elements with inline buttons
258 // In Frio we use some input elements where the submit button is visually
259 // inside the the input field (through css). We need to set a padding-right
260 // to the input element where the padding value would be at least the width
261 // of the button. Otherwise long user input would be invisible because it is
262 // behind the button.
263 $("body").on('click', '.form-group-search > input', function() {
264 // Get the width of the button (if the button isn't available
265 // buttonWidth will be null
266 var buttonWidth = $(this).next('.form-button-search').outerWidth();
269 // Take the width of the button and ad 5px
270 var newWidth = buttonWidth + 5;
271 // Set the padding of the input element according
272 // to the width of the button
273 $(this).css('padding-right', newWidth);
279 * This event handler hides all comment UI when the user clicks anywhere on the page
280 * It ensures that we aren't closing the current comment box
282 * We are making an exception for buttons because of a race condition with the
283 * comment opening button that results in an already closed comment UI.
285 $(document).on('click', function(event) {
286 if (event.target.type === 'button') {
290 var $dontclosethis = $(event.target).closest('.wall-item-comment-wrapper').find('.comment-edit-form');
291 $('.wall-item-comment-wrapper .comment-edit-submit-wrapper:visible').each(function() {
292 var $parent = $(this).parent('.comment-edit-form');
293 var itemId = $parent.data('itemId');
295 if ($dontclosethis[0] != $parent[0]) {
296 var textarea = $parent.find('textarea').get(0)
298 commentCloseUI(textarea, itemId);
305 function openClose(theID) {
306 var elem = document.getElementById(theID);
308 if( $(elem).is(':visible')) {
309 $(elem).slideUp(200);
312 $(elem).slideDown(200);
316 function showHide(theID) {
317 if(document.getElementById(theID).style.display == "block") {
318 document.getElementById(theID).style.display = "none"
321 document.getElementById(theID).style.display = "block"
326 function showHideComments(id) {
327 if( $('#collapsed-comments-' + id).is(':visible')) {
328 $('#collapsed-comments-' + id).slideUp();
329 $('#hide-comments-' + id).html(window.showMore);
330 $('#hide-comments-total-' + id).show();
333 $('#collapsed-comments-' + id).slideDown();
334 $('#hide-comments-' + id).html(window.showFewer);
335 $('#hide-comments-total-' + id).hide();
340 function justifyPhotos() {
341 justifiedGalleryActive = true;
342 $('#photo-album-contents').justifiedGallery({
353 }).on('jg.complete', function(e){ justifiedGalleryActive = false; });
356 function justifyPhotosAjax() {
357 justifiedGalleryActive = true;
358 $('#photo-album-contents').justifiedGallery('norewind').on('jg.complete', function(e){ justifiedGalleryActive = false; });
361 function loadScript(url, callback) {
362 // Adding the script tag to the head as suggested before
363 var head = document.getElementsByTagName('head')[0];
364 var script = document.createElement('script');
365 script.type = 'text/javascript';
368 // Then bind the event to the callback function.
369 // There are several events for cross browser compatibility.
370 script.onreadystatechange = callback;
371 script.onload = callback;
374 head.appendChild(script);
377 function random_digits(digits) {
381 for(var i = 0; i < digits; i++) {
382 var rn = Math.round(Math.random() * (9));
389 // Does we need a ? or a & to append values to a url
390 function qOrAmp(url) {
391 if(url.search('\\?') < 0) {
398 function contact_filter(item) {
399 // get the html content from the js template of the contact-wrapper
400 contact_tpl = unescape($(".javascript-template[rel=contact-template]").html());
405 username: item.username,
407 img_hover: item.img_hover,
408 edit_hover: item.edit_hover,
409 account_type: item.account_type,
410 photo_menu: item.photo_menu,
411 alt_text: item.alt_text,
412 dir_icon: item.dir_icon,
413 sparkle: item.sparkle,
414 itemurl: item.itemurl,
416 network: item.network,
418 details: item.details,
421 // open a new jSmart instance with the template
422 var tpl = new jSmart (contact_tpl);
424 // replace the variable with the values
425 var html = tpl.fetch(variables);
430 function filter_replace(item) {
436 $.fn.contact_filter = function(backend_url, typ, autosubmit, onselect) {
437 if(typeof typ === 'undefined') typ = '';
438 if(typeof autosubmit === 'undefined') autosubmit = false;
440 // Autocomplete contacts
442 match: /(^)([^\n]+)$/,
444 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
445 replace: filter_replace,
446 template: contact_filter,
449 this.attr('autocomplete','off');
450 var a = this.textcomplete([contacts], {className:'accontacts', appendTo: '#contact-list'});
452 a.on('textComplete:select', function(e, value, strategy) { $(".dropdown-menu.textcomplete-dropdown.media-list").show(); });
457 // current time in milliseconds, to send each request to make sure
458 // we 're not getting 304 response
460 return new Date().getTime();
463 String.prototype.normalizeLink = function () {
464 var ret = this.replace('https:', 'http:');
465 var ret = ret.replace('//www', '//');
469 function cleanContactUrl(url) {
470 var parts = parseUrl(url);
472 if(! ("scheme" in parts) || ! ("host" in parts)) {
476 var newUrl =parts["scheme"] + "://" + parts["host"];
478 if("port" in parts) {
479 newUrl += ":" + parts["port"];
482 if("path" in parts) {
483 newUrl += parts["path"];
486 // if(url != newUrl) {
487 // console.log("Cleaned contact url " + url + " to " + newUrl);
493 function parseUrl (str, component) { // eslint-disable-line camelcase
494 // discuss at: http://locutusjs.io/php/parse_url/
495 // original by: Steven Levithan (http://blog.stevenlevithan.com)
496 // reimplemented by: Brett Zamir (http://brett-zamir.me)
497 // input by: Lorenzo Pisani
499 // improved by: Brett Zamir (http://brett-zamir.me)
500 // note 1: original by http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
501 // note 1: blog post at http://blog.stevenlevithan.com/archives/parseuri
502 // note 1: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
503 // note 1: Does not replace invalid characters with '_' as in PHP,
504 // note 1: nor does it return false with
505 // note 1: a seriously malformed URL.
506 // note 1: Besides function name, is essentially the same as parseUri as
507 // note 1: well as our allowing
508 // note 1: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
509 // example 1: parse_url('http://user:pass@host/path?a=v#a')
510 // returns 1: {scheme: 'http', host: 'host', user: 'user', pass: 'pass', path: '/path', query: 'a=v', fragment: 'a'}
511 // example 2: parse_url('http://en.wikipedia.org/wiki/%22@%22_%28album%29')
512 // returns 2: {scheme: 'http', host: 'en.wikipedia.org', path: '/wiki/%22@%22_%28album%29'}
513 // example 3: parse_url('https://host.domain.tld/a@b.c/folder')
514 // returns 3: {scheme: 'https', host: 'host.domain.tld', path: '/a@b.c/folder'}
515 // example 4: parse_url('https://gooduser:secretpassword@www.example.com/a@b.c/folder?foo=bar')
516 // returns 4: { scheme: 'https', host: 'www.example.com', path: '/a@b.c/folder', query: 'foo=bar', user: 'gooduser', pass: 'secretpassword' }
520 var mode = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.mode') : undefined) || 'php'
539 // For loose we added one optional slash to post-scheme to catch file:/// (should restrict this)
542 '(?:([^:\\/?#]+):)?',
543 '(?:\\/\\/()(?:(?:()(?:([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
545 '(?:(()(?:(?:[^?#\\/]*\\/)*)()(?:[^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
548 '(?:([^:\\/?#]+):)?',
549 '(?:\\/\\/((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
550 '((((?:[^?#\\/]*\\/)*)([^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
553 '(?:(?![^:@]+:[^:@\\/]*@)([^:\\/?#.]+):)?',
555 '((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?)',
556 '(((\\/(?:[^?#](?![^?#\\/]*\\.[^?#\\/.]+(?:[?#]|$)))*\\/?)?([^?#\\/]*))',
557 '(?:\\?([^#]*))?(?:#(.*))?)'
561 var m = parser[mode].exec(str)
572 return uri[component.replace('PHP_URL_', '').toLowerCase()]
575 if (mode !== 'php') {
576 var name = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.queryKey') : undefined) || 'queryKey'
577 parser = /(?:^|&)([^&=]*)=?([^&]*)/g
579 query = uri[key[12]] || ''
580 query.replace(parser, function ($0, $1, $2) {
591 // trim function to replace whithespace after the string
592 String.prototype.rtrim = function() {
593 var trimmed = this.replace(/\s+$/g, '');
598 * Scroll the screen to the item element whose id is provided, then highlights it
600 * Note: jquery.color.js is required
602 * @param {string} elementId The item element id
603 * @returns {undefined}
605 function scrollToItem(elementId) {
606 if (typeof elementId === "undefined") {
610 var $el = $(document.getElementById(elementId));
611 // Test if the Item exists
616 // Define the colors which are used for highlighting
617 var colWhite = {backgroundColor:'#F5F5F5'};
618 var colShiny = {backgroundColor:'#FFF176'};
620 // Get the Item Position (we need to substract 100 to match correct position
621 var itemPos = $el.offset().top - 100;
623 // Scroll to the DIV with the ID (GUID)
624 $('html, body').animate({
627 // Highlight post/commenent with ID (GUID)
628 $el.animate(colWhite, 1000).animate(colShiny).animate(colWhite, 600);
632 // format a html string to pure text
633 function htmlToText(htmlString) {
634 // Replace line breaks with spaces
635 var text = htmlString.replace(/<br>/g, ' ');
636 // Strip the text out of the html string
637 text = text.replace(/<[^>]*>/g, '');
643 * Sends a /like API call and updates the display of the relevant action button
644 * before the update reloads the item.
646 * @param {string} ident The id of the relevant item
647 * @param {string} verb The verb of the action
648 * @returns {undefined}
650 function doLikeAction(ident, verb) {
653 if (verb.indexOf('attend') === 0) {
654 $('.item-' + ident + ' .button-event:not(#' + verb + '-' + ident + ')').removeClass('active');
656 $('#' + verb + '-' + ident).toggleClass('active');
657 $('#like-rotator-' + ident.toString()).show();
658 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate );
663 // Decodes a hexadecimally encoded binary string
664 function hex2bin (s) {
665 // discuss at: http://locutus.io/php/hex2bin/
666 // original by: Dumitru Uzun (http://duzun.me)
667 // example 1: hex2bin('44696d61')
669 // example 2: hex2bin('00')
671 // example 3: hex2bin('2f1q')
678 for (l = s.length; i < l; i += 2) {
679 var c = parseInt(s.substr(i, 1), 16);
680 var k = parseInt(s.substr(i + 1, 1), 16);
681 if (isNaN(c) || isNaN(k)) {
684 ret.push((c << 4) | k);
686 return String.fromCharCode.apply(String, ret);
689 // Convert binary data into hexadecimal representation
690 function bin2hex (s) {
691 // From: http://phpjs.org/functions
692 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
693 // + bugfixed by: Onno Marsman
694 // + bugfixed by: Linuxworld
695 // + improved by: ntoniazzi (http://phpjs.org/functions/bin2hex:361#comment_177616)
696 // * example 1: bin2hex('Kev');
697 // * returns 1: '4b6576'
698 // * example 2: bin2hex(String.fromCharCode(0x00));
705 for (i = 0, l = s.length; i < l; i++) {
706 n = s.charCodeAt(i).toString(16);
707 o += n.length < 2 ? "0" + n : n;
713 // Dropdown menus with the class "dropdown-head" will display the active tab
715 function toggleDropdownText(elm) {
716 $(elm).closest(".dropdown").find('.btn').html($(elm).text() + ' <span class="caret"></span>');
717 $(elm).closest(".dropdown").find('.btn').val($(elm).data('value'));
718 $(elm).closest("ul").children("li").show();
719 $(elm).parent("li").hide();
722 // Check if element does have a specific class
723 function hasClass(elem, cls) {
724 return (" " + elem.className + " " ).indexOf( " "+cls+" " ) > -1;