]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/theme.js
f3bd543c170a5601110556d63d49b14efa1e39ba
[friendica.git] / view / theme / frio / js / theme.js
1
2 var jotcache = ''; //The jot cache. We use it as cache to restore old/original jot content
3
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();
9                 } else {
10                         $("#back-to-top").fadeOut();
11                 }
12         });
13  
14         // scroll body to 0px on click
15         $("#back-to-top").click(function () {
16                 $("body,html").animate({
17                         scrollTop: 0
18                 }, 400);
19                 return false;
20         });
21
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");
25         }
26
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");
30         }
31
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");
35         }
36
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");
41
42         // move the tabbar to the second nav bar
43         if( $("ul.tabbar").length ) {
44                 $("ul.tabbar").appendTo("#topbar-second > .container > #tabmenu");
45         }
46
47         // add mask css url to the logo-img container
48         //
49         // This is for firefox - we use a mask which looks like the friendica logo to apply user collers
50         // to the friendica logo (the mask is in nav.tpl at the botom). To make it work we need to apply the
51         // correct url. The only way which comes to my mind was to do this with js
52         // So we apply the correct url (with the link to the id of the mask) after the page is loaded.
53         if($("#logo-img").length ) {
54                 var pageurl = "url('" + window.location.href + "#logo-mask')";
55                 $("#logo-img").css({"mask": pageurl});
56         }
57
58         // make responsive tabmenu with flexmenu.js
59         // the menupoints which doesn't fit in the second nav bar will moved to a 
60         // dropdown menu. Look at common_tabs.tpl
61         $("ul.tabs.flex-nav").flexMenu({
62                 'cutoff': 2,
63                 'popupClass': "dropdown-menu pull-right",
64                 'popupAbsolute': false,
65                 'target': ".flex-target"
66         });
67
68         // add Jot botton to the scecond navbar
69         if( $("section #jotOpen").length ) {
70                 $("section #jotOpen").appendTo("#topbar-second > .container > #navbar-button");
71                 if( $("#jot-popup").is(":hidden")) $("#topbar-second > .container > #navbar-button #jotOpen").hide();
72         }
73
74         // show bulk deletion button at network page if checkbox is checked
75         $("body").change("input.item-select", function(){
76                 var checked = false;
77
78                 // We need to get all checked items, so it would close the delete button
79                 // if we uncheck one item and others are still checked.
80                 // So return checked = true if there is any checked item
81                 $('input.item-select').each( function() {
82                         if($(this).is(':checked')) {
83                                 checked = true;
84                                 return false;
85                         }
86                 });
87                 
88                 if(checked == true) {
89                         $("a#item-delete-selected").fadeTo(400, 1);
90                         $("a#item-delete-selected").show();
91                 } else {
92                         $("a#item-delete-selected").fadeTo(400, 0, function(){
93                                 $("a#item-delete-selected").hide();
94                         });     
95                 }
96         });
97                 
98         //$('ul.flex-nav').flexMenu();
99
100         // initialize the bootstrap tooltips
101         $('body').tooltip({
102                 selector: '[data-toggle="tooltip"]',
103                 animation: true,
104                 html: true,
105                 placement: 'auto',
106                 trigger: 'hover',
107                 delay: {
108                         show: 500,
109                         hide: 100
110                 }
111         });
112
113         // initialize the bootstrap-select
114         $('.selectpicker').selectpicker();
115
116         // add search-heading to the seccond navbar
117         if( $(".search-heading").length) {
118                 $(".search-heading").appendTo("#topbar-second > .container > #tabmenu");
119         }
120
121         // add search results heading to the second navbar
122         // and insert the search value to the top nav search input
123         if( $(".search-content-wrapper").length ) {
124                 // get the text of the heading (we catch the plain text because we don't
125                 // want to have a h4 heading in the navbar
126                 var searchText = $(".section-title-wrapper > h2").text();
127                 // insert the plain text in a <h4> heading and give it a class
128                 var newText = '<h4 class="search-heading">'+searchText+'</h4>';
129                 // append the new heading to the navbar
130                 $("#topbar-second > .container > #tabmenu").append(newText);
131
132                 // try to get the value of the original search input to insert it 
133                 // as value in the nav-search-input
134                 var searchValue = $("#search-wrapper .form-group-search input").val();
135
136                 // if the orignal search value isn't available use the location path as value
137                 if( typeof searchValue === "undefined") {
138                         // get the location path
139                         var urlPath = window.location.search
140                         // and split it up in its parts
141                         var splitPath = urlPath.split(/(\?search?=)(.*$)/);
142
143                         if(typeof splitPath[2] !== 'undefined') {
144                                 // decode the path (e.g to decode %40 to the character @)
145                                 var searchValue = decodeURIComponent(splitPath[2]);
146                         }
147                 }
148
149                 if( typeof searchValue !== "undefined") {
150                         $("#nav-search-input-field").val(searchValue);
151                 }
152
153         }
154
155         // move the "Save the search" button to the second navbar
156         $(".search-content-wrapper #search-save-form ").appendTo("#topbar-second > .container > #navbar-button");
157
158 });
159 //function commentOpenUI(obj, id) {
160 //      $(document).unbind( "click.commentOpen", handler );
161 //
162 //      var handler = function() {
163 //              if(obj.value == '{{$comment}}') {
164 //                      obj.value = '';
165 //                      $("#comment-edit-text-" + id).addClass("comment-edit-text-full").removeClass("comment-edit-text-empty");
166 //                      // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
167 //                      // The submit button gets tabindex + 1
168 //                      $("#comment-edit-text-" + id).attr('tabindex','9');
169 //                      $("#comment-edit-submit-" + id).attr('tabindex','10');
170 //                      $("#comment-edit-submit-wrapper-" + id).show();
171 //              }
172 //      };
173 //
174 //      $(document).bind( "click.commentOpen", handler );
175 //}
176 //
177 //function commentCloseUI(obj, id) {
178 //      $(document).unbind( "click.commentClose", handler );
179 //
180 //      var handler = function() {
181 //              if(obj.value === '') {
182 //              obj.value = '{{$comment}}';
183 //                      $("#comment-edit-text-" + id).removeClass("comment-edit-text-full").addClass("comment-edit-text-empty");
184 //                      $("#comment-edit-text-" + id).removeAttr('tabindex');
185 //                      $("#comment-edit-submit-" + id).removeAttr('tabindex');
186 //                      $("#comment-edit-submit-wrapper-" + id).hide();
187 //              }
188 //      };
189 //
190 //      $(document).bind( "click.commentClose", handler );
191 //}
192
193 function openClose(theID) {
194         var elem = document.getElementById(theID);
195
196         if( $(elem).is(':visible')) {
197                 $(elem).slideUp(200);
198         }
199         else {
200                 $(elem).slideDown(200);
201         }
202 }
203
204 function showHide(theID) {
205         if(document.getElementById(theID).style.display == "block") {
206                 document.getElementById(theID).style.display = "none"
207         }
208         else {
209                 document.getElementById(theID).style.display = "block"
210         }
211 }
212
213
214 function showHideComments(id) {
215         if( $('#collapsed-comments-' + id).is(':visible')) {
216                 $('#collapsed-comments-' + id).slideUp();
217                 $('#hide-comments-' + id).html(window.showMore);
218                 $('#hide-comments-total-' + id).show();
219         }
220         else {
221                 $('#collapsed-comments-' + id).slideDown();
222                 $('#hide-comments-' + id).html(window.showFewer);
223                 $('#hide-comments-total-' + id).hide();
224         }
225 }
226
227
228 function justifyPhotos() {
229         justifiedGalleryActive = true;
230         $('#photo-album-contents').justifiedGallery({
231                 margins: 3,
232                 border: 0,
233                 sizeRangeSuffixes: {
234                         'lt100': '-2',
235                         'lt240': '-2',
236                         'lt320': '-2',
237                         'lt500': '',
238                         'lt640': '-1',
239                         'lt1024': '-0'
240                 }
241         }).on('jg.complete', function(e){ justifiedGalleryActive = false; });
242 }
243
244 function justifyPhotosAjax() {
245         justifiedGalleryActive = true;
246         $('#photo-album-contents').justifiedGallery('norewind').on('jg.complete', function(e){ justifiedGalleryActive = false; });
247 }
248
249 function loadScript(url, callback) {
250         // Adding the script tag to the head as suggested before
251         var head = document.getElementsByTagName('head')[0];
252         var script = document.createElement('script');
253         script.type = 'text/javascript';
254         script.src = url;
255
256         // Then bind the event to the callback function.
257         // There are several events for cross browser compatibility.
258         script.onreadystatechange = callback;
259         script.onload = callback;
260
261         // Fire the loading
262         head.appendChild(script);
263 }
264
265 function random_digits(digits) {
266         var rn = "";
267         var rnd = "";
268
269         for(var i = 0; i < digits; i++) {
270                 var rn = Math.round(Math.random() * (9));
271                 rnd += rn;
272         }
273
274         return rnd;
275 }
276
277 // Does we need a ? or a & to append values to a url
278 function qOrAmp(url) {
279         if(url.search('\\?') < 0) {
280                 return '?';
281         } else {
282                 return '&';
283         }
284 }
285
286 function contact_filter(item) {
287         // get the html content from the js template of the contact-wrapper
288         contact_tpl = unescape($(".javascript-template[rel=contact-template]").html());
289
290         var variables = {
291                         id:             item.id,
292                         name:           item.name,
293                         username:       item.username,
294                         thumb:          item.thumb,
295                         img_hover:      item.img_hover,
296                         edit_hover:     item.edit_hover,
297                         account_type:   item.account_type,
298                         photo_menu:     item.photo_menu,
299                         alt_text:       item.alt_text,
300                         dir_icon:       item.dir_icon,
301                         sparkle:        item.sparkle,
302                         itemurl:        item.itemurl,
303                         url:            item.url,
304                         network:        item.network,
305                         tags:           item.tags,
306                         details:        item.details,
307         };
308
309         // open a new jSmart instance with the template
310         var tpl = new jSmart (contact_tpl);
311
312         // replace the variable with the values
313         var html = tpl.fetch(variables);
314
315         return html;
316 }
317
318 function filter_replace(item) {
319
320         return item.name;
321 }
322
323 (function( $ ) {
324         $.fn.contact_filter = function(backend_url, typ, autosubmit, onselect) {
325                 if(typeof typ === 'undefined') typ = '';
326                 if(typeof autosubmit === 'undefined') autosubmit = false;
327
328                 // Autocomplete contacts
329                 contacts = {
330                         match: /(^)([^\n]+)$/,
331                         index: 2,
332                         search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
333                         replace: filter_replace,
334                         template: contact_filter,
335                 };
336
337                 this.attr('autocomplete','off');
338                 var a = this.textcomplete([contacts], {className:'accontacts', appendTo: '#contact-list'});
339
340                 a.on('textComplete:select', function(e, value, strategy) { $(".dropdown-menu.textcomplete-dropdown.media-list").show(); });
341         };
342 })( jQuery );
343
344
345 // current time in milliseconds, to send each request to make sure
346 // we 're not getting 304 response
347 function timeNow() {
348         return new Date().getTime();
349 }
350
351 String.prototype.normalizeLink = function () {
352         var ret = this.replace('https:', 'http:');
353         var ret = ret.replace('//www', '//');
354         return ret.rtrim();
355 };
356
357 function cleanContactUrl(url) {
358         var parts = parseUrl(url);
359
360         if(! ("scheme" in parts) || ! ("host" in parts)) {
361                 return url;
362         }
363
364         var newUrl =parts["scheme"] + "://" + parts["host"];
365
366         if("port" in parts) {
367                 newUrl += ":" + parts["port"];
368         }
369
370         if("path" in parts) {
371                 newUrl += parts["path"];
372         }
373
374 //      if(url != newUrl) {
375 //              console.log("Cleaned contact url " + url + " to " + newUrl);
376 //      }
377
378         return newUrl;
379 }
380
381 function parseUrl (str, component) { // eslint-disable-line camelcase
382         //       discuss at: http://locutusjs.io/php/parse_url/
383         //      original by: Steven Levithan (http://blog.stevenlevithan.com)
384         // reimplemented by: Brett Zamir (http://brett-zamir.me)
385         //         input by: Lorenzo Pisani
386         //         input by: Tony
387         //      improved by: Brett Zamir (http://brett-zamir.me)
388         //           note 1: original by http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
389         //           note 1: blog post at http://blog.stevenlevithan.com/archives/parseuri
390         //           note 1: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
391         //           note 1: Does not replace invalid characters with '_' as in PHP,
392         //           note 1: nor does it return false with
393         //           note 1: a seriously malformed URL.
394         //           note 1: Besides function name, is essentially the same as parseUri as
395         //           note 1: well as our allowing
396         //           note 1: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
397         //        example 1: parse_url('http://user:pass@host/path?a=v#a')
398         //        returns 1: {scheme: 'http', host: 'host', user: 'user', pass: 'pass', path: '/path', query: 'a=v', fragment: 'a'}
399         //        example 2: parse_url('http://en.wikipedia.org/wiki/%22@%22_%28album%29')
400         //        returns 2: {scheme: 'http', host: 'en.wikipedia.org', path: '/wiki/%22@%22_%28album%29'}
401         //        example 3: parse_url('https://host.domain.tld/a@b.c/folder')
402         //        returns 3: {scheme: 'https', host: 'host.domain.tld', path: '/a@b.c/folder'}
403         //        example 4: parse_url('https://gooduser:secretpassword@www.example.com/a@b.c/folder?foo=bar')
404         //        returns 4: { scheme: 'https', host: 'www.example.com', path: '/a@b.c/folder', query: 'foo=bar', user: 'gooduser', pass: 'secretpassword' }
405
406         var query
407
408         var mode = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.mode') : undefined) || 'php'
409
410         var key = [
411                 'source',
412                 'scheme',
413                 'authority',
414                 'userInfo',
415                 'user',
416                 'pass',
417                 'host',
418                 'port',
419                 'relative',
420                 'path',
421                 'directory',
422                 'file',
423                 'query',
424                 'fragment'
425         ]
426
427         // For loose we added one optional slash to post-scheme to catch file:/// (should restrict this)
428         var parser = {
429                 php: new RegExp([
430                         '(?:([^:\\/?#]+):)?',
431                         '(?:\\/\\/()(?:(?:()(?:([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
432                         '()',
433                         '(?:(()(?:(?:[^?#\\/]*\\/)*)()(?:[^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
434                 ].join('')),
435                 strict: new RegExp([
436                         '(?:([^:\\/?#]+):)?',
437                         '(?:\\/\\/((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
438                         '((((?:[^?#\\/]*\\/)*)([^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
439                 ].join('')),
440                 loose: new RegExp([
441                         '(?:(?![^:@]+:[^:@\\/]*@)([^:\\/?#.]+):)?',
442                         '(?:\\/\\/\\/?)?',
443                         '((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?)',
444                         '(((\\/(?:[^?#](?![^?#\\/]*\\.[^?#\\/.]+(?:[?#]|$)))*\\/?)?([^?#\\/]*))',
445                         '(?:\\?([^#]*))?(?:#(.*))?)'
446                 ].join(''))
447         }
448
449         var m = parser[mode].exec(str)
450         var uri = {}
451         var i = 14
452
453         while (i--) {
454                 if (m[i]) {
455                         uri[key[i]] = m[i]
456                 }
457         }
458
459         if (component) {
460                 return uri[component.replace('PHP_URL_', '').toLowerCase()]
461         }
462
463         if (mode !== 'php') {
464                 var name = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.queryKey') : undefined) || 'queryKey'
465                 parser = /(?:^|&)([^&=]*)=?([^&]*)/g
466                 uri[name] = {}
467                 query = uri[key[12]] || ''
468                 query.replace(parser, function ($0, $1, $2) {
469                         if ($1) {
470                                 uri[name][$1] = $2
471                         }
472                 })
473         }
474
475         delete uri.source
476         return uri
477 }
478
479 // trim function to replace whithespace after the string
480 String.prototype.rtrim = function() {
481         var trimmed = this.replace(/\s+$/g, '');
482         return trimmed;
483 };