]> git.wh0rd.org - tt-rss.git/blame - tt-rss.js
MSIE5 compatibility workarounds
[tt-rss.git] / tt-rss.js
CommitLineData
1cd17194
AD
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
6var xmlhttp = false;
525116d4 7var xmlhttp_rpc = false;
c3a8d71a 8var xmlhttp_view = false;
1cd17194 9
76798ff3 10var total_unread = 0;
525116d4 11var first_run = true;
76798ff3 12
9cfc649a
AD
13var active_post_id = false;
14var active_feed_id = false;
c3a8d71a
AD
15var active_offset = false;
16
17var total_feed_entries = false;
18
19var _viewfeed_autoselect_first = false;
20var _viewfeed_autoselect_last = false;
9cfc649a 21
c374a3fe
AD
22var search_query = "";
23
1cd17194
AD
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.
28try {
29 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
30} catch (e) {
31 try {
32 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
508a81e1 33 xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
c3a8d71a 34 xmlhttp_view = new ActiveXObject("Microsoft.XMLHTTP");
1cd17194
AD
35 } catch (E) {
36 xmlhttp = false;
508a81e1 37 xmlhttp_rpc = false;
c3a8d71a 38 xmlhttp_view = false;
1cd17194
AD
39 }
40}
41@end @*/
42
43if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
44 xmlhttp = new XMLHttpRequest();
525116d4 45 xmlhttp_rpc = new XMLHttpRequest();
c3a8d71a 46 xmlhttp_view = new XMLHttpRequest();
1cd17194
AD
47}
48
1cd17194 49function feedlist_callback() {
d76a3b03 50 var container = document.getElementById('feeds');
1cd17194 51 if (xmlhttp.readyState == 4) {
d76a3b03 52 container.innerHTML=xmlhttp.responseText;
76798ff3 53
525116d4 54 if (first_run) {
cb246176 55 scheduleFeedUpdate(false);
525116d4
AD
56 first_run = false;
57 } else {
58 notify("");
c0e5a40e
AD
59 }
60 }
1cd17194
AD
61}
62
63function viewfeed_callback() {
d76a3b03 64 var container = document.getElementById('headlines');
1cd17194 65 if (xmlhttp.readyState == 4) {
d76a3b03 66 container.innerHTML = xmlhttp.responseText;
a1a8a2be
AD
67
68 var factive = document.getElementById("FACTIVE");
69 var funread = document.getElementById("FUNREAD");
70 var ftotal = document.getElementById("FTOTAL");
71
c3a8d71a
AD
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
a1a8a2be
AD
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
c3a8d71a
AD
92 total_feed_entries = ftotal.innerHTML;
93
a1a8a2be
AD
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 }
1c37c607 101
c374a3fe
AD
102 var searchbox = document.getElementById("searchbox");
103
104 searchbox.value = search_query;
105
1c37c607
AD
106 notify("");
107
108 }
1cd17194
AD
109}
110
111function view_callback() {
d76a3b03 112 var container = document.getElementById('content');
c3a8d71a
AD
113 if (xmlhttp_view.readyState == 4) {
114 container.innerHTML=xmlhttp_view.responseText;
1cd17194
AD
115 }
116}
117
525116d4 118function refetch_callback() {
c0e5a40e 119
525116d4 120 if (xmlhttp_rpc.readyState == 4) {
c3b81db0 121 notify("All feeds updated");
c3b81db0 122 var container = document.getElementById('feeds');
c3b81db0 123 container.innerHTML = xmlhttp_rpc.responseText;
55193822 124 document.title = "Tiny Tiny RSS";
c0e5a40e 125 }
525116d4
AD
126}
127
cb246176 128function scheduleFeedUpdate(force) {
525116d4
AD
129
130 notify("Updating feeds in background...");
131
55193822
AD
132 document.title = "Tiny Tiny RSS - Updating...";
133
cb246176
AD
134 var query_str = "backend.php?op=rpc&subop=";
135
136 if (force) {
c3b81db0 137 query_str = query_str + "forceUpdateAllFeeds";
cb246176 138 } else {
c3b81db0 139 query_str = query_str + "updateAllFeeds";
cb246176 140 }
525116d4 141
c0e5a40e 142 if (xmlhttp_ready(xmlhttp_rpc)) {
525116d4
AD
143 xmlhttp_rpc.open("GET", query_str, true);
144 xmlhttp_rpc.onreadystatechange=refetch_callback;
145 xmlhttp_rpc.send(null);
146 } else {
147 printLockingError();
c0e5a40e 148 }
525116d4 149}
1cd17194 150
525116d4 151function updateFeedList(silent, fetch) {
c0e5a40e 152
525116d4 153 if (silent != true) {
11c2f3fa 154 notify("Loading feed list...");
40d13c28 155 }
82baad4a 156
331900c6
AD
157 var query_str = "backend.php?op=feeds";
158
159 if (fetch) query_str = query_str + "&fetch=yes";
160
c0e5a40e 161 if (xmlhttp_ready(xmlhttp)) {
076682aa
AD
162 xmlhttp.open("GET", query_str, true);
163 xmlhttp.onreadystatechange=feedlist_callback;
164 xmlhttp.send(null);
165 } else {
a234aee5 166 printLockingError();
076682aa 167 }
1cd17194
AD
168}
169
175847de
AD
170function catchupPage(feed) {
171
c0e5a40e 172 if (!xmlhttp_ready(xmlhttp)) {
a234aee5 173 printLockingError();
076682aa
AD
174 return
175 }
176
175847de
AD
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) {
cb0bd8bd
AD
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 }
175847de
AD
188 }
189 }
190
cb0bd8bd 191 if (rows.length > 0) {
175847de 192
cb0bd8bd
AD
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);
175847de 212
cb0bd8bd
AD
213 } else {
214 notify("No unread items on this page.");
175847de 215
cb0bd8bd 216 }
175847de
AD
217}
218
476cac42 219function catchupAllFeeds() {
076682aa 220
c0e5a40e 221 if (!xmlhttp_ready(xmlhttp)) {
a234aee5 222 printLockingError();
076682aa
AD
223 return
224 }
476cac42
AD
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}
1cd17194 234
476cac42 235function viewfeed(feed, skip, subop) {
1cd17194 236
36bf7496
AD
237 enableHotkeys();
238
c374a3fe
AD
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 } */
d76a3b03 251
c3a8d71a
AD
252 if (skip < 0 || skip > total_feed_entries) {
253 return;
254 }
255
c0e5a40e 256 if (!xmlhttp_ready(xmlhttp)) {
a234aee5 257 printLockingError();
076682aa
AD
258 return
259 }
c3a8d71a 260
9cfc649a
AD
261 active_feed_id = feed;
262 active_post_id = false;
c3a8d71a 263 active_offset = skip;
076682aa 264
c374a3fe
AD
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);
1cd17194
AD
273 xmlhttp.onreadystatechange=viewfeed_callback;
274 xmlhttp.send(null);
275
1c37c607
AD
276 notify("Loading headlines...");
277
1cd17194
AD
278}
279
9cfc649a
AD
280function 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
a1a8a2be 291function view(id,feed_id) {
d76a3b03 292
36bf7496
AD
293 enableHotkeys();
294
c0e5a40e 295 if (!xmlhttp_ready(xmlhttp_view)) {
a234aee5 296 printLockingError();
076682aa
AD
297 return
298 }
299
d76a3b03
AD
300 var crow = document.getElementById("RROW-" + id);
301
a1a8a2be
AD
302 if (crow.className.match("Unread")) {
303 var umark = document.getElementById("FEEDU-" + feed_id);
304 umark.innerHTML = umark.innerHTML - 1;
d76a3b03 305 crow.className = crow.className.replace("Unread", "");
d76a3b03 306
a1a8a2be
AD
307 if (umark.innerHTML == "0") {
308 var feedr = document.getElementById("FEEDR-" + feed_id);
309 feedr.className = feedr.className.replace("Unread", "");
310 }
9cfc649a 311
76798ff3 312 total_unread--;
76798ff3 313 }
1cd17194 314
9cfc649a
AD
315 cleanSelectedHeadlines();
316
317 crow.className = crow.className + "Selected";
318
b197f117
AD
319 var upd_img_pic = document.getElementById("FUPDPIC-" + id);
320
321 if (upd_img_pic) {
322 upd_img_pic.innerHTML = "";
323 }
324
d76a3b03 325 document.getElementById('content').innerHTML='Loading, please wait...';
1cd17194 326
9cfc649a
AD
327 active_post_id = id;
328
c3a8d71a
AD
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);
1cd17194 332
c0e5a40e 333
1cd17194
AD
334}
335
40d13c28 336function timeout() {
cb246176 337 scheduleFeedUpdate(true);
ac53063a 338 setTimeout("timeout()", 1800*1000);
ac53063a
AD
339}
340
c374a3fe
AD
341function resetSearch() {
342 document.getElementById("searchbox").value = "";
343 viewfeed(active_feed_id, 0, "");
344}
ac53063a 345
c374a3fe 346function search(feed) {
c374a3fe 347 viewfeed(feed, 0, "");
76798ff3 348}
1cd17194 349
13ad9102
AD
350function localPiggieFunction(enable) {
351 if (enable) {
352 var query_str = "backend.php?op=feeds&subop=piggie";
353
c0e5a40e 354 if (xmlhttp_ready(xmlhttp)) {
13ad9102
AD
355
356 xmlhttp.open("GET", query_str, true);
357 xmlhttp.onreadystatechange=feedlist_callback;
358 xmlhttp.send(null);
359 }
360 }
361}
362
9cfc649a
AD
363function relativeid_callback() {
364
365 if (xmlhttp_rpc.readyState == 4) {
366 notify(xmlhttp_rpc.responseText);
367 }
368
369}
370
371function 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 }
9cfc649a 383 return rows;
9cfc649a
AD
384}
385
c3a8d71a
AD
386function getFirstVisibleHeadlineId() {
387 var rows = getVisibleHeadlineIds();
388 return rows[0];
389}
9cfc649a 390
c3a8d71a
AD
391function getLastVisibleHeadlineId() {
392 var rows = getVisibleHeadlineIds();
393 return rows[rows.length-1];
394}
9cfc649a 395
c3a8d71a 396function moveToPost(mode) {
9cfc649a
AD
397
398 var rows = getVisibleHeadlineIds();
399
400 var prev_id;
401 var next_id;
402
c3a8d71a
AD
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 }
9cfc649a
AD
412 }
413 }
414
c3a8d71a
AD
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 }
9cfc649a
AD
424 }
425
c3a8d71a
AD
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 }
9cfc649a
AD
433 }
434
435}
436
437function localHotkeyHandler(keycode) {
438
9cfc649a 439 if (keycode == 78) {
c3a8d71a 440 return moveToPost('next');
9cfc649a
AD
441 }
442
443 if (keycode == 80) {
c3a8d71a 444 return moveToPost('prev');
9cfc649a 445 }
c3a8d71a
AD
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
9cfc649a
AD
457}
458
13ad9102 459
76798ff3 460function init() {
c0e5a40e
AD
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) {
c3a8d71a
AD
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
476cac42 476 updateFeedList(false, false);
13ad9102 477 document.onkeydown = hotkey_handler;
ac53063a 478 setTimeout("timeout()", 1800*1000);
1cd17194 479}