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