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