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