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