]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
print additional info on fatalError(3)
[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 _qfd_deleted_feed = 0;
10 var firsttime_update = true;
11
12 /*@cc_on @*/
13 /*@if (@_jscript_version >= 5)
14 // JScript gives us Conditional compilation, we can cope with old IE versions.
15 // and security blocked creation of the objects.
16 try {
17 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
18 } catch (e) {
19 try {
20 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
21 } catch (E) {
22 xmlhttp = false;
23 }
24 }
25 @end @*/
26
27 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
28 xmlhttp = new XMLHttpRequest();
29 }
30
31 function toggleTags() {
32 display_tags = !display_tags;
33
34 var p = document.getElementById("dispSwitchPrompt");
35
36 if (display_tags) {
37 p.innerHTML = "display feeds";
38 } else {
39 p.innerHTML = "display tags";
40 }
41
42 updateFeedList();
43 }
44
45 function dlg_frefresh_callback() {
46 if (xmlhttp.readyState == 4) {
47 notify(xmlhttp.responseText);
48 updateFeedList(false, false);
49 if (_qfd_deleted_feed) {
50 var hframe = document.getElementById("headlines-frame");
51 if (hframe) {
52 hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
53 }
54 }
55 closeDlg();
56 }
57 }
58
59 function dlg_submit_callback() {
60 if (xmlhttp.readyState == 4) {
61 notify(xmlhttp.responseText);
62 closeDlg();
63 }
64 }
65
66 function dlg_display_callback() {
67 if (xmlhttp.readyState == 4) {
68 var dlg = document.getElementById("userDlg");
69 var dlg_s = document.getElementById("userDlgShadow");
70
71 dlg.innerHTML = xmlhttp.responseText;
72 dlg_s.style.display = "block";
73 }
74 }
75
76 function hide_unread_callback() {
77 if (xmlhttp.readyState == 4) {
78
79 try {
80
81 var reply = xmlhttp.responseXML.firstChild.firstChild;
82 var value = reply.getAttribute("value");
83 var hide_read_feeds = (value != "false")
84 var feeds_doc = window.frames["feeds-frame"].document;
85
86 hideOrShowFeeds(feeds_doc, hide_read_feeds);
87
88 if (hide_read_feeds) {
89 setCookie("ttrss_vf_hreadf", 1);
90 } else {
91 setCookie("ttrss_vf_hreadf", 0);
92 }
93
94 } catch (e) {
95 exception_error("hide_unread_callback", e);
96 }
97
98 }
99 }
100
101 function refetch_callback() {
102 if (xmlhttp.readyState == 4) {
103 try {
104
105 if (!xmlhttp.responseXML) {
106 notify("refetch_callback: backend did not return valid XML");
107 return;
108 }
109
110 var reply = xmlhttp.responseXML.firstChild;
111
112 if (!reply) {
113 notify("refetch_callback: backend did not return expected XML object");
114 updateTitle("");
115 return;
116 }
117
118 var error_code = reply.getAttribute("error-code");
119
120 if (error_code && error_code != 0) {
121 return fatalError(error_code);
122 }
123
124 var f_document = window.frames["feeds-frame"].document;
125
126 parse_counters(reply, f_document, window);
127
128 if (!daemon_enabled) {
129 notify("All feeds updated.");
130 updateTitle("");
131 }
132 } catch (e) {
133 exception_error("refetch_callback", e);
134 updateTitle("");
135 }
136 }
137 }
138
139 function backend_sanity_check_callback() {
140
141 if (xmlhttp.readyState == 4) {
142
143 try {
144
145 if (!xmlhttp.responseXML) {
146 fatalError(3, "D001;" + xmlhttp.responseText);
147 return;
148 }
149
150 var reply = xmlhttp.responseXML.firstChild;
151
152 if (!reply) {
153 fatalError(3, "D002;" + xmlhttp.responseText);
154 return;
155 }
156
157 var error_code = reply.getAttribute("error-code");
158
159 if (error_code && error_code != 0) {
160 return fatalError(error_code);
161 }
162
163 init_second_stage();
164
165 } catch (e) {
166 exception_error("backend_sanity_check_callback", e);
167 }
168 }
169 }
170
171 function scheduleFeedUpdate(force) {
172
173 if (!daemon_enabled) {
174 notify("Updating feeds, please wait.");
175 updateTitle("Updating");
176 }
177
178 var query_str = "backend.php?op=rpc&subop=";
179
180 if (force) {
181 query_str = query_str + "forceUpdateAllFeeds";
182 } else {
183 query_str = query_str + "updateAllFeeds";
184 }
185
186 var omode;
187
188 if (firsttime_update) {
189 firsttime_update = false;
190 omode = "T";
191 } else {
192 if (display_tags) {
193 omode = "t";
194 } else {
195 omode = "flc";
196 }
197 }
198
199 query_str = query_str + "&omode=" + omode;
200 query_str = query_str + "&uctr=" + global_unread;
201
202
203 if (xmlhttp_ready(xmlhttp)) {
204 xmlhttp.open("GET", query_str, true);
205 xmlhttp.onreadystatechange=refetch_callback;
206 xmlhttp.send(null);
207 } else {
208 printLockingError();
209 }
210 }
211
212 function updateFeedList(silent, fetch) {
213
214 // if (silent != true) {
215 // notify("Loading feed list...");
216 // }
217
218 var query_str = "backend.php?op=feeds";
219
220 if (display_tags) {
221 query_str = query_str + "&tags=1";
222 }
223
224 if (getActiveFeedId()) {
225 query_str = query_str + "&actid=" + getActiveFeedId();
226 }
227
228 if (fetch) query_str = query_str + "&fetch=yes";
229
230 var feeds_frame = document.getElementById("feeds-frame");
231
232 feeds_frame.src = query_str;
233 }
234
235 function catchupAllFeeds() {
236
237 var query_str = "backend.php?op=feeds&subop=catchupAll";
238
239 notify("Marking all feeds as read...");
240
241 var feeds_frame = document.getElementById("feeds-frame");
242
243 feeds_frame.src = query_str;
244
245 global_unread = 0;
246 updateTitle("");
247
248 }
249
250 function viewCurrentFeed(skip, subop) {
251
252 if (getActiveFeedId()) {
253 viewfeed(getActiveFeedId(), skip, subop);
254 } else {
255 disableContainerChildren("headlinesToolbar", false, document);
256 viewfeed(-1, skip, subop); // FIXME
257 }
258 }
259
260 function viewfeed(feed, skip, subop) {
261 var f = window.frames["feeds-frame"];
262 f.viewfeed(feed, skip, subop);
263 }
264
265 function timeout() {
266 scheduleFeedUpdate(false);
267
268 var refresh_time = getCookie('ttrss_vf_refresh');
269
270 if (!refresh_time) refresh_time = 600;
271
272 setTimeout("timeout()", refresh_time*1000);
273 }
274
275 function resetSearch() {
276 var searchbox = document.getElementById("searchbox")
277
278 if (searchbox.value != "" && getActiveFeedId()) {
279 searchbox.value = "";
280 viewfeed(getActiveFeedId(), 0, "");
281 }
282 }
283
284 function search() {
285 closeDlg();
286 viewCurrentFeed(0, "");
287 }
288
289 function localPiggieFunction(enable) {
290 if (enable) {
291 var query_str = "backend.php?op=feeds&subop=piggie";
292
293 if (xmlhttp_ready(xmlhttp)) {
294
295 xmlhttp.open("GET", query_str, true);
296 xmlhttp.onreadystatechange=feedlist_callback;
297 xmlhttp.send(null);
298 }
299 }
300 }
301
302 function localHotkeyHandler(keycode) {
303
304 if (keycode == 82) { // r
305 return scheduleFeedUpdate(true);
306 }
307
308 if (keycode == 85) { // u
309 if (getActiveFeedId()) {
310 return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
311 }
312 }
313
314 if (keycode == 65) { // a
315 return toggleDispRead();
316 }
317
318 var f_doc = window.frames["feeds-frame"].document;
319 var feedlist = f_doc.getElementById('feedList');
320
321 if (keycode == 74) { // j
322 var feed = getActiveFeedId();
323 var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
324 if (new_feed) viewfeed(new_feed, 0, '');
325 }
326
327 if (keycode == 75) { // k
328 var feed = getActiveFeedId();
329 var new_feed = getRelativeFeedId(feedlist, feed, 'next');
330 if (new_feed) viewfeed(new_feed, 0, '');
331 }
332
333 // notify("KC: " + keycode);
334
335 }
336
337 // if argument is undefined, current subtitle is not updated
338 // use blank string to clear subtitle
339 function updateTitle(s) {
340 var tmp = "Tiny Tiny RSS";
341
342 if (s != undefined) {
343 current_subtitle = s;
344 }
345
346 if (global_unread > 0) {
347 tmp = tmp + " (" + global_unread + ")";
348 }
349
350 if (current_subtitle) {
351 tmp = tmp + " - " + current_subtitle;
352 }
353
354 if (active_title_text.length > 0) {
355 tmp = tmp + " > " + active_title_text;
356 }
357
358 document.title = tmp;
359 }
360
361 function genericSanityCheck() {
362
363 if (!xmlhttp) fatalError(1);
364
365 setCookie("ttrss_vf_test", "TEST");
366
367 if (getCookie("ttrss_vf_test") != "TEST") {
368 fatalError(2);
369 }
370
371 return true;
372 }
373
374 function init() {
375
376 try {
377
378 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
379
380 if (arguments.callee.done) return;
381 arguments.callee.done = true;
382
383 disableContainerChildren("headlinesToolbar", true);
384
385 if (!genericSanityCheck())
386 return;
387
388 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
389 xmlhttp.onreadystatechange=backend_sanity_check_callback;
390 xmlhttp.send(null);
391
392 } catch (e) {
393 exception_error("init", e);
394 }
395 }
396
397 function resize_feeds_frame() {
398 var f = document.getElementById("feeds-frame");
399 var tf = document.getElementById("mainFooter");
400 var th = document.getElementById("mainHeader");
401
402 f.style.height = document.body.scrollHeight - tf.scrollHeight -
403 th.scrollHeight - 50 + "px";
404 }
405
406 function init_second_stage() {
407
408 try {
409
410 setCookie("ttrss_vf_actfeed", "");
411
412 updateFeedList(false, false);
413 document.onkeydown = hotkey_handler;
414
415 var viewbox = document.getElementById("viewbox");
416 var limitbox = document.getElementById("limitbox");
417
418 dropboxSelect(viewbox, getCookie("ttrss_vf_vmode"));
419 dropboxSelect(limitbox, getCookie("ttrss_vf_limit"));
420
421 daemon_enabled = getCookie("ttrss_vf_daemon");
422
423 // FIXME should be callled after window resize
424
425 if (navigator.userAgent.match("Opera")) {
426 resize_feeds_frame();
427
428 // fix headlines frame height for Opera
429 var h = document.getElementById("headlines");
430 var c = document.getElementById("content");
431 var nh = document.body.scrollHeight * 0.25;
432
433 h.style.height = nh + "px";
434 c.style.height = c.scrollHeight - nh + "px";
435
436 }
437
438 } catch (e) {
439 exception_error("init_second_stage", e);
440 }
441 }
442
443 function quickMenuChange() {
444 var chooser = document.getElementById("quickMenuChooser");
445 var opid = chooser[chooser.selectedIndex].id;
446
447 chooser.selectedIndex = 0;
448 quickMenuGo(opid);
449 }
450
451 function quickMenuGo(opid) {
452
453
454 if (opid == "qmcPrefs") {
455 gotoPreferences();
456 }
457
458 if (opid == "qmcSearch") {
459 displayDlg("search", getActiveFeedId());
460 return;
461 }
462
463 if (opid == "qmcAddFeed") {
464 displayDlg("quickAddFeed");
465 return;
466 }
467
468 if (opid == "qmcRemoveFeed") {
469 var actid = getActiveFeedId();
470
471 if (!actid) {
472 notify("Please select some feed first.");
473 return;
474 }
475
476 if (confirm("Remove current feed?")) {
477 qfdDelete(actid);
478 }
479
480 return;
481 }
482
483 if (opid == "qmcUpdateFeeds") {
484 scheduleFeedUpdate(true);
485 return;
486 }
487
488 if (opid == "qmcCatchupAll") {
489 catchupAllFeeds();
490 return;
491 }
492
493 if (opid == "qmcShowOnlyUnread") {
494 toggleDispRead();
495 return;
496 }
497
498 if (opid == "qmcAddFilter") {
499 displayDlg("quickAddFilter", getActiveFeedId());
500 }
501
502 }
503
504 function qafAdd() {
505
506 if (!xmlhttp_ready(xmlhttp)) {
507 printLockingError();
508 return
509 }
510
511 var link = document.getElementById("qafInput");
512
513 if (link.value.length == 0) {
514 notify("Missing feed URL.");
515 } else {
516 notify("Adding feed...");
517
518 var cat = document.getElementById("qafCat");
519 var cat_id = "";
520
521 if (cat) {
522 cat_id = cat[cat.selectedIndex].id;
523 } else {
524 cat_id = 0;
525 }
526
527 var feeds_doc = window.frames["feeds-frame"].document;
528
529 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
530
531 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=add&link=" +
532 param_escape(link.value) + "&cid=" + param_escape(cat_id), true);
533 xmlhttp.onreadystatechange=dlg_frefresh_callback;
534 xmlhttp.send(null);
535
536 link.value = "";
537
538 }
539 }
540
541 function qaddFilter() {
542
543 if (!xmlhttp_ready(xmlhttp)) {
544 printLockingError();
545 return
546 }
547
548 var regexp = document.getElementById("fadd_regexp");
549 var match = document.getElementById("fadd_match");
550 var feed = document.getElementById("fadd_feed");
551 var action = document.getElementById("fadd_action");
552
553 if (regexp.value.length == 0) {
554 notify("Missing filter expression.");
555 } else {
556 notify("Adding filter...");
557
558 var v_match = match[match.selectedIndex].text;
559 var feed_id = feed[feed.selectedIndex].id;
560 var action_id = action[action.selectedIndex].id;
561
562 xmlhttp.open("GET", "backend.php?op=pref-filters&quiet=1&subop=add&regexp=" +
563 param_escape(regexp.value) + "&match=" + v_match +
564 "&fid=" + param_escape(feed_id) + "&aid=" + param_escape(action_id), true);
565
566 xmlhttp.onreadystatechange=dlg_submit_callback;
567 xmlhttp.send(null);
568
569 regexp.value = "";
570 }
571
572 }
573
574
575 function displayDlg(id, param) {
576
577 if (!xmlhttp_ready(xmlhttp)) {
578 printLockingError();
579 return
580 }
581
582 notify("");
583
584 xmlhttp.open("GET", "backend.php?op=dlg&id=" +
585 param_escape(id) + "&param=" + param_escape(param), true);
586 xmlhttp.onreadystatechange=dlg_display_callback;
587 xmlhttp.send(null);
588
589 disableHotkeys();
590 }
591
592 function closeDlg() {
593 var dlg = document.getElementById("userDlgShadow");
594 dlg.style.display = "none";
595 enableHotkeys();
596 }
597
598 function qfdDelete(feed_id) {
599
600 notify("Removing feed...");
601
602 if (!xmlhttp_ready(xmlhttp)) {
603 printLockingError();
604 return
605 }
606
607 // var feeds_doc = window.frames["feeds-frame"].document;
608 // feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
609
610 _qfd_deleted_feed = feed_id;
611
612 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
613 xmlhttp.onreadystatechange=dlg_frefresh_callback;
614 xmlhttp.send(null);
615 }
616
617
618 function allFeedsMenuChange() {
619 var chooser = document.getElementById("allFeedsChooser");
620
621 var opname = chooser[chooser.selectedIndex].text;
622
623 chooser.selectedIndex = 0;
624
625 if (opname == "Update") {
626 scheduleFeedUpdate(true);
627 return;
628 }
629
630 if (opname == "Mark as read") {
631 catchupAllFeeds();
632 return;
633 }
634
635 if (opname == "Show only unread") {
636 toggleDispRead();
637 return;
638 }
639
640 }
641
642 function updateFeedTitle(t) {
643 active_title_text = t;
644 updateTitle();
645 }
646
647 function toggleDispRead() {
648 try {
649
650 if (!xmlhttp_ready(xmlhttp)) {
651 printLockingError();
652 return
653 }
654
655 var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
656
657 hide_read_feeds = !hide_read_feeds;
658
659 var query = "backend.php?op=rpc&subop=setpref" +
660 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
661
662 xmlhttp.open("GET", query);
663 xmlhttp.onreadystatechange=hide_unread_callback;
664 xmlhttp.send(null);
665
666 } catch (e) {
667 exception_error("toggleDispRead", e);
668 }
669 }
670