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