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