]> git.wh0rd.org - tt-rss.git/blame - tt-rss.js
mark currently selected post on headlines update, remove debug msg on content hash...
[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
a3ee2a38
AD
100 cleanSelected("feedsList");
101
102 feedr.className = feedr.className + "Selected";
a1a8a2be 103 }
1c37c607 104
c374a3fe 105 var searchbox = document.getElementById("searchbox");
c374a3fe
AD
106 searchbox.value = search_query;
107
b5daec98
AD
108 markHeadline(active_post_id);
109
1c37c607
AD
110 notify("");
111
112 }
1cd17194
AD
113}
114
115function view_callback() {
d76a3b03 116 var container = document.getElementById('content');
c3a8d71a
AD
117 if (xmlhttp_view.readyState == 4) {
118 container.innerHTML=xmlhttp_view.responseText;
1cd17194
AD
119 }
120}
121
525116d4 122function refetch_callback() {
c0e5a40e 123
525116d4 124 if (xmlhttp_rpc.readyState == 4) {
c3b81db0 125 notify("All feeds updated");
c3b81db0 126 var container = document.getElementById('feeds');
c3b81db0 127 container.innerHTML = xmlhttp_rpc.responseText;
55193822 128 document.title = "Tiny Tiny RSS";
c0e5a40e 129 }
525116d4
AD
130}
131
cb246176 132function scheduleFeedUpdate(force) {
525116d4
AD
133
134 notify("Updating feeds in background...");
135
55193822
AD
136 document.title = "Tiny Tiny RSS - Updating...";
137
cb246176
AD
138 var query_str = "backend.php?op=rpc&subop=";
139
140 if (force) {
c3b81db0 141 query_str = query_str + "forceUpdateAllFeeds";
cb246176 142 } else {
c3b81db0 143 query_str = query_str + "updateAllFeeds";
cb246176 144 }
525116d4 145
c0e5a40e 146 if (xmlhttp_ready(xmlhttp_rpc)) {
525116d4
AD
147 xmlhttp_rpc.open("GET", query_str, true);
148 xmlhttp_rpc.onreadystatechange=refetch_callback;
149 xmlhttp_rpc.send(null);
150 } else {
151 printLockingError();
c0e5a40e 152 }
525116d4 153}
1cd17194 154
525116d4 155function updateFeedList(silent, fetch) {
c0e5a40e 156
525116d4 157 if (silent != true) {
11c2f3fa 158 notify("Loading feed list...");
40d13c28 159 }
82baad4a 160
331900c6
AD
161 var query_str = "backend.php?op=feeds";
162
163 if (fetch) query_str = query_str + "&fetch=yes";
164
c0e5a40e 165 if (xmlhttp_ready(xmlhttp)) {
076682aa
AD
166 xmlhttp.open("GET", query_str, true);
167 xmlhttp.onreadystatechange=feedlist_callback;
168 xmlhttp.send(null);
169 } else {
a234aee5 170 printLockingError();
076682aa 171 }
1cd17194
AD
172}
173
175847de
AD
174function catchupPage(feed) {
175
c0e5a40e 176 if (!xmlhttp_ready(xmlhttp)) {
a234aee5 177 printLockingError();
076682aa
AD
178 return
179 }
180
175847de
AD
181 var content = document.getElementById("headlinesList");
182
183 var rows = new Array();
184
185 for (i = 0; i < content.rows.length; i++) {
186 var row_id = content.rows[i].id.replace("RROW-", "");
187 if (row_id.length > 0) {
cb0bd8bd
AD
188 if (content.rows[i].className.match("Unread")) {
189 rows.push(row_id);
190 content.rows[i].className = content.rows[i].className.replace("Unread", "");
191 }
175847de
AD
192 }
193 }
194
cb0bd8bd 195 if (rows.length > 0) {
175847de 196
cb0bd8bd
AD
197 var feedr = document.getElementById("FEEDR-" + feed);
198 var feedu = document.getElementById("FEEDU-" + feed);
199
200 feedu.innerHTML = feedu.innerHTML - rows.length;
201
202 if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
203 feedr.className = feedr.className + "Unread";
204 } else if (feedu.innerHTML <= 0) {
205 feedr.className = feedr.className.replace("Unread", "");
206 }
207
208 var query_str = "backend.php?op=rpc&subop=catchupPage&ids=" +
209 param_escape(rows.toString());
210
211 notify("Marking this page as read...");
212
213 xmlhttp.open("GET", query_str, true);
214 xmlhttp.onreadystatechange=notify_callback;
215 xmlhttp.send(null);
175847de 216
cb0bd8bd
AD
217 } else {
218 notify("No unread items on this page.");
175847de 219
cb0bd8bd 220 }
175847de
AD
221}
222
476cac42 223function catchupAllFeeds() {
076682aa 224
c0e5a40e 225 if (!xmlhttp_ready(xmlhttp)) {
a234aee5 226 printLockingError();
076682aa
AD
227 return
228 }
476cac42
AD
229 var query_str = "backend.php?op=feeds&subop=catchupAll";
230
231 notify("Marking all feeds as read...");
232
233 xmlhttp.open("GET", query_str, true);
234 xmlhttp.onreadystatechange=feedlist_callback;
235 xmlhttp.send(null);
236
237}
1cd17194 238
476cac42 239function viewfeed(feed, skip, subop) {
1cd17194 240
36bf7496
AD
241 enableHotkeys();
242
c374a3fe
AD
243 var searchbox = document.getElementById("searchbox");
244
245 if (searchbox) {
246 search_query = searchbox.value;
247 } else {
248 search_query = "";
249 }
250
251/* if (active_feed_id == feed && subop != "ForceUpdate") {
252 notify("This feed is currently selected.");
253 return;
254 } */
d76a3b03 255
c3a8d71a
AD
256 if (skip < 0 || skip > total_feed_entries) {
257 return;
258 }
259
c0e5a40e 260 if (!xmlhttp_ready(xmlhttp)) {
a234aee5 261 printLockingError();
076682aa
AD
262 return
263 }
c3a8d71a 264
b5daec98
AD
265 if (active_feed_id != feed)
266 active_post_id = false;
267
9cfc649a 268 active_feed_id = feed;
c3a8d71a 269 active_offset = skip;
076682aa 270
c374a3fe
AD
271 var query = "backend.php?op=viewfeed&feed=" + param_escape(feed) +
272 "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop);
273
274 if (search_query != "") {
275 query = query + "&search=" + param_escape(search_query);
276 }
277
278 xmlhttp.open("GET", query, true);
1cd17194
AD
279 xmlhttp.onreadystatechange=viewfeed_callback;
280 xmlhttp.send(null);
281
1c37c607
AD
282 notify("Loading headlines...");
283
1cd17194
AD
284}
285
b5daec98
AD
286function markHeadline(id) {
287 var row = document.getElementById("RROW-" + id);
288 if (row) {
289 row.className = row.className + "Selected";
290 }
291}
292
a3ee2a38
AD
293function cleanSelected(element) {
294 var content = document.getElementById(element);
9cfc649a
AD
295
296 var rows = new Array();
297
298 for (i = 0; i < content.rows.length; i++) {
299 content.rows[i].className = content.rows[i].className.replace("Selected", "");
300 }
301
302}
303
a1a8a2be 304function view(id,feed_id) {
d76a3b03 305
36bf7496
AD
306 enableHotkeys();
307
c0e5a40e 308 if (!xmlhttp_ready(xmlhttp_view)) {
a234aee5 309 printLockingError();
076682aa
AD
310 return
311 }
312
d76a3b03
AD
313 var crow = document.getElementById("RROW-" + id);
314
a1a8a2be
AD
315 if (crow.className.match("Unread")) {
316 var umark = document.getElementById("FEEDU-" + feed_id);
317 umark.innerHTML = umark.innerHTML - 1;
d76a3b03 318 crow.className = crow.className.replace("Unread", "");
d76a3b03 319
a1a8a2be
AD
320 if (umark.innerHTML == "0") {
321 var feedr = document.getElementById("FEEDR-" + feed_id);
322 feedr.className = feedr.className.replace("Unread", "");
323 }
9cfc649a 324
76798ff3 325 total_unread--;
76798ff3 326 }
1cd17194 327
a3ee2a38 328 cleanSelected("headlinesList");
9cfc649a
AD
329
330 crow.className = crow.className + "Selected";
331
b197f117
AD
332 var upd_img_pic = document.getElementById("FUPDPIC-" + id);
333
334 if (upd_img_pic) {
335 upd_img_pic.innerHTML = "";
336 }
337
d76a3b03 338 document.getElementById('content').innerHTML='Loading, please wait...';
1cd17194 339
9cfc649a
AD
340 active_post_id = id;
341
c3a8d71a
AD
342 xmlhttp_view.open("GET", "backend.php?op=view&id=" + param_escape(id), true);
343 xmlhttp_view.onreadystatechange=view_callback;
344 xmlhttp_view.send(null);
1cd17194 345
c0e5a40e 346
1cd17194
AD
347}
348
40d13c28 349function timeout() {
cb246176 350 scheduleFeedUpdate(true);
ac53063a 351 setTimeout("timeout()", 1800*1000);
ac53063a
AD
352}
353
c374a3fe
AD
354function resetSearch() {
355 document.getElementById("searchbox").value = "";
356 viewfeed(active_feed_id, 0, "");
357}
ac53063a 358
c374a3fe 359function search(feed) {
c374a3fe 360 viewfeed(feed, 0, "");
76798ff3 361}
1cd17194 362
13ad9102
AD
363function localPiggieFunction(enable) {
364 if (enable) {
365 var query_str = "backend.php?op=feeds&subop=piggie";
366
c0e5a40e 367 if (xmlhttp_ready(xmlhttp)) {
13ad9102
AD
368
369 xmlhttp.open("GET", query_str, true);
370 xmlhttp.onreadystatechange=feedlist_callback;
371 xmlhttp.send(null);
372 }
373 }
374}
375
9cfc649a
AD
376function relativeid_callback() {
377
378 if (xmlhttp_rpc.readyState == 4) {
379 notify(xmlhttp_rpc.responseText);
380 }
381
382}
383
384function getVisibleHeadlineIds() {
385
386 var content = document.getElementById("headlinesList");
387
388 var rows = new Array();
389
390 for (i = 0; i < content.rows.length; i++) {
391 var row_id = content.rows[i].id.replace("RROW-", "");
392 if (row_id.length > 0) {
393 rows.push(row_id);
394 }
395 }
9cfc649a 396 return rows;
9cfc649a
AD
397}
398
c3a8d71a
AD
399function getFirstVisibleHeadlineId() {
400 var rows = getVisibleHeadlineIds();
401 return rows[0];
402}
9cfc649a 403
c3a8d71a
AD
404function getLastVisibleHeadlineId() {
405 var rows = getVisibleHeadlineIds();
406 return rows[rows.length-1];
407}
9cfc649a 408
c3a8d71a 409function moveToPost(mode) {
9cfc649a
AD
410
411 var rows = getVisibleHeadlineIds();
412
413 var prev_id;
414 var next_id;
415
c3a8d71a
AD
416 if (active_post_id == false) {
417 next_id = getFirstVisibleHeadlineId();
418 prev_id = getLastVisibleHeadlineId();
419 } else {
420 for (var i = 0; i < rows.length; i++) {
421 if (rows[i] == active_post_id) {
422 prev_id = rows[i-1];
423 next_id = rows[i+1];
424 }
9cfc649a
AD
425 }
426 }
427
c3a8d71a
AD
428 var content = document.getElementById("headlinesList");
429
430 if (mode == "next") {
431 if (next_id != undefined) {
432 view(next_id, active_feed_id);
433 } else {
434 _viewfeed_autoselect_first = true;
435 viewfeed(active_feed_id, active_offset+15);
436 }
9cfc649a
AD
437 }
438
c3a8d71a
AD
439 if (mode == "prev") {
440 if ( prev_id != undefined) {
441 view(prev_id, active_feed_id);
442 } else {
443 _viewfeed_autoselect_last = true;
444 viewfeed(active_feed_id, active_offset-15);
445 }
9cfc649a
AD
446 }
447
448}
449
450function localHotkeyHandler(keycode) {
451
9cfc649a 452 if (keycode == 78) {
c3a8d71a 453 return moveToPost('next');
9cfc649a
AD
454 }
455
456 if (keycode == 80) {
c3a8d71a 457 return moveToPost('prev');
9cfc649a 458 }
c3a8d71a
AD
459
460 if (keycode == 82) {
461 return scheduleFeedUpdate(true);
462 }
463
464 if (keycode == 85) {
465 return viewfeed(active_feed_id, active_offset, "ForceUpdate");
466 }
467
468// notify("KC: " + keycode);
469
9cfc649a
AD
470}
471
13ad9102 472
76798ff3 473function init() {
c0e5a40e
AD
474
475 // IE kludge
476
477 if (xmlhttp && !xmlhttp_rpc) {
478 xmlhttp_rpc = xmlhttp;
479 xmlhttp_view = xmlhttp;
480 }
481
482 if (!xmlhttp || !xmlhttp_rpc || !xmlhttp_view) {
c3a8d71a
AD
483 document.getElementById("headlines").innerHTML =
484 "<b>Fatal error:</b> This program needs XmlHttpRequest " +
485 "to function properly. Your browser doesn't seem to support it.";
486 return;
487 }
488
476cac42 489 updateFeedList(false, false);
13ad9102 490 document.onkeydown = hotkey_handler;
ac53063a 491 setTimeout("timeout()", 1800*1000);
1cd17194 492}