]> git.wh0rd.org - tt-rss.git/blame - tt-rss.js
rework auto-refresh method if number of subscriptions changed
[tt-rss.git] / tt-rss.js
CommitLineData
1cd17194 1var xmlhttp = false;
76798ff3 2var total_unread = 0;
525116d4 3var first_run = true;
8143ae1f 4var display_tags = false;
806a3d14 5var global_unread = -1;
21703604 6var active_title_text = "";
21703604 7var current_subtitle = "";
c6784aea 8var daemon_enabled = false;
0d51e25d 9var daemon_refresh_only = false;
15da5cc1 10var _qfd_deleted_feed = 0;
2bf6e0a8 11var firsttime_update = true;
c441662f 12var last_refetch = 0;
76b4eae1 13var cookie_lifetime = 0;
33d13e72 14var active_feed_id = 0;
0a6c4846 15var active_feed_is_cat = false;
9e397d0f 16var number_of_feeds = 0;
15da5cc1 17
a58069db 18var xmlhttp = Ajax.getTransport();
1cd17194 19
1035fcec 20var init_params = new Object();
3ac2b520 21
8143ae1f
AD
22function toggleTags() {
23 display_tags = !display_tags;
24
25 var p = document.getElementById("dispSwitchPrompt");
26
27 if (display_tags) {
28 p.innerHTML = "display feeds";
29 } else {
30 p.innerHTML = "display tags";
31 }
32
33 updateFeedList();
34}
35
6de5d056 36function dlg_frefresh_callback() {
1cd17194 37 if (xmlhttp.readyState == 4) {
a24f525c 38 notify(xmlhttp.responseText);
e2f8f7b4 39 updateFeedList(false, false);
15da5cc1
AD
40 if (_qfd_deleted_feed) {
41 var hframe = document.getElementById("headlines-frame");
42 if (hframe) {
43 hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
44 }
45 }
eff4997c 46 closeInfoBox();
c0e5a40e 47 }
1cd17194 48}
e2f8f7b4 49
8158c57a 50function refetch_callback() {
29fb8c70 51 if (xmlhttp.readyState == 4) {
7719618b 52 try {
310da49d 53
c441662f
AD
54 var date = new Date();
55
56 last_refetch = date.getTime() / 1000;
57
7719618b 58 if (!xmlhttp.responseXML) {
0530ddd8 59 notify("refetch_callback: backend did not return valid XML", true, true);
7719618b
AD
60 return;
61 }
62
63 var reply = xmlhttp.responseXML.firstChild;
262bd8ea 64
7719618b 65 if (!reply) {
0530ddd8 66 notify("refetch_callback: backend did not return expected XML object", true, true);
e2cb4c6d 67 updateTitle("");
7719618b
AD
68 return;
69 }
262bd8ea 70
7719618b
AD
71 var error_code = reply.getAttribute("error-code");
72
73 if (error_code && error_code != 0) {
af106b0e 74 return fatalError(error_code, reply.getAttribute("error-msg"));
7719618b 75 }
f54f515f
AD
76
77 var counters = reply.firstChild;
7719618b 78
f54f515f 79 parse_counters(counters, true);
c6784aea 80
71ad883b
AD
81 var runtime_info = counters.nextSibling;
82
83 parse_runtime_info(runtime_info);
84
0ee1d1a0
AD
85 debug("refetch_callback: done");
86
0d51e25d 87 if (!daemon_enabled && !daemon_refresh_only) {
c6784aea 88 notify("All feeds updated.");
5a180505 89 updateTitle("");
4c059b7b
AD
90 } else {
91 notify("");
c6784aea 92 }
7719618b
AD
93 } catch (e) {
94 exception_error("refetch_callback", e);
e2cb4c6d 95 updateTitle("");
7719618b 96 }
090e250b
AD
97 }
98}
1a66d16e 99
295f9b42
AD
100function backend_sanity_check_callback() {
101
102 if (xmlhttp.readyState == 4) {
295f9b42 103
7719618b
AD
104 try {
105
106 if (!xmlhttp.responseXML) {
462a192b 107 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
7719618b
AD
108 return;
109 }
295f9b42 110
3ac2b520 111 var reply = xmlhttp.responseXML.firstChild.firstChild;
7719618b
AD
112
113 if (!reply) {
af106b0e 114 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
7719618b
AD
115 return;
116 }
117
118 var error_code = reply.getAttribute("error-code");
119
120 if (error_code && error_code != 0) {
af106b0e 121 return fatalError(error_code, reply.getAttribute("error-msg"));
7719618b
AD
122 }
123
0ee1d1a0
AD
124 debug("sanity check ok");
125
3ac2b520
AD
126 var params = reply.nextSibling;
127
128 if (params) {
129 debug('reading init-params...');
130 var param = params.firstChild;
131
132 while (param) {
133 var k = param.getAttribute("key");
134 var v = param.getAttribute("value");
135 debug(k + " => " + v);
136 init_params[k] = v;
137 param = param.nextSibling;
138 }
139 }
140
7719618b 141 init_second_stage();
295f9b42 142
7719618b
AD
143 } catch (e) {
144 exception_error("backend_sanity_check_callback", e);
145 }
295f9b42
AD
146 }
147}
148
cb246176 149function scheduleFeedUpdate(force) {
525116d4 150
0d51e25d 151 if (!daemon_enabled && !daemon_refresh_only) {
4d4200a8 152 notify("Updating feeds, please wait.", true);
c6784aea
AD
153 updateTitle("Updating");
154 }
55193822 155
cb246176
AD
156 var query_str = "backend.php?op=rpc&subop=";
157
158 if (force) {
c3b81db0 159 query_str = query_str + "forceUpdateAllFeeds";
cb246176 160 } else {
c3b81db0 161 query_str = query_str + "updateAllFeeds";
cb246176 162 }
525116d4 163
9826bd2e
AD
164 var omode;
165
b6104dee 166 if (firsttime_update && !navigator.userAgent.match("Opera")) {
2bf6e0a8
AD
167 firsttime_update = false;
168 omode = "T";
9826bd2e 169 } else {
2bf6e0a8
AD
170 if (display_tags) {
171 omode = "t";
172 } else {
173 omode = "flc";
174 }
9826bd2e 175 }
2bf6e0a8 176
9826bd2e 177 query_str = query_str + "&omode=" + omode;
78ea1de0 178 query_str = query_str + "&uctr=" + global_unread;
9826bd2e 179
0ee1d1a0 180 debug("in scheduleFeedUpdate");
2bf6e0a8 181
c441662f
AD
182 var date = new Date();
183
184 if (!xmlhttp_ready(xmlhttp) && last_refetch < date.getTime() / 1000 - 60) {
a7565293 185 debug("<b>xmlhttp seems to be stuck, aborting</b>");
c441662f
AD
186 xmlhttp.abort();
187 }
188
29fb8c70
AD
189 if (xmlhttp_ready(xmlhttp)) {
190 xmlhttp.open("GET", query_str, true);
191 xmlhttp.onreadystatechange=refetch_callback;
192 xmlhttp.send(null);
525116d4 193 } else {
c441662f 194 debug("xmlhttp busy");
bed0f18f 195 //printLockingError();
c0e5a40e 196 }
525116d4 197}
1cd17194 198
525116d4 199function updateFeedList(silent, fetch) {
c0e5a40e 200
1a66d16e
AD
201// if (silent != true) {
202// notify("Loading feed list...");
203// }
82baad4a 204
331900c6
AD
205 var query_str = "backend.php?op=feeds";
206
8143ae1f
AD
207 if (display_tags) {
208 query_str = query_str + "&tags=1";
209 }
210
5c365f60 211 if (getActiveFeedId() && !activeFeedIsCat()) {
86741347 212 query_str = query_str + "&actid=" + getActiveFeedId();
175847de
AD
213 }
214
59b8192f
AD
215 if (navigator.userAgent.match("Opera")) {
216 var date = new Date();
217 var timestamp = Math.round(date.getTime() / 1000);
218 query_str = query_str + "&ts=" + timestamp
219 }
220
1a66d16e 221 if (fetch) query_str = query_str + "&fetch=yes";
e1123aee 222
1a66d16e 223 var feeds_frame = document.getElementById("feeds-frame");
e1123aee 224
1a66d16e
AD
225 feeds_frame.src = query_str;
226}
175847de 227
476cac42 228function catchupAllFeeds() {
076682aa 229
476cac42
AD
230 var query_str = "backend.php?op=feeds&subop=catchupAll";
231
232 notify("Marking all feeds as read...");
233
1a66d16e
AD
234 var feeds_frame = document.getElementById("feeds-frame");
235
236 feeds_frame.src = query_str;
476cac42 237
fc69e641 238 global_unread = 0;
21703604 239 updateTitle("");
fc69e641 240
476cac42 241}
1cd17194 242
767e2486 243function viewCurrentFeed(subop) {
1a66d16e 244
ec6e2fd3
AD
245// if (getActiveFeedId()) {
246 if (getActiveFeedId() != undefined) {
767e2486 247 viewfeed(getActiveFeedId(), subop);
033e47e0
AD
248 } else {
249 disableContainerChildren("headlinesToolbar", false, document);
ec6e2fd3 250// viewfeed(-1, subop); // FIXME
f0601b87 251 }
164f4738 252 return false; // block unneeded form submits
f0601b87
AD
253}
254
767e2486 255function viewfeed(feed, subop) {
db8d6f67 256 var f = window.frames["feeds-frame"];
767e2486 257 f.viewfeed(feed, subop);
9cfc649a
AD
258}
259
40d13c28 260function timeout() {
05732aa0 261 scheduleFeedUpdate(false);
f5de0d8d 262
3ac2b520 263 var refresh_time = getInitParam("feeds_frame_refresh");
f5de0d8d 264
3ac2b520 265 if (!refresh_time) refresh_time = 600;
f5de0d8d
AD
266
267 setTimeout("timeout()", refresh_time*1000);
ac53063a
AD
268}
269
c374a3fe 270function resetSearch() {
64c620ce
AD
271 var searchbox = document.getElementById("searchbox")
272
86741347 273 if (searchbox.value != "" && getActiveFeedId()) {
64c620ce 274 searchbox.value = "";
767e2486 275 viewfeed(getActiveFeedId(), "");
ac43eba1 276 }
c374a3fe 277}
ac53063a 278
86b682ce
AD
279function searchCancel() {
280 closeInfoBox(true);
281}
282
f0601b87 283function search() {
eff4997c 284 closeInfoBox();
4ce19859 285 viewCurrentFeed(0, "");
76798ff3 286}
1cd17194 287
13ad9102
AD
288function localPiggieFunction(enable) {
289 if (enable) {
290 var query_str = "backend.php?op=feeds&subop=piggie";
291
c0e5a40e 292 if (xmlhttp_ready(xmlhttp)) {
13ad9102
AD
293
294 xmlhttp.open("GET", query_str, true);
295 xmlhttp.onreadystatechange=feedlist_callback;
296 xmlhttp.send(null);
297 }
298 }
299}
300
806a3d14
AD
301// if argument is undefined, current subtitle is not updated
302// use blank string to clear subtitle
fc69e641
AD
303function updateTitle(s) {
304 var tmp = "Tiny Tiny RSS";
21703604 305
5a494a0b 306 if (s != undefined) {
21703604
AD
307 current_subtitle = s;
308 }
309
fc69e641
AD
310 if (global_unread > 0) {
311 tmp = tmp + " (" + global_unread + ")";
312 }
313
5a494a0b 314 if (current_subtitle) {
21703604 315 tmp = tmp + " - " + current_subtitle;
fc69e641 316 }
21703604
AD
317
318 if (active_title_text.length > 0) {
319 tmp = tmp + " > " + active_title_text;
320 }
321
fc69e641
AD
322 document.title = tmp;
323}
324
22a93ad8 325function genericSanityCheck() {
ac43eba1 326
295f9b42
AD
327 if (!xmlhttp) fatalError(1);
328
329 setCookie("ttrss_vf_test", "TEST");
330
331 if (getCookie("ttrss_vf_test") != "TEST") {
332 fatalError(2);
333 }
334
22a93ad8
AD
335 return true;
336}
337
338function init() {
339
7719618b 340 try {
fe2f1970 341
97dcd654
AD
342 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
343
344 if (arguments.callee.done) return;
345 arguments.callee.done = true;
346
7719618b 347 disableContainerChildren("headlinesToolbar", true);
70830c87 348
86b682ce
AD
349 Form.disable("main_toolbar_form");
350
7719618b
AD
351 if (!genericSanityCheck())
352 return;
ac43eba1 353
0ee1d1a0
AD
354 if (getURLParam('debug')) {
355 document.getElementById('debug_output').style.display = 'block';
356 debug('debug mode activated');
357 }
358
7719618b
AD
359 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
360 xmlhttp.onreadystatechange=backend_sanity_check_callback;
361 xmlhttp.send(null);
47179952 362
7719618b
AD
363 } catch (e) {
364 exception_error("init", e);
a8d28f48 365 }
7719618b 366}
86741347 367
1de2b92c
AD
368function resize_feeds_frame() {
369 var f = document.getElementById("feeds-frame");
370 var tf = document.getElementById("mainFooter");
371 var th = document.getElementById("mainHeader");
a6c3f28c
AD
372
373 var footer_height = 0;
374 var header_height = 0;
375
376 if (tf) {
377 footer_height = tf.scrollHeight;
378 }
379
380 if (th) {
381 header_height = th.scrollHeight;
382 }
383
384 f.style.height = document.body.scrollHeight - footer_height -
385 header_height - 50 + "px";
1de2b92c
AD
386}
387
7719618b 388function init_second_stage() {
295f9b42 389
7719618b 390 try {
2f587484 391
76b4eae1
AD
392 cookie_lifetime = getCookie("ttrss_cltime");
393
394 delCookie("ttrss_vf_test");
7719618b
AD
395
396 updateFeedList(false, false);
397 document.onkeydown = hotkey_handler;
1b0809ae 398
f31673f7
AD
399 var tb = parent.document.forms["main_toolbar_form"];
400
3ac2b520
AD
401 dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
402 dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
c6784aea 403
0d51e25d
AD
404 daemon_enabled = getInitParam("daemon_enabled") == 1;
405 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
5f136c9a 406
1de2b92c
AD
407 // FIXME should be callled after window resize
408
caa53a7c
AD
409 var h = document.getElementById("headlines");
410 var c = document.getElementById("content");
411
5f136c9a 412 if (navigator.userAgent.match("Opera")) {
1de2b92c 413 resize_feeds_frame();
5f136c9a 414 }
0ee1d1a0
AD
415
416 debug("second stage ok");
7719618b 417
7719618b
AD
418 } catch (e) {
419 exception_error("init_second_stage", e);
2f587484 420 }
1cd17194 421}
ac43eba1 422
c09ec856 423function quickMenuChange() {
cbe45fa8 424 var chooser = document.getElementById("quickMenuChooser");
86b682ce 425 var opid = chooser[chooser.selectedIndex].value;
e2f8f7b4 426
c09ec856
AD
427 chooser.selectedIndex = 0;
428 quickMenuGo(opid);
429}
430
431function quickMenuGo(opid) {
bb3423cf 432 try {
c09ec856 433
bb3423cf
AD
434 if (opid == "qmcPrefs") {
435 gotoPreferences();
436 }
437
438 if (opid == "qmcSearch") {
0a6c4846 439 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
6de5d056
AD
440 return;
441 }
bb3423cf
AD
442
443 if (opid == "qmcAddFeed") {
444 displayDlg("quickAddFeed");
445 return;
69668465 446 }
6de5d056 447
bb3423cf
AD
448 if (opid == "qmcRemoveFeed") {
449 var actid = getActiveFeedId();
450
5c365f60 451 if (!actid || activeFeedIsCat()) {
0530ddd8 452 alert("Please select some feed first.");
bb3423cf
AD
453 return;
454 }
64a2875d
AD
455
456 var fn = getFeedName(actid);
bb3423cf 457
64a2875d 458 if (confirm("Unsubscribe from " + fn + "?")) {
bb3423cf
AD
459 qfdDelete(actid);
460 }
461
462 return;
463 }
464
465 if (opid == "qmcUpdateFeeds") {
466 scheduleFeedUpdate(true);
467 return;
468 }
469
470 if (opid == "qmcCatchupAll") {
471 catchupAllFeeds();
472 return;
473 }
474
475 if (opid == "qmcShowOnlyUnread") {
476 toggleDispRead();
477 return;
478 }
479
480 if (opid == "qmcAddFilter") {
481 displayDlg("quickAddFilter", getActiveFeedId());
482 }
483 } catch (e) {
484 exception_error("quickMenuGo", e);
a24f525c 485 }
e2f8f7b4
AD
486}
487
6de5d056
AD
488function qfdDelete(feed_id) {
489
490 notify("Removing feed...");
491
7f123cda
AD
492 if (!xmlhttp_ready(xmlhttp)) {
493 printLockingError();
494 return
495 }
496
15da5cc1
AD
497 _qfd_deleted_feed = feed_id;
498
69668465 499 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
6de5d056
AD
500 xmlhttp.onreadystatechange=dlg_frefresh_callback;
501 xmlhttp.send(null);
6de5d056 502}
033e47e0 503
3745788e 504
21703604
AD
505function updateFeedTitle(t) {
506 active_title_text = t;
507 updateTitle();
508}
509
3745788e 510function toggleDispRead() {
7f123cda 511 try {
3745788e 512
7f123cda
AD
513 if (!xmlhttp_ready(xmlhttp)) {
514 printLockingError();
515 return
516 }
3745788e 517
e8bd0da9 518 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
3745788e 519
7f123cda 520 hide_read_feeds = !hide_read_feeds;
e8bd0da9
AD
521
522 debug("toggle_disp_read => " + hide_read_feeds);
523
524 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
525
7f123cda
AD
526 var query = "backend.php?op=rpc&subop=setpref" +
527 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
3745788e 528
e8bd0da9
AD
529 storeInitParam("hide_read_feeds", hide_read_feeds, true);
530
531 new Ajax.Request(query);
7f123cda
AD
532
533 } catch (e) {
534 exception_error("toggleDispRead", e);
3745788e 535 }
3745788e 536}
295f9b42 537
71ad883b
AD
538function parse_runtime_info(elem) {
539 var param = elem.firstChild;
540
1cb7492d
AD
541 debug("parse_runtime_info");
542
71ad883b
AD
543 while (param) {
544 var k = param.getAttribute("key");
545 var v = param.getAttribute("value");
3ac2b520 546
1cb7492d
AD
547 debug("RI: " + k + " => " + v);
548
71ad883b
AD
549 var w = document.getElementById("noDaemonWarning");
550
551 if (w) {
552 if (k == "daemon_is_running" && v != 1) {
553 w.style.display = "block";
554 } else {
555 w.style.display = "none";
556 }
557 }
558 param = param.nextSibling;
559 }
560}
fce24838
AD
561
562function catchupCurrentFeed() {
563
234e467c 564 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
fce24838 565
234e467c
AD
566 var str = "Mark all articles in " + fn + " as read?";
567
568/* if (active_feed_is_cat) {
569 str = "Mark all articles in this category as read?";
570 } */
571
572 if (confirm(str)) {
28de3732 573 return viewCurrentFeed('MarkAllRead')
fce24838
AD
574 }
575}
88040f57 576