]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
e3f1fc906ab724d160a35b6b5e2e91974473916b
[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(false);
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                 updateFeedList(true, false);
107         }
108 }
109
110 function scheduleFeedUpdate(force) {
111
112         notify("Updating feeds in background...");
113
114         var query_str = "backend.php?op=rpc&subop=";
115
116         if (force) {
117                 query_str = query_str = "forceUpdateAllFeeds";
118         } else {
119                 query_str = query_str = "updateAllFeeds";
120         }
121
122         if (xmlhttp_rpc.readyState == 4 || xmlhttp_rpc.readyState == 0) {
123                 xmlhttp_rpc.open("GET", query_str, true);
124                 xmlhttp_rpc.onreadystatechange=refetch_callback;
125                 xmlhttp_rpc.send(null);
126         } else {
127                 printLockingError();
128         }
129 }
130
131 function updateFeedList(silent, fetch) {
132         
133         if (silent != true) {
134                 notify("Loading feed list...");
135         }
136
137         var query_str = "backend.php?op=feeds";
138
139         if (fetch) query_str = query_str + "&fetch=yes";
140
141         if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
142                 xmlhttp.open("GET", query_str, true);
143                 xmlhttp.onreadystatechange=feedlist_callback;
144                 xmlhttp.send(null);
145         } else {
146                 printLockingError();
147         }
148 }
149
150 function catchupPage(feed) {
151
152         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
153                 printLockingError();
154                 return
155         }
156
157         var content = document.getElementById("headlinesList");
158
159         var rows = new Array();
160
161         for (i = 0; i < content.rows.length; i++) {
162                 var row_id = content.rows[i].id.replace("RROW-", "");
163                 if (row_id.length > 0) {
164                         rows.push(row_id);      
165                         content.rows[i].className = content.rows[i].className.replace("Unread", "");
166                 }
167         }
168
169         var feedr = document.getElementById("FEEDR-" + feed);
170         var feedu = document.getElementById("FEEDU-" + feed);
171
172         feedu.innerHTML = feedu.innerHTML - rows.length;
173
174         if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
175                         feedr.className = feedr.className + "Unread";
176         } else if (feedu.innerHTML <= 0) {      
177                         feedr.className = feedr.className.replace("Unread", "");
178         } 
179
180         var query_str = "backend.php?op=rpc&subop=catchupPage&ids=" + 
181                 param_escape(rows.toString());
182
183         notify("Marking this page as read...");
184
185         xmlhttp.open("GET", query_str, true);
186         xmlhttp.onreadystatechange=notify_callback;
187         xmlhttp.send(null);
188
189 }
190
191 function catchupAllFeeds() {
192
193         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
194                 printLockingError();
195                 return
196         }
197         var query_str = "backend.php?op=feeds&subop=catchupAll";
198
199         notify("Marking all feeds as read...");
200
201         xmlhttp.open("GET", query_str, true);
202         xmlhttp.onreadystatechange=feedlist_callback;
203         xmlhttp.send(null);
204
205 }
206
207 function viewfeed(feed, skip, subop) {
208
209 //      document.getElementById('headlines').innerHTML='Loading headlines, please wait...';             
210 //      document.getElementById('content').innerHTML='&nbsp;';          
211
212         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
213                 printLockingError();
214                 return
215         }
216
217         xmlhttp.open("GET", "backend.php?op=viewfeed&feed=" + param_escape(feed) +
218                 "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop) , true);
219         xmlhttp.onreadystatechange=viewfeed_callback;
220         xmlhttp.send(null);
221
222         notify("Loading headlines...");
223
224 }
225
226 function view(id,feed_id) {
227
228         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
229                 printLockingError();
230                 return
231         }
232
233         var crow = document.getElementById("RROW-" + id);
234
235         if (crow.className.match("Unread")) {
236                 var umark = document.getElementById("FEEDU-" + feed_id);
237                 umark.innerHTML = umark.innerHTML - 1;
238                 crow.className = crow.className.replace("Unread", "");
239
240                 if (umark.innerHTML == "0") {
241                         var feedr = document.getElementById("FEEDR-" + feed_id);
242                         feedr.className = feedr.className.replace("Unread", "");
243                 }
244         
245                 total_unread--;
246
247                 update_title(); 
248         }       
249
250         var upd_img_pic = document.getElementById("FUPDPIC-" + id);
251
252         if (upd_img_pic) {
253                 upd_img_pic.innerHTML = "";
254         } 
255
256         document.getElementById('content').innerHTML='Loading, please wait...';         
257
258         xmlhttp.open("GET", "backend.php?op=view&id=" + param_escape(id), true);
259         xmlhttp.onreadystatechange=view_callback;
260         xmlhttp.send(null);
261
262 }
263
264 function timeout() {
265
266         scheduleFeedUpdate(true);
267
268         setTimeout("timeout()", 1800*1000);
269
270 }
271
272 function search(feed, sender) {
273
274         if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
275                 printLockingError();
276                 return
277         }
278
279         notify("Search: " + feed + ", " + sender.value)
280
281         document.getElementById('headlines').innerHTML='Loading headlines, please wait...';             
282         document.getElementById('content').innerHTML='&nbsp;';          
283
284         xmlhttp.open("GET", "backend.php?op=viewfeed&feed=" + param_escape(feed) +
285                 "&search=" + param_escape(sender.value) + "&ext=SEARCH", true);
286         xmlhttp.onreadystatechange=viewfeed_callback;
287         xmlhttp.send(null);
288
289 }
290
291 function update_title() {
292         //document.title = "Tiny Tiny RSS (" + total_unread + " unread)";
293 }
294
295 function init() {
296         updateFeedList(false, false);
297         setTimeout("timeout()", 1800*1000);
298 }