]> git.mxchange.org Git - friendica.git/blob - include/main.js
1a377a1a4315b447bf50411e29aa61e03895b23c
[friendica.git] / include / 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                 
38                 /* nav update event  */
39                 $('nav').bind('nav-update', function(e,data){;
40                         var net = $(data).find('net').text();
41                         if(net == 0) { net = ''; $('#net-update').hide() } else { $('#net-update').show() }
42                         $('#net-update').html(net);
43                         var home = $(data).find('home').text();
44                         if(home == 0) { home = '';  $('#home-update').hide() } else { $('#home-update').show() }
45                         $('#home-update').html(home);
46                         var mail = $(data).find('mail').text();
47                         if(mail == 0) { mail = '';  $('#mail-update').hide() } else { $('#mail-update').show() }
48                         $('#mail-update').html(mail);
49                         var intro = $(data).find('intro').text();
50                         var register = $(data).find('register').text();
51                         if(intro == 0) { intro = ''; }
52                         if(register != 0 && intro != '') { intro = intro+'/'+register; }
53                         if(register != 0 && intro == '') { intro = '0/'+register; }
54                         if (intro == '') { $('#notify-update').hide() } else { $('#notify-update').show() }
55                         $('#notify-update').html(intro);
56                 });
57                 
58                 
59                 NavUpdate(); 
60                 // Allow folks to stop the ajax page updates with the pause/break key
61                 $(document).keypress(function(event) {
62                         if(event.keyCode == '19') {
63                                 event.preventDefault();
64                                 if(stopped == false) {
65                                         stopped = true;
66                                         $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
67                                 }
68                                 else {
69                                         stopped = false;
70                                         $('#pause').html('');
71                                 }
72                         }
73 //                      // F8 - show/hide language selector
74 //                      if(event.keyCode == '119') {
75 //                              if(langSelect) {
76 //                                      langSelect = false;
77 //                                      $('#language-selector').hide();
78 //                              }
79 //                              else {
80 //                                      langSelect = true;
81 //                                      $('#language-selector').show();
82 //                              }
83 //                      }               
84 //
85 // this is shift-home on FF, but $ on IE, disabling until I figure out why the diff.
86 // update: incompatible usage of onKeyDown vs onKeyPress
87 //                      if(event.keyCode == '36' && event.shiftKey == true) {
88 //                              if(homebase !== undefined) {
89 //                                      event.preventDefault();
90 //                                      document.location = homebase;
91 //                              }
92 //                      }
93                 });                                     
94         });
95
96         function NavUpdate() {
97
98                 if($('#live-network').length) { src = 'network'; liveUpdate(); }
99                 if($('#live-profile').length) { src = 'profile'; liveUpdate(); }
100                 if($('#live-display').length) { 
101                         if(liking) {
102                                 liking = 0;
103                                 window.location.href=window.location.href 
104                         }
105                 }
106                 if($('#live-photos').length)  { 
107                         if(liking) {
108                                 liking = 0;
109                                 window.location.href=window.location.href 
110                         }
111                 }
112
113                 if(! stopped) {
114                         $.get("ping",function(data) {
115                                 $(data).find('result').each(function() {
116                                         // send nav-update event
117                                         $('nav').trigger('nav-update', this);
118                                 });
119                         }) ;
120                 }
121                 timer = setTimeout(NavUpdate,30000);
122         }
123
124         function liveUpdate() {
125                 if((src == null) || (stopped) || (! profile_uid)) { $('.like-rotator').hide(); return; }
126                 if(($('.comment-edit-text-full').length) || (in_progress)) {
127                         livetime = setTimeout(liveUpdate, 10000);
128                         return;
129                 }
130                 prev = 'live-' + src;
131
132                 in_progress = true;
133                 var udargs = ((netargs.length) ? '/' + netargs : '');
134                 var update_url = 'update_' + src + udargs + '?p=' + profile_uid + '&page=' + profile_page + '&msie=' + ((msie) ? 1 : 0);
135
136                 $.get(update_url,function(data) {
137                         in_progress = false;
138                         $('.ccollapse-wrapper',data).each(function() {
139                                 var ident = $(this).attr('id');
140                                 var is_hidden = $('#' + ident).is(':hidden');
141                                 if($('#' + ident).length) {
142                                         $('#' + ident).replaceWith($(this));
143                                         if(is_hidden)
144                                                 $('#' + ident).hide();
145                                 }
146                         });
147                         $('.wall-item-outside-wrapper',data).each(function() {
148                                 var ident = $(this).attr('id');
149                                 if($('#' + ident).length == 0) {
150                                         $('img',this).each(function() {
151                                                 $(this).attr('src',$(this).attr('dst'));
152                                         });
153                                         $('#' + prev).after($(this));
154                                 }
155                                 else { 
156
157                                         $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago')); 
158                                         $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
159                                         $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
160                                         $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
161                                         $('#' + ident + ' ' + '.my-comment-photo').each(function() {
162                                                 $(this).attr('src',$(this).attr('dst'));
163                                         });
164                                 }
165                                 prev = ident; 
166                         });
167                         $('.like-rotator').hide();
168                         if(commentBusy) {
169                                 commentBusy = false;
170                                 $('body').css('cursor', 'auto');
171                         }
172                 });
173         }
174
175         function imgbright(node) {
176                 $(node).removeClass("drophide").addClass("drop");
177         }
178
179         function imgdull(node) {
180                 $(node).removeClass("drop").addClass("drophide");
181         }
182
183         // Since our ajax calls are asynchronous, we will give a few 
184         // seconds for the first ajax call (setting like/dislike), then 
185         // run the updater to pick up any changes and display on the page.
186         // The updater will turn any rotators off when it's done. 
187         // This function will have returned long before any of these
188         // events have completed and therefore there won't be any
189         // visible feedback that anything changed without all this
190         // trickery. This still could cause confusion if the "like" ajax call
191         // is delayed and NavUpdate runs before it completes.
192
193         function dolike(ident,verb) {
194                 $('#like-rotator-' + ident.toString()).show();
195                 $.get('like/' + ident.toString() + '?verb=' + verb );
196                 if(timer) clearTimeout(timer);
197                 timer = setTimeout(NavUpdate,3000);
198                 liking = 1;
199         }
200
201         function getPosition(e) {
202                 var cursor = {x:0, y:0};
203                 if ( e.pageX || e.pageY  ) {
204                         cursor.x = e.pageX;
205                         cursor.y = e.pageY;
206                 }
207                 else {
208                         if( e.clientX || e.clientY ) {
209                                 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
210                                 cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
211                         }
212                         else {
213                                 if( e.x || e.y ) {
214                                         cursor.x = e.x;
215                                         cursor.y = e.y;
216                                 }
217                         }
218                 }
219                 return cursor;
220         }
221
222         var lockvisible = false;
223
224         function lockview(event,id) {
225                 event = event || window.event;
226                 cursor = getPosition(event);
227                 if(lockvisible) {
228                         lockviewhide();
229                 }
230                 else {
231                         lockvisible = true;
232                         $.get('lockview/' + id, function(data) {
233                                 $('#panel').html(data);
234                                 $('#panel').css({ 'left': cursor.x + 5 , 'top': cursor.y + 5});
235                                 $('#panel').show();
236                         });
237                 }
238         }
239
240         function lockviewhide() {
241                 lockvisible = false;
242                 $('#panel').hide();
243         }
244
245         function post_comment(id) {
246                 commentBusy = true;
247                 $('body').css('cursor', 'wait');
248                 $.post(  
249              "item",  
250              $("#comment-edit-form-" + id).serialize(),
251                         function(data) {
252                                 if(data.success) {
253                                         $("#comment-edit-wrapper-" + id).hide();
254                                         $("#comment-edit-text-" + id).val('');
255                                 var tarea = document.getElementById("comment-edit-text-" + id);
256                                         if(tarea)
257                                                 commentClose(tarea,id);
258                                         if(timer) clearTimeout(timer);
259                                         timer = setTimeout(NavUpdate,10);
260                                 }
261                                 if(data.reload) {
262                                         window.location.href=data.reload;
263                                 }
264                         },
265                         "json"  
266          );  
267          return false;  
268         }
269
270
271     function bin2hex(s){  
272         // Converts the binary representation of data to hex    
273         //   
274         // version: 812.316  
275         // discuss at: http://phpjs.org/functions/bin2hex  
276         // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
277         // +   bugfixed by: Onno Marsman  
278         // +   bugfixed by: Linuxworld  
279         // *     example 1: bin2hex('Kev');  
280         // *     returns 1: '4b6576'  
281         // *     example 2: bin2hex(String.fromCharCode(0x00));  
282         // *     returns 2: '00'  
283         var v,i, f = 0, a = [];  
284         s += '';  
285         f = s.length;  
286           
287         for (i = 0; i<f; i++) {  
288             a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");  
289         }  
290           
291         return a.join('');  
292     }  
293
294         function groupChangeMember(gid,cid) {
295                 $('body .fakelink').css('cursor', 'wait');
296                 $.get('group/' + gid + '/' + cid, function(data) {
297                                 $('#group-update-wrapper').html(data);
298                                 $('body .fakelink').css('cursor', 'auto');                              
299                 });
300         }
301
302         function profChangeMember(gid,cid) {
303                 $('body .fakelink').css('cursor', 'wait');
304                 $.get('profperm/' + gid + '/' + cid, function(data) {
305                                 $('#prof-update-wrapper').html(data);
306                                 $('body .fakelink').css('cursor', 'auto');                              
307                 });
308         }
309
310 /** 
311  * sprintf in javascript 
312  *      "{0} and {1}".format('zero','uno'); 
313  **/
314 String.prototype.format = function() {
315     var formatted = this;
316     for (var i = 0; i < arguments.length; i++) {
317         var regexp = new RegExp('\\{'+i+'\\}', 'gi');
318         formatted = formatted.replace(regexp, arguments[i]);
319     }
320     return formatted;
321 };