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