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