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