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