]> git.wh0rd.org - tt-rss.git/blame_incremental - functions.js
getRelativeFeedId iterates over hidden categories correctly (2)
[tt-rss.git] / functions.js
... / ...
CommitLineData
1var hotkeys_enabled = true;
2var debug_mode_enabled = false;
3var xmlhttp_rpc = Ajax.getTransport();
4
5function browser_has_opacity() {
6 return navigator.userAgent.match("Gecko") != null ||
7 navigator.userAgent.match("Opera") != null;
8}
9
10function is_msie() {
11 return navigator.userAgent.match("MSIE");
12}
13
14function is_opera() {
15 return navigator.userAgent.match("Opera");
16}
17
18function is_khtml() {
19 return navigator.userAgent.match("KHTML");
20}
21
22function is_safari() {
23 return navigator.userAgent.match("Safari");
24}
25
26function 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
47function disableHotkeys() {
48 hotkeys_enabled = false;
49}
50
51function enableHotkeys() {
52 hotkeys_enabled = true;
53}
54
55function xmlhttp_ready(obj) {
56 return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
57}
58
59function 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
72function notify_callback() {
73 var container = document.getElementById('notify');
74 if (xmlhttp.readyState == 4) {
75 container.innerHTML=xmlhttp.responseText;
76 }
77}
78
79function rpc_notify_callback() {
80 var container = document.getElementById('notify');
81 if (xmlhttp_rpc.readyState == 4) {
82 container.innerHTML=xmlhttp_rpc.responseText;
83 }
84}
85
86function param_escape(arg) {
87 if (typeof encodeURIComponent != 'undefined')
88 return encodeURIComponent(arg);
89 else
90 return escape(arg);
91}
92
93function param_unescape(arg) {
94 if (typeof decodeURIComponent != 'undefined')
95 return decodeURIComponent(arg);
96 else
97 return unescape(arg);
98}
99
100function 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
109var notify_hide_timerid = false;
110
111function hide_notify() {
112 var n = document.getElementById("notify");
113 if (n) {
114 n.style.display = "none";
115 }
116}
117
118function 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
173function notify(msg, no_hide) {
174 notify_real(msg, no_hide, 1);
175}
176
177function notify_progress(msg, no_hide) {
178 notify_real(msg, no_hide, 2);
179}
180
181function notify_error(msg, no_hide) {
182 notify_real(msg, no_hide, 3);
183
184}
185
186function notify_info(msg, no_hide) {
187 notify_real(msg, no_hide, 4);
188}
189
190function printLockingError() {
191 notify_info("Please wait until operation finishes.");
192}
193
194function 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
312function 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
340function 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
348function 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
362function 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
377function getFirstVisibleHeadlineId() {
378 var rows = getVisibleHeadlineIds();
379 return rows[0];
380}
381
382function getLastVisibleHeadlineId() {
383 var rows = getVisibleHeadlineIds();
384 return rows[rows.length-1];
385}
386
387function 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
414function 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
429function 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
444function 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
452function 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
462function 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
481function 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
514function gotoPreferences() {
515 document.location.href = "prefs.php";
516}
517
518function gotoMain() {
519 document.location.href = "tt-rss.php";
520}
521
522function gotoExportOpml() {
523 document.location.href = "opml.php?op=Export";
524}
525
526function 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
536function activeFeedIsCat() {
537 return active_feed_is_cat;
538}
539
540function 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
550function 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
677function 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
726function 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
764function 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
779function 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
809function 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
833function 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
859function 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
899function 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
926function 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
985function 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
993function 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
1013function 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
1060function 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
1084function 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
1098function 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
1113function 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
1127function openExternalUrl(url) {
1128 var w = window.open(url);
1129}
1130
1131function 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" && child.className != "invisible") {
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 if (e.parentNode.parentNode && e.parentNode.parentNode.className
1211 != "invisible") {
1212 return e.id.replace("FEEDR-", "");
1213 }
1214 }
1215 }
1216 }
1217
1218 } else if (direction == "prev") {
1219
1220 var e = feed;
1221
1222 while (e) {
1223
1224 if (e.previousSibling) {
1225
1226 e = e.previousSibling;
1227
1228 } else if (e.parentNode.parentNode.previousSibling) {
1229
1230 var this_cat = e.parentNode.parentNode;
1231
1232 e = false;
1233
1234 if (this_cat && this_cat.previousSibling) {
1235 while (!e && this_cat.previousSibling) {
1236 this_cat = this_cat.previousSibling;
1237 if (this_cat.id == "feedCatHolder") {
1238 e = this_cat.firstChild.lastChild;
1239 }
1240 }
1241 }
1242
1243 } else {
1244 e = false;
1245 }
1246
1247 if (e) {
1248 if (!unread_only || (unread_only && e.className != "feed" &&
1249 e.className.match("feed"))) {
1250 if (e.parentNode.parentNode && e.parentNode.parentNode.className
1251 != "invisible") {
1252 return e.id.replace("FEEDR-", "");
1253 }
1254 }
1255 }
1256 }
1257 }
1258 }
1259}
1260
1261function showBlockElement(id, h_id) {
1262 var elem = document.getElementById(id);
1263
1264 if (elem) {
1265 elem.style.display = "block";
1266
1267 if (h_id) {
1268 elem = document.getElementById(h_id);
1269 if (elem) {
1270 elem.style.display = "none";
1271 }
1272 }
1273 } else {
1274 alert("[showBlockElement] can't find element with id " + id);
1275 }
1276}
1277
1278function hideParentElement(e) {
1279 e.parentNode.style.display = "none";
1280}
1281
1282function dropboxSelect(e, v) {
1283 for (i = 0; i < e.length; i++) {
1284 if (e[i].value == v) {
1285 e.selectedIndex = i;
1286 break;
1287 }
1288 }
1289}
1290
1291// originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
1292// bugfixed just a little bit :-)
1293function getURLParam(strParamName){
1294 var strReturn = "";
1295 var strHref = window.location.href;
1296
1297 if (strHref.indexOf("#") == strHref.length-1) {
1298 strHref = strHref.substring(0, strHref.length-1);
1299 }
1300
1301 if ( strHref.indexOf("?") > -1 ){
1302 var strQueryString = strHref.substr(strHref.indexOf("?"));
1303 var aQueryString = strQueryString.split("&");
1304 for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
1305 if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
1306 var aParam = aQueryString[iParam].split("=");
1307 strReturn = aParam[1];
1308 break;
1309 }
1310 }
1311 }
1312 return strReturn;
1313}
1314
1315function leading_zero(p) {
1316 var s = String(p);
1317 if (s.length == 1) s = "0" + s;
1318 return s;
1319}
1320
1321function closeInfoBox(cleanup) {
1322 var box = document.getElementById('infoBox');
1323 var shadow = document.getElementById('infoBoxShadow');
1324
1325 if (shadow) {
1326 shadow.style.display = "none";
1327 } else if (box) {
1328 box.style.display = "none";
1329 }
1330
1331 if (cleanup) box.innerHTML = "&nbsp;";
1332
1333 enableHotkeys();
1334
1335 return false;
1336}
1337
1338
1339function displayDlg(id, param) {
1340
1341 if (!xmlhttp_ready(xmlhttp)) {
1342 printLockingError();
1343 return
1344 }
1345
1346 notify_progress("Loading, please wait...");
1347
1348 xmlhttp.open("GET", "backend.php?op=dlg&id=" +
1349 param_escape(id) + "&param=" + param_escape(param), true);
1350 xmlhttp.onreadystatechange=infobox_callback;
1351 xmlhttp.send(null);
1352
1353 disableHotkeys();
1354
1355 return false;
1356}
1357
1358function infobox_submit_callback() {
1359 if (xmlhttp.readyState == 4) {
1360 closeInfoBox();
1361
1362 try {
1363 // called from prefs, reload tab
1364 if (active_tab) {
1365 selectTab(active_tab, false);
1366 }
1367 } catch (e) { }
1368
1369 if (xmlhttp.responseText) {
1370 notify_info(xmlhttp.responseText);
1371 }
1372
1373 }
1374}
1375
1376function infobox_callback() {
1377 if (xmlhttp.readyState == 4) {
1378 var box = document.getElementById('infoBox');
1379 var shadow = document.getElementById('infoBoxShadow');
1380 if (box) {
1381 box.innerHTML=xmlhttp.responseText;
1382 if (shadow) {
1383 shadow.style.display = "block";
1384 } else {
1385 box.style.display = "block";
1386 }
1387 }
1388 notify("");
1389 }
1390}
1391
1392function addFilter() {
1393
1394 if (!xmlhttp_ready(xmlhttp)) {
1395 printLockingError();
1396 return
1397 }
1398
1399 var form = document.forms['filter_add_form'];
1400 var reg_exp = form.reg_exp.value;
1401
1402 if (reg_exp == "") {
1403 alert("Can't add filter: nothing to match on.");
1404 return false;
1405 }
1406
1407 var query = Form.serialize("filter_add_form");
1408
1409 xmlhttp.open("GET", "backend.php?" + query, true);
1410 xmlhttp.onreadystatechange=infobox_submit_callback;
1411 xmlhttp.send(null);
1412
1413 return true;
1414}
1415
1416function toggleSubmitNotEmpty(e, submit_id) {
1417 try {
1418 document.getElementById(submit_id).disabled = (e.value == "")
1419 } catch (e) {
1420 exception_error("toggleSubmitNotEmpty", e);
1421 }
1422}
1423
1424function isValidURL(s) {
1425 return s.match("http://") != null || s.match("https://") != null || s.match("feed://") != null;
1426}
1427
1428function qaddFeed() {
1429
1430 if (!xmlhttp_ready(xmlhttp)) {
1431 printLockingError();
1432 return
1433 }
1434
1435 var form = document.forms['feed_add_form'];
1436 var feed_url = form.feed_url.value;
1437
1438 if (feed_url == "") {
1439 alert("Can't subscribe: no feed URL given.");
1440 return false;
1441 }
1442
1443 notify_progress("Adding feed...");
1444
1445 closeInfoBox();
1446
1447 var feeds_doc = document;
1448
1449// feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
1450
1451 var query = Form.serialize("feed_add_form");
1452
1453 debug("subscribe q: " + query);
1454
1455/* xmlhttp.open("GET", "backend.php?" + query, true);
1456 xmlhttp.onreadystatechange=dlg_frefresh_callback;
1457 xmlhttp.send(null); */
1458
1459 xmlhttp.open("POST", "backend.php", true);
1460 xmlhttp.onreadystatechange=dlg_frefresh_callback;
1461 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
1462 xmlhttp.send(query);
1463
1464 return false;
1465}
1466
1467function filterCR(e, f)
1468{
1469 var key;
1470
1471 if(window.event)
1472 key = window.event.keyCode; //IE
1473 else
1474 key = e.which; //firefox
1475
1476 if (key == 13) {
1477 if (typeof f != 'undefined') {
1478 f();
1479 return false;
1480 } else {
1481 return false;
1482 }
1483 } else {
1484 return true;
1485 }
1486}
1487
1488function getMainContext() {
1489 return this.window;
1490}
1491
1492function getFeedsContext() {
1493 return this.window;
1494}
1495
1496function getContentContext() {
1497 return this.window;
1498}
1499
1500function getHeadlinesContext() {
1501 return this.window;
1502}
1503
1504var debug_last_class = "even";
1505
1506function debug(msg) {
1507
1508 if (debug_last_class == "even") {
1509 debug_last_class = "odd";
1510 } else {
1511 debug_last_class = "even";
1512 }
1513
1514 var c = document.getElementById('debug_output');
1515 if (c && c.style.display == "block") {
1516 while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
1517 c.removeChild(c.lastChild);
1518 }
1519
1520 var d = new Date();
1521 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
1522 ":" + leading_zero(d.getSeconds());
1523 c.innerHTML = "<li class=\"" + debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " +
1524 msg + "</li>" + c.innerHTML;
1525 }
1526}
1527
1528function getInitParam(key) {
1529 return init_params[key];
1530}
1531
1532function storeInitParam(key, value) {
1533 debug("<b>storeInitParam is OBSOLETE: " + key + " => " + value + "</b>");
1534 init_params[key] = value;
1535}
1536
1537function fatalError(code, message) {
1538 try {
1539
1540 if (code == 6) {
1541 window.location.href = "tt-rss.php";
1542 } else if (code == 5) {
1543 window.location.href = "update.php";
1544 } else {
1545 var fe = document.getElementById("fatal_error");
1546 var fc = document.getElementById("fatal_error_msg");
1547
1548 if (message == "") message = "Unknown error";
1549
1550 fc.innerHTML = "<img src='images/sign_excl.png'> " + message + " (Code " + code + ")";
1551
1552 fe.style.display = "block";
1553 }
1554
1555 } catch (e) {
1556 exception_error("fatalError", e);
1557 }
1558}
1559
1560function getFeedName(id, is_cat) {
1561 var d = getFeedsContext().document;
1562
1563 var e;
1564
1565 if (is_cat) {
1566 e = d.getElementById("FCATN-" + id);
1567 } else {
1568 e = d.getElementById("FEEDN-" + id);
1569 }
1570 if (e) {
1571 return e.innerHTML.stripTags();
1572 } else {
1573 return null;
1574 }
1575}
1576
1577function viewContentUrl(url) {
1578 getContentContext().location = url;
1579}
1580
1581function filterDlgCheckAction(sender) {
1582
1583 try {
1584
1585 var action = sender[sender.selectedIndex].value;
1586
1587 var form = document.forms["filter_add_form"];
1588
1589 if (!form) {
1590 form = document.forms["filter_edit_form"];
1591 }
1592
1593 if (!form) {
1594 debug("filterDlgCheckAction: can't find form!");
1595 return;
1596 }
1597
1598 var action_param = form.action_param;
1599
1600 if (!action_param) {
1601 debug("filterDlgCheckAction: can't find action param!");
1602 return;
1603 }
1604
1605 // if selected action supports parameters, enable params field
1606 if (action == 4) {
1607 action_param.disabled = false;
1608 } else {
1609 action_param.disabled = true;
1610 }
1611
1612 } catch (e) {
1613 exception_error(e, "filterDlgCheckAction");
1614 }
1615
1616}
1617
1618function explainError(code) {
1619 return displayDlg("explainError", code);
1620}
1621
1622function logoutUser() {
1623 try {
1624 if (xmlhttp_ready(xmlhttp_rpc)) {
1625
1626 notify_progress("Logging out, please wait...", true);
1627
1628 xmlhttp_rpc.open("GET", "backend.php?op=rpc&subop=logout", true);
1629 xmlhttp_rpc.onreadystatechange=logout_callback;
1630 xmlhttp_rpc.send(null);
1631 } else {
1632 printLockingError();
1633 }
1634 } catch (e) {
1635 exception_error("logoutUser", e);
1636 }
1637}