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