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