]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
tag editor
[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 = "t";
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 var query_str = "backend.php?op=feeds";
194
195 if (display_tags) {
196 query_str = query_str + "&tags=1";
197 }
198
199 if (getActiveFeedId() && !activeFeedIsCat()) {
200 query_str = query_str + "&actid=" + getActiveFeedId();
201 }
202
203 if (navigator.userAgent.match("Opera")) {
204 var date = new Date();
205 var timestamp = Math.round(date.getTime() / 1000);
206 query_str = query_str + "&ts=" + timestamp
207 }
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 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
366 xmlhttp.onreadystatechange=backend_sanity_check_callback;
367 xmlhttp.send(null);
368
369 } catch (e) {
370 exception_error("init", e);
371 }
372 }
373
374 function init_second_stage() {
375
376 try {
377
378 cookie_lifetime = getCookie("ttrss_cltime");
379
380 delCookie("ttrss_vf_test");
381
382 document.onkeydown = hotkey_handler;
383
384 var tb = parent.document.forms["main_toolbar_form"];
385
386 // dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
387 // dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
388
389 daemon_enabled = getInitParam("daemon_enabled") == 1;
390 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
391
392 setTimeout('updateFeedList(false, false)', 50);
393
394 debug("second stage ok");
395
396 } catch (e) {
397 exception_error("init_second_stage", e);
398 }
399 }
400
401 function quickMenuChange() {
402 var chooser = document.getElementById("quickMenuChooser");
403 var opid = chooser[chooser.selectedIndex].value;
404
405 chooser.selectedIndex = 0;
406 quickMenuGo(opid);
407 }
408
409 function quickMenuGo(opid) {
410 try {
411
412 if (opid == "qmcPrefs") {
413 gotoPreferences();
414 }
415
416 if (opid == "qmcSearch") {
417 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
418 return;
419 }
420
421 if (opid == "qmcAddFeed") {
422 displayDlg("quickAddFeed");
423 return;
424 }
425
426 if (opid == "qmcEditFeed") {
427 editFeedDlg(getActiveFeedId());
428 }
429
430 if (opid == "qmcRemoveFeed") {
431 var actid = getActiveFeedId();
432
433 if (!actid || activeFeedIsCat()) {
434 alert("Please select some feed first.");
435 return;
436 }
437
438 var fn = getFeedName(actid);
439
440 if (confirm("Unsubscribe from " + fn + "?")) {
441 qfdDelete(actid);
442 }
443
444 return;
445 }
446
447 if (opid == "qmcUpdateFeeds") {
448 scheduleFeedUpdate(true);
449 return;
450 }
451
452 if (opid == "qmcCatchupAll") {
453 catchupAllFeeds();
454 return;
455 }
456
457 if (opid == "qmcShowOnlyUnread") {
458 toggleDispRead();
459 return;
460 }
461
462 if (opid == "qmcAddFilter") {
463 displayDlg("quickAddFilter", getActiveFeedId());
464 }
465 } catch (e) {
466 exception_error("quickMenuGo", e);
467 }
468 }
469
470 function qfdDelete(feed_id) {
471
472 notify("Removing feed...");
473
474 if (!xmlhttp_ready(xmlhttp)) {
475 printLockingError();
476 return
477 }
478
479 _qfd_deleted_feed = feed_id;
480
481 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
482 xmlhttp.onreadystatechange=dlg_frefresh_callback;
483 xmlhttp.send(null);
484
485 return false;
486 }
487
488
489 function updateFeedTitle(t) {
490 active_title_text = t;
491 updateTitle();
492 }
493
494 function toggleDispRead() {
495 try {
496
497 if (!xmlhttp_ready(xmlhttp)) {
498 printLockingError();
499 return
500 }
501
502 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
503
504 hide_read_feeds = !hide_read_feeds;
505
506 debug("toggle_disp_read => " + hide_read_feeds);
507
508 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
509
510 var query = "backend.php?op=rpc&subop=setpref" +
511 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
512
513 storeInitParam("hide_read_feeds", hide_read_feeds, true);
514
515 new Ajax.Request(query);
516
517 } catch (e) {
518 exception_error("toggleDispRead", e);
519 }
520 }
521
522 function parse_runtime_info(elem) {
523 if (!elem) {
524 debug("parse_runtime_info: elem is null, aborting");
525 return;
526 }
527
528 var param = elem.firstChild;
529
530 debug("parse_runtime_info: " + param);
531
532 while (param) {
533 var k = param.getAttribute("key");
534 var v = param.getAttribute("value");
535
536 debug("RI: " + k + " => " + v);
537
538 var w = document.getElementById("noDaemonWarning");
539
540 if (w) {
541 if (k == "daemon_is_running" && v != 1) {
542 w.style.display = "block";
543 } else {
544 w.style.display = "none";
545 }
546 }
547 param = param.nextSibling;
548 }
549 }
550
551 function catchupCurrentFeed() {
552
553 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
554
555 var str = "Mark all articles in " + fn + " as read?";
556
557 /* if (active_feed_is_cat) {
558 str = "Mark all articles in this category as read?";
559 } */
560
561 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
562 return viewCurrentFeed('MarkAllRead')
563 }
564 }
565
566 function userSwitch() {
567 var chooser = document.getElementById("userSwitch");
568 var user = chooser[chooser.selectedIndex].value;
569 window.location = "tt-rss.php?swu=" + user;
570 }
571
572 function editFeedDlg(feed) {
573
574 disableHotkeys();
575
576 if (!feed) {
577 alert("Please select some feed first.");
578 return;
579 }
580
581 if (xmlhttp_ready(xmlhttp)) {
582 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editfeed&id=" +
583 param_escape(feed), true);
584 xmlhttp.onreadystatechange=infobox_callback;
585 xmlhttp.send(null);
586 } else {
587 printLockingError();
588 }
589 }
590
591 /* this functions duplicate those of prefs.js feed editor, with
592 some differences because there is no feedlist */
593
594 function feedEditCancel() {
595 closeInfoBox();
596 return false;
597 }
598
599 function feedEditSave() {
600
601 try {
602
603 if (!xmlhttp_ready(xmlhttp)) {
604 printLockingError();
605 return
606 }
607
608 // FIXME: add parameter validation
609
610 var query = Form.serialize("edit_feed_form");
611
612 notify("Saving feed...");
613
614 xmlhttp.open("POST", "backend.php", true);
615 xmlhttp.onreadystatechange=dlg_frefresh_callback;
616 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
617 xmlhttp.send(query);
618
619 closeInfoBox();
620
621 return false;
622
623 } catch (e) {
624 exception_error("feedEditSave (main)", e);
625 }
626 }
627
628 function localHotkeyHandler(e) {
629
630 var keycode;
631
632 if (window.event) {
633 keycode = window.event.keyCode;
634 } else if (e) {
635 keycode = e.which;
636 }
637
638 var shift_key = false;
639
640 try {
641 shift_key = e.shiftKey;
642 } catch (e) { }
643
644 if (keycode == 66 && shift_key) { // shift-B
645
646 var op = history_pop();
647
648 if (op) {
649 var op_s = op.split(":");
650
651 var i;
652 for (i = 0; i < op_s.length; i++) {
653 if (op_s[i] == 'undefined') {
654 op_s[i] = undefined;
655 }
656
657 if (op_s[i] == 'false') {
658 op_s[i] = false;
659 }
660
661 if (op_s[i] == 'true') {
662 op_s[i] = true;
663 }
664
665 }
666
667 debug("history split: " + op_s);
668
669 if (op_s[0] == "ARTICLE") {
670 debug("history: reverting to article " + op_s[1] + "/" + op_s[2]);
671 view(op_s[1], op_s[2], true);
672 }
673
674 if (op_s[0] == "FEED") {
675 viewfeed(op_s[1], op_s[2], op_s[3], op_s[4], true);
676 }
677
678 } else {
679 notify("No operation to undo");
680 }
681
682 return false;
683
684 }
685
686 debug("LKP=" + keycode);
687 }
688
689 function history_push(op) {
690 debug("history_push: " + op);
691 op_history.push(op);
692
693 while (op_history.length > 30) {
694 op_history.shift();
695 }
696 }
697
698 function history_pop() {
699 var op = op_history.pop();
700 debug("history_pop: " + op);
701 return op;
702 }
703
704 function history_clear() {
705 debug("history_clear");
706 op_history.clear();
707 }