]> git.wh0rd.org - tt-rss.git/blame - tt-rss.js
add back-req iframe
[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
52db9978
AD
189 debug("REFETCH query: " + query_str);
190
29fb8c70
AD
191 if (xmlhttp_ready(xmlhttp)) {
192 xmlhttp.open("GET", query_str, true);
193 xmlhttp.onreadystatechange=refetch_callback;
194 xmlhttp.send(null);
525116d4 195 } else {
c441662f 196 debug("xmlhttp busy");
bed0f18f 197 //printLockingError();
c0e5a40e 198 }
525116d4 199}
1cd17194 200
525116d4 201function updateFeedList(silent, fetch) {
c0e5a40e 202
1a66d16e
AD
203// if (silent != true) {
204// notify("Loading feed list...");
205// }
82baad4a 206
331900c6
AD
207 var query_str = "backend.php?op=feeds";
208
8143ae1f
AD
209 if (display_tags) {
210 query_str = query_str + "&tags=1";
211 }
212
5c365f60 213 if (getActiveFeedId() && !activeFeedIsCat()) {
86741347 214 query_str = query_str + "&actid=" + getActiveFeedId();
175847de
AD
215 }
216
59b8192f
AD
217 if (navigator.userAgent.match("Opera")) {
218 var date = new Date();
219 var timestamp = Math.round(date.getTime() / 1000);
220 query_str = query_str + "&ts=" + timestamp
221 }
222
1a66d16e 223 if (fetch) query_str = query_str + "&fetch=yes";
e1123aee 224
6b4163cb
AD
225// var feeds_frame = document.getElementById("feeds-frame");
226// feeds_frame.src = query_str;
227
80e4dc34 228 debug("updateFeedList: " + query_str);
6b4163cb
AD
229
230 if (xmlhttp_ready(xmlhttp)) {
231 xmlhttp.open("GET", query_str, true);
232 xmlhttp.onreadystatechange=feedlist_callback;
233 xmlhttp.send(null);
234 } else {
235 debug("xmlhttp busy");
236 //printLockingError();
237 }
e1123aee 238
1a66d16e 239}
175847de 240
476cac42 241function catchupAllFeeds() {
076682aa 242
476cac42
AD
243 var query_str = "backend.php?op=feeds&subop=catchupAll";
244
245 notify("Marking all feeds as read...");
246
1a66d16e
AD
247 var feeds_frame = document.getElementById("feeds-frame");
248
249 feeds_frame.src = query_str;
476cac42 250
fc69e641 251 global_unread = 0;
21703604 252 updateTitle("");
fc69e641 253
476cac42 254}
1cd17194 255
767e2486 256function viewCurrentFeed(subop) {
1a66d16e 257
ec6e2fd3
AD
258// if (getActiveFeedId()) {
259 if (getActiveFeedId() != undefined) {
767e2486 260 viewfeed(getActiveFeedId(), subop);
033e47e0
AD
261 } else {
262 disableContainerChildren("headlinesToolbar", false, document);
ec6e2fd3 263// viewfeed(-1, subop); // FIXME
f0601b87 264 }
164f4738 265 return false; // block unneeded form submits
f0601b87
AD
266}
267
767e2486 268function viewfeed(feed, subop) {
db8d6f67 269 var f = window.frames["feeds-frame"];
767e2486 270 f.viewfeed(feed, subop);
9cfc649a
AD
271}
272
40d13c28 273function timeout() {
05732aa0 274 scheduleFeedUpdate(false);
f5de0d8d 275
3ac2b520 276 var refresh_time = getInitParam("feeds_frame_refresh");
f5de0d8d 277
3ac2b520 278 if (!refresh_time) refresh_time = 600;
f5de0d8d
AD
279
280 setTimeout("timeout()", refresh_time*1000);
ac53063a
AD
281}
282
c374a3fe 283function resetSearch() {
64c620ce
AD
284 var searchbox = document.getElementById("searchbox")
285
86741347 286 if (searchbox.value != "" && getActiveFeedId()) {
64c620ce 287 searchbox.value = "";
767e2486 288 viewfeed(getActiveFeedId(), "");
ac43eba1 289 }
c374a3fe 290}
ac53063a 291
86b682ce
AD
292function searchCancel() {
293 closeInfoBox(true);
294}
295
f0601b87 296function search() {
eff4997c 297 closeInfoBox();
4ce19859 298 viewCurrentFeed(0, "");
76798ff3 299}
1cd17194 300
13ad9102
AD
301function localPiggieFunction(enable) {
302 if (enable) {
303 var query_str = "backend.php?op=feeds&subop=piggie";
304
c0e5a40e 305 if (xmlhttp_ready(xmlhttp)) {
13ad9102
AD
306
307 xmlhttp.open("GET", query_str, true);
308 xmlhttp.onreadystatechange=feedlist_callback;
309 xmlhttp.send(null);
310 }
311 }
312}
313
806a3d14
AD
314// if argument is undefined, current subtitle is not updated
315// use blank string to clear subtitle
fc69e641
AD
316function updateTitle(s) {
317 var tmp = "Tiny Tiny RSS";
21703604 318
5a494a0b 319 if (s != undefined) {
21703604
AD
320 current_subtitle = s;
321 }
322
fc69e641
AD
323 if (global_unread > 0) {
324 tmp = tmp + " (" + global_unread + ")";
325 }
326
5a494a0b 327 if (current_subtitle) {
21703604 328 tmp = tmp + " - " + current_subtitle;
fc69e641 329 }
21703604
AD
330
331 if (active_title_text.length > 0) {
332 tmp = tmp + " > " + active_title_text;
333 }
334
fc69e641
AD
335 document.title = tmp;
336}
337
22a93ad8 338function genericSanityCheck() {
ac43eba1 339
295f9b42
AD
340 if (!xmlhttp) fatalError(1);
341
342 setCookie("ttrss_vf_test", "TEST");
343
344 if (getCookie("ttrss_vf_test") != "TEST") {
345 fatalError(2);
346 }
347
22a93ad8
AD
348 return true;
349}
350
351function init() {
352
7719618b 353 try {
fe2f1970 354
97dcd654
AD
355 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
356
357 if (arguments.callee.done) return;
358 arguments.callee.done = true;
359
7719618b 360 disableContainerChildren("headlinesToolbar", true);
70830c87 361
86b682ce
AD
362 Form.disable("main_toolbar_form");
363
7719618b
AD
364 if (!genericSanityCheck())
365 return;
ac43eba1 366
0ee1d1a0
AD
367 if (getURLParam('debug')) {
368 document.getElementById('debug_output').style.display = 'block';
369 debug('debug mode activated');
370 }
371
7719618b
AD
372 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
373 xmlhttp.onreadystatechange=backend_sanity_check_callback;
374 xmlhttp.send(null);
47179952 375
7719618b
AD
376 } catch (e) {
377 exception_error("init", e);
a8d28f48 378 }
7719618b 379}
86741347 380
1de2b92c
AD
381function resize_feeds_frame() {
382 var f = document.getElementById("feeds-frame");
383 var tf = document.getElementById("mainFooter");
384 var th = document.getElementById("mainHeader");
a6c3f28c
AD
385
386 var footer_height = 0;
387 var header_height = 0;
388
389 if (tf) {
390 footer_height = tf.scrollHeight;
391 }
392
393 if (th) {
394 header_height = th.scrollHeight;
395 }
6b4163cb
AD
396
397 if (f) {
398 f.style.height = document.body.scrollHeight - footer_height -
399 header_height - 50 + "px";
400 }
1de2b92c
AD
401}
402
7719618b 403function init_second_stage() {
295f9b42 404
7719618b 405 try {
2f587484 406
76b4eae1
AD
407 cookie_lifetime = getCookie("ttrss_cltime");
408
409 delCookie("ttrss_vf_test");
7719618b
AD
410
411 updateFeedList(false, false);
412 document.onkeydown = hotkey_handler;
1b0809ae 413
f31673f7
AD
414 var tb = parent.document.forms["main_toolbar_form"];
415
3ac2b520
AD
416 dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
417 dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
c6784aea 418
0d51e25d
AD
419 daemon_enabled = getInitParam("daemon_enabled") == 1;
420 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
5f136c9a 421
1de2b92c
AD
422 // FIXME should be callled after window resize
423
caa53a7c
AD
424 var h = document.getElementById("headlines");
425 var c = document.getElementById("content");
426
5f136c9a 427 if (navigator.userAgent.match("Opera")) {
1de2b92c 428 resize_feeds_frame();
5f136c9a 429 }
0ee1d1a0
AD
430
431 debug("second stage ok");
7719618b 432
7719618b
AD
433 } catch (e) {
434 exception_error("init_second_stage", e);
2f587484 435 }
1cd17194 436}
ac43eba1 437
c09ec856 438function quickMenuChange() {
cbe45fa8 439 var chooser = document.getElementById("quickMenuChooser");
86b682ce 440 var opid = chooser[chooser.selectedIndex].value;
e2f8f7b4 441
c09ec856
AD
442 chooser.selectedIndex = 0;
443 quickMenuGo(opid);
444}
445
446function quickMenuGo(opid) {
bb3423cf 447 try {
c09ec856 448
bb3423cf
AD
449 if (opid == "qmcPrefs") {
450 gotoPreferences();
451 }
452
453 if (opid == "qmcSearch") {
0a6c4846 454 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
6de5d056
AD
455 return;
456 }
bb3423cf
AD
457
458 if (opid == "qmcAddFeed") {
459 displayDlg("quickAddFeed");
460 return;
69668465 461 }
6de5d056 462
bb3423cf
AD
463 if (opid == "qmcRemoveFeed") {
464 var actid = getActiveFeedId();
465
5c365f60 466 if (!actid || activeFeedIsCat()) {
0530ddd8 467 alert("Please select some feed first.");
bb3423cf
AD
468 return;
469 }
64a2875d
AD
470
471 var fn = getFeedName(actid);
bb3423cf 472
64a2875d 473 if (confirm("Unsubscribe from " + fn + "?")) {
bb3423cf
AD
474 qfdDelete(actid);
475 }
476
477 return;
478 }
479
480 if (opid == "qmcUpdateFeeds") {
481 scheduleFeedUpdate(true);
482 return;
483 }
484
485 if (opid == "qmcCatchupAll") {
486 catchupAllFeeds();
487 return;
488 }
489
490 if (opid == "qmcShowOnlyUnread") {
491 toggleDispRead();
492 return;
493 }
494
495 if (opid == "qmcAddFilter") {
496 displayDlg("quickAddFilter", getActiveFeedId());
497 }
498 } catch (e) {
499 exception_error("quickMenuGo", e);
a24f525c 500 }
e2f8f7b4
AD
501}
502
6de5d056
AD
503function qfdDelete(feed_id) {
504
505 notify("Removing feed...");
506
7f123cda
AD
507 if (!xmlhttp_ready(xmlhttp)) {
508 printLockingError();
509 return
510 }
511
15da5cc1
AD
512 _qfd_deleted_feed = feed_id;
513
69668465 514 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
6de5d056
AD
515 xmlhttp.onreadystatechange=dlg_frefresh_callback;
516 xmlhttp.send(null);
6de5d056 517}
033e47e0 518
3745788e 519
21703604
AD
520function updateFeedTitle(t) {
521 active_title_text = t;
522 updateTitle();
523}
524
3745788e 525function toggleDispRead() {
7f123cda 526 try {
3745788e 527
7f123cda
AD
528 if (!xmlhttp_ready(xmlhttp)) {
529 printLockingError();
530 return
531 }
3745788e 532
e8bd0da9 533 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
3745788e 534
7f123cda 535 hide_read_feeds = !hide_read_feeds;
e8bd0da9
AD
536
537 debug("toggle_disp_read => " + hide_read_feeds);
538
539 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
540
7f123cda
AD
541 var query = "backend.php?op=rpc&subop=setpref" +
542 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
3745788e 543
e8bd0da9
AD
544 storeInitParam("hide_read_feeds", hide_read_feeds, true);
545
546 new Ajax.Request(query);
7f123cda
AD
547
548 } catch (e) {
549 exception_error("toggleDispRead", e);
3745788e 550 }
3745788e 551}
295f9b42 552
71ad883b
AD
553function parse_runtime_info(elem) {
554 var param = elem.firstChild;
555
52db9978 556 debug("parse_runtime_info: " + param);
1cb7492d 557
71ad883b
AD
558 while (param) {
559 var k = param.getAttribute("key");
560 var v = param.getAttribute("value");
3ac2b520 561
1cb7492d
AD
562 debug("RI: " + k + " => " + v);
563
71ad883b
AD
564 var w = document.getElementById("noDaemonWarning");
565
566 if (w) {
567 if (k == "daemon_is_running" && v != 1) {
568 w.style.display = "block";
569 } else {
570 w.style.display = "none";
571 }
572 }
573 param = param.nextSibling;
574 }
575}
fce24838
AD
576
577function catchupCurrentFeed() {
578
234e467c 579 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
fce24838 580
234e467c
AD
581 var str = "Mark all articles in " + fn + " as read?";
582
583/* if (active_feed_is_cat) {
584 str = "Mark all articles in this category as read?";
585 } */
586
f6d6e22f 587 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
28de3732 588 return viewCurrentFeed('MarkAllRead')
fce24838
AD
589 }
590}
88040f57 591