]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
tweak category counter display, new styles: .catCtrNoUnread, .catCtrHasUnread
[tt-rss.git] / tt-rss.js
1 var xmlhttp = false;
2 var total_unread = 0;
3 var first_run = true;
4 var display_tags = false;
5 var global_unread = -1;
6 var active_title_text = "";
7 var current_subtitle = "";
8 var daemon_enabled = false;
9 var daemon_refresh_only = false;
10 var _qfd_deleted_feed = 0;
11 var firsttime_update = true;
12 var last_refetch = 0;
13 var cookie_lifetime = 0;
14 var active_feed_id = 0;
15 var active_feed_is_cat = false;
16 var number_of_feeds = 0;
17
18 var xmlhttp = Ajax.getTransport();
19 var xmlhttp_ctr = Ajax.getTransport();
20
21 var init_params = new Object();
22
23 var op_history = new Array();
24
25 function toggleTags() {
26 display_tags = !display_tags;
27
28 var p = document.getElementById("dispSwitchPrompt");
29
30 if (display_tags) {
31 p.innerHTML = "display feeds";
32 } else {
33 p.innerHTML = "display tags";
34 }
35
36 notify("Loading, please wait...");
37
38 updateFeedList();
39 }
40
41 function dlg_frefresh_callback() {
42 if (xmlhttp.readyState == 4) {
43 // notify(xmlhttp.responseText);
44
45 if (getActiveFeedId() == _qfd_deleted_feed) {
46 var h = document.getElementById("headlines-frame");
47 if (h) {
48 h.innerHTML = "<div class='whiteBox'>No feed selected.</div>";
49 }
50 }
51
52 setTimeout('updateFeedList(false, false)', 50);
53 closeInfoBox();
54 }
55 }
56
57 function refetch_callback() {
58 if (xmlhttp_ctr.readyState == 4) {
59 try {
60
61 var date = new Date();
62
63 last_refetch = date.getTime() / 1000;
64
65 parse_counters_reply(xmlhttp_ctr, true);
66
67 debug("refetch_callback: done");
68
69 if (!daemon_enabled && !daemon_refresh_only) {
70 notify("All feeds updated.");
71 updateTitle("");
72 } else {
73 notify("");
74 }
75 } catch (e) {
76 exception_error("refetch_callback", e);
77 updateTitle("");
78 }
79 }
80 }
81
82 function backend_sanity_check_callback() {
83
84 if (xmlhttp.readyState == 4) {
85
86 try {
87
88 if (!xmlhttp.responseXML) {
89 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
90 return;
91 }
92
93 var reply = xmlhttp.responseXML.firstChild.firstChild;
94
95 if (!reply) {
96 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
97 return;
98 }
99
100 var error_code = reply.getAttribute("error-code");
101
102 if (error_code && error_code != 0) {
103 return fatalError(error_code, reply.getAttribute("error-msg"));
104 }
105
106 debug("sanity check ok");
107
108 var params = reply.nextSibling;
109
110 if (params) {
111 debug('reading init-params...');
112 var param = params.firstChild;
113
114 while (param) {
115 var k = param.getAttribute("key");
116 var v = param.getAttribute("value");
117 debug(k + " => " + v);
118 init_params[k] = v;
119 param = param.nextSibling;
120 }
121 }
122
123 init_second_stage();
124
125 } catch (e) {
126 exception_error("backend_sanity_check_callback", e);
127 }
128 }
129 }
130
131 function scheduleFeedUpdate(force) {
132
133 if (!daemon_enabled && !daemon_refresh_only) {
134 notify("Updating feeds, please wait.", true);
135 updateTitle("Updating");
136 }
137
138 var query_str = "backend.php?op=rpc&subop=";
139
140 if (force) {
141 query_str = query_str + "forceUpdateAllFeeds";
142 } else {
143 query_str = query_str + "updateAllFeeds";
144 }
145
146 var omode;
147
148 if (firsttime_update && !navigator.userAgent.match("Opera")) {
149 firsttime_update = false;
150 omode = "T";
151 } else {
152 if (display_tags) {
153 omode = "t";
154 } else {
155 omode = "flc";
156 }
157 }
158
159 query_str = query_str + "&omode=" + omode;
160 query_str = query_str + "&uctr=" + global_unread;
161
162 debug("in scheduleFeedUpdate");
163
164 var date = new Date();
165
166 if (!xmlhttp_ready(xmlhttp_ctr) && last_refetch < date.getTime() / 1000 - 60) {
167 debug("<b>xmlhttp seems to be stuck, aborting</b>");
168 xmlhttp_ctr.abort();
169 }
170
171 debug("REFETCH query: " + query_str);
172
173 if (xmlhttp_ready(xmlhttp_ctr)) {
174 xmlhttp_ctr.open("GET", query_str, true);
175 xmlhttp_ctr.onreadystatechange=refetch_callback;
176 xmlhttp_ctr.send(null);
177 } else {
178 debug("xmlhttp_ctr busy");
179 //printLockingError();
180 }
181 }
182
183 function updateFeedList(silent, fetch) {
184
185 // if (silent != true) {
186 // notify("Loading feed list...");
187 // }
188
189 var query_str = "backend.php?op=feeds";
190
191 if (display_tags) {
192 query_str = query_str + "&tags=1";
193 }
194
195 if (getActiveFeedId() && !activeFeedIsCat()) {
196 query_str = query_str + "&actid=" + getActiveFeedId();
197 }
198
199 if (navigator.userAgent.match("Opera")) {
200 var date = new Date();
201 var timestamp = Math.round(date.getTime() / 1000);
202 query_str = query_str + "&ts=" + timestamp
203 }
204
205 if (fetch) query_str = query_str + "&fetch=yes";
206
207 // var feeds_frame = document.getElementById("feeds-frame");
208 // feeds_frame.src = query_str;
209
210 debug("updateFeedList Q=" + query_str);
211
212 if (xmlhttp_ready(xmlhttp)) {
213 xmlhttp.open("GET", query_str, true);
214 xmlhttp.onreadystatechange=feedlist_callback;
215 xmlhttp.send(null);
216 } else {
217 debug("xmlhttp busy");
218 //printLockingError();
219 }
220
221 }
222
223 function catchupAllFeeds() {
224
225 var query_str = "backend.php?op=feeds&subop=catchupAll";
226
227 notify("Marking all feeds as read...");
228
229 debug("catchupAllFeeds Q=" + query_str);
230
231 if (xmlhttp_ready(xmlhttp)) {
232 xmlhttp.open("GET", query_str, true);
233 xmlhttp.onreadystatechange=feedlist_callback;
234 xmlhttp.send(null);
235 } else {
236 debug("xmlhttp busy");
237 //printLockingError();
238 }
239
240 global_unread = 0;
241 updateTitle("");
242
243 }
244
245 function viewCurrentFeed(subop) {
246
247 // if (getActiveFeedId()) {
248 if (getActiveFeedId() != undefined) {
249 viewfeed(getActiveFeedId(), subop);
250 } else {
251 disableContainerChildren("headlinesToolbar", false, document);
252 // viewfeed(-1, subop); // FIXME
253 }
254 return false; // block unneeded form submits
255 }
256
257 function viewfeed(feed, subop) {
258 var f = window.frames["feeds-frame"];
259 f.viewfeed(feed, subop);
260 }
261
262 function timeout() {
263 scheduleFeedUpdate(false);
264
265 var refresh_time = getInitParam("feeds_frame_refresh");
266
267 if (!refresh_time) refresh_time = 600;
268
269 setTimeout("timeout()", refresh_time*1000);
270 }
271
272 function resetSearch() {
273 var searchbox = document.getElementById("searchbox")
274
275 if (searchbox.value != "" && getActiveFeedId()) {
276 searchbox.value = "";
277 viewfeed(getActiveFeedId(), "");
278 }
279 }
280
281 function searchCancel() {
282 closeInfoBox(true);
283 }
284
285 function search() {
286 closeInfoBox();
287 viewCurrentFeed(0, "");
288 }
289
290 function localPiggieFunction(enable) {
291 if (enable) {
292 var query_str = "backend.php?op=feeds&subop=piggie";
293
294 if (xmlhttp_ready(xmlhttp)) {
295
296 xmlhttp.open("GET", query_str, true);
297 xmlhttp.onreadystatechange=feedlist_callback;
298 xmlhttp.send(null);
299 }
300 }
301 }
302
303 // if argument is undefined, current subtitle is not updated
304 // use blank string to clear subtitle
305 function updateTitle(s) {
306 var tmp = "Tiny Tiny RSS";
307
308 if (s != undefined) {
309 current_subtitle = s;
310 }
311
312 if (global_unread > 0) {
313 tmp = tmp + " (" + global_unread + ")";
314 }
315
316 if (current_subtitle) {
317 tmp = tmp + " - " + current_subtitle;
318 }
319
320 if (active_title_text.length > 0) {
321 tmp = tmp + " > " + active_title_text;
322 }
323
324 document.title = tmp;
325 }
326
327 function genericSanityCheck() {
328
329 if (!xmlhttp) fatalError(1);
330
331 setCookie("ttrss_vf_test", "TEST");
332
333 if (getCookie("ttrss_vf_test") != "TEST") {
334 fatalError(2);
335 }
336
337 return true;
338 }
339
340 function init() {
341
342 try {
343
344 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
345
346 if (arguments.callee.done) return;
347 arguments.callee.done = true;
348
349 disableContainerChildren("headlinesToolbar", true);
350
351 Form.disable("main_toolbar_form");
352
353 if (!genericSanityCheck())
354 return;
355
356 if (getURLParam('debug')) {
357 document.getElementById('debug_output').style.display = 'block';
358 debug('debug mode activated');
359 }
360
361 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
362 xmlhttp.onreadystatechange=backend_sanity_check_callback;
363 xmlhttp.send(null);
364
365 } catch (e) {
366 exception_error("init", e);
367 }
368 }
369
370 function init_second_stage() {
371
372 try {
373
374 cookie_lifetime = getCookie("ttrss_cltime");
375
376 delCookie("ttrss_vf_test");
377
378 document.onkeydown = hotkey_handler;
379
380 var tb = parent.document.forms["main_toolbar_form"];
381
382 // dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
383 // dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
384
385 daemon_enabled = getInitParam("daemon_enabled") == 1;
386 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
387
388 setTimeout('updateFeedList(false, false)', 50);
389
390 debug("second stage ok");
391
392 } catch (e) {
393 exception_error("init_second_stage", e);
394 }
395 }
396
397 function quickMenuChange() {
398 var chooser = document.getElementById("quickMenuChooser");
399 var opid = chooser[chooser.selectedIndex].value;
400
401 chooser.selectedIndex = 0;
402 quickMenuGo(opid);
403 }
404
405 function quickMenuGo(opid) {
406 try {
407
408 if (opid == "qmcPrefs") {
409 gotoPreferences();
410 }
411
412 if (opid == "qmcSearch") {
413 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
414 return;
415 }
416
417 if (opid == "qmcAddFeed") {
418 displayDlg("quickAddFeed");
419 return;
420 }
421
422 if (opid == "qmcEditFeed") {
423 editFeedDlg(getActiveFeedId());
424 }
425
426 if (opid == "qmcRemoveFeed") {
427 var actid = getActiveFeedId();
428
429 if (!actid || activeFeedIsCat()) {
430 alert("Please select some feed first.");
431 return;
432 }
433
434 var fn = getFeedName(actid);
435
436 if (confirm("Unsubscribe from " + fn + "?")) {
437 qfdDelete(actid);
438 }
439
440 return;
441 }
442
443 if (opid == "qmcUpdateFeeds") {
444 scheduleFeedUpdate(true);
445 return;
446 }
447
448 if (opid == "qmcCatchupAll") {
449 catchupAllFeeds();
450 return;
451 }
452
453 if (opid == "qmcShowOnlyUnread") {
454 toggleDispRead();
455 return;
456 }
457
458 if (opid == "qmcAddFilter") {
459 displayDlg("quickAddFilter", getActiveFeedId());
460 }
461 } catch (e) {
462 exception_error("quickMenuGo", e);
463 }
464 }
465
466 function qfdDelete(feed_id) {
467
468 notify("Removing feed...");
469
470 if (!xmlhttp_ready(xmlhttp)) {
471 printLockingError();
472 return
473 }
474
475 _qfd_deleted_feed = feed_id;
476
477 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
478 xmlhttp.onreadystatechange=dlg_frefresh_callback;
479 xmlhttp.send(null);
480
481 return false;
482 }
483
484
485 function updateFeedTitle(t) {
486 active_title_text = t;
487 updateTitle();
488 }
489
490 function toggleDispRead() {
491 try {
492
493 if (!xmlhttp_ready(xmlhttp)) {
494 printLockingError();
495 return
496 }
497
498 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
499
500 hide_read_feeds = !hide_read_feeds;
501
502 debug("toggle_disp_read => " + hide_read_feeds);
503
504 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
505
506 var query = "backend.php?op=rpc&subop=setpref" +
507 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
508
509 storeInitParam("hide_read_feeds", hide_read_feeds, true);
510
511 new Ajax.Request(query);
512
513 } catch (e) {
514 exception_error("toggleDispRead", e);
515 }
516 }
517
518 function parse_runtime_info(elem) {
519 if (!elem) {
520 debug("parse_runtime_info: elem is null, aborting");
521 return;
522 }
523
524 var param = elem.firstChild;
525
526 debug("parse_runtime_info: " + param);
527
528 while (param) {
529 var k = param.getAttribute("key");
530 var v = param.getAttribute("value");
531
532 debug("RI: " + k + " => " + v);
533
534 var w = document.getElementById("noDaemonWarning");
535
536 if (w) {
537 if (k == "daemon_is_running" && v != 1) {
538 w.style.display = "block";
539 } else {
540 w.style.display = "none";
541 }
542 }
543 param = param.nextSibling;
544 }
545 }
546
547 function catchupCurrentFeed() {
548
549 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
550
551 var str = "Mark all articles in " + fn + " as read?";
552
553 /* if (active_feed_is_cat) {
554 str = "Mark all articles in this category as read?";
555 } */
556
557 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
558 return viewCurrentFeed('MarkAllRead')
559 }
560 }
561
562 function userSwitch() {
563 var chooser = document.getElementById("userSwitch");
564 var user = chooser[chooser.selectedIndex].value;
565 window.location = "tt-rss.php?swu=" + user;
566 }
567
568 function editFeedDlg(feed) {
569
570 disableHotkeys();
571
572 if (!feed) {
573 alert("Please select some feed first.");
574 return;
575 }
576
577 if (xmlhttp_ready(xmlhttp)) {
578 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editfeed&id=" +
579 param_escape(feed), true);
580 xmlhttp.onreadystatechange=infobox_callback;
581 xmlhttp.send(null);
582 } else {
583 printLockingError();
584 }
585 }
586
587 /* this functions duplicate those of prefs.js feed editor, with
588 some differences because there is no feedlist */
589
590 function feedEditCancel() {
591 closeInfoBox();
592 return false;
593 }
594
595 function feedEditSave() {
596
597 try {
598
599 if (!xmlhttp_ready(xmlhttp)) {
600 printLockingError();
601 return
602 }
603
604 // FIXME: add parameter validation
605
606 var query = Form.serialize("edit_feed_form");
607
608 notify("Saving feed...");
609
610 xmlhttp.open("POST", "backend.php", true);
611 xmlhttp.onreadystatechange=dlg_frefresh_callback;
612 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
613 xmlhttp.send(query);
614
615 closeInfoBox();
616
617 return false;
618
619 } catch (e) {
620 exception_error("feedEditSave (main)", e);
621 }
622 }
623
624 function localHotkeyHandler(e) {
625
626 var keycode;
627
628 if (window.event) {
629 keycode = window.event.keyCode;
630 } else if (e) {
631 keycode = e.which;
632 }
633
634 var shift_key = false;
635
636 try {
637 shift_key = e.shiftKey;
638 } catch (e) { }
639
640 if (keycode == 66 && shift_key) { // shift-B
641
642 var op = history_pop();
643
644 if (op) {
645 var op_s = op.split(":");
646
647 var i;
648 for (i = 0; i < op_s.length; i++) {
649 if (op_s[i] == 'undefined') {
650 op_s[i] = undefined;
651 }
652
653 if (op_s[i] == 'false') {
654 op_s[i] = false;
655 }
656
657 if (op_s[i] == 'true') {
658 op_s[i] = true;
659 }
660
661 }
662
663 debug("history split: " + op_s);
664
665 if (op_s[0] == "ARTICLE") {
666 debug("history: reverting to article " + op_s[1] + "/" + op_s[2]);
667 view(op_s[1], op_s[2], true);
668 }
669
670 if (op_s[0] == "FEED") {
671 viewfeed(op_s[1], op_s[2], op_s[3], op_s[4], true);
672 }
673
674 } else {
675 notify("No operation to undo");
676 }
677
678 return false;
679
680 }
681
682 debug("LKP=" + keycode);
683 }
684
685 function history_push(op) {
686 debug("history_push: " + op);
687 op_history.push(op);
688
689 while (op_history.length > 30) {
690 op_history.shift();
691 }
692 }
693
694 function history_pop() {
695 var op = op_history.pop();
696 debug("history_pop: " + op);
697 return op;
698 }
699
700 function history_clear() {
701 debug("history_clear");
702 op_history.clear();
703 }