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