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