]> git.mxchange.org Git - friendica.git/blob - js/main.js
Merge https://github.com/friendica/friendica into pull
[friendica.git] / js / main.js
1
2   function openClose(theID) {
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   }
10
11   function openMenu(theID) {
12       document.getElementById(theID).style.display = "block" 
13   }
14
15   function closeMenu(theID) {
16       document.getElementById(theID).style.display = "none" 
17   }
18
19
20
21         var src = null;
22         var prev = null;
23         var livetime = null;
24         var msie = false;
25         var stopped = false;
26         var totStopped = false;
27         var timer = null;
28         var pr = 0;
29         var liking = 0;
30         var in_progress = false;
31         var langSelect = false;
32         var commentBusy = false;
33         var last_popup_menu = null;
34         var last_popup_button = null;
35
36         $(function() {
37                 $.ajaxSetup({cache: false});
38
39                 msie = $.browser.msie ;
40                 
41                 /* setup tooltips *//*
42                 $("a,.tt").each(function(){
43                         var e = $(this);
44                         var pos="bottom";
45                         if (e.hasClass("tttop")) pos="top";
46                         if (e.hasClass("ttbottom")) pos="bottom";
47                         if (e.hasClass("ttleft")) pos="left";
48                         if (e.hasClass("ttright")) pos="right";
49                         e.tipTip({defaultPosition: pos, edgeOffset: 8});
50                 });*/
51                 
52                 
53                 
54                 /* setup onoff widgets */
55                 $(".onoff input").each(function(){
56                         val = $(this).val();
57                         id = $(this).attr("id");
58                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
59                         
60                 });
61                 $(".onoff > a").click(function(event){
62                         event.preventDefault(); 
63                         var input = $(this).siblings("input");
64                         var val = 1-input.val();
65                         var id = input.attr("id");
66                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
67                         $("#"+id+"_onoff ."+ (val==1?"on":"off")).removeClass("hidden");
68                         input.val(val);
69                         //console.log(id);
70                 });
71                 
72                 /* setup field_richtext */
73                 setupFieldRichtext();
74
75                 /* popup menus */
76         function close_last_popup_menu() {
77                 if(last_popup_menu) {
78                 last_popup_menu.hide();
79                 last_popup_button.removeClass("selected");
80                 last_popup_menu = null;
81                 last_popup_button = null;
82                 }
83                 }
84                 $('a[rel^=#]').click(function(e){
85                         close_last_popup_menu();
86                         menu = $( $(this).attr('rel') );
87                         e.preventDefault();
88                         e.stopPropagation();
89                         if (menu.attr('popup')=="false") return false;
90                         $(this).parent().toggleClass("selected");
91                         menu.toggle();
92                         if (menu.css("display") == "none") {
93                                 last_popup_menu = null;
94                                 last_popup_button = null;
95                         } else {
96                                 last_popup_menu = menu;
97                                 last_popup_button = $(this).parent();
98                         }
99                         return false;
100                 });
101                 $('html').click(function() {
102                                                 close_last_popup_menu();
103                 });
104                 
105                 // fancyboxes
106                 $("a.popupbox").fancybox({
107                         'transitionIn' : 'elastic',
108                         'transitionOut' : 'elastic'
109                 });
110                 
111
112                 /* notifications template */
113                 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
114                 var notifications_all = unescape($('<div>').append( $("#nav-notifications-see-all").clone() ).html()); //outerHtml hack
115                 var notifications_mark = unescape($('<div>').append( $("#nav-notifications-mark-all").clone() ).html()); //outerHtml hack
116                 var notifications_empty = unescape($("#nav-notifications-menu").html());
117                 
118                 /* nav update event  */
119                 $('nav').bind('nav-update', function(e,data){;
120                         var net = $(data).find('net').text();
121                         if(net == 0) { net = ''; $('#net-update').removeClass('show') } else { $('#net-update').addClass('show') }
122                         $('#net-update').html(net);
123
124                         var home = $(data).find('home').text();
125                         if(home == 0) { home = '';  $('#home-update').removeClass('show') } else { $('#home-update').addClass('show') }
126                         $('#home-update').html(home);
127                         
128                         var intro = $(data).find('intro').text();
129                         if(intro == 0) { intro = '';  $('#intro-update').removeClass('show') } else { $('#intro-update').addClass('show') }
130                         $('#intro-update').html(intro);
131
132                         var mail = $(data).find('mail').text();
133                         if(mail == 0) { mail = '';  $('#mail-update').removeClass('show') } else { $('#mail-update').addClass('show') }
134                         $('#mail-update').html(mail);
135                         
136                         var intro = $(data).find('intro').text();
137                         if(intro == 0) { intro = '';  $('#intro-update-li').removeClass('show') } else { $('#intro-update-li').addClass('show') }
138                         $('#intro-update-li').html(intro);
139
140                         var mail = $(data).find('mail').text();
141                         if(mail == 0) { mail = '';  $('#mail-update-li').removeClass('show') } else { $('#mail-update-li').addClass('show') }
142                         $('#mail-update-li').html(mail);
143
144                         var eNotif = $(data).find('notif')
145                         
146                         if (eNotif.children("note").length==0){
147                                 $("#nav-notifications-menu").html(notifications_empty);
148                         } else {
149                                 nnm = $("#nav-notifications-menu");
150                                 nnm.html(notifications_all + notifications_mark);
151                                 //nnm.attr('popup','true');
152                                 eNotif.children("note").each(function(){
153                                         e = $(this);
154                                         text = e.text().format("<span class='contactname'>"+e.attr('name')+"</span>");
155                                         html = notifications_tpl.format(e.attr('href'),e.attr('photo'), text, e.attr('date'), e.attr('seen'));
156                                         nnm.append(html);
157                                 });
158                         }
159                         notif = eNotif.attr('count');
160                         if (notif>0){
161                                 $("#nav-notifications-linkmenu").addClass("on");
162                         } else {
163                                 $("#nav-notifications-linkmenu").removeClass("on");
164                         }
165                         if(notif == 0) { notif = ''; $('#notify-update').removeClass('show') } else { $('#notify-update').addClass('show') }
166                         $('#notify-update').html(notif);
167                         
168                         var eSysmsg = $(data).find('sysmsgs');
169                         eSysmsg.children("notice").each(function(){
170                                 text = $(this).text();
171                                 $.jGrowl(text, { sticky: true, theme: 'notice' });
172                         });
173                         eSysmsg.children("info").each(function(){
174                                 text = $(this).text();
175                                 $.jGrowl(text, { sticky: false, theme: 'info', life: 10000 });
176                         });
177                         
178                 });
179                 
180                 
181                 NavUpdate(); 
182                 // Allow folks to stop the ajax page updates with the pause/break key
183                 $(document).keydown(function(event) {
184                         if(event.keyCode == '8') {
185                                 var target = event.target || event.srcElement;
186                                 if (!/input|textarea/i.test(target.nodeName)) {
187                                         return false;
188                                 }
189                         }
190                         if(event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
191                                 event.preventDefault();
192                                 if(stopped == false) {
193                                         stopped = true;
194                                         if (event.ctrlKey) {
195                                                 totStopped = true;
196                                         }
197                                         $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
198                                 } else {
199                                         unpause();
200                                 }
201                         } else {
202                                 if (!totStopped) {
203                                         unpause();
204                                 }
205                         }
206                 });
207                 
208                 
209         });
210
211         function NavUpdate() {
212
213                 if(! stopped) {
214                         $.get("ping",function(data) {
215                                 $(data).find('result').each(function() {
216                                         // send nav-update event
217                                         $('nav').trigger('nav-update', this);
218                                         
219                                         
220                                         // start live update
221
222                                         if($('#live-network').length)   { src = 'network'; liveUpdate(); }
223                                         if($('#live-profile').length)   { src = 'profile'; liveUpdate(); }
224                                         if($('#live-community').length) { src = 'community'; liveUpdate(); }
225                                         if($('#live-notes').length)     { src = 'notes'; liveUpdate(); }
226                                         if($('#live-display').length) {
227                                                 if(liking) {
228                                                         liking = 0;
229                                                         window.location.href=window.location.href 
230                                                 }
231                                         }
232                                         if($('#live-photos').length) { 
233                                                 if(liking) {
234                                                         liking = 0;
235                                                         window.location.href=window.location.href 
236                                                 }
237                                         }
238
239                                         
240                                         
241                                         
242                                 });
243                         }) ;
244                 }
245                 timer = setTimeout(NavUpdate,updateInterval);
246         }
247
248         function liveUpdate() {
249                 if((src == null) || (stopped) || (! profile_uid)) { $('.like-rotator').hide(); return; }
250                 if(($('.comment-edit-text-full').length) || (in_progress)) {
251                         if(livetime) {
252                                 clearTimeout(livetime);
253                         }
254                         livetime = setTimeout(liveUpdate, 10000);
255                         return;
256                 }
257                 if(livetime != null)
258                         livetime = null;
259
260                 prev = 'live-' + src;
261
262                 in_progress = true;
263                 var udargs = ((netargs.length) ? '/' + netargs : '');
264                 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&msie=' + ((msie) ? 1 : 0);
265
266                 $.get(update_url,function(data) {
267                         in_progress = false;
268                         //                      $('.collapsed-comments',data).each(function() {
269                         //      var ident = $(this).attr('id');
270                         //      var is_hidden = $('#' + ident).is(':hidden');
271                         //      if($('#' + ident).length) {
272                         //              $('#' + ident).replaceWith($(this));
273                         //              if(is_hidden)
274                         //                      $('#' + ident).hide();
275                         //      }
276                         //});
277
278                         // add a new thread
279
280                         $('.tread-wrapper',data).each(function() {
281                                 var ident = $(this).attr('id');
282
283                                 if($('#' + ident).length == 0 && profile_page == 1) {
284                                         $('img',this).each(function() {
285                                                 $(this).attr('src',$(this).attr('dst'));
286                                         });
287                                         $('#' + prev).after($(this));
288                                 }
289                                 else {
290                                         $('img',this).each(function() {
291                                                 $(this).attr('src',$(this).attr('dst'));
292                                         });
293                                         $('#' + ident).replaceWith($(this));
294                                 }
295                                 prev = ident;
296                         });
297
298                         // reset vars for inserting individual items
299
300                         /*                      prev = 'live-' + src;
301
302                         $('.wall-item-outside-wrapper',data).each(function() {
303                                 var ident = $(this).attr('id');
304
305                                 if($('#' + ident).length == 0 && prev != 'live-' + src) {
306                                                 $('img',this).each(function() {
307                                                         $(this).attr('src',$(this).attr('dst'));
308                                                 });
309                                                 $('#' + prev).after($(this));
310                                 }
311                                 else { 
312                                         $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago')); 
313                                         if($('#' + ident + ' ' + '.comment-edit-text-empty').length)
314                                                 $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
315                                         $('#' + ident + ' ' + '.hide-comments-total').replaceWith($(this).find('.hide-comments-total'));
316                                         $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
317                                         $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
318                                         $('#' + ident + ' ' + '.my-comment-photo').each(function() {
319                                                 $(this).attr('src',$(this).attr('dst'));
320                                         });
321                                 }
322                                 prev = ident; 
323                         });
324                         */
325                         $('.like-rotator').hide();
326                         if(commentBusy) {
327                                 commentBusy = false;
328                                 $('body').css('cursor', 'auto');
329                         }
330                         /* autocomplete @nicknames */
331                         $(".comment-edit-form  textarea").contact_autocomplete(baseurl+"/acl");
332                 });
333         }
334
335         function imgbright(node) {
336                 $(node).removeClass("drophide").addClass("drop");
337         }
338
339         function imgdull(node) {
340                 $(node).removeClass("drop").addClass("drophide");
341         }
342
343         // Since our ajax calls are asynchronous, we will give a few 
344         // seconds for the first ajax call (setting like/dislike), then 
345         // run the updater to pick up any changes and display on the page.
346         // The updater will turn any rotators off when it's done. 
347         // This function will have returned long before any of these
348         // events have completed and therefore there won't be any
349         // visible feedback that anything changed without all this
350         // trickery. This still could cause confusion if the "like" ajax call
351         // is delayed and NavUpdate runs before it completes.
352
353         function dolike(ident,verb) {
354                 unpause();
355                 $('#like-rotator-' + ident.toString()).show();
356                 $.get('like/' + ident.toString() + '?verb=' + verb );
357                 if(timer) clearTimeout(timer);
358                 timer = setTimeout(NavUpdate,3000);
359                 liking = 1;
360         }
361
362         function dostar(ident) {
363                 ident = ident.toString();
364                 $('#like-rotator-' + ident).show();
365                 $.get('starred/' + ident, function(data) {
366                         if(data.match(/1/)) {
367                                 $('#starred-' + ident).addClass('starred');
368                                 $('#starred-' + ident).removeClass('unstarred');
369                                 $('#star-' + ident).addClass('hidden');
370                                 $('#unstar-' + ident).removeClass('hidden');
371                         }
372                         else {                  
373                                 $('#starred-' + ident).addClass('unstarred');
374                                 $('#starred-' + ident).removeClass('starred');
375                                 $('#star-' + ident).removeClass('hidden');
376                                 $('#unstar-' + ident).addClass('hidden');
377                         }
378                         $('#like-rotator-' + ident).hide();     
379                 });
380         }
381
382         function getPosition(e) {
383                 var cursor = {x:0, y:0};
384                 if ( e.pageX || e.pageY  ) {
385                         cursor.x = e.pageX;
386                         cursor.y = e.pageY;
387                 }
388                 else {
389                         if( e.clientX || e.clientY ) {
390                                 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
391                                 cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
392                         }
393                         else {
394                                 if( e.x || e.y ) {
395                                         cursor.x = e.x;
396                                         cursor.y = e.y;
397                                 }
398                         }
399                 }
400                 return cursor;
401         }
402
403         var lockvisible = false;
404
405         function lockview(event,id) {
406                 event = event || window.event;
407                 cursor = getPosition(event);
408                 if(lockvisible) {
409                         lockviewhide();
410                 }
411                 else {
412                         lockvisible = true;
413                         $.get('lockview/' + id, function(data) {
414                                 $('#panel').html(data);
415                                 $('#panel').css({ 'left': cursor.x + 5 , 'top': cursor.y + 5});
416                                 $('#panel').show();
417                         });
418                 }
419         }
420
421         function lockviewhide() {
422                 lockvisible = false;
423                 $('#panel').hide();
424         }
425
426         function post_comment(id) {
427                 unpause();
428                 commentBusy = true;
429                 $('body').css('cursor', 'wait');
430                 $("#comment-preview-inp-" + id).val("0");
431                 $.post(  
432              "item",  
433              $("#comment-edit-form-" + id).serialize(),
434                         function(data) {
435                                 if(data.success) {
436                                         $("#comment-edit-wrapper-" + id).hide();
437                                         $("#comment-edit-text-" + id).val('');
438                                 var tarea = document.getElementById("comment-edit-text-" + id);
439                                         if(tarea)
440                                                 commentClose(tarea,id);
441                                         if(timer) clearTimeout(timer);
442                                         timer = setTimeout(NavUpdate,10);
443                                 }
444                                 if(data.reload) {
445                                         window.location.href=data.reload;
446                                 }
447                         },
448                         "json"  
449          );  
450          return false;  
451         }
452
453
454         function preview_comment(id) {
455                 $("#comment-preview-inp-" + id).val("1");
456                 $("#comment-edit-preview-" + id).show();
457                 $.post(  
458              "item",  
459              $("#comment-edit-form-" + id).serialize(),
460                         function(data) {
461                                 if(data.preview) {
462                                                 
463                                         $("#comment-edit-preview-" + id).html(data.preview);
464                                         $("#comment-edit-preview-" + id + " a").click(function() { return false; });
465                                 }
466                         },
467                         "json"  
468          );  
469          return true;  
470         }
471
472
473
474         function preview_post() {
475                 $("#jot-preview").val("1");
476                 $("#jot-preview-content").show();
477                 tinyMCE.triggerSave();
478                 $.post(  
479                         "item",  
480                         $("#profile-jot-form").serialize(),
481                         function(data) {
482                                 if(data.preview) {                      
483                                         $("#jot-preview-content").html(data.preview);
484                                         $("#jot-preview-content" + " a").click(function() { return false; });
485                                 }
486                         },
487                         "json"  
488                 );  
489                 $("#jot-preview").val("0");
490                 return true;  
491         }
492
493
494         function unpause() {
495                 // unpause auto reloads if they are currently stopped
496                 totStopped = false;
497                 stopped = false;
498             $('#pause').html('');
499         }
500                 
501
502     function bin2hex(s){  
503         // Converts the binary representation of data to hex    
504         //   
505         // version: 812.316  
506         // discuss at: http://phpjs.org/functions/bin2hex  
507         // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
508         // +   bugfixed by: Onno Marsman  
509         // +   bugfixed by: Linuxworld  
510         // *     example 1: bin2hex('Kev');  
511         // *     returns 1: '4b6576'  
512         // *     example 2: bin2hex(String.fromCharCode(0x00));  
513         // *     returns 2: '00'  
514         var v,i, f = 0, a = [];  
515         s += '';  
516         f = s.length;  
517           
518         for (i = 0; i<f; i++) {  
519             a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");  
520         }  
521           
522         return a.join('');  
523     }  
524
525         function groupChangeMember(gid, cid, sec_token) {
526                 $('body .fakelink').css('cursor', 'wait');
527                 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
528                                 $('#group-update-wrapper').html(data);
529                                 $('body .fakelink').css('cursor', 'auto');                              
530                 });
531         }
532
533         function profChangeMember(gid,cid) {
534                 $('body .fakelink').css('cursor', 'wait');
535                 $.get('profperm/' + gid + '/' + cid, function(data) {
536                                 $('#prof-update-wrapper').html(data);
537                                 $('body .fakelink').css('cursor', 'auto');                              
538                 });
539         }
540
541         function contactgroupChangeMember(gid,cid) {
542                 $('body').css('cursor', 'wait');
543                 $.get('contactgroup/' + gid + '/' + cid, function(data) {
544                                 $('body').css('cursor', 'auto');
545                 });
546         }
547
548
549 function checkboxhighlight(box) {
550   if($(box).is(':checked')) {
551         $(box).addClass('checkeditem');
552   }
553   else {
554         $(box).removeClass('checkeditem');
555   }
556 }
557
558 function notifyMarkAll() {
559         $.get('notify/mark/all', function(data) {
560                 if(timer) clearTimeout(timer);
561                 timer = setTimeout(NavUpdate,1000);
562         });
563 }
564
565
566 // code from http://www.tinymce.com/wiki.php/How-to_implement_a_custom_file_browser
567 function fcFileBrowser (field_name, url, type, win) {
568     /* TODO: If you work with sessions in PHP and your client doesn't accept cookies you might need to carry
569        the session name and session ID in the request string (can look like this: "?PHPSESSID=88p0n70s9dsknra96qhuk6etm5").
570        These lines of code extract the necessary parameters and add them back to the filebrowser URL again. */
571
572
573     var cmsURL = baseurl+"/fbrowser/"+type+"/";
574
575     tinyMCE.activeEditor.windowManager.open({
576         file : cmsURL,
577         title : 'File Browser',
578         width : 420,  // Your dimensions may differ - toy around with them!
579         height : 400,
580         resizable : "yes",
581         inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
582         close_previous : "no"
583     }, {
584         window : win,
585         input : field_name
586     });
587     return false;
588   }
589
590 function setupFieldRichtext(){
591         tinyMCE.init({
592                 theme : "advanced",
593                 mode : "specific_textareas",
594                 editor_selector: "fieldRichtext",
595                 plugins : "bbcode,paste, inlinepopups",
596                 theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,image,forecolor,formatselect,code",
597                 theme_advanced_buttons2 : "",
598                 theme_advanced_buttons3 : "",
599                 theme_advanced_toolbar_location : "top",
600                 theme_advanced_toolbar_align : "center",
601                 theme_advanced_blockformats : "blockquote,code",
602                 paste_text_sticky : true,
603                 entity_encoding : "raw",
604                 add_unload_trigger : false,
605                 remove_linebreaks : false,
606                 force_p_newlines : false,
607                 force_br_newlines : true,
608                 forced_root_block : '',
609                 convert_urls: false,
610                 content_css: baseurl+"/view/custom_tinymce.css",
611                 theme_advanced_path : false,
612                 file_browser_callback : "fcFileBrowser",
613         });
614 }
615
616
617 /** 
618  * sprintf in javascript 
619  *      "{0} and {1}".format('zero','uno'); 
620  **/
621 String.prototype.format = function() {
622     var formatted = this;
623     for (var i = 0; i < arguments.length; i++) {
624         var regexp = new RegExp('\\{'+i+'\\}', 'gi');
625         formatted = formatted.replace(regexp, arguments[i]);
626     }
627     return formatted;
628 };
629 // Array Remove
630 Array.prototype.remove = function(item) {
631   to=undefined; from=this.indexOf(item);
632   var rest = this.slice((to || from) + 1 || this.length);
633   this.length = from < 0 ? this.length + from : from;
634   return this.push.apply(this, rest);
635 };
636
637 function previewTheme(elm) {
638         theme = $(elm).val();
639         $.getJSON('pretheme?f=&theme=' + theme,function(data) {
640                         $('#theme-preview').html('<div id="theme-desc">' + data.desc + '</div><a href="' + data.img + '"><img src="' + data.img + '" width="320" height="240" alt="' + theme + '" /></a>');
641         });
642
643 }