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