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