]> git.wh0rd.org - tt-rss.git/blob - functions.js
hideOrShowFeedList: fix
[tt-rss.git] / functions.js
1 var hotkeys_enabled = true;
2 var debug_mode_enabled = false;
3 var xmlhttp_rpc = Ajax.getTransport();
4
5 function browser_has_opacity() {
6 return navigator.userAgent.match("Gecko") != null ||
7 navigator.userAgent.match("Opera") != null;
8 }
9
10 function is_msie() {
11 return navigator.userAgent.match("MSIE");
12 }
13
14 function is_opera() {
15 return navigator.userAgent.match("Opera");
16 }
17
18 function is_khtml() {
19 return navigator.userAgent.match("KHTML");
20 }
21
22 function is_safari() {
23 return navigator.userAgent.match("Safari");
24 }
25
26 function exception_error(location, e, silent) {
27 var msg;
28
29 if (e.fileName) {
30 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
31
32 msg = "Exception: " + e.name + ", " + e.message +
33 "\nFunction: " + location + "()" +
34 "\nLocation: " + base_fname + ":" + e.lineNumber;
35
36 } else {
37 msg = "Exception: " + e + "\nFunction: " + location + "()";
38 }
39
40 debug("<b>EXCEPTION: " + msg + "</b>");
41
42 if (!silent) {
43 alert(msg);
44 }
45 }
46
47 function disableHotkeys() {
48 hotkeys_enabled = false;
49 }
50
51 function enableHotkeys() {
52 hotkeys_enabled = true;
53 }
54
55 function xmlhttp_ready(obj) {
56 return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
57 }
58
59 function logout_callback() {
60 var container = document.getElementById('notify');
61 if (xmlhttp.readyState == 4) {
62 try {
63 var date = new Date();
64 var timestamp = Math.round(date.getTime() / 1000);
65 window.location.href = "tt-rss.php";
66 } catch (e) {
67 exception_error("logout_callback", e);
68 }
69 }
70 }
71
72 function notify_callback() {
73 var container = document.getElementById('notify');
74 if (xmlhttp.readyState == 4) {
75 container.innerHTML=xmlhttp.responseText;
76 }
77 }
78
79 function rpc_notify_callback() {
80 var container = document.getElementById('notify');
81 if (xmlhttp_rpc.readyState == 4) {
82 container.innerHTML=xmlhttp_rpc.responseText;
83 }
84 }
85
86 function param_escape(arg) {
87 if (typeof encodeURIComponent != 'undefined')
88 return encodeURIComponent(arg);
89 else
90 return escape(arg);
91 }
92
93 function param_unescape(arg) {
94 if (typeof decodeURIComponent != 'undefined')
95 return decodeURIComponent(arg);
96 else
97 return unescape(arg);
98 }
99
100 function delay(gap) {
101 var then,now;
102 then=new Date().getTime();
103 now=then;
104 while((now-then)<gap) {
105 now=new Date().getTime();
106 }
107 }
108
109 var notify_hide_timerid = false;
110
111 function hide_notify() {
112 var n = document.getElementById("notify");
113 if (n) {
114 n.style.display = "none";
115 }
116 }
117
118 function notify_real(msg, no_hide, n_type) {
119
120 var n = document.getElementById("notify");
121 var nb = document.getElementById("notify_body");
122
123 if (!n || !nb) return;
124
125 if (notify_hide_timerid) {
126 window.clearTimeout(notify_hide_timerid);
127 }
128
129 if (msg == "") {
130 if (n.style.display == "block") {
131 notify_hide_timerid = window.setTimeout("hide_notify()", 0);
132 }
133 return;
134 } else {
135 n.style.display = "block";
136 }
137
138 /* types:
139
140 1 - generic
141 2 - progress
142 3 - error
143 4 - info
144
145 */
146
147 if (typeof __ != 'undefined') {
148 msg = __(msg);
149 }
150
151 if (n_type == 1) {
152 n.className = "notify";
153 } else if (n_type == 2) {
154 n.className = "notifyProgress";
155 msg = "<img src='images/indicator_white.gif'> " + msg;
156 } else if (n_type == 3) {
157 n.className = "notifyError";
158 msg = "<img src='images/sign_excl.png'> " + msg;
159 } else if (n_type == 4) {
160 n.className = "notifyInfo";
161 msg = "<img src='images/sign_info.png'> " + msg;
162 }
163
164 // msg = "<img src='images/live_com_loading.gif'> " + msg;
165
166 nb.innerHTML = msg;
167
168 if (!no_hide) {
169 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
170 }
171 }
172
173 function notify(msg, no_hide) {
174 notify_real(msg, no_hide, 1);
175 }
176
177 function notify_progress(msg, no_hide) {
178 notify_real(msg, no_hide, 2);
179 }
180
181 function notify_error(msg, no_hide) {
182 notify_real(msg, no_hide, 3);
183
184 }
185
186 function notify_info(msg, no_hide) {
187 notify_real(msg, no_hide, 4);
188 }
189
190 function printLockingError() {
191 notify_info("Please wait until operation finishes.");
192 }
193
194 function hotkey_handler(e) {
195
196 try {
197
198 var keycode;
199 var shift_key = false;
200
201 try {
202 shift_key = e.shiftKey;
203 } catch (e) {
204
205 }
206
207 if (!hotkeys_enabled) return;
208
209 if (window.event) {
210 keycode = window.event.keyCode;
211 } else if (e) {
212 keycode = e.which;
213 }
214
215 if (keycode == 82) { // r
216 return scheduleFeedUpdate(true);
217 }
218
219 if (keycode == 83) { // s
220 return displayDlg("search", getActiveFeedId());
221 }
222
223 if (keycode == 85) { // u
224 if (getActiveFeedId()) {
225 return viewfeed(getActiveFeedId(), "ForceUpdate");
226 }
227 }
228
229 if (keycode == 65) { // a
230 return toggleDispRead();
231 }
232
233 var feedlist = document.getElementById('feedList');
234
235 if (keycode == 74) { // j
236 var feed = getActiveFeedId();
237 var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
238 if (new_feed) viewfeed(new_feed, '');
239 }
240
241 if (keycode == 75) { // k
242 var feed = getActiveFeedId();
243 var new_feed = getRelativeFeedId(feedlist, feed, 'next');
244 if (new_feed) viewfeed(new_feed, '');
245 }
246
247 if (!is_safari() && (keycode == 78 || keycode == 40)) { // n, down
248 if (typeof moveToPost != 'undefined') {
249 return moveToPost('next');
250 }
251 }
252
253 if (!is_safari() && (keycode == 80 || keycode == 38)) { // p, up
254 if (typeof moveToPost != 'undefined') {
255 return moveToPost('prev');
256 }
257 }
258
259 if (keycode == 68 && shift_key) { // d
260 if (!debug_mode_enabled) {
261 document.getElementById('debug_output').style.display = 'block';
262 debug('debug mode activated');
263 } else {
264 document.getElementById('debug_output').style.display = 'none';
265 }
266
267 debug_mode_enabled = !debug_mode_enabled;
268 }
269
270 if (keycode == 190 && shift_key) { // >
271 viewFeedGoPage(1);
272 }
273
274 if (keycode == 188 && shift_key) { // <
275 viewFeedGoPage(-1);
276 }
277
278 if (keycode == 191 && shift_key) { // ?
279 viewFeedGoPage(0);
280 }
281
282 if (keycode == 69 && shift_key) { // e
283 return editFeedDlg(getActiveFeedId());
284 }
285
286 if (keycode == 70 && shift_key) { // f
287 if (getActiveFeedId()) {
288 return catchupCurrentFeed();
289 }
290 }
291
292 if (keycode == 80 && shift_key) { // p
293 if (getActiveFeedId()) {
294 return catchupPage();
295 }
296 }
297
298 if (typeof localHotkeyHandler != 'undefined') {
299 try {
300 return localHotkeyHandler(e);
301 } catch (e) {
302 exception_error("hotkey_handler, local:", e);
303 }
304 }
305
306 debug("KP=" + keycode);
307 } catch (e) {
308 exception_error("hotkey_handler", e);
309 }
310 }
311
312 function cleanSelectedList(element) {
313 var content = document.getElementById(element);
314
315 if (!document.getElementById("feedCatHolder")) {
316 for (i = 0; i < content.childNodes.length; i++) {
317 var child = content.childNodes[i];
318 try {
319 child.className = child.className.replace("Selected", "");
320 } catch (e) {
321 //
322 }
323 }
324 } else {
325 for (i = 0; i < content.childNodes.length; i++) {
326 var child = content.childNodes[i];
327 if (child.id == "feedCatHolder") {
328 debug(child.id);
329 var fcat = child.lastChild;
330 for (j = 0; j < fcat.childNodes.length; j++) {
331 var feed = fcat.childNodes[j];
332 feed.className = feed.className.replace("Selected", "");
333 }
334 }
335 }
336 }
337 }
338
339
340 function cleanSelected(element) {
341 var content = document.getElementById(element);
342
343 for (i = 0; i < content.rows.length; i++) {
344 content.rows[i].className = content.rows[i].className.replace("Selected", "");
345 }
346 }
347
348 function getVisibleUnreadHeadlines() {
349 var content = document.getElementById("headlinesList");
350
351 var rows = new Array();
352
353 for (i = 0; i < content.rows.length; i++) {
354 var row_id = content.rows[i].id.replace("RROW-", "");
355 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
356 rows.push(row_id);
357 }
358 }
359 return rows;
360 }
361
362 function getVisibleHeadlineIds() {
363
364 var content = document.getElementById("headlinesList");
365
366 var rows = new Array();
367
368 for (i = 0; i < content.rows.length; i++) {
369 var row_id = content.rows[i].id.replace("RROW-", "");
370 if (row_id.length > 0) {
371 rows.push(row_id);
372 }
373 }
374 return rows;
375 }
376
377 function getFirstVisibleHeadlineId() {
378 var rows = getVisibleHeadlineIds();
379 return rows[0];
380 }
381
382 function getLastVisibleHeadlineId() {
383 var rows = getVisibleHeadlineIds();
384 return rows[rows.length-1];
385 }
386
387 function markHeadline(id) {
388 var row = document.getElementById("RROW-" + id);
389 if (row) {
390 var is_active = false;
391
392 if (row.className.match("Active")) {
393 is_active = true;
394 }
395 row.className = row.className.replace("Selected", "");
396 row.className = row.className.replace("Active", "");
397 row.className = row.className.replace("Insensitive", "");
398
399 if (is_active) {
400 row.className = row.className = "Active";
401 }
402
403 var check = document.getElementById("RCHK-" + id);
404
405 if (check) {
406 check.checked = true;
407 }
408
409 row.className = row.className + "Selected";
410
411 }
412 }
413
414 function getFeedIds() {
415 var content = document.getElementById("feedsList");
416
417 var rows = new Array();
418
419 for (i = 0; i < content.rows.length; i++) {
420 var id = content.rows[i].id.replace("FEEDR-", "");
421 if (id.length > 0) {
422 rows.push(id);
423 }
424 }
425
426 return rows;
427 }
428
429 function setCookie(name, value, lifetime, path, domain, secure) {
430
431 var d = false;
432
433 if (lifetime) {
434 d = new Date();
435 d.setTime(d.getTime() + (lifetime * 1000));
436 }
437
438 debug("setCookie: " + name + " => " + value + ": " + d);
439
440 int_setCookie(name, value, d, path, domain, secure);
441
442 }
443
444 function int_setCookie(name, value, expires, path, domain, secure) {
445 document.cookie= name + "=" + escape(value) +
446 ((expires) ? "; expires=" + expires.toGMTString() : "") +
447 ((path) ? "; path=" + path : "") +
448 ((domain) ? "; domain=" + domain : "") +
449 ((secure) ? "; secure" : "");
450 }
451
452 function delCookie(name, path, domain) {
453 if (getCookie(name)) {
454 document.cookie = name + "=" +
455 ((path) ? ";path=" + path : "") +
456 ((domain) ? ";domain=" + domain : "" ) +
457 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
458 }
459 }
460
461
462 function getCookie(name) {
463
464 var dc = document.cookie;
465 var prefix = name + "=";
466 var begin = dc.indexOf("; " + prefix);
467 if (begin == -1) {
468 begin = dc.indexOf(prefix);
469 if (begin != 0) return null;
470 }
471 else {
472 begin += 2;
473 }
474 var end = document.cookie.indexOf(";", begin);
475 if (end == -1) {
476 end = dc.length;
477 }
478 return unescape(dc.substring(begin + prefix.length, end));
479 }
480
481 function disableContainerChildren(id, disable, doc) {
482
483 if (!doc) doc = document;
484
485 var container = doc.getElementById(id);
486
487 if (!container) {
488 //alert("disableContainerChildren: element " + id + " not found");
489 return;
490 }
491
492 for (var i = 0; i < container.childNodes.length; i++) {
493 var child = container.childNodes[i];
494
495 try {
496 child.disabled = disable;
497 } catch (E) {
498
499 }
500
501 if (disable) {
502 if (child.className && child.className.match("button")) {
503 child.className = "disabledButton";
504 }
505 } else {
506 if (child.className && child.className.match("disabledButton")) {
507 child.className = "button";
508 }
509 }
510 }
511
512 }
513
514 function gotoPreferences() {
515 document.location.href = "prefs.php";
516 }
517
518 function gotoMain() {
519 document.location.href = "tt-rss.php";
520 }
521
522 function gotoExportOpml() {
523 document.location.href = "opml.php?op=Export";
524 }
525
526 function getActiveFeedId() {
527 // return getCookie("ttrss_vf_actfeed");
528 try {
529 debug("gAFID: " + active_feed_id);
530 return active_feed_id;
531 } catch (e) {
532 exception_error("getActiveFeedId", e);
533 }
534 }
535
536 function activeFeedIsCat() {
537 return active_feed_is_cat;
538 }
539
540 function setActiveFeedId(id) {
541 // return setCookie("ttrss_vf_actfeed", id);
542 try {
543 debug("sAFID(" + id + ")");
544 active_feed_id = id;
545 } catch (e) {
546 exception_error("setActiveFeedId", e);
547 }
548 }
549
550 function parse_counters(reply, scheduled_call) {
551 try {
552
553 var feeds_found = 0;
554
555 if (reply.firstChild && reply.firstChild.firstChild) {
556 debug("<b>wrong element passed to parse_counters, adjusting.</b>");
557 reply = reply.firstChild;
558 }
559
560 for (var l = 0; l < reply.childNodes.length; l++) {
561 if (!reply.childNodes[l] ||
562 typeof(reply.childNodes[l].getAttribute) == "undefined") {
563 // where did this come from?
564 continue;
565 }
566
567 var id = reply.childNodes[l].getAttribute("id");
568 var t = reply.childNodes[l].getAttribute("type");
569 var ctr = reply.childNodes[l].getAttribute("counter");
570 var error = reply.childNodes[l].getAttribute("error");
571 var has_img = reply.childNodes[l].getAttribute("hi");
572 var updated = reply.childNodes[l].getAttribute("updated");
573
574 if (id == "global-unread") {
575 global_unread = ctr;
576 updateTitle();
577 continue;
578 }
579
580 if (id == "subscribed-feeds") {
581 feeds_found = ctr;
582 continue;
583 }
584
585 if (t == "category") {
586 var catctr = document.getElementById("FCATCTR-" + id);
587 if (catctr) {
588 catctr.innerHTML = "(" + ctr + ")";
589 if (ctr > 0) {
590 catctr.className = "catCtrHasUnread";
591 } else {
592 catctr.className = "catCtrNoUnread";
593 }
594 }
595 continue;
596 }
597
598 var feedctr = document.getElementById("FEEDCTR-" + id);
599 var feedu = document.getElementById("FEEDU-" + id);
600 var feedr = document.getElementById("FEEDR-" + id);
601 var feed_img = document.getElementById("FIMG-" + id);
602 var feedlink = document.getElementById("FEEDL-" + id);
603 var feedupd = document.getElementById("FLUPD-" + id);
604
605 if (updated && feedlink) {
606 if (error) {
607 feedlink.title = "Error: " + error + " (" + updated + ")";
608 } else {
609 feedlink.title = "Updated: " + updated;
610 }
611 }
612
613 if (updated && feedupd) {
614 if (error) {
615 feedupd.innerHTML = updated + " (Error)";
616 } else {
617 feedupd.innerHTML = updated;
618 }
619 }
620
621 if (feedctr && feedu && feedr) {
622
623 if (feedu.innerHTML != ctr && id == getActiveFeedId() && scheduled_call) {
624 viewCurrentFeed();
625 }
626
627 feedu.innerHTML = ctr;
628
629 if (error) {
630 feedr.className = feedr.className.replace("feed", "error");
631 } else if (id > 0) {
632 feedr.className = feedr.className.replace("error", "feed");
633 }
634
635 if (ctr > 0) {
636 feedctr.className = "odd";
637 if (!feedr.className.match("Unread")) {
638 var is_selected = feedr.className.match("Selected");
639
640 feedr.className = feedr.className.replace("Selected", "");
641 feedr.className = feedr.className.replace("Unread", "");
642
643 feedr.className = feedr.className + "Unread";
644
645 if (is_selected) {
646 feedr.className = feedr.className + "Selected";
647 }
648
649 }
650 } else {
651 feedctr.className = "invisible";
652 feedr.className = feedr.className.replace("Unread", "");
653 }
654 }
655 }
656
657 hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1);
658
659 var feeds_stored = number_of_feeds;
660
661 debug("Feed counters, C: " + feeds_found + ", S:" + feeds_stored);
662
663 if (feeds_stored != feeds_found) {
664 number_of_feeds = feeds_found;
665
666 if (feeds_stored != 0) {
667 debug("Subscribed feed number changed, refreshing feedlist");
668 setTimeout('updateFeedList(false, false)', 50);
669 }
670 }
671
672 } catch (e) {
673 exception_error("parse_counters", e);
674 }
675 }
676
677 function parse_counters_reply(xmlhttp, scheduled_call) {
678
679 if (!xmlhttp.responseXML) {
680 notify_error("Backend did not return valid XML", true);
681 return;
682 }
683
684 var reply = xmlhttp.responseXML.firstChild;
685
686 if (!reply) {
687 notify_error("Backend did not return expected XML object", true);
688 updateTitle("");
689 return;
690 }
691
692 var error_code = false;
693 var error_msg = false;
694
695 if (reply.firstChild) {
696 error_code = reply.firstChild.getAttribute("error-code");
697 error_msg = reply.firstChild.getAttribute("error-msg");
698 }
699
700 if (!error_code) {
701 error_code = reply.getAttribute("error-code");
702 error_msg = reply.getAttribute("error-msg");
703 }
704
705 if (error_code && error_code != 0) {
706 debug("refetch_callback: got error code " + error_code);
707 return fatalError(error_code, error_msg);
708 }
709
710 var counters = reply.firstChild;
711
712 parse_counters(counters, scheduled_call);
713
714 var runtime_info = counters.nextSibling;
715
716 parse_runtime_info(runtime_info);
717
718 if (getInitParam("feeds_sort_by_unread") == 1) {
719 resort_feedlist();
720 }
721
722 hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1);
723
724 }
725
726 function all_counters_callback() {
727 if (xmlhttp_rpc.readyState == 4) {
728 try {
729 /* if (!xmlhttp_rpc.responseXML || !xmlhttp_rpc.responseXML.firstChild) {
730 debug("[all_counters_callback] backend did not return valid XML");
731 return;
732 }
733
734 debug("in all_counters_callback : " + xmlhttp_rpc.responseXML);
735
736 var reply = xmlhttp_rpc.responseXML.firstChild;
737
738 var counters = reply.firstChild;
739
740 parse_counters(counters);
741
742 var runtime = counters.nextSibling;
743
744 if (runtime) {
745 parse_runtime_info(runtime);
746 }
747
748 if (getInitParam("feeds_sort_by_unread") == 1) {
749 resort_feedlist();
750 }
751
752 hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1); */
753
754 debug("in all_counters_callback");
755
756 parse_counters_reply(xmlhttp_rpc);
757
758 } catch (e) {
759 exception_error("all_counters_callback", e);
760 }
761 }
762 }
763
764 function get_feed_entry_unread(doc, elem) {
765
766 var id = elem.id.replace("FEEDR-", "");
767
768 if (id <= 0) {
769 return -1;
770 }
771
772 try {
773 return parseInt(doc.getElementById("FEEDU-" + id).innerHTML);
774 } catch (e) {
775 return -1;
776 }
777 }
778
779 function resort_category(doc, node) {
780 debug("resort_category: " + node);
781
782 if (node.hasChildNodes() && node.firstChild.nextSibling != false) {
783 for (i = 0; i < node.childNodes.length; i++) {
784 if (node.childNodes[i].nodeName != "LI") { continue; }
785
786 if (get_feed_entry_unread(doc, node.childNodes[i]) < 0) {
787 continue;
788 }
789
790 for (j = i+1; j < node.childNodes.length; j++) {
791 if (node.childNodes[j].nodeName != "LI") { continue; }
792
793 var tmp_val = get_feed_entry_unread(doc, node.childNodes[i]);
794 var cur_val = get_feed_entry_unread(doc, node.childNodes[j]);
795
796 if (cur_val > tmp_val) {
797 tempnode_i = node.childNodes[i].cloneNode(true);
798 tempnode_j = node.childNodes[j].cloneNode(true);
799 node.replaceChild(tempnode_i, node.childNodes[j]);
800 node.replaceChild(tempnode_j, node.childNodes[i]);
801 }
802 }
803
804 }
805 }
806
807 }
808
809 function resort_feedlist() {
810 debug("resort_feedlist");
811
812 var fd = document;
813
814 if (fd.getElementById("feedCatHolder")) {
815
816 var feeds = fd.getElementById("feedList");
817 var child = feeds.firstChild;
818
819 while (child) {
820
821 if (child.id == "feedCatHolder") {
822 resort_category(fd, child.firstChild);
823 }
824
825 child = child.nextSibling;
826 }
827
828 } else {
829 resort_category(fd, fd.getElementById("feedList"));
830 }
831 }
832
833 function update_all_counters(feed) {
834 if (xmlhttp_ready(xmlhttp_rpc)) {
835 var query = "backend.php?op=rpc&subop=getAllCounters";
836
837 if (feed > 0) {
838 query = query + "&aid=" + feed;
839 }
840
841 if (tagsAreDisplayed()) {
842 query = query + "&omode=lt";
843 } else {
844 query = query + "&omode=flc";
845 }
846
847 debug("update_all_counters QUERY: " + query);
848
849 var date = new Date();
850 var timestamp = Math.round(date.getTime() / 1000);
851 query = query + "&ts=" + timestamp
852
853 xmlhttp_rpc.open("GET", query, true);
854 xmlhttp_rpc.onreadystatechange=all_counters_callback;
855 xmlhttp_rpc.send(null);
856 }
857 }
858
859 function popupHelp(tid) {
860 var w = window.open("backend.php?op=help&tid=" + tid,
861 "Popup Help",
862 "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
863 }
864
865 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
866 * * @author Sundar Dorai-Raj
867 * * Email: sdoraira@vt.edu
868 * * This program is free software; you can redistribute it and/or
869 * * modify it under the terms of the GNU General Public License
870 * * as published by the Free Software Foundation; either version 2
871 * * of the License, or (at your option) any later version,
872 * * provided that any use properly credits the author.
873 * * This program is distributed in the hope that it will be useful,
874 * * but WITHOUT ANY WARRANTY; without even the implied warranty of
875 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
876 * * GNU General Public License for more details at http://www.gnu.org * * */
877
878 var numbers=".0123456789";
879 function isNumeric(x) {
880 // is x a String or a character?
881 if(x.length>1) {
882 // remove negative sign
883 x=Math.abs(x)+"";
884 for(j=0;j<x.length;j++) {
885 // call isNumeric recursively for each character
886 number=isNumeric(x.substring(j,j+1));
887 if(!number) return number;
888 }
889 return number;
890 }
891 else {
892 // if x is number return true
893 if(numbers.indexOf(x)>=0) return true;
894 return false;
895 }
896 }
897
898
899 function hideOrShowFeeds(doc, hide) {
900
901 debug("hideOrShowFeeds: " + doc + ", " + hide);
902
903 var fd = document;
904
905 var list = fd.getElementById("feedList");
906
907 if (fd.getElementById("feedCatHolder")) {
908
909 var feeds = fd.getElementById("feedList");
910 var child = feeds.firstChild;
911
912 while (child) {
913
914 if (child.id == "feedCatHolder") {
915 hideOrShowFeedsCategory(fd, child.firstChild, hide, child.previousSibling);
916 }
917
918 child = child.nextSibling;
919 }
920
921 } else {
922 hideOrShowFeedsCategory(fd, fd.getElementById("feedList"), hide);
923 }
924 }
925
926 function hideOrShowFeedsCategory(doc, node, hide, cat_node) {
927
928 // debug("hideOrShowFeedsCategory: " + node + " (" + hide + ")");
929
930 var cat_unread = 0;
931
932 if (!node) {
933 debug("hideOrShowFeeds: passed node is null, aborting");
934 return;
935 }
936
937 if (node.hasChildNodes() && node.firstChild.nextSibling != false) {
938 for (i = 0; i < node.childNodes.length; i++) {
939 if (node.childNodes[i].nodeName != "LI") { continue; }
940
941 if (node.childNodes[i].style != undefined) {
942
943 var has_unread = (node.childNodes[i].className != "feed");
944
945 // debug(node.childNodes[i].id + " --> " + has_unread);
946
947 if (hide && !has_unread) {
948 node.childNodes[i].style.display = "none";
949 }
950
951 if (!hide) {
952 node.childNodes[i].style.display = "list-item";
953 }
954
955 if (has_unread) {
956 node.childNodes[i].style.display = "list-item";
957 cat_unread++;
958 }
959 }
960 }
961 }
962
963 if (cat_unread == 0) {
964 if (cat_node.style == undefined) {
965 debug("ERROR: supplied cat_node " + cat_node +
966 " has no styles. WTF?");
967 return;
968 }
969 if (hide) {
970 cat_node.style.display = "none";
971 } else {
972 cat_node.style.display = "list-item";
973 }
974 } else {
975 try {
976 cat_node.style.display = "list-item";
977 } catch (e) {
978 debug(e);
979 }
980 }
981
982 // debug("unread for category: " + cat_unread);
983 }
984
985 function selectTableRow(r, do_select) {
986 r.className = r.className.replace("Selected", "");
987
988 if (do_select) {
989 r.className = r.className + "Selected";
990 }
991 }
992
993 function selectTableRowById(elem_id, check_id, do_select) {
994
995 try {
996
997 var row = document.getElementById(elem_id);
998
999 if (row) {
1000 selectTableRow(row, do_select);
1001 }
1002
1003 var check = document.getElementById(check_id);
1004
1005 if (check) {
1006 check.checked = do_select;
1007 }
1008 } catch (e) {
1009 exception_error("selectTableRowById", e);
1010 }
1011 }
1012
1013 function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select,
1014 classcheck, reset_others) {
1015
1016 var content = document.getElementById(content_id);
1017
1018 if (!content) {
1019 alert("[selectTableRows] Element " + content_id + " not found.");
1020 return;
1021 }
1022
1023 for (i = 0; i < content.rows.length; i++) {
1024 if (!classcheck || content.rows[i].className.match(classcheck)) {
1025
1026 if (content.rows[i].id.match(prefix)) {
1027 selectTableRow(content.rows[i], do_select);
1028
1029 var row_id = content.rows[i].id.replace(prefix, "");
1030 var check = document.getElementById(check_prefix + row_id);
1031
1032 if (check) {
1033 check.checked = do_select;
1034 }
1035 } else if (reset_others) {
1036 selectTableRow(content.rows[i], false);
1037
1038 var row_id = content.rows[i].id.replace(prefix, "");
1039 var check = document.getElementById(check_prefix + row_id);
1040
1041 if (check) {
1042 check.checked = false;
1043 }
1044
1045 }
1046 } else if (reset_others) {
1047 selectTableRow(content.rows[i], false);
1048
1049 var row_id = content.rows[i].id.replace(prefix, "");
1050 var check = document.getElementById(check_prefix + row_id);
1051
1052 if (check) {
1053 check.checked = false;
1054 }
1055
1056 }
1057 }
1058 }
1059
1060 function getSelectedTableRowIds(content_id, prefix) {
1061
1062 var content = document.getElementById(content_id);
1063
1064 if (!content) {
1065 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
1066 return;
1067 }
1068
1069 var sel_rows = new Array();
1070
1071 for (i = 0; i < content.rows.length; i++) {
1072 if (content.rows[i].id.match(prefix) &&
1073 content.rows[i].className.match("Selected")) {
1074
1075 var row_id = content.rows[i].id.replace(prefix + "-", "");
1076 sel_rows.push(row_id);
1077 }
1078 }
1079
1080 return sel_rows;
1081
1082 }
1083
1084 function toggleSelectRowById(sender, id) {
1085 var row = document.getElementById(id);
1086
1087 if (sender.checked) {
1088 if (!row.className.match("Selected")) {
1089 row.className = row.className + "Selected";
1090 }
1091 } else {
1092 if (row.className.match("Selected")) {
1093 row.className = row.className.replace("Selected", "");
1094 }
1095 }
1096 }
1097
1098 function toggleSelectListRow(sender) {
1099 var parent_row = sender.parentNode;
1100
1101 if (sender.checked) {
1102 if (!parent_row.className.match("Selected")) {
1103 parent_row.className = parent_row.className + "Selected";
1104 }
1105 } else {
1106 if (parent_row.className.match("Selected")) {
1107 parent_row.className = parent_row.className.replace("Selected", "");
1108 }
1109 }
1110 }
1111
1112
1113 function toggleSelectRow(sender) {
1114 var parent_row = sender.parentNode.parentNode;
1115
1116 if (sender.checked) {
1117 if (!parent_row.className.match("Selected")) {
1118 parent_row.className = parent_row.className + "Selected";
1119 }
1120 } else {
1121 if (parent_row.className.match("Selected")) {
1122 parent_row.className = parent_row.className.replace("Selected", "");
1123 }
1124 }
1125 }
1126
1127 function openExternalUrl(url) {
1128 var w = window.open(url);
1129 }
1130
1131 function getRelativeFeedId(list, id, direction, unread_only) {
1132 if (!id) {
1133 if (direction == "next") {
1134 for (i = 0; i < list.childNodes.length; i++) {
1135 var child = list.childNodes[i];
1136 if (child.id && child.id == "feedCatHolder") {
1137 if (child.lastChild) {
1138 var cr = getRelativeFeedId(child.firstChild, id, direction, unread_only);
1139 if (cr) return cr;
1140 }
1141 } else if (child.id && child.id.match("FEEDR-")) {
1142 return child.id.replace('FEEDR-', '');
1143 }
1144 }
1145 }
1146
1147 // FIXME select last feed doesn't work when only unread feeds are visible
1148
1149 if (direction == "prev") {
1150 for (i = list.childNodes.length-1; i >= 0; i--) {
1151 var child = list.childNodes[i];
1152 if (child.id == "feedCatHolder") {
1153 if (child.firstChild) {
1154 var cr = getRelativeFeedId(child.firstChild, id, direction);
1155 if (cr) return cr;
1156 }
1157 } else if (child.id.match("FEEDR-")) {
1158
1159 if (getInitParam("hide_read_feeds") == 1) {
1160 if (child.className != "feed") {
1161 alert(child.className);
1162 return child.id.replace('FEEDR-', '');
1163 }
1164 } else {
1165 return child.id.replace('FEEDR-', '');
1166 }
1167 }
1168 }
1169 }
1170 } else {
1171
1172 var feed = list.ownerDocument.getElementById("FEEDR-" + id);
1173
1174 if (getInitParam("hide_read_feeds") == 1) {
1175 unread_only = true;
1176 }
1177
1178 if (direction == "next") {
1179
1180 var e = feed;
1181
1182 while (e) {
1183
1184 if (e.nextSibling) {
1185
1186 e = e.nextSibling;
1187
1188 } else if (e.parentNode.parentNode.nextSibling) {
1189
1190 var this_cat = e.parentNode.parentNode;
1191
1192 e = false;
1193
1194 if (this_cat && this_cat.nextSibling) {
1195 while (!e && this_cat.nextSibling) {
1196 this_cat = this_cat.nextSibling;
1197 if (this_cat.id == "feedCatHolder") {
1198 e = this_cat.firstChild.firstChild;
1199 }
1200 }
1201 }
1202
1203 } else {
1204 e = false;
1205 }
1206
1207 if (e) {
1208 if (!unread_only || (unread_only && e.className != "feed" &&
1209 e.className.match("feed"))) {
1210 return e.id.replace("FEEDR-", "");
1211 }
1212 }
1213 }
1214
1215 } else if (direction == "prev") {
1216
1217 var e = feed;
1218
1219 while (e) {
1220
1221 if (e.previousSibling) {
1222
1223 e = e.previousSibling;
1224
1225 } else if (e.parentNode.parentNode.previousSibling) {
1226
1227 var this_cat = e.parentNode.parentNode;
1228
1229 e = false;
1230
1231 if (this_cat && this_cat.previousSibling) {
1232 while (!e && this_cat.previousSibling) {
1233 this_cat = this_cat.previousSibling;
1234 if (this_cat.id == "feedCatHolder") {
1235 e = this_cat.firstChild.lastChild;
1236 }
1237 }
1238 }
1239
1240 } else {
1241 e = false;
1242 }
1243
1244 if (e) {
1245 if (!unread_only || (unread_only && e.className != "feed" &&
1246 e.className.match("feed"))) {
1247 return e.id.replace("FEEDR-", "");
1248 }
1249 }
1250 }
1251 }
1252 }
1253 }
1254
1255 function showBlockElement(id, h_id) {
1256 var elem = document.getElementById(id);
1257
1258 if (elem) {
1259 elem.style.display = "block";
1260
1261 if (h_id) {
1262 elem = document.getElementById(h_id);
1263 if (elem) {
1264 elem.style.display = "none";
1265 }
1266 }
1267 } else {
1268 alert("[showBlockElement] can't find element with id " + id);
1269 }
1270 }
1271
1272 function hideParentElement(e) {
1273 e.parentNode.style.display = "none";
1274 }
1275
1276 function dropboxSelect(e, v) {
1277 for (i = 0; i < e.length; i++) {
1278 if (e[i].value == v) {
1279 e.selectedIndex = i;
1280 break;
1281 }
1282 }
1283 }
1284
1285 // originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
1286 // bugfixed just a little bit :-)
1287 function getURLParam(strParamName){
1288 var strReturn = "";
1289 var strHref = window.location.href;
1290
1291 if (strHref.indexOf("#") == strHref.length-1) {
1292 strHref = strHref.substring(0, strHref.length-1);
1293 }
1294
1295 if ( strHref.indexOf("?") > -1 ){
1296 var strQueryString = strHref.substr(strHref.indexOf("?"));
1297 var aQueryString = strQueryString.split("&");
1298 for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
1299 if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
1300 var aParam = aQueryString[iParam].split("=");
1301 strReturn = aParam[1];
1302 break;
1303 }
1304 }
1305 }
1306 return strReturn;
1307 }
1308
1309 function leading_zero(p) {
1310 var s = String(p);
1311 if (s.length == 1) s = "0" + s;
1312 return s;
1313 }
1314
1315 function closeInfoBox(cleanup) {
1316 var box = document.getElementById('infoBox');
1317 var shadow = document.getElementById('infoBoxShadow');
1318
1319 if (shadow) {
1320 shadow.style.display = "none";
1321 } else if (box) {
1322 box.style.display = "none";
1323 }
1324
1325 if (cleanup) box.innerHTML = "&nbsp;";
1326
1327 enableHotkeys();
1328
1329 return false;
1330 }
1331
1332
1333 function displayDlg(id, param) {
1334
1335 if (!xmlhttp_ready(xmlhttp)) {
1336 printLockingError();
1337 return
1338 }
1339
1340 notify_progress("Loading, please wait...");
1341
1342 xmlhttp.open("GET", "backend.php?op=dlg&id=" +
1343 param_escape(id) + "&param=" + param_escape(param), true);
1344 xmlhttp.onreadystatechange=infobox_callback;
1345 xmlhttp.send(null);
1346
1347 disableHotkeys();
1348
1349 return false;
1350 }
1351
1352 function infobox_submit_callback() {
1353 if (xmlhttp.readyState == 4) {
1354 closeInfoBox();
1355
1356 try {
1357 // called from prefs, reload tab
1358 if (active_tab) {
1359 selectTab(active_tab, false);
1360 }
1361 } catch (e) { }
1362
1363 if (xmlhttp.responseText) {
1364 notify_info(xmlhttp.responseText);
1365 }
1366
1367 }
1368 }
1369
1370 function infobox_callback() {
1371 if (xmlhttp.readyState == 4) {
1372 var box = document.getElementById('infoBox');
1373 var shadow = document.getElementById('infoBoxShadow');
1374 if (box) {
1375 box.innerHTML=xmlhttp.responseText;
1376 if (shadow) {
1377 shadow.style.display = "block";
1378 } else {
1379 box.style.display = "block";
1380 }
1381 }
1382 notify("");
1383 }
1384 }
1385
1386 function addFilter() {
1387
1388 if (!xmlhttp_ready(xmlhttp)) {
1389 printLockingError();
1390 return
1391 }
1392
1393 var form = document.forms['filter_add_form'];
1394 var reg_exp = form.reg_exp.value;
1395
1396 if (reg_exp == "") {
1397 alert("Can't add filter: nothing to match on.");
1398 return false;
1399 }
1400
1401 var query = Form.serialize("filter_add_form");
1402
1403 xmlhttp.open("GET", "backend.php?" + query, true);
1404 xmlhttp.onreadystatechange=infobox_submit_callback;
1405 xmlhttp.send(null);
1406
1407 return true;
1408 }
1409
1410 function toggleSubmitNotEmpty(e, submit_id) {
1411 try {
1412 document.getElementById(submit_id).disabled = (e.value == "")
1413 } catch (e) {
1414 exception_error("toggleSubmitNotEmpty", e);
1415 }
1416 }
1417
1418 function isValidURL(s) {
1419 return s.match("http://") != null || s.match("https://") != null || s.match("feed://") != null;
1420 }
1421
1422 function qaddFeed() {
1423
1424 if (!xmlhttp_ready(xmlhttp)) {
1425 printLockingError();
1426 return
1427 }
1428
1429 var form = document.forms['feed_add_form'];
1430 var feed_url = form.feed_url.value;
1431
1432 if (feed_url == "") {
1433 alert("Can't subscribe: no feed URL given.");
1434 return false;
1435 }
1436
1437 notify_progress("Adding feed...");
1438
1439 closeInfoBox();
1440
1441 var feeds_doc = document;
1442
1443 // feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
1444
1445 var query = Form.serialize("feed_add_form");
1446
1447 debug("subscribe q: " + query);
1448
1449 /* xmlhttp.open("GET", "backend.php?" + query, true);
1450 xmlhttp.onreadystatechange=dlg_frefresh_callback;
1451 xmlhttp.send(null); */
1452
1453 xmlhttp.open("POST", "backend.php", true);
1454 xmlhttp.onreadystatechange=dlg_frefresh_callback;
1455 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
1456 xmlhttp.send(query);
1457
1458 return false;
1459 }
1460
1461 function filterCR(e, f)
1462 {
1463 var key;
1464
1465 if(window.event)
1466 key = window.event.keyCode; //IE
1467 else
1468 key = e.which; //firefox
1469
1470 if (key == 13) {
1471 if (typeof f != 'undefined') {
1472 f();
1473 return false;
1474 } else {
1475 return false;
1476 }
1477 } else {
1478 return true;
1479 }
1480 }
1481
1482 function getMainContext() {
1483 return this.window;
1484 }
1485
1486 function getFeedsContext() {
1487 return this.window;
1488 }
1489
1490 function getContentContext() {
1491 return this.window;
1492 }
1493
1494 function getHeadlinesContext() {
1495 return this.window;
1496 }
1497
1498 var debug_last_class = "even";
1499
1500 function debug(msg) {
1501
1502 if (debug_last_class == "even") {
1503 debug_last_class = "odd";
1504 } else {
1505 debug_last_class = "even";
1506 }
1507
1508 var c = document.getElementById('debug_output');
1509 if (c && c.style.display == "block") {
1510 while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
1511 c.removeChild(c.lastChild);
1512 }
1513
1514 var d = new Date();
1515 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
1516 ":" + leading_zero(d.getSeconds());
1517 c.innerHTML = "<li class=\"" + debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " +
1518 msg + "</li>" + c.innerHTML;
1519 }
1520 }
1521
1522 function getInitParam(key) {
1523 return init_params[key];
1524 }
1525
1526 function storeInitParam(key, value) {
1527 debug("<b>storeInitParam is OBSOLETE: " + key + " => " + value + "</b>");
1528 init_params[key] = value;
1529 }
1530
1531 function fatalError(code, message) {
1532 try {
1533
1534 if (code == 6) {
1535 window.location.href = "tt-rss.php";
1536 } else if (code == 5) {
1537 window.location.href = "update.php";
1538 } else {
1539 var fe = document.getElementById("fatal_error");
1540 var fc = document.getElementById("fatal_error_msg");
1541
1542 if (message == "") message = "Unknown error";
1543
1544 fc.innerHTML = "<img src='images/sign_excl.png'> " + message + " (Code " + code + ")";
1545
1546 fe.style.display = "block";
1547 }
1548
1549 } catch (e) {
1550 exception_error("fatalError", e);
1551 }
1552 }
1553
1554 function getFeedName(id, is_cat) {
1555 var d = getFeedsContext().document;
1556
1557 var e;
1558
1559 if (is_cat) {
1560 e = d.getElementById("FCATN-" + id);
1561 } else {
1562 e = d.getElementById("FEEDN-" + id);
1563 }
1564 if (e) {
1565 return e.innerHTML.stripTags();
1566 } else {
1567 return null;
1568 }
1569 }
1570
1571 function viewContentUrl(url) {
1572 getContentContext().location = url;
1573 }
1574
1575 function filterDlgCheckAction(sender) {
1576
1577 try {
1578
1579 var action = sender[sender.selectedIndex].value;
1580
1581 var form = document.forms["filter_add_form"];
1582
1583 if (!form) {
1584 form = document.forms["filter_edit_form"];
1585 }
1586
1587 if (!form) {
1588 debug("filterDlgCheckAction: can't find form!");
1589 return;
1590 }
1591
1592 var action_param = form.action_param;
1593
1594 if (!action_param) {
1595 debug("filterDlgCheckAction: can't find action param!");
1596 return;
1597 }
1598
1599 // if selected action supports parameters, enable params field
1600 if (action == 4) {
1601 action_param.disabled = false;
1602 } else {
1603 action_param.disabled = true;
1604 }
1605
1606 } catch (e) {
1607 exception_error(e, "filterDlgCheckAction");
1608 }
1609
1610 }
1611
1612 function explainError(code) {
1613 return displayDlg("explainError", code);
1614 }
1615
1616 function logoutUser() {
1617 try {
1618 if (xmlhttp_ready(xmlhttp_rpc)) {
1619
1620 notify_progress("Logging out, please wait...", true);
1621
1622 xmlhttp_rpc.open("GET", "backend.php?op=rpc&subop=logout", true);
1623 xmlhttp_rpc.onreadystatechange=logout_callback;
1624 xmlhttp_rpc.send(null);
1625 } else {
1626 printLockingError();
1627 }
1628 } catch (e) {
1629 exception_error("logoutUser", e);
1630 }
1631 }