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