]> git.mxchange.org Git - friendica.git/blob - include/main.js
prevent concurrent ajax updates
[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
30         $(document).ready(function() {
31                 $.ajaxSetup({cache: false});
32                 msie = $.browser.msie ;
33                 NavUpdate(); 
34                 // Allow folks to stop the ajax page updates with the pause/break key
35                 $(document).keypress(function(event) {
36                         if(event.keyCode == '19') {
37                                 event.preventDefault();
38                                 if(stopped == false) {
39                                         stopped = true;
40                                         $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
41                                 }
42                                 else {
43                                         stopped = false;
44                                         $('#pause').html('');
45                                 }
46                         }
47                 });                                     
48         });
49
50         function NavUpdate() {
51
52                 if($('#live-network').length) { src = 'network'; liveUpdate(); }
53                 if($('#live-profile').length) { src = 'profile'; liveUpdate(); }
54                 if($('#live-display').length) { 
55                         if(liking) {
56                                 liking = 0;
57                                 window.location.href=window.location.href 
58                         }
59                 }
60                 if($('#live-photos').length)  { 
61                         if(liking) {
62                                 liking = 0;
63                                 window.location.href=window.location.href 
64                         }
65                 }
66
67                 if(! stopped) {
68                         $.get("ping",function(data) {
69                                 $(data).find('result').each(function() {
70                                         var net = $(this).find('net').text();
71                                         if(net == 0) { net = ''; }
72                                         $('#net-update').html(net);
73                                         var home = $(this).find('home').text();
74                                         if(home == 0) { home = ''; }
75                                         $('#home-update').html(home);
76                                         var mail = $(this).find('mail').text();
77                                         if(mail == 0) { mail = ''; }
78                                         $('#mail-update').html(mail);
79                                         var intro = $(this).find('intro').text();
80                                         if(intro == 0) { intro = ''; }
81                                         $('#notify-update').html(intro);
82                                 });
83                         }) ;
84                 }
85                 timer = setTimeout(NavUpdate,30000);
86
87         }
88
89         function liveUpdate() {
90                 if((src == null) || (stopped) || (! profile_uid)) { $('.like-rotator').hide(); return; }
91                 if(($('.comment-edit-text-full').length) || (in_progress)) {
92                         livetime = setTimeout(liveUpdate, 10000);
93                         return;
94                 }
95                 prev = 'live-' + src;
96
97                 in_progress = true;
98                 $.get('update_' + src + '?p=' + profile_uid + '&msie=' + ((msie) ? 1 : 0),function(data) {
99                         in_progress = false;
100                         $('.wall-item-outside-wrapper',data).each(function() {
101                                 var ident = $(this).attr('id');
102                                 if($('#' + ident).length == 0) { 
103                                         $('img',this).each(function() {
104                                                 $(this).attr('src',$(this).attr('dst'));
105                                         });
106                                         $('#' + prev).after($(this));
107                                 }
108                                 else { 
109
110                                         $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago')); 
111                                         $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
112                                         $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
113                                         $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
114                                         $('#' + ident + ' ' + '.my-comment-photo').each(function() {
115                                                 $(this).attr('src',$(this).attr('dst'));
116                                         });
117
118
119                                 }
120                                 prev = ident; 
121                         });
122                         $('.like-rotator').hide();
123                 });
124
125         }
126
127         function imgbright(node) {
128                 $(node).attr("src",$(node).attr("src").replace('hide','show'));
129                 $(node).css('width',24);
130                 $(node).css('height',24);
131         }
132
133         function imgdull(node) {
134                 $(node).attr("src",$(node).attr("src").replace('show','hide'));
135                 $(node).css('width',16);
136                 $(node).css('height',16);
137         }
138
139         // Since our ajax calls are asynchronous, we will give a few 
140         // seconds for the first ajax call (setting like/dislike), then 
141         // run the updater to pick up any changes and display on the page.
142         // The updater will turn any rotators off when it's done. 
143         // This function will have returned long before any of these
144         // events have completed and therefore there won't be any
145         // visible feedback that anything changed without all this
146         // trickery. This still could cause confusion if the "like" ajax call
147         // is delayed and NavUpdate runs before it completes.
148
149         function dolike(ident,verb) {
150                 $('#like-rotator-' + ident.toString()).show();
151                 $.get('like/' + ident.toString() + '?verb=' + verb );
152                 if(timer) clearTimeout(timer);
153                 timer = setTimeout(NavUpdate,3000);
154                 liking = 1;
155         }
156
157         function getPosition(e) {
158                 var cursor = {x:0, y:0};
159                 if ( e.pageX || e.pageY  ) {
160                         cursor.x = e.pageX;
161                         cursor.y = e.pageY;
162                 }
163                 else {
164                         if( e.clientX || e.clientY ) {
165                                 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
166                                 cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
167                         }
168                         else {
169                                 if( e.x || e.y ) {
170                                         cursor.x = e.x;
171                                         cursor.y = e.y;
172                                 }
173                         }
174                 }
175                 return cursor;
176         }
177
178         var lockvisible = false;
179
180         function lockview(event,id) {
181                 event = event || window.event;
182                 cursor = getPosition(event);
183                 if(lockvisible) {
184                         lockviewhide();
185                 }
186                 else {
187                         lockvisible = true;
188                         $.get('lockview/' + id, function(data) {
189                                 $('#panel').html(data);
190                                 $('#panel').css({ 'left': cursor.x + 5 , 'top': cursor.y + 5});
191                                 $('#panel').show();
192                         });
193                 }
194         }
195
196         function lockviewhide() {
197                 lockvisible = false;
198                 $('#panel').hide();
199         }
200