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