]> git.mxchange.org Git - friendica.git/blob - view/theme/frost/js/main.js
Merge pull request #4827 from AndyHee/doc20180413
[friendica.git] / view / theme / frost / js / main.js
1
2         function openClose(listID) {
3 /*              if(document.getElementById(theID).style.display == "block") {
4                         document.getElementById(theID).style.display = "none"
5                 }
6                 else {
7                         document.getElementById(theID).style.display = "block"
8                 }*/
9                 listID = "#" + listID.replace(/:/g, "\\:");
10                 listID = listID.replace(/\./g, "\\.");
11                 listID = listID.replace(/@/g, "\\@");
12
13                 if($(listID).is(":visible")) {
14                         $(listID).hide();
15                         $(listID+"-wrapper").show();
16                 }
17                 else {
18                         $(listID).show();
19                         $(listID+"-wrapper").hide();
20                 }
21         }
22
23         function openMenu(theID) {
24                 document.getElementById(theID).style.display = "block"
25         }
26
27         function closeMenu(theID) {
28                 document.getElementById(theID).style.display = "none"
29         }
30
31
32
33         var src = null;
34         var prev = null;
35         var livetime = null;
36         var stopped = false;
37         var totStopped = false;
38         var timer = null;
39         var pr = 0;
40         var liking = 0;
41         var in_progress = false;
42         var langSelect = false;
43         var commentBusy = false;
44         var last_popup_menu = null;
45         var last_popup_button = null;
46
47         $(function() {
48                 $.ajaxSetup({cache: false});
49
50                 collapseHeight();
51
52                 /* setup onoff widgets */
53                 $(".onoff input").each(function(){
54                         val = $(this).val();
55                         id = $(this).attr("id");
56                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
57
58                 });
59                 $(".onoff > a").click(function(event){
60                         event.preventDefault();
61                         var input = $(this).siblings("input");
62                         var val = 1-input.val();
63                         var id = input.attr("id");
64                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
65                         $("#"+id+"_onoff ."+ (val==1?"on":"off")).removeClass("hidden");
66                         input.val(val);
67                         //console.log(id);
68                 });
69
70                 /* popup menus */
71                 function close_last_popup_menu(e) {
72
73                         if( last_popup_menu ) {
74                                 if( '#' + last_popup_menu.attr('id') !== $(e.target).attr('rel')) {
75                                         last_popup_menu.hide();
76                                         last_popup_button.removeClass("selected");
77                                         last_popup_menu = null;
78                                         last_popup_button = null;
79                                 }
80                         }
81                 }
82                 $('a[rel^=#]').click(function(e){
83
84                         close_last_popup_menu(e);
85                         menu = $( $(this).attr('rel') );
86                         e.preventDefault();
87                         e.stopPropagation();
88
89                         if (menu.attr('popup')=="false") return false;
90
91                         $(this).parent().toggleClass("selected");
92                         menu.slideToggle('fast');
93
94                         if (menu.css("display") == "none") {
95                                 last_popup_menu = null;
96                                 last_popup_button = null;
97                         } else {
98                                 last_popup_menu = menu;
99                                 last_popup_button = $(this).parent();
100                         }
101                         return false;
102                 });
103                 $('html').click(function(e) {
104                         close_last_popup_menu(e);
105                 });
106
107                 // fancyboxes
108                 $("a.popupbox").colorbox({
109                         'inline' : true,
110                         'transition' : 'elastic'
111                 });
112
113
114                 /* notifications template */
115                 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
116                 var notifications_all = unescape($('<div>').append( $("#nav-notifications-see-all").clone() ).html()); //outerHtml hack
117                 var notifications_mark = unescape($('<div>').append( $("#nav-notifications-mark-all").clone() ).html()); //outerHtml hack
118                 var notifications_empty = unescape($("#nav-notifications-menu").html());
119
120                 /* nav update event  */
121                 $('nav').bind('nav-update', function(e,data){;
122                         var invalid = $(data).find('invalid').text();
123                         if(invalid == 1) { window.location.href=window.location.href }
124
125                         var net = $(data).find('net').text();
126                         if(net == 0) { net = ''; $('#net-update').removeClass('show') } else { $('#net-update').addClass('show') }
127                         $('#net-update').html(net);
128
129                         var home = $(data).find('home').text();
130                         if(home == 0) { home = '';  $('#home-update').removeClass('show') } else { $('#home-update').addClass('show') }
131                         $('#home-update').html(home);
132
133                         var intro = $(data).find('intro').text();
134                         if(intro == 0) { intro = '';  $('#intro-update').removeClass('show') } else { $('#intro-update').addClass('show') }
135                         $('#intro-update').html(intro);
136
137                         var mail = $(data).find('mail').text();
138                         if(mail == 0) { mail = '';  $('#mail-update').removeClass('show') } else { $('#mail-update').addClass('show') }
139                         $('#mail-update').html(mail);
140
141                         var intro = $(data).find('intro').text();
142                         if(intro == 0) { intro = '';  $('#intro-update-li').removeClass('show') } else { $('#intro-update-li').addClass('show') }
143                         $('#intro-update-li').html(intro);
144
145                         var mail = $(data).find('mail').text();
146                         if(mail == 0) { mail = '';  $('#mail-update-li').removeClass('show') } else { $('#mail-update-li').addClass('show') }
147                         $('#mail-update-li').html(mail);
148
149                         var eNotif = $(data).find('notif')
150
151                         if (eNotif.children("note").length==0){
152                                 $("#nav-notifications-menu").html(notifications_empty);
153                         } else {
154                                 nnm = $("#nav-notifications-menu");
155                                 nnm.html(notifications_all + notifications_mark);
156                                 //nnm.attr('popup','true');
157                                 eNotif.children("note").each(function(){
158                                         e = $(this);
159                                         text = e.text().format("<span class='contactname'>"+e.attr('name')+"</span>");
160                                         html = notifications_tpl.format(e.attr('href'),e.attr('photo'), text, e.attr('date'), e.attr('seen'));
161                                         nnm.append(html);
162                                 });
163
164                                 $("img[data-src]", nnm).each(function(i, el){
165                                         // Add src attribute for images with a data-src attribute
166                                         // However, don't bother if the data-src attribute is empty, because
167                                         // an empty "src" tag for an image will cause some browsers
168                                         // to prefetch the root page of the Friendica hub, which will
169                                         // unnecessarily load an entire profile/ or network/ page
170                                         if($(el).data("src") != '') $(el).attr('src', $(el).data("src"));
171                                 });
172                         }
173                         notif = eNotif.attr('count');
174                         if (notif>0){
175                                 $("#nav-notifications-linkmenu").addClass("on");
176                         } else {
177                                 $("#nav-notifications-linkmenu").removeClass("on");
178                         }
179                         if(notif == 0) { notif = ''; $('#notify-update').removeClass('show') } else { $('#notify-update').addClass('show') }
180                         $('#notify-update').html(notif);
181
182                         var eSysmsg = $(data).find('sysmsgs');
183                         eSysmsg.children("notice").each(function(){
184                                 text = $(this).text();
185                                 $.jGrowl(text, { sticky: false, theme: 'notice', life: 3000 }); // originally: sticky: true,
186                         });
187                         eSysmsg.children("info").each(function(){
188                                 text = $(this).text();
189                                 $.jGrowl(text, { sticky: false, theme: 'info', life: 1000 });
190                         });
191
192                 });
193
194
195                 NavUpdate();
196                 // Allow folks to stop the ajax page updates with the pause/break key
197                 $(document).keydown(function(event) {
198                         if(event.keyCode == '8') {
199                                 var target = event.target || event.srcElement;
200                                 if (!/input|textarea/i.test(target.nodeName)) {
201                                         return false;
202                                 }
203                         }
204                         if(event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
205                                 event.preventDefault();
206                                 if(stopped == false) {
207                                         stopped = true;
208                                         if (event.ctrlKey) {
209                                                 totStopped = true;
210                                         }
211                                         $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
212                                 } else {
213                                         unpause();
214                                 }
215                         } else {
216                                 if (!totStopped) {
217                                         unpause();
218                                 }
219                         }
220                 });
221
222
223         });
224
225         function NavUpdate() {
226
227                 if(! stopped) {
228                         var pingCmd = 'ping' + ((localUser != 0) ? '?f=&uid=' + localUser : '');
229                         $.get(pingCmd,function(data) {
230                                 $(data).find('result').each(function() {
231                                         // send nav-update event
232                                         $('nav').trigger('nav-update', this);
233
234
235                                         // start live update
236
237
238
239                                         if($('#live-network').length)   { src = 'network'; liveUpdate(); }
240                                         if($('#live-profile').length)   { src = 'profile'; liveUpdate(); }
241                                         if($('#live-community').length) { src = 'community'; liveUpdate(); }
242                                         if($('#live-notes').length)     { src = 'notes'; liveUpdate(); }
243                                         if($('#live-display').length) { src = 'display'; liveUpdate(); }
244                                         /*if($('#live-display').length) {
245                                                 if(liking) {
246                                                         liking = 0;
247                                                         window.location.href=window.location.href
248                                                 }
249                                         }*/
250                                         if($('#live-photos').length) {
251                                                 if(liking) {
252                                                         liking = 0;
253                                                         window.location.href=window.location.href
254                                                 }
255                                         }
256
257
258
259
260                                 });
261                         }) ;
262                 }
263                 timer = setTimeout(NavUpdate,updateInterval);
264         }
265
266         function liveUpdate() {
267                 if((src == null) || (stopped) || (typeof profile_uid == 'undefined') || (! profile_uid)) { $('.like-rotator').hide(); return; }
268                 if(($('.comment-edit-text-full').length) || (in_progress)) {
269                         if(livetime) {
270                                 clearTimeout(livetime);
271                         }
272                         livetime = setTimeout(liveUpdate, 5000);
273                         return;
274                 }
275                 if(livetime != null)
276                         livetime = null;
277
278                 prev = 'live-' + src;
279
280                 in_progress = true;
281                 var udargs = ((netargs.length) ? '/' + netargs : '');
282                 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page;
283
284                 $.get(update_url,function(data) {
285                         in_progress = false;
286                         //                      $('.collapsed-comments',data).each(function() {
287                         //      var ident = $(this).attr('id');
288                         //      var is_hidden = $('#' + ident).is(':hidden');
289                         //      if($('#' + ident).length) {
290                         //              $('#' + ident).replaceWith($(this));
291                         //              if(is_hidden)
292                         //                      $('#' + ident).hide();
293                         //      }
294                         //});
295
296                         // add a new thread
297
298                         $('.toplevel_item',data).each(function() {
299                                 var ident = $(this).attr('id');
300
301                                 if($('#' + ident).length == 0 && profile_page == 1) {
302                                         $('img',this).each(function() {
303                                                 $(this).attr('src',$(this).attr('dst'));
304                                         });
305                                         $('#' + prev).after($(this));
306                                 }
307                                 else {
308                                         // Find out if the hidden comments are open, so we can keep it that way
309                                         // if a new comment has been posted
310                                         var id = $('.hide-comments-total', this).attr('id');
311                                         if(typeof id != 'undefined') {
312                                                 id = id.split('-')[3];
313                                                 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
314                                         }
315
316                                         $('img',this).each(function() {
317                                                 $(this).attr('src',$(this).attr('dst'));
318                                         });
319                                         //vScroll = $(document).scrollTop();
320                                         $('html').height($('html').height());
321                                         $('#' + ident).replaceWith($(this));
322
323                                         if(typeof id != 'undefined') {
324                                                 if(commentsOpen) showHideComments(id);
325                                         }
326                                         $('html').height('auto');
327                                         //$(document).scrollTop(vScroll);
328                                 }
329
330                                 // Add Colorbox for viewing Network page images
331                                 $("#" + ident + " .wall-item-body a img").each(function(){
332                                         var aElem = $(this).parent();
333                                         var imgHref = aElem.attr("href");
334
335                                         // We need to make sure we only put a Colorbox on links to Friendica images
336                                         // We'll try to do this by looking for links of the form
337                                         // .../photo/ab803d8eg08daf85023adfec08(-0.jpg) (with nothing more following), in hopes
338                                         // that that will be unique enough
339                                         if(imgHref.match(/\/photo\/[a-fA-F0-9]+(-[0-9]\.[\w]+?)?$/)) {
340
341                                                 // Add a unique class to all the images of a certain post, to allow scrolling through
342                                                 var cBoxClass = $(this).closest(".wall-item-body").attr("id") + "-lightbox";
343                                                 $(this).addClass(cBoxClass);
344
345                                                 aElem.colorbox({
346                                                         maxHeight: '90%',
347                                                         photo: true,
348                                                         rel: cBoxClass
349                                                 });
350                                         }
351                                 });
352
353                                 prev = ident;
354                         });
355
356                         // reset vars for inserting individual items
357
358                         /*prev = 'live-' + src;
359
360                         $('.wall-item-outside-wrapper',data).each(function() {
361                                 var ident = $(this).attr('id');
362
363                                 if($('#' + ident).length == 0 && prev != 'live-' + src) {
364                                                 $('img',this).each(function() {
365                                                         $(this).attr('src',$(this).attr('dst'));
366                                                 });
367                                                 $('#' + prev).after($(this));
368                                 }
369                                 else {
370                                         $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago'));
371                                         if($('#' + ident + ' ' + '.comment-edit-text-empty').length)
372                                                 $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
373                                         $('#' + ident + ' ' + '.hide-comments-total').replaceWith($(this).find('.hide-comments-total'));
374                                         $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
375                                         $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
376                                         $('#' + ident + ' ' + '.my-comment-photo').each(function() {
377                                                 $(this).attr('src',$(this).attr('dst'));
378                                         });
379                                 }
380                                 prev = ident;
381                         });*/
382
383                         $('.like-rotator').hide();
384                         if(commentBusy) {
385                                 commentBusy = false;
386                                 $('body').css('cursor', 'auto');
387                         }
388                         /* autocomplete @nicknames */
389                         $(".comment-edit-form  textarea").editor_autocomplete(baseurl+"/acl");
390
391                         collapseHeight();
392
393                         // setup videos, since VideoJS won't take care of any loaded via AJAX
394                         if(typeof videojs != 'undefined') videojs.autoSetup();
395                 });
396         }
397
398         function collapseHeight(elems) {
399                 var elemName = '.wall-item-body:not(.divmore)';
400                 if(typeof elems != 'undefined') {
401                         elemName = elems + ' ' + elemName;
402                 }
403                 $(elemName).each(function() {
404                         if($(this).height() > 450) {
405                                 $('html').height($('html').height());
406                                 $(this).divgrow({ initialHeight: 400, showBrackets: false, speed: 0 });
407                                 $(this).addClass('divmore');
408                                 $('html').height('auto');
409                         }
410                 });
411         }
412
413         /*function imgbright(node) {
414                 $(node).removeClass("drophide").addClass("drop");
415         }
416
417         function imgdull(node) {
418                 $(node).removeClass("drop").addClass("drophide");
419         }*/
420
421         // Since our ajax calls are asynchronous, we will give a few
422         // seconds for the first ajax call (setting like/dislike), then
423         // run the updater to pick up any changes and display on the page.
424         // The updater will turn any rotators off when it's done.
425         // This function will have returned long before any of these
426         // events have completed and therefore there won't be any
427         // visible feedback that anything changed without all this
428         // trickery. This still could cause confusion if the "like" ajax call
429         // is delayed and NavUpdate runs before it completes.
430
431         function dolike(ident,verb) {
432                 unpause();
433                 $('#like-rotator-' + ident.toString()).show();
434                 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate );
435                 liking = 1;
436         }
437
438         function dostar(ident) {
439                 ident = ident.toString();
440 //              $('#like-rotator-' + ident).show();
441                 $.get('starred/' + ident, function(data) {
442                         if(data.match(/1/)) {
443                                 $('#starred-' + ident).addClass('starred');
444                                 $('#starred-' + ident).removeClass('unstarred');
445                                 $('#star-' + ident).addClass('hidden');
446                                 $('#unstar-' + ident).removeClass('hidden');
447                         }
448                         else {
449                                 $('#starred-' + ident).addClass('unstarred');
450                                 $('#starred-' + ident).removeClass('starred');
451                                 $('#star-' + ident).removeClass('hidden');
452                                 $('#unstar-' + ident).addClass('hidden');
453                         }
454 //                      $('#like-rotator-' + ident).hide();
455                 });
456         }
457
458         function getPosition(e) {
459                 var cursor = {x:0, y:0};
460                 if ( e.pageX || e.pageY  ) {
461                         cursor.x = e.pageX;
462                         cursor.y = e.pageY;
463                 }
464                 else {
465                         if( e.clientX || e.clientY ) {
466                                 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
467                                 cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
468                         }
469                         else {
470                                 if( e.x || e.y ) {
471                                         cursor.x = e.x;
472                                         cursor.y = e.y;
473                                 }
474                         }
475                 }
476                 return cursor;
477         }
478
479         var lockvisible = false;
480
481         function lockview(event,id) {
482                 event = event || window.event;
483                 cursor = getPosition(event);
484                 if(lockvisible) {
485                         lockviewhide();
486                 }
487                 else {
488                         lockvisible = true;
489                         $.get('lockview/' + id, function(data) {
490                                 $('#panel').html(data);
491                                 $('#panel').css({ 'left': cursor.x + 5 , 'top': cursor.y + 5});
492                                 $('#panel').show();
493                         });
494                 }
495         }
496
497         function lockviewhide() {
498                 lockvisible = false;
499                 $('#panel').hide();
500         }
501
502         function post_comment(id) {
503                 unpause();
504                 commentBusy = true;
505                 $('body').css('cursor', 'wait');
506                 $("#comment-preview-inp-" + id).val("0");
507                 $.post(
508              "item",
509              $("#comment-edit-form-" + id).serialize(),
510                         function(data) {
511                                 if(data.success) {
512                                         $("#comment-edit-wrapper-" + id).hide();
513                                         $("#comment-edit-text-" + id).val('');
514                                 var tarea = document.getElementById("comment-edit-text-" + id);
515                                         if(tarea)
516                                                 commentClose(tarea,id);
517                                         if(timer) clearTimeout(timer);
518                                         timer = setTimeout(NavUpdate,10);
519                                 }
520                                 if(data.reload) {
521                                         window.location.href=data.reload;
522                                 }
523                         },
524                         "json"
525          );
526          return false;
527         }
528
529
530         function preview_comment(id) {
531                 $("#comment-preview-inp-" + id).val("1");
532                 $("#comment-edit-preview-" + id).show();
533                 $.post(
534              "item",
535              $("#comment-edit-form-" + id).serialize(),
536                         function(data) {
537                                 if(data.preview) {
538
539                                         $("#comment-edit-preview-" + id).html(data.preview);
540                                         $("#comment-edit-preview-" + id + " a").click(function() { return false; });
541                                 }
542                         },
543                         "json"
544          );
545          return true;
546         }
547
548
549         function showHideComments(id) {
550                 if( $("#collapsed-comments-" + id).is(":visible")) {
551                         $("#collapsed-comments-" + id).hide();
552                         $("#hide-comments-" + id).html(window.showMore);
553                 }
554                 else {
555                         $("#collapsed-comments-" + id).show();
556                         $("#hide-comments-" + id).html(window.showFewer);
557                         collapseHeight("#collapsed-comments-" + id);
558                 }
559         }
560
561
562         function preview_post() {
563                 $("#jot-preview").val("1");
564                 $("#jot-preview-content").show();
565                 $.post(
566                         "item",
567                         $("#profile-jot-form").serialize(),
568                         function(data) {
569                                 if(data.preview) {
570                                         $("#jot-preview-content").html(data.preview);
571                                         $("#jot-preview-content" + " a").click(function() { return false; });
572                                 }
573                         },
574                         "json"
575                 );
576                 $("#jot-preview").val("0");
577                 return true;
578         }
579
580
581         function unpause() {
582                 // unpause auto reloads if they are currently stopped
583                 totStopped = false;
584                 stopped = false;
585             $('#pause').html('');
586         }
587                 
588
589     function bin2hex(s){  
590         // Converts the binary representation of data to hex    
591         //   
592         // version: 812.316  
593         // discuss at: http://phpjs.org/functions/bin2hex  
594         // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
595         // +   bugfixed by: Onno Marsman  
596         // +   bugfixed by: Linuxworld  
597         // *     example 1: bin2hex('Kev');  
598         // *     returns 1: '4b6576'  
599         // *     example 2: bin2hex(String.fromCharCode(0x00));  
600         // *     returns 2: '00'  
601         var v,i, f = 0, a = [];  
602         s += '';  
603         f = s.length;  
604           
605         for (i = 0; i<f; i++) {  
606             a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");  
607         }  
608           
609         return a.join('');  
610     }  
611
612         function groupChangeMember(gid, cid, sec_token) {
613                 $('body .fakelink').css('cursor', 'wait');
614                 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
615                                 $('#group-update-wrapper').html(data);
616                                 $('body .fakelink').css('cursor', 'auto');
617                 });
618         }
619
620         function profChangeMember(gid,cid) {
621                 $('body .fakelink').css('cursor', 'wait');
622                 $.get('profperm/' + gid + '/' + cid, function(data) {
623                                 $('#prof-update-wrapper').html(data);
624                                 $('body .fakelink').css('cursor', 'auto');
625                 });
626         }
627
628         function contactgroupChangeMember(gid,cid) {
629                 $('body').css('cursor', 'wait');
630                 $.get('contactgroup/' + gid + '/' + cid, function(data) {
631                                 $('body').css('cursor', 'auto');
632                 });
633         }
634
635
636 function checkboxhighlight(box) {
637   if($(box).is(':checked')) {
638         $(box).addClass('checkeditem');
639   }
640   else {
641         $(box).removeClass('checkeditem');
642   }
643 }
644
645 function notifyMarkAll() {
646         $.get('notify/mark/all', function(data) {
647                 if(timer) clearTimeout(timer);
648                 timer = setTimeout(NavUpdate,1000);
649         });
650 }
651
652 /**
653  * sprintf in javascript
654  *      "{0} and {1}".format('zero','uno');
655  **/
656 String.prototype.format = function() {
657     var formatted = this;
658     for (var i = 0; i < arguments.length; i++) {
659         var regexp = new RegExp('\\{'+i+'\\}', 'gi');
660         formatted = formatted.replace(regexp, arguments[i]);
661     }
662     return formatted;
663 };
664 // Array Remove
665 Array.prototype.remove = function(item) {
666   to=undefined; from=this.indexOf(item);
667   var rest = this.slice((to || from) + 1 || this.length);
668   this.length = from < 0 ? this.length + from : from;
669   return this.push.apply(this, rest);
670 };
671
672 function previewTheme(elm) {
673         theme = $(elm).val();
674         $.getJSON('pretheme?f=&theme=' + theme,function(data) {
675                         $('#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>');
676         });
677
678 }