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