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