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