]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
timeout() now calls async feed update function
[tt-rss.git] / tt-rss.js
1 /*
2         This program is Copyright (c) 2003-2005 Andrew Dolgov <cthulhoo@gmail.com>              
3         Licensed under GPL v.2 or (at your preference) any later version.
4 */
5
6 var xmlhttp = false;
7 var xmlhttp_rpc = false;
8
9 var total_unread = 0;
10 var first_run = true;
11
12 /*@cc_on @*/
13 /*@if (@_jscript_version >= 5)
14 // JScript gives us Conditional compilation, we can cope with old IE versions.
15 // and security blocked creation of the objects.
16 try {
17         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
18 } catch (e) {
19         try {
20                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
21         } catch (E) {
22                 xmlhttp = false;
23         }
24 }
25 @end @*/
26
27 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
28         xmlhttp = new XMLHttpRequest();
29         xmlhttp_rpc = new XMLHttpRequest();
30
31 }
32
33 function printLockingError() {
34         notify("Please wait until operation finishes");
35 }
36
37 function notify_callback() {
38         var container = document.getElementById('notify');
39         if (xmlhttp.readyState == 4) {
40                 container.innerHTML=xmlhttp.responseText;
41         }
42 }
43
44 function feedlist_callback() {
45         var container = document.getElementById('feeds');
46         if (xmlhttp.readyState == 4) {
47                 container.innerHTML=xmlhttp.responseText;
48
49                 var feedtu = document.getElementById("FEEDTU");
50
51                 if (feedtu) {
52                         total_unread = feedtu.innerHTML;
53                         update_title();
54                 }
55
56                 if (first_run) {
57                         scheduleFeedUpdate();
58                         first_run = false;
59                 } else {
60                         notify("");
61                 }
62         }
63 }
64
65 function viewfeed_callback() {
66         var container = document.getElementById('headlines');
67         if (xmlhttp.readyState == 4) {
68                 container.innerHTML = xmlhttp.responseText;
69
70                 var factive = document.getElementById("FACTIVE");
71                 var funread = document.getElementById("FUNREAD");
72                 var ftotal = document.getElementById("FTOTAL");
73
74                 if (ftotal && factive && funread) {
75                         var feed_id = factive.innerHTML;
76
77                         var feedr = document.getElementById("FEEDR-" + feed_id);
78                         var feedt = document.getElementById("FEEDT-" + feed_id);
79                         var feedu = document.getElementById("FEEDU-" + feed_id);
80
81                         feedt.innerHTML = ftotal.innerHTML;
82                         feedu.innerHTML = funread.innerHTML;
83
84                         if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
85                                         feedr.className = feedr.className + "Unread";
86                         } else if (feedu.innerHTML <= 0) {      
87                                         feedr.className = feedr.className.replace("Unread", "");
88                         }
89
90                 }
91
92                 notify("");
93
94         }       
95 }
96
97 function view_callback() {
98         var container = document.getElementById('content');
99         if (xmlhttp.readyState == 4) {
100                 container.innerHTML=xmlhttp.responseText;               
101         }
102 }
103
104 function refetch_callback() {
105         if (xmlhttp_rpc.readyState == 4) {
106                 // feeds are updated in background
107                 updateFeedList(false, false);
108 //              notify("All feeds updated");
109         }
110 }
111
112 function scheduleFeedUpdate() {
113
114         notify("Updating feeds in background...");
115
116         var query_str = "backend.php?op=rpc&subop=forceUpdateAllFeeds";
117
118         if (xmlhttp_rpc.readyState == 4 || xmlhttp_rpc.readyState == 0) {
119                 xmlhttp_rpc.open("GET", query_str, true);
120                 xmlhttp_rpc.onreadystatechange=refetch_callback;
121                 xmlhttp_rpc.send(null);
122         } else {
123                 printLockingError();
124         }
125 }
126
127 function updateFeedList(silent, fetch) {
128         
129         if (silent != true) {
130                 notify("Updating feed list...");
131         }
132
133         var query_str = "backend.php?op=feeds";
134
135         if (fetch) query_str = query_str + "&fetch=yes";
136
137         if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
138                 xmlhttp.open("GET", query_str, true);
139                 xmlhttp.onreadystatechange=feedlist_callback;
140                 xmlhttp.send(null);
141         } else {
142                 printLockingError();
143         }
144 }
145
146 function catchupPage(feed) {
147
148         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
149                 printLockingError();
150                 return
151         }
152
153         var content = document.getElementById("headlinesList");
154
155         var rows = new Array();
156
157         for (i = 0; i < content.rows.length; i++) {
158                 var row_id = content.rows[i].id.replace("RROW-", "");
159                 if (row_id.length > 0) {
160                         rows.push(row_id);      
161                         content.rows[i].className = content.rows[i].className.replace("Unread", "");
162                 }
163         }
164
165         var feedr = document.getElementById("FEEDR-" + feed);
166         var feedu = document.getElementById("FEEDU-" + feed);
167
168         feedu.innerHTML = feedu.innerHTML - rows.length;
169
170         if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
171                         feedr.className = feedr.className + "Unread";
172         } else if (feedu.innerHTML <= 0) {      
173                         feedr.className = feedr.className.replace("Unread", "");
174         } 
175
176         var query_str = "backend.php?op=rpc&subop=catchupPage&ids=" + 
177                 param_escape(rows.toString());
178
179         notify("Marking this page as read...");
180
181         xmlhttp.open("GET", query_str, true);
182         xmlhttp.onreadystatechange=notify_callback;
183         xmlhttp.send(null);
184
185 }
186
187 function catchupAllFeeds() {
188
189         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
190                 printLockingError();
191                 return
192         }
193         var query_str = "backend.php?op=feeds&subop=catchupAll";
194
195         notify("Marking all feeds as read...");
196
197         xmlhttp.open("GET", query_str, true);
198         xmlhttp.onreadystatechange=feedlist_callback;
199         xmlhttp.send(null);
200
201 }
202
203 function viewfeed(feed, skip, subop) {
204
205 //      document.getElementById('headlines').innerHTML='Loading headlines, please wait...';             
206 //      document.getElementById('content').innerHTML='&nbsp;';          
207
208         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
209                 printLockingError();
210                 return
211         }
212
213         xmlhttp.open("GET", "backend.php?op=viewfeed&feed=" + param_escape(feed) +
214                 "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop) , true);
215         xmlhttp.onreadystatechange=viewfeed_callback;
216         xmlhttp.send(null);
217
218         notify("Loading headlines...");
219
220 }
221
222 function view(id,feed_id) {
223
224         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
225                 printLockingError();
226                 return
227         }
228
229         var crow = document.getElementById("RROW-" + id);
230
231         if (crow.className.match("Unread")) {
232                 var umark = document.getElementById("FEEDU-" + feed_id);
233                 umark.innerHTML = umark.innerHTML - 1;
234                 crow.className = crow.className.replace("Unread", "");
235
236                 if (umark.innerHTML == "0") {
237                         var feedr = document.getElementById("FEEDR-" + feed_id);
238                         feedr.className = feedr.className.replace("Unread", "");
239                 }
240         
241                 total_unread--;
242
243                 update_title(); 
244         }       
245
246         var upd_img_pic = document.getElementById("FUPDPIC-" + id);
247
248         if (upd_img_pic) {
249                 upd_img_pic.innerHTML = "";
250         } 
251
252         document.getElementById('content').innerHTML='Loading, please wait...';         
253
254         xmlhttp.open("GET", "backend.php?op=view&id=" + param_escape(id), true);
255         xmlhttp.onreadystatechange=view_callback;
256         xmlhttp.send(null);
257
258 }
259
260 function timeout() {
261
262         scheduleFeedUpdate();
263
264         setTimeout("timeout()", 1800*1000);
265
266 }
267
268 function search(feed, sender) {
269
270         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
271                 printLockingError();
272                 return
273         }
274
275         notify("Search: " + feed + ", " + sender.value)
276
277         document.getElementById('headlines').innerHTML='Loading headlines, please wait...';             
278         document.getElementById('content').innerHTML='&nbsp;';          
279
280         xmlhttp.open("GET", "backend.php?op=viewfeed&feed=" + param_escape(feed) +
281                 "&search=" + param_escape(sender.value) + "&ext=SEARCH", true);
282         xmlhttp.onreadystatechange=viewfeed_callback;
283         xmlhttp.send(null);
284
285 }
286
287 function update_title() {
288         //document.title = "Tiny Tiny RSS (" + total_unread + " unread)";
289 }
290
291 function init() {
292         updateFeedList(false, false);
293         setTimeout("timeout()", 1800*1000);
294 }