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