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