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