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