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