]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
MSIE5 compatibility workarounds
[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 var xmlhttp_view = false;
9
10 var total_unread = 0;
11 var first_run = true;
12
13 var active_post_id = false;
14 var active_feed_id = false;
15 var active_offset = false;
16
17 var total_feed_entries = false;
18
19 var _viewfeed_autoselect_first = false;
20 var _viewfeed_autoselect_last = false;
21
22 var search_query = "";
23
24 /*@cc_on @*/
25 /*@if (@_jscript_version >= 5)
26 // JScript gives us Conditional compilation, we can cope with old IE versions.
27 // and security blocked creation of the objects.
28 try {
29 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
30 } catch (e) {
31 try {
32 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
33 xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
34 xmlhttp_view = new ActiveXObject("Microsoft.XMLHTTP");
35 } catch (E) {
36 xmlhttp = false;
37 xmlhttp_rpc = false;
38 xmlhttp_view = false;
39 }
40 }
41 @end @*/
42
43 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
44 xmlhttp = new XMLHttpRequest();
45 xmlhttp_rpc = new XMLHttpRequest();
46 xmlhttp_view = new XMLHttpRequest();
47 }
48
49 function feedlist_callback() {
50 var container = document.getElementById('feeds');
51 if (xmlhttp.readyState == 4) {
52 container.innerHTML=xmlhttp.responseText;
53
54 if (first_run) {
55 scheduleFeedUpdate(false);
56 first_run = false;
57 } else {
58 notify("");
59 }
60 }
61 }
62
63 function viewfeed_callback() {
64 var container = document.getElementById('headlines');
65 if (xmlhttp.readyState == 4) {
66 container.innerHTML = xmlhttp.responseText;
67
68 var factive = document.getElementById("FACTIVE");
69 var funread = document.getElementById("FUNREAD");
70 var ftotal = document.getElementById("FTOTAL");
71
72 if (_viewfeed_autoselect_first == true) {
73 _viewfeed_autoselect_first = false;
74 view(getFirstVisibleHeadlineId(), active_feed_id);
75 }
76
77 if (_viewfeed_autoselect_last == true) {
78 _viewfeed_autoselect_last = false;
79 view(getLastVisibleHeadlineId(), active_feed_id);
80 }
81
82 if (ftotal && factive && funread) {
83 var feed_id = factive.innerHTML;
84
85 var feedr = document.getElementById("FEEDR-" + feed_id);
86 var feedt = document.getElementById("FEEDT-" + feed_id);
87 var feedu = document.getElementById("FEEDU-" + feed_id);
88
89 feedt.innerHTML = ftotal.innerHTML;
90 feedu.innerHTML = funread.innerHTML;
91
92 total_feed_entries = ftotal.innerHTML;
93
94 if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
95 feedr.className = feedr.className + "Unread";
96 } else if (feedu.innerHTML <= 0) {
97 feedr.className = feedr.className.replace("Unread", "");
98 }
99
100 }
101
102 var searchbox = document.getElementById("searchbox");
103
104 searchbox.value = search_query;
105
106 notify("");
107
108 }
109 }
110
111 function view_callback() {
112 var container = document.getElementById('content');
113 if (xmlhttp_view.readyState == 4) {
114 container.innerHTML=xmlhttp_view.responseText;
115 }
116 }
117
118 function refetch_callback() {
119
120 if (xmlhttp_rpc.readyState == 4) {
121 notify("All feeds updated");
122 var container = document.getElementById('feeds');
123 container.innerHTML = xmlhttp_rpc.responseText;
124 document.title = "Tiny Tiny RSS";
125 }
126 }
127
128 function scheduleFeedUpdate(force) {
129
130 notify("Updating feeds in background...");
131
132 document.title = "Tiny Tiny RSS - Updating...";
133
134 var query_str = "backend.php?op=rpc&subop=";
135
136 if (force) {
137 query_str = query_str + "forceUpdateAllFeeds";
138 } else {
139 query_str = query_str + "updateAllFeeds";
140 }
141
142 if (xmlhttp_ready(xmlhttp_rpc)) {
143 xmlhttp_rpc.open("GET", query_str, true);
144 xmlhttp_rpc.onreadystatechange=refetch_callback;
145 xmlhttp_rpc.send(null);
146 } else {
147 printLockingError();
148 }
149 }
150
151 function updateFeedList(silent, fetch) {
152
153 if (silent != true) {
154 notify("Loading feed list...");
155 }
156
157 var query_str = "backend.php?op=feeds";
158
159 if (fetch) query_str = query_str + "&fetch=yes";
160
161 if (xmlhttp_ready(xmlhttp)) {
162 xmlhttp.open("GET", query_str, true);
163 xmlhttp.onreadystatechange=feedlist_callback;
164 xmlhttp.send(null);
165 } else {
166 printLockingError();
167 }
168 }
169
170 function catchupPage(feed) {
171
172 if (!xmlhttp_ready(xmlhttp)) {
173 printLockingError();
174 return
175 }
176
177 var content = document.getElementById("headlinesList");
178
179 var rows = new Array();
180
181 for (i = 0; i < content.rows.length; i++) {
182 var row_id = content.rows[i].id.replace("RROW-", "");
183 if (row_id.length > 0) {
184 if (content.rows[i].className.match("Unread")) {
185 rows.push(row_id);
186 content.rows[i].className = content.rows[i].className.replace("Unread", "");
187 }
188 }
189 }
190
191 if (rows.length > 0) {
192
193 var feedr = document.getElementById("FEEDR-" + feed);
194 var feedu = document.getElementById("FEEDU-" + feed);
195
196 feedu.innerHTML = feedu.innerHTML - rows.length;
197
198 if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
199 feedr.className = feedr.className + "Unread";
200 } else if (feedu.innerHTML <= 0) {
201 feedr.className = feedr.className.replace("Unread", "");
202 }
203
204 var query_str = "backend.php?op=rpc&subop=catchupPage&ids=" +
205 param_escape(rows.toString());
206
207 notify("Marking this page as read...");
208
209 xmlhttp.open("GET", query_str, true);
210 xmlhttp.onreadystatechange=notify_callback;
211 xmlhttp.send(null);
212
213 } else {
214 notify("No unread items on this page.");
215
216 }
217 }
218
219 function catchupAllFeeds() {
220
221 if (!xmlhttp_ready(xmlhttp)) {
222 printLockingError();
223 return
224 }
225 var query_str = "backend.php?op=feeds&subop=catchupAll";
226
227 notify("Marking all feeds as read...");
228
229 xmlhttp.open("GET", query_str, true);
230 xmlhttp.onreadystatechange=feedlist_callback;
231 xmlhttp.send(null);
232
233 }
234
235 function viewfeed(feed, skip, subop) {
236
237 enableHotkeys();
238
239 var searchbox = document.getElementById("searchbox");
240
241 if (searchbox) {
242 search_query = searchbox.value;
243 } else {
244 search_query = "";
245 }
246
247 /* if (active_feed_id == feed && subop != "ForceUpdate") {
248 notify("This feed is currently selected.");
249 return;
250 } */
251
252 if (skip < 0 || skip > total_feed_entries) {
253 return;
254 }
255
256 if (!xmlhttp_ready(xmlhttp)) {
257 printLockingError();
258 return
259 }
260
261 active_feed_id = feed;
262 active_post_id = false;
263 active_offset = skip;
264
265 var query = "backend.php?op=viewfeed&feed=" + param_escape(feed) +
266 "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop);
267
268 if (search_query != "") {
269 query = query + "&search=" + param_escape(search_query);
270 }
271
272 xmlhttp.open("GET", query, true);
273 xmlhttp.onreadystatechange=viewfeed_callback;
274 xmlhttp.send(null);
275
276 notify("Loading headlines...");
277
278 }
279
280 function cleanSelectedHeadlines() {
281 var content = document.getElementById("headlinesList");
282
283 var rows = new Array();
284
285 for (i = 0; i < content.rows.length; i++) {
286 content.rows[i].className = content.rows[i].className.replace("Selected", "");
287 }
288
289 }
290
291 function view(id,feed_id) {
292
293 enableHotkeys();
294
295 if (!xmlhttp_ready(xmlhttp_view)) {
296 printLockingError();
297 return
298 }
299
300 var crow = document.getElementById("RROW-" + id);
301
302 if (crow.className.match("Unread")) {
303 var umark = document.getElementById("FEEDU-" + feed_id);
304 umark.innerHTML = umark.innerHTML - 1;
305 crow.className = crow.className.replace("Unread", "");
306
307 if (umark.innerHTML == "0") {
308 var feedr = document.getElementById("FEEDR-" + feed_id);
309 feedr.className = feedr.className.replace("Unread", "");
310 }
311
312 total_unread--;
313 }
314
315 cleanSelectedHeadlines();
316
317 crow.className = crow.className + "Selected";
318
319 var upd_img_pic = document.getElementById("FUPDPIC-" + id);
320
321 if (upd_img_pic) {
322 upd_img_pic.innerHTML = "";
323 }
324
325 document.getElementById('content').innerHTML='Loading, please wait...';
326
327 active_post_id = id;
328
329 xmlhttp_view.open("GET", "backend.php?op=view&id=" + param_escape(id), true);
330 xmlhttp_view.onreadystatechange=view_callback;
331 xmlhttp_view.send(null);
332
333
334 }
335
336 function timeout() {
337 scheduleFeedUpdate(true);
338 setTimeout("timeout()", 1800*1000);
339 }
340
341 function resetSearch() {
342 document.getElementById("searchbox").value = "";
343 viewfeed(active_feed_id, 0, "");
344 }
345
346 function search(feed) {
347 viewfeed(feed, 0, "");
348 }
349
350 function localPiggieFunction(enable) {
351 if (enable) {
352 var query_str = "backend.php?op=feeds&subop=piggie";
353
354 if (xmlhttp_ready(xmlhttp)) {
355
356 xmlhttp.open("GET", query_str, true);
357 xmlhttp.onreadystatechange=feedlist_callback;
358 xmlhttp.send(null);
359 }
360 }
361 }
362
363 function relativeid_callback() {
364
365 if (xmlhttp_rpc.readyState == 4) {
366 notify(xmlhttp_rpc.responseText);
367 }
368
369 }
370
371 function getVisibleHeadlineIds() {
372
373 var content = document.getElementById("headlinesList");
374
375 var rows = new Array();
376
377 for (i = 0; i < content.rows.length; i++) {
378 var row_id = content.rows[i].id.replace("RROW-", "");
379 if (row_id.length > 0) {
380 rows.push(row_id);
381 }
382 }
383 return rows;
384 }
385
386 function getFirstVisibleHeadlineId() {
387 var rows = getVisibleHeadlineIds();
388 return rows[0];
389 }
390
391 function getLastVisibleHeadlineId() {
392 var rows = getVisibleHeadlineIds();
393 return rows[rows.length-1];
394 }
395
396 function moveToPost(mode) {
397
398 var rows = getVisibleHeadlineIds();
399
400 var prev_id;
401 var next_id;
402
403 if (active_post_id == false) {
404 next_id = getFirstVisibleHeadlineId();
405 prev_id = getLastVisibleHeadlineId();
406 } else {
407 for (var i = 0; i < rows.length; i++) {
408 if (rows[i] == active_post_id) {
409 prev_id = rows[i-1];
410 next_id = rows[i+1];
411 }
412 }
413 }
414
415 var content = document.getElementById("headlinesList");
416
417 if (mode == "next") {
418 if (next_id != undefined) {
419 view(next_id, active_feed_id);
420 } else {
421 _viewfeed_autoselect_first = true;
422 viewfeed(active_feed_id, active_offset+15);
423 }
424 }
425
426 if (mode == "prev") {
427 if ( prev_id != undefined) {
428 view(prev_id, active_feed_id);
429 } else {
430 _viewfeed_autoselect_last = true;
431 viewfeed(active_feed_id, active_offset-15);
432 }
433 }
434
435 }
436
437 function localHotkeyHandler(keycode) {
438
439 if (keycode == 78) {
440 return moveToPost('next');
441 }
442
443 if (keycode == 80) {
444 return moveToPost('prev');
445 }
446
447 if (keycode == 82) {
448 return scheduleFeedUpdate(true);
449 }
450
451 if (keycode == 85) {
452 return viewfeed(active_feed_id, active_offset, "ForceUpdate");
453 }
454
455 // notify("KC: " + keycode);
456
457 }
458
459
460 function init() {
461
462 // IE kludge
463
464 if (xmlhttp && !xmlhttp_rpc) {
465 xmlhttp_rpc = xmlhttp;
466 xmlhttp_view = xmlhttp;
467 }
468
469 if (!xmlhttp || !xmlhttp_rpc || !xmlhttp_view) {
470 document.getElementById("headlines").innerHTML =
471 "<b>Fatal error:</b> This program needs XmlHttpRequest " +
472 "to function properly. Your browser doesn't seem to support it.";
473 return;
474 }
475
476 updateFeedList(false, false);
477 document.onkeydown = hotkey_handler;
478 setTimeout("timeout()", 1800*1000);
479 }