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