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